首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用C++测试具有多个模板参数的GTest模板类?

如何用C++测试具有多个模板参数的GTest模板类?
EN

Stack Overflow用户
提问于 2015-11-06 08:47:36
回答 2查看 1.3K关注 0票数 0

我有个小班:

代码语言:javascript
复制
template <class key_t, class value_t>
class node_t {
public:
    node_t();
    void set_key(key_t);
    key_t get_key();
    void set_value(value_t);
    value_t get_value();

private:
    key_t key;
    value_t value;
};

template <class key_t, class value_t>
node_t<key_t, value_t>::node_t() {
    key = (key_t)0;
    value = (value_t)0;
}

template <class key_t, class value_t>
void node_t<key_t, value_t>::set_key(key_t key) {
    this->key = key;
}

template <class key_t, class value_t>
key_t node_t<key_t, value_t>::get_key() {
    return this->key;
}

template <class key_t, class value_t>
void node_t<key_t, value_t>::set_value(value_t value) {
    this->value = value;
}

template <class key_t, class value_t>
value_t node_t<key_t, value_t>::get_value() {
    return this->value;
}

我如何用Google测试框架来测试它呢?

代码语言:javascript
复制
/*
TEST(node_test_t, node_test) {
    node_t<std::string, float> node;

    std::string key = node.get_key();
    ASSERT_STREQ(key, 0);

    node.set_key("one");
    ASSERT_STREQ(node.get_key(), "one");

    float value = node.get_value();
    ASSERT_EQ(value, 0);

    node.set_value(2.4);
    ASSERT_EQ(node.get_value(), 2.4);
}
*/

// Updated
TEST(node_test_t, node_test)
{
    node_t<std::string, float> node;

    std::string key = node.get_key();
    ASSERT_STREQ("0", key.c_str());

    node.set_key("one");
    ASSERT_STREQ("one", node.get_key().c_str());

    float value = node.get_value();
    ASSERT_EQ(value, 0);

    node.set_value(2.4f);
    ASSERT_EQ(node.get_value(), 2.4f);
}

我还发现:

UPD:我也尝试过这个:https://groups.google.com/forum/#!searchin/googletestframework/class$20template/googletestframework/wSlsjwU7vls/9ulYoFliuLgJ

代码语言:javascript
复制
template <class key_t, class value_t>
class node_testing_t : public node_t<key_t, value_t> {
public:
    using node_t<key_t, value_t>::get_value;
};

TEST(node_test_case, get_value) {
    node_testing_t<std::string, float> node_testing;
    ASSERT_EQ(0, node_testing.get_value());
}

我也有LNK2019错误:

代码语言:javascript
复制
1>------ Skipped Build: Project: RUN_TESTS, Configuration: Debug Win32 ------
1>Project not selected to build for this solution configuration 
2>------ Build started: Project: googlemock, Configuration: Debug Win32 ------
3>------ Build started: Project: tests, Configuration: Debug Win32 ------
3>node_test.obj : error LNK2019: unresolved external symbol "public: __thiscall node_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>::node_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>(void)" (??0?$node_t@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@@QAE@XZ) referenced in function "public: __thiscall node_testing_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>::node_testing_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>(void)" (??0?$node_testing_t@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@@QAE@XZ)
3>node_test.obj : error LNK2019: unresolved external symbol "public: __thiscall node_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>::~node_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>(void)" (??1?$node_t@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@@QAE@XZ) referenced in function "public: __thiscall node_testing_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>::~node_testing_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>(void)" (??1?$node_testing_t@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@@QAE@XZ)
3>node_test.obj : error LNK2019: unresolved external symbol "public: float __thiscall node_t<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,float>::get_value(void)" (?get_value@?$node_t@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@M@@QAEMXZ) referenced in function "private: virtual void __thiscall node_test_case_get_value_Test::TestBody(void)" (?TestBody@node_test_case_get_value_Test@@EAEXXZ)
3>C:\Users\user\Desktop\hill-climbing\bin\tests.exe : fatal error LNK1120: 3 unresolved externals
4>------ Skipped Build: Project: ALL_BUILD, Configuration: Debug Win32 ------
4>Project not selected to build for this solution configuration 
========== Build: 1 succeeded, 1 failed, 2 up-to-date, 2 skipped ==========
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-06 13:16:15

我在Linux4.8.4上尝试了您的示例,链接没有问题,尽管测试执行失败了,因为node对象构造会抛出一个异常,因为构造函数中的key = (key_t)0行试图将一个0转换为std::string对象。

尽管如此,在您的示例中,MSVC链接器抱怨说,在node_test.obj文件中有未解决的对与node_t<std::string, float>相关的符号的引用,即构造函数、析构函数和get_value()成员函数。只是你测试中用到的那些。

在测试用例的范围内实例化模板,尝试在外部范围内执行。

代码语言:javascript
复制
using node_string_float_t = node_t<std::string, float>;

TEST(node_test_t, node_test)
{
    node_string_float_t node;

    std::string key = node.get_key();
    ASSERT_STREQ("0", key.c_str());

    node.set_key("one");
    ASSERT_STREQ("one", node.get_key().c_str());

    float value = node.get_value();
    ASSERT_EQ(value, 0);

    node.set_value(2.4f);
    ASSERT_EQ(node.get_value(), 2.4f);
}
票数 1
EN

Stack Overflow用户

发布于 2015-11-06 09:26:30

你读过ASSERT_STREQ文档吗?这个宏接受两个C-strings,您试图首先传递它std::stringint,在第二种情况下传递std::stringC-string。顺便说一下,第一个参数是期望值,第二个参数是实际值,所以您可能需要切换提供参数的顺序。

代码语言:javascript
复制
TEST(node_test_t, node_test)
{
    node_t<std::string, float> node;

    std::string key = node.get_key();
    ASSERT_STREQ("0", key.c_str());

    node.set_key("one");
    ASSERT_STREQ("one", node.get_key().c_str());

    float value = node.get_value();
    ASSERT_EQ(value, 0);

    node.set_value(2.4f);
    ASSERT_EQ(node.get_value(), 2.4f);
}

P.S.如果您想比较std::string对象,可以使用EXPECT_EQEXPECT_NE等等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33562749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档