0

I have a small class:

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;
}

How I can test it with Google Testing framework?

/*
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);
}

I found also:

UPD: I also tried this: https://groups.google.com/forum/#!searchin/googletestframework/class$20template/googletestframework/wSlsjwU7vls/9ulYoFliuLgJ

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());
}

and I also have LNK2019 error:

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 ==========
Community
  • 1
  • 1
Valeriy
  • 1,365
  • 3
  • 18
  • 45

2 Answers2

1

Have you read ASSERT_STREQ documentation? This macro accepts two C-strings, and you're attempting to pass it std::string and int in first, and std::string and C-string in second case. Btw, first argument is expected value and second one is actual, so you may want to switch the order in which you're providing parameters.

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. If you want to compare std::string objects you can use EXPECT_EQ, EXPECT_NE and so on.

dbajgoric
  • 1,447
  • 11
  • 17
1

I tried your example on linux with gcc 4.8.4 and it has no problem to link, although the test execution fails because an exception is thrown upon node object construction since the line key = (key_t)0 in the constructor tries to cast a 0 into a std::string object.

That said, in your case, the MSVC linker complains that in the node_test.obj file there are unresolved references to symbols related to node_t<std::string, float>, namely constructor, destructor and get_value() member functions. Just the ones in use in your test.

You are instantiating the template within the scope of the test case, try to do it in the outer scope.

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);
}
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61