2015-11-06 117 views
0

我有一個小類:如何使用GTest測試具有多個模板參數的C++模板類?

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

我怎麼能與谷歌測試框架測試嗎?

/* 
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

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錯誤:

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)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 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)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 
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)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 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)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 
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)" ([email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@QAEMXZ) referenced in function "private: virtual void __thiscall node_test_case_get_value_Test::TestBody(void)" ([email protected][email protected]@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 ========== 
+0

爲什麼需要'delete'? 'node_t ::〜node_t(){delete_key; 刪除值; }'。你永遠不會分配內存,所以你不需要'刪除'。 – BlackMamba

+0

@BlackMamba謝謝,修正 – Valeriy

+0

如果您可以包含完整的編譯錯誤輸出,將會很有幫助。 –

回答

1

我想你的例子在Linux上用gcc 4.8.4和它沒有任何問題聯繫起來,儘管因爲一個異常,因爲在構造線key = (key_t)0node對象的構造拋出的測試執行失敗嘗試投了0成一個std::string對象。

這就是說,在你的情況下,MSVC鏈接器抱怨在node_test.obj文件中有與node_t<std::string, float>有關的符號的未解析引用,即構造函數,析構函數和get_value()成員函數。只是在測試中使用的那些。

您正在實例化測試用例範圍內的模板,請嘗試在外部範圍內執行此操作。

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

你讀過ASSERT_STREQ文檔?該宏接受兩個C-strings,並且您試圖在第一種情況下將std::stringint傳遞給std::stringC-string。順便說一句,第一個參數是期望值,第二個參數是實際的,所以你可能想要切換你提供參數的順序。

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等。

相關問題