googletest:sample6分析
目录
googletest:sample1
googletest:sample2
googletest:sample3
googletest:sample4
googletest:sample5
googletest:sample6
googletest:sample7
googletest:sample8
googletest:sample9
待测文件
PrimeTable
类声明了素数表的一系列接口,包括
1)IsPrime
:判断当前输入数n是否为素数;
2)GetNextPrime
:找比输入数n大的下一个素数.
prime_tables.h
#include <algorithm>
// The prime table interface.
class PrimeTable {
public:
virtual ~PrimeTable() = default;
// Returns true if and only if n is a prime number.
virtual bool IsPrime(int n) const = 0;
// Returns the smallest prime number greater than p; or returns -1
// if the next prime is beyond the capacity of the table.
virtual int GetNextPrime(int p) const = 0;
};
派生类OnTheFlyPrimeTable
实现了PrimeTable
接口,基于当前输入数进行实时计算.
// Implementation #1 calculates the primes on-the-fly.
class OnTheFlyPrimeTable : public PrimeTable {
public:
bool IsPrime(int n) const override {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
// n is divisible by an integer other than 1 and itself.
if ((n % i) == 0) return false;
}
return true;
}
int GetNextPrime(int p) const override {
if (p < 0) return -1;
for (int n = p + 1;; n++) {
if (IsPrime(n)) return n;
}
}
};
派生类PreCalculatedPrimeTable
实现了PrimeTable
接口,不过多了一个数组is_prime_[0..is_prime_size_-1]
用于记录已经判断过的数是否为素数. 数组缓存大小由用户在构建对象时指定.
// Implementation #2 pre-calculates the primes and stores the result
// in an array.
class PreCalculatedPrimeTable : public PrimeTable {
public:
// 'max' specifies the maximum number the prime table holds.
explicit PreCalculatedPrimeTable(int max)
: is_prime_size_(std::max(1, max + 1)),
is_prime_(new bool[static_cast<size_t>(is_prime_size_)]) {
CalculatePrimesUpTo(is_prime_size_ - 1);
}
~PreCalculatedPrimeTable() override { delete[] is_prime_; }
bool IsPrime(int n) const override {
return 0 <= n && n < is_prime_size_ && is_prime_[n];
}
int GetNextPrime(int p) const override {
for (int n = p + 1; n < is_prime_size_; n++) {
if (is_prime_[n]) return n;
}
return -1;
}
private:
void CalculatePrimesUpTo(int max) {
::std::fill(is_prime_, is_prime_ + is_prime_size_, true);
is_prime_[0] = is_prime_[1] = false;
// Checks every candidate for prime number (we know that 2 is the only even
// prime).
for (int i = 2; i * i <= max; i += i % 2 + 1) {
if (!is_prime_[i]) continue;
// Marks all multiples of i (except i itself) as non-prime.
// We are starting here from i-th multiplier, because all smaller
// complex numbers were already marked.
for (int j = i * i; j <= max; j += i) {
is_prime_[j] = false;
}
}
}
const int is_prime_size_;
bool* const is_prime_;
// Disables compiler warning "assignment operator could not be generated."
void operator=(const PreCalculatedPrimeTable& rhs);
};
测试文件
对于测试这种同一个父类的派生类,能否复用测试?
googletest提供了2种方法:
- "typed test":类型测试. 要求写测试时,已知所有类型;
- "type-parameterized tests":类型参数化测试. 写测试时,不必知晓所有类型,只需要知道接口即可.
共同步骤:
首先,我们通过模板的特例化定义工厂方法,用于创建待测的类对象OnTheFlyPrimeTable, PreCalculatedPrimeTable
,如果有一些对象初值,也可以在这一步构造时传入;
接着,定义测试夹具类模板PrimeTableTest
,利用夹具类实现测试时自动创建、析构待测类对象.
sample6_unittest.cc
// First, we define some factory functions for creating instances of
// the implementations. You may be able to skip this step if all your
// implementations can be constructed the same way.
template <class T>
PrimeTable* CreatePrimeTable();
template <>
PrimeTable* CreatePrimeTable<OnTheFlyPrimeTable>() {
return new OnTheFlyPrimeTable;
}
template <>
PrimeTable* CreatePrimeTable<PreCalculatedPrimeTable>() {
return new PreCalculatedPrimeTable(10000);
}
// Then we define a test fixture class template.
template <class T>
class PrimeTableTest : public testing::Test {
protected:
// The ctor calls the factory function to create a prime table
// implemented by T.
PrimeTableTest() : table_(CreatePrimeTable<T>()) {}
~PrimeTableTest() override { delete table_; }
// Note that we test an implementation via the base interface
// instead of the actual implementation class. This is important
// for keeping the tests close to the real world scenario, where the
// implementation is invoked via the base interface. It avoids
// got-yas where the implementation class has a method that shadows
// a method with the same name (but slightly different argument
// types) in the base interface, for example.
PrimeTable* const table_;
};
- 对于方法1("typed test"):
1)需要先定义我们想要测试的所有类型的列表,形如typedef Types<type1, type2, ...> MyTypeList
;
2)然后用TYPED_TEST_SUITE
定义类型化的测试组件;
3)最后用TYPED_TEST
定义类型化的测试用例;
tips:TYPED_TEST_SUITE(TestCaseName, TypeList)
定义类型化的测试组件,TestCaseName
必须匹配测试夹具名,TypeList
是前面用typedef
定义的类型列表;
TYPED_TEST(TestCaseName, TestName)
定义类型化的测试用例,功能类似于TEST_F
,TestCaseName
必须匹配测试夹具名,TestName
是我们针对每个测试自定义的测试名.
"typed test":
using testing::Types;
// Google Test offers two ways for reusing tests for different types.
// The first is called "typed tests". You should use it if you
// already know *all* the types you are gonna exercise when you write
// the tests.
// To write a typed test case, first use
//
// TYPED_TEST_SUITE(TestCaseName, TypeList);
//
// to declare it and specify the type parameters. As with TEST_F,
// TestCaseName must match the test fixture name.
// The list of types we want to test.
typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable> Implementations;
TYPED_TEST_SUITE(PrimeTableTest, Implementations);
// Then use TYPED_TEST(TestCaseName, TestName) to define a typed test,
// similar to TEST_F.
TYPED_TEST(PrimeTableTest, ReturnsFalseForNonPrimes) {
// Inside the test body, you can refer to the type parameter by
// TypeParam, and refer to the fixture class by TestFixture. We
// don't need them in this example.
// Since we are in the template world, C++ requires explicitly
// writing 'this->' when referring to members of the fixture class.
// This is something you have to learn to live with.
EXPECT_FALSE(this->table_->IsPrime(-5));
EXPECT_FALSE(this->table_->IsPrime(0));
EXPECT_FALSE(this->table_->IsPrime(1));
EXPECT_FALSE(this->table_->IsPrime(4));
EXPECT_FALSE(this->table_->IsPrime(6));
EXPECT_FALSE(this->table_->IsPrime(100));
}
TYPED_TEST(PrimeTableTest, ReturnsTrueForPrimes) {
EXPECT_TRUE(this->table_->IsPrime(2));
EXPECT_TRUE(this->table_->IsPrime(3));
EXPECT_TRUE(this->table_->IsPrime(5));
EXPECT_TRUE(this->table_->IsPrime(7));
EXPECT_TRUE(this->table_->IsPrime(11));
EXPECT_TRUE(this->table_->IsPrime(131));
}
TYPED_TEST(PrimeTableTest, CanGetNextPrime) {
EXPECT_EQ(2, this->table_->GetNextPrime(0));
EXPECT_EQ(3, this->table_->GetNextPrime(2));
EXPECT_EQ(5, this->table_->GetNextPrime(3));
EXPECT_EQ(7, this->table_->GetNextPrime(5));
EXPECT_EQ(11, this->table_->GetNextPrime(7));
EXPECT_EQ(131, this->table_->GetNextPrime(128));
}
// That's it! Google Test will repeat each TYPED_TEST for each type
// in the type list specified in TYPED_TEST_SUITE. Sit back and be
// happy that you don't have to define them multiple times.
注意:因为在模板中,C++在引用fixture
类成员时,需要显式地加上this->
.
- 对于方法2("type-parameterized tests"):
1)用TYPED_TEST_SUITE_P
声明测试用例. 参数名是测试夹具名称,也通常是测试用例的名称. 后缀名_P
表示参数化("parameterized")或模式("pattern");
2)用TYPED_TEST_P
定义测试,类似于TEST_F
;
3)用REGISTER_TYPED_TEST_SUITE_P
枚举前面定义的测试. 到这一步,已经完成了测试模式,不过没有真正执行具体的测试,还没给类型参数;
4)用INSTANTIATE_TYPED_TEST_SUITE_P
实例化类型参数测试,需要指定类型列表.
using testing::Types;
// Sometimes, however, you don't yet know all the types that you want
// to test when you write the tests. For example, if you are the
// author of an interface and expect other people to implement it, you
// might want to write a set of tests to make sure each implementation
// conforms to some basic requirements, but you don't know what
// implementations will be written in the future.
//
// How can you write the tests without committing to the type
// parameters? That's what "type-parameterized tests" can do for you.
// It is a bit more involved than typed tests, but in return you get a
// test pattern that can be reused in many contexts, which is a big
// win. Here's how you do it:
// First, define a test fixture class template. Here we just reuse
// the PrimeTableTest fixture defined earlier:
template <class T>
class PrimeTableTest2 : public PrimeTableTest<T> {};
// Then, declare the test case. The argument is the name of the test
// fixture, and also the name of the test case (as usual). The _P
// suffix is for "parameterized" or "pattern".
TYPED_TEST_SUITE_P(PrimeTableTest2);
// Next, use TYPED_TEST_P(TestCaseName, TestName) to define a test,
// similar to what you do with TEST_F.
TYPED_TEST_P(PrimeTableTest2, ReturnsFalseForNonPrimes) {
EXPECT_FALSE(this->table_->IsPrime(-5));
EXPECT_FALSE(this->table_->IsPrime(0));
EXPECT_FALSE(this->table_->IsPrime(1));
EXPECT_FALSE(this->table_->IsPrime(4));
EXPECT_FALSE(this->table_->IsPrime(6));
EXPECT_FALSE(this->table_->IsPrime(100));
}
TYPED_TEST_P(PrimeTableTest2, ReturnsTrueForPrimes) {
EXPECT_TRUE(this->table_->IsPrime(2));
EXPECT_TRUE(this->table_->IsPrime(3));
EXPECT_TRUE(this->table_->IsPrime(5));
EXPECT_TRUE(this->table_->IsPrime(7));
EXPECT_TRUE(this->table_->IsPrime(11));
EXPECT_TRUE(this->table_->IsPrime(131));
}
TYPED_TEST_P(PrimeTableTest2, CanGetNextPrime) {
EXPECT_EQ(2, this->table_->GetNextPrime(0));
EXPECT_EQ(3, this->table_->GetNextPrime(2));
EXPECT_EQ(5, this->table_->GetNextPrime(3));
EXPECT_EQ(7, this->table_->GetNextPrime(5));
EXPECT_EQ(11, this->table_->GetNextPrime(7));
EXPECT_EQ(131, this->table_->GetNextPrime(128));
}
// Type-parameterized tests involve one extra step: you have to
// enumerate the tests you defined:
REGISTER_TYPED_TEST_SUITE_P(
PrimeTableTest2, // The first argument is the test case name.
// The rest of the arguments are the test names.
ReturnsFalseForNonPrimes, ReturnsTrueForPrimes, CanGetNextPrime);
// At this point the test pattern is done. However, you don't have
// any real test yet as you haven't said which types you want to run
// the tests with.
// To turn the abstract test pattern into real tests, you instantiate
// it with a list of types. Usually the test pattern will be defined
// in a .h file, and anyone can #include and instantiate it. You can
// even instantiate it more than once in the same program. To tell
// different instances apart, you give each of them a name, which will
// become part of the test case name and can be used in test filters.
// The list of types we want to test. Note that it doesn't have to be
// defined at the time we write the TYPED_TEST_P()s.
typedef Types<OnTheFlyPrimeTable, PreCalculatedPrimeTable>
PrimeTableImplementations;
INSTANTIATE_TYPED_TEST_SUITE_P(OnTheFlyAndPreCalculated, // Instance name
PrimeTableTest2, // Test case name
PrimeTableImplementations); // Type list
小结
对于派生类,或者实现同一接口的类,有两种方式可以复用test:
- typed test;
- type-parameterized tests.
如果已知要测试的派生类类型,可用第一种;如果还在设计阶段,或者派生类类型未知,可用第二种.