2012-04-08 13 views
5

मैंने this official document पढ़ा है, सीखें कि बाइनरी तुलना और स्ट्रिंग तुलना कैसे करें।gtest दो सरणी में मानों की तुलना कैसे करता है?

ASSERT_EQ और ASSERT_STREQ सरणी तुलना मामले में काम नहीं कर सका।

उदाहरण के लिए

[email protected]:~/poc$ g++ -I${GTEST_DIR}/include insertion_sort.cpp insertion_sort_unittest.cpp /home/li/libgtest.a -lpthread -o inser_unit 
[email protected]:~/poc$ ./inser_unit 
[==========] Running 1 test from 1 test case. 
[----------] Global test environment set-up. 
[----------] 1 test from InsertionSortTest 
[ RUN  ] InsertionSortTest.Two 
insertion_sort_unittest.cpp:18: Failure 
Value of: two_sorted 
    Actual: { 2, 5 } 
Expected: two 
Which is: { 2, 5 } 
[ FAILED ] InsertionSortTest.Two (1 ms) 
[----------] 1 test from InsertionSortTest (1 ms total) 

[----------] Global test environment tear-down 
[==========] 1 test from 1 test case ran. (1 ms total) 
[ PASSED ] 0 tests. 
[ FAILED ] 1 test, listed below: 
[ FAILED ] InsertionSortTest.Two 

1 FAILED TEST 

insertion_sort_unittest.cpp

#include <limits.h> 
#include "insertionsort.h" 
#include "gtest/gtest.h" 

namespace{ 
    class InsertionSortTest : public ::testing::Test{ 
     protected: 
      InsertionSortTest() {} 
      virtual ~InsertionSortTest() {} 
      virtual void SetUp() {} 
      virtual void TearDown() {} 
    }; 

    TEST(InsertionSortTest, Two){ 
     int two[] = {5, 2}; 
     int two_sorted[] = {2, 5}; 
     insertionSort(two, 2); 
     EXPECT_EQ(two, two_sorted); 
    } 
} 

int main(int argc, char **argv){ 
    ::testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 

insertion_sort.cpp

#include "insertionsort.h" 
void insertionSort(int *data, int size){ 
    for (int i=1,j; i<size; i++){ 
     int key = data[i]; 
     for (j=i-1; j>=0; j--){ 
      if (data[j] > key){ 
       data[j+1]=data[j]; 
       data[j]=key; 
      } 
     } 
    } 
} 

insertionsort.h

#ifndef INSERTIONSORT_H_ 
#define INSERTIONSORT_H_ 
void insertionSort(int *data, int size); 
#endif 
+2

शायद मैं कैसे googlemock उपयोग करने के लिए सीखना चाहिए, मैं इस सवाल का जवाब मिल गया [यहां] (http://stackoverflow.com/questions/1460703/comparison-of-arrays-in-google-test) और [यहां] (http://code.google.com/p/googletest/issues/detail?id=231) – liweijian

उत्तर

13

यदि आप नहीं चाहते हैं तो आपको googlemock पर निर्भरता जोड़ने की आवश्यकता नहीं है, तो आप अपना खुद का सरल फ़ंक्शन लिख सकते हैं जो testing::AssertionResult देता है, उदा।

template<typename T, size_t size> 
    ::testing::AssertionResult ArraysMatch(const T (&expected)[size], 
              const T (&actual)[size]){ 
     for (size_t i(0); i < size; ++i){ 
      if (expected[i] != actual[i]){ 
       return ::testing::AssertionFailure() << "array[" << i 
        << "] (" << actual[i] << ") != expected[" << i 
        << "] (" << expected[i] << ")"; 
      } 
     } 

     return ::testing::AssertionSuccess(); 
    } 

फिर अपने परीक्षण में, फोन:

EXPECT_TRUE(ArraysMatch(two_sorted, two)); 
+0

धन्यवाद फ्रेज़र, आप मेरी बहुत मदद करते हैं :) – liweijian

6

ASSERT_EQoperator== का उपयोग कर अपने तर्कों तुलना करती है। operator== के साथ तुलना std::vector के लिए काम करता है लेकिन सी ++ में सी-एरे के लिए नहीं। इसका कारण यह है कि, जब आप किसी अभिव्यक्ति में सी-सरणी के मान का उपयोग करने का प्रयास करते हैं, तो ज्यादातर परिस्थितियों में मान उस स्मृति में शुरू होता है जो स्मृति की शुरुआत को इंगित करता है जहां सरणी संग्रहीत होती है। आप दो पॉइंटर्स की तुलना करते हैं। दो अलग-अलग सी-एरे के मामले में उन पॉइंटर्स के पास समान मूल्य नहीं होगा।

Google मॉक के ASSERT_THAT मैक्रो और ContainerEq मैचर का उपयोग करने का आपका सबसे आसान तरीका है। ASSERT_EQ के बजाय,

ASSERT_THAT(two, ContainerEq(two_sorted)); 
+0

बस स्पष्ट करने के लिए - यह अभी भी सी पर काम नहीं करता है सरणी, है ना? क्योंकि सरणी की लंबाई को पास करने के लिए कहीं भी नहीं है? – dwanderson

+0

यह करता है - https://github.com/google/googletest/blob/786564fa4a3c8e0f908acca32cce481de5481b9f/googlemock/test/gmock-matchers_test.cc#L4311 देखें। – VladLosev

+0

ओह, कोई रास्ता नहीं! मैंने शपथ ली थी कि मैंने कोशिश की, लेकिन मुझे दोबारा जांच करनी होगी। शायद मुझे लगता है कि यह काम नहीं करेगा। इस पर ध्यान दिलाने के लिए धन्यवाद! – dwanderson

1

बारे में मुझे लगता है कि यह सिर्फ इस तरह लिखने के लिए पर्याप्त है।

EXPECT_EQ(memcmp(two, two_sorted, 2 * sizeof(int)), 0);

+0

एक स्पष्टीकरण प्रदान करता है कि आपका उत्तर क्यों काम करता है? –

संबंधित मुद्दे