8

I have read this official document, learn how to do Binary Comparison and String Comparison.

The ASSERT_EQ and ASSERT_STREQ could not work in the array comparison case.

For example

li@li:~/poc$ g++ -I${GTEST_DIR}/include insertion_sort.cpp insertion_sort_unittest.cpp /home/li/libgtest.a -lpthread -o inser_unit
li@li:~/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
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
alwaysday1
  • 1,683
  • 5
  • 20
  • 36
  • 2
    Maybe I should learn how to use googlemock, I found the answer [here](http://stackoverflow.com/questions/1460703/comparison-of-arrays-in-google-test) and [here](http://code.google.com/p/googletest/issues/detail?id=231) – alwaysday1 Apr 08 '12 at 06:04

3 Answers3

17

You don't need to add a dependency on googlemock if you don't want, you could write your own simple function that returns a testing::AssertionResult, e.g.

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

Then in your test, call:

    EXPECT_TRUE(ArraysMatch(two_sorted, two));
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • Compiling using MVSC (with gmock 1.11.0) returns error `error C2678: binary '<<': no operator found which takes a left-hand operand of type '_Ty'`, having to do with the custom string that was directed to `::testing::AssertionFailure()`. Any idea why is this? – MTV Apr 03 '22 at 21:46
8

ASSERT_EQ compares its arguments using operator==. Comparing with operator== works for std::vector's but not for C-arrays in C++. The reason is, when you try to use a value of a C-array in an expression, in most circumstances the value decays into a pointer pointing to the beginning of the memory where the array is stored. You end up comparing two pointers. In case of two different C-arrays those pointers will never have the same value.

Your easiest way out is to use Google Mock's ASSERT_THAT macro and the ContainerEq matcher. Instead of ASSERT_EQ, write

ASSERT_THAT(two, ContainerEq(two_sorted));
VladLosev
  • 7,296
  • 2
  • 35
  • 46
  • Just to clarify - this still doesn't work on C arrays, right? because there's nowhere to pass the length of the array(s)? – dwanderson Dec 10 '15 at 21:44
  • It does - see https://github.com/google/googletest/blob/786564fa4a3c8e0f908acca32cce481de5481b9f/googlemock/test/gmock-matchers_test.cc#L4311. – VladLosev Dec 30 '15 at 22:40
  • Oh, no way! I could've sworn I tried it, but I'll have to double-check. Maybe I just assumed it wouldn't work. Thanks for pointing this out! – dwanderson Dec 31 '15 at 14:17
5

I think it is enough to write just like this.

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

Wakana Nogami
  • 63
  • 1
  • 5