bbt*_*trb 4 c++ unit-testing googletest
我有一个算法的两个实现工作在数组上并返回一个单独的值,一个缓慢而天真但正确的方法A和一个优化的方法B,可能在输入参数空间的角落有问题.方法B根据输入数组的大小有分支,我想B针对A不同的输入数组大小进行测试.这两种方法都可以模仿不同的类型.
我刚开始第一次使用googletest,但我真的没有看到如何用夹具做一个明确的方法(以下是简化的,有更多的设置来进行测试,我我还想对数据进行其他测试:
template<typename T, unsigned int length> // type to test on, test array size
class BTest : public ::testing:Test {
public:
T* data; // test data
public:
BTest(); // allocate data, populate data with random elements
~BTest();
T run_method_a_on_data(); // reference: method A implementation
};
// ...
TYPED_TEST_CASE(...) // set up types, see text below
TYPED_TEST(...) {
// test that runs method B on data and compares to run_method_a_on_data()
}
Run Code Online (Sandbox Code Playgroud)
在googletest文档中,在fixture定义之后运行实际测试的步骤是定义类型
typedef ::testing::Types<char, int, unsigned int> MyTypes;
TYPED_TEST_CASE(BTest, MyTypes);
Run Code Online (Sandbox Code Playgroud)
但是这显示了限制,即从派生的类只允许一个模板参数::testing::Test.我看对了吗?怎么会这样呢?
您始终可以将多个参数类型打包到元组中.要打包整数值,您可以使用类似值转换器,如下所示:
template <size_t N> class TypeValue {
public:
static const size_t value = N;
};
template <size_t N> const size_t TypeValue<N>::value;
#include <tuple> /// Or <tr1/tuple>
using testing::Test;
using testing::Types;
using std::tuple; // Or std::tr1::tuple
using std::element;
template<typename T> // tuple<type to test on, test array size>
class BTest : public Test {
public:
typedef element<0, T>::type ElementType;
static const size_t kElementCount = element<1, T>::type::value;
ElementType* data; // test data
public:
BTest() {
// allocate data, populate data with random elements
}
~BTest();
ElementType run_method_a_on_data(); // reference: method A implementation
};
template <typename T> const size_t BTest<T>::kElementCount;
....
typedef Types<tuple<char, TypeValue<10> >, tuple<int, TypeValue<199> > MyTypes;
TYPED_TEST_CASE(BTest, MyTypes);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2795 次 |
| 最近记录: |