gtest-测试模板类 [英] gtest - testing template class
本文介绍了gtest-测试模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用gtest测试一个模板类.我在 Google测试手册中了解了TYPED_TEST. a>并查看了他们引用的官方示例,但仍然无法解决在测试中实例化模板类对象的问题.
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.
假设以下简单模板类:
template <typename T>
class Foo
{
public:
T data ;
};
在测试课中,我们声明
typedef ::testing::Types<int, float> MyTypes ;
现在如何在测试中为MyTypes中列出的Ts实例化Foo类的对象? 例如
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) ;
}
推荐答案
在测试中,请参考特殊名称TypeParam
以获取类型参数.所以你可以做
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) ;
}
这篇关于gtest-测试模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文