Googletest: build fixtures with parameters?

I have two implementations of an algorithm that works on arrays and returns the same value, slow and naive, but the correct method A and optimized method B , which may be wrong in the corners of the space of the input parameters. Method B has branches depending on the size of the input array, and I would like to check B on A for different sizes of input arrays. Both methods are designed to work with different types.

I’m just starting to use googletest for the first time, but in fact I don’t see a clear way to do this using fasteners (the following is simplified: there is more to configure the tests and I would also like to run other tests according to the data):

 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() } 

In the googletest documentation, the step to run the actual tests after determining the device will determine the types

 typedef ::testing::Types<char, int, unsigned int> MyTypes; TYPED_TEST_CASE(BTest, MyTypes); 

but this shows the limitation that for classes derived from ::testing::Test , only one template parameter is allowed. Am I reading this correctly? How to do it?

+4
source share
1 answer

You can always pack several types of parameters into a tuple. To package integer values, you can use type converters like:

 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); 
+6
source

All Articles