How to test c++ template class with multiple template parameters using gtest? -
i want test template class gtest. read typed_test
s in gtest manual , looked @ official example (samples\sample6_unittest.cc) reference. template example has 1 template parameter. but, code has 2 template parameters, how can test it?
i have following code:
// 2 element type template <typename e, typename f> class queuenew { public: queuenew() {} void enqueue(const e& element) {} e* dequeue() {} f size() const { return (f)123; } };
for wrote test code below:
template <class e, class f> queuenew<e, f>* createqueue(); template <> queuenew<int, int>* createqueue<int, int>() { return new queuenew < int, int > ; } template <> queuenew<char, char>* createqueue<char, char>() { return new queuenew < char, char > ; } template <class e, class f> class queuetestnew; template <class e> class queuetestnew<e, int> : public testing::test { protected: queuetestnew() : queue(createqueue<e, int>()){} virtual ~queuetestnew(){ delete queue; } queuenew<e, int>* const queue; }; template <class e> class queuetestnew<char, e> : public testing::test { protected: queuetestnew() : queue(createqueue<char, e>()){} virtual ~queuetestnew(){ delete queue; } queuenew<char, e>* const queue; }; // list of types want test. typedef ::testing::types <char, int> implementations; typed_test_case(queuetestnew, implementations); typed_test(queuetestnew, defaultconstructor) { expect_eq(123u, this->queue->size()); }
but when building, error:
error c2976: 'queuetestnew' : few template arguments see declaration of 'queuetestnew' ...
i think test template method gtest wrong, how should this?
a trick make gtest see single type parameter, nested types. this, can define templated structure such as:
template <typename a, typename b> struct typedefinitions { typedef typename mya; typedef typename b myb; };
which can pass typed-test fixture:
template <class t> class queuetestnew : public testing::test { protected: queuetestnew() : queue(createqueue<typename t::mya, typename t::myb>()){} virtual ~queuetestnew(){ delete queue; } queuenew<typename t::mya, typename t::myb>* const queue; }; // list of types want test. typedef ::testing::types <typedefinitions<char,char>, typedefinitions<int,int> > implementations; typed_test_case(queuetestnew, implementations); typed_test(queuetestnew, defaultconstructor) { typename typeparam::mya somea; // if need access subtypes in test expect_eq(123u, this->queue->size()); }
Comments
Post a Comment