Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtest - testing template class

I want to test a template class with gtest. I read about TYPED_TESTs in Google Test manual and looked at official example they reference, but still can't wrap my head around getting an object of template class instantiated in my test.

Suppose the following simple template class:

template <typename T>
class Foo
{
public:
    T data ;
};

In testing class we declare

typedef ::testing::Types<int, float> MyTypes ;

Now how can I instantiate an object of class Foo for Ts listed in MyTypes in a test? E.g.

TYPED_TEST(TestFoo, test1)
{
    Foo<T> object ;
    object.data = 1.0 ;

    ASSERT_FLOAT_EQ(object.data, 1.0) ;
}
like image 315
Puchatek Avatar asked Jun 13 '13 05:06

Puchatek


1 Answers

Inside a test, refer to the special name TypeParam to get the type parameter. So you could do

TYPED_TEST(TestFoo, test1)
{
    Foo<TypeParam> object ; // not Foo<T>
    object.data = 1.0 ;

    ASSERT_FLOAT_EQ(object.data, 1.0) ;
}
like image 69
TemplateRex Avatar answered Nov 18 '22 01:11

TemplateRex