c++ 我如何< T>在测试中为MyTypes中列出的Ts示例化类Foo的对象?

v8wbuo2f  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(113)

我想用gtest测试一个模板类,我在Google测试手册中读到了TYPED_TEST,并查看了他们引用的official example,但仍然无法在测试中示例化模板类的对象。
假设下面的简单模板类:

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

在测试类中我们声明

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

现在,我如何在测试中为MyTypes中列出的T示例化Foo<T>类的对象?
例如:

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

vawmfj5a1#

在测试中,引用特殊名称TypeParam来获取类型参数。

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

    ASSERT_FLOAT_EQ(object.data, 1.0) ;
}
ar5n3qh5

ar5n3qh52#

由于我自己无法弄清楚如何将TemplateRex的答案与Puchatek的原始代码片段结合起来,所以这里有一个完整的可编译示例,我是通过阅读GTest自己的注解得到的,这些注解非常有帮助。
将以下内容放入test.cpp

#include <gtest/gtest.h>
#include <regex>
#include <vector>

using vector_typelist = testing::Types<char, int*, std::regex>;
template<class> struct vector_suite : testing::Test {};
TYPED_TEST_SUITE(vector_suite, vector_typelist);

TYPED_TEST(vector_suite, bigness)
{
    std::vector<TypeParam> v;
    EXPECT_TRUE(sizeof(v) >= sizeof(TypeParam));
}

使用适当的-I-l选项进行编译。如下所示:

g++ -std=c++14 -I/usr/local/include test.cpp -L/usr/local/lib -lgtest -lgtest_main

或者(如果使用pkg-config):

g++ -std=c++14 $(pkg-config --cflags gtest) test.cpp $(pkg-config --libs gtest_main)

运行可执行文件,您应该看到类似以下的内容:

$ ./a.out
Running main() from /foobar/gtest_main.cc
[==========] Running 3 tests from 3 test suites.
[----------] Global test environment set-up.
[----------] 1 test from vector_suite/0, where TypeParam = char
[ RUN      ] vector_suite/0.bigness
[       OK ] vector_suite/0.bigness (0 ms)
[----------] 1 test from vector_suite/0 (0 ms total)

[----------] 1 test from vector_suite/1, where TypeParam = int*
[ RUN      ] vector_suite/1.bigness
[       OK ] vector_suite/1.bigness (0 ms)
[----------] 1 test from vector_suite/1 (0 ms total)

[----------] 1 test from vector_suite/2, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> >
[ RUN      ] vector_suite/2.bigness
test.cpp:12: Failure
Value of: sizeof(v) >= sizeof(TypeParam)
  Actual: false
Expected: true
[  FAILED  ] vector_suite/2.bigness, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> > (0 ms)
[----------] 1 test from vector_suite/2 (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 3 test suites ran. (0 ms total)
[  PASSED  ] 2 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] vector_suite/2.bigness, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> >

 1 FAILED TEST

(我加入这个失败的测试纯粹是为了演示当类型化测试失败时会发生什么。实际上sizeof(std::vector<std::regex>)确实小于sizeof(std::regex)。)
请注意,TYPED_TEST产生了一系列名称相关但不相同的测试套件,不再有单一的vector_suite

$ ./a.out --gtest_filter=vector_suite.*
Running main() from /foobar/gtest_main.cc
Note: Google Test filter = vector_suite.*
[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.

而是vector_suite/0vector_suite/1vector_suite/2

$ ./a.out --gtest_filter=vector_suite/0.*
Running main() from /foobar/gtest_main.cc
Note: Google Test filter = vector_suite/0.*
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from vector_suite/0, where TypeParam = char
[ RUN      ] vector_suite/0.bigness
[       OK ] vector_suite/0.bigness (0 ms)
[----------] 1 test from vector_suite/0 (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

相关问题