Comparing arrays in google test?

I want to compare two arrays in Google Test. In UnitTest ++, this is done through CHECK_ARRAY_EQUAL. How do you do this in a Google test?

+68
c ++ unit-testing googletest
Sep 22 '09 at 15:12
source share
8 answers

I would really advise taking a look at the Google C ++ Mocking Framework . Even if you don’t want to mock something, it allows you to easily write quite complex statements.

for example

//checks that vector v is {5, 10, 15} ASSERT_THAT(v, ElementsAre(5, 10, 15)); //checks that map m only have elements 1 => 10, 2 => 20 ASSERT_THAT(m, ElementsAre(Pair(1, 10), Pair(2, 20))); //checks that in vector v all the elements are greater than 10 and less than 20 ASSERT_THAT(v, Each(AllOf(Gt(10), Lt(20)))); //checks that vector v consist of // 5, number greater than 10, anything. ASSERT_THAT(v, ElementsAre(5, Gt(10), _)); 

There are many matches for all kinds of situations, and you can combine them to achieve just about anything.

Did I tell you that ElementsAre only needs iterators and the size() method in the class? So it works not only with any container from STL, but also with custom containers.

Google Mock claims to be nearly as portable as Google Test, and to be honest, I don’t understand why you won’t use it. This is just awesome.

+94
May 9 '10 at
source share

If you just need to check if the arrays are equal, then brute force also works:

 int arr1[10]; int arr2[10]; // initialize arr1 and arr2 EXPECT_TRUE( 0 == std::memcmp( arr1, arr2, sizeof( arr1 ) ) ); 

However, this does not mean which element is different.

+15
Dec 17 '12 at 12:23
source share

If you want to compare a c-style array pointer with an array using Google Mock, you can go through std :: vector. For example:

 uint8_t expect[] = {1, 2, 3, 42}; uint8_t * buffer = expect; uint32_t buffer_size = sizeof(expect) / sizeof(expect[0]); ASSERT_THAT(std::vector<uint8_t>(buffer, buffer + buffer_size), ::testing::ElementsAreArray(expect)); 

Google Mock ElementsAreArray also accepts a pointer and a length that allow you to compare two pointers to a c-style array. For example:

 ASSERT_THAT(std::vector<uint8_t>(buffer, buffer + buffer_size), ::testing::ElementsAreArray(buffer, buffer_size)); 

For too long I tried to put this together. Thanks to this post, StackOverlow to remind you to initialize the std :: vector iterator. Note that this method will copy the elements of the buffer array to std :: vector before comparing.

+12
Sep 10 '13 at 14:16
source share

I had the same question, so I wrote a couple of macros that make comparisons between two generic containers. It is available for any container that has const_iterator , begin and end . If it fails, it will display a detailed message about where the array went wrong, and will do so for each element that fails; he will ensure that they are the same length; and the place in your code that it reports as a failure is the same line where you call EXPECT_ITERABLE_EQ( std::vector< double >, a, b) .

 //! Using the google test framework, check all elements of two containers #define EXPECT_ITERABLE_BASE( PREDICATE, REFTYPE, TARTYPE, ref, target) \ { \ const REFTYPE& ref_(ref); \ const TARTYPE& target_(target); \ REFTYPE::const_iterator refIter = ref_.begin(); \ TARTYPE::const_iterator tarIter = target_.begin(); \ unsigned int i = 0; \ while(refIter != ref_.end()) { \ if ( tarIter == target_.end() ) { \ ADD_FAILURE() << #target " has a smaller length than " #ref ; \ break; \ } \ PREDICATE(* refIter, * tarIter) \ << "Containers " #ref " (refIter) and " #target " (tarIter)" \ " differ at index " << i; \ ++refIter; ++tarIter; ++i; \ } \ EXPECT_TRUE( tarIter == target_.end() ) \ << #ref " has a smaller length than " #target ; \ } //! Check that all elements of two same-type containers are equal #define EXPECT_ITERABLE_EQ( TYPE, ref, target) \ EXPECT_ITERABLE_BASE( EXPECT_EQ, TYPE, TYPE, ref, target ) //! Check that all elements of two different-type containers are equal #define EXPECT_ITERABLE_EQ2( REFTYPE, TARTYPE, ref, target) \ EXPECT_ITERABLE_BASE( EXPECT_EQ, REFTYPE, TARTYPE, ref, target ) //! Check that all elements of two same-type containers of doubles are equal #define EXPECT_ITERABLE_DOUBLE_EQ( TYPE, ref, target) \ EXPECT_ITERABLE_BASE( EXPECT_DOUBLE_EQ, TYPE, TYPE, ref, target ) 

Hope this works for you (and that you really check this answer two months after posting your question).

+9
Nov 05 '09 at 16:03
source share

I had a similar problem when comparing arrays in google test.

Since I needed to compare with the base void* and char* (for low-level code testing), I do not do either mogle (which I also use in the project), or Seth a large macro can help me in a particular situation. I wrote the following macro:

 #define EXPECT_ARRAY_EQ(TARTYPE, reference, actual, element_count) \ {\ TARTYPE* reference_ = static_cast<TARTYPE *> (reference); \ TARTYPE* actual_ = static_cast<TARTYPE *> (actual); \ for(int cmp_i = 0; cmp_i < element_count; cmp_i++ ){\ EXPECT_EQ(reference_[cmp_i], actual_[cmp_i]);\ }\ } 

Techniques for macros can be used when comparing void* with other materials:

  void* retrieved = ptr->getData(); EXPECT_EQ(6, ptr->getSize()); EXPECT_ARRAY_EQ(char, "data53", retrieved, 6) 

In the comments, Tobias suggested casting void* into char* and using EXPECT_STREQ , a macro that I somehow missed earlier, which looks like the best alternative.

+4
Mar 10 2018-12-12T00:
source share

The following is the statement I wrote to compare [fragments] of two floating point arrays:

 /* See http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ for thorough information about comparing floating point values. For this particular application we know that the value range is -1 to 1 (audio signal), so we can compare to absolute delta of 1/2^22 which is the smallest representable value in a 22-bit recording. */ const float FLOAT_INEQUALITY_TOLERANCE = float(1.0 / (1 << 22)); template <class T> ::testing::AssertionResult AreFloatingPointArraysEqual( const T* const expected, const T* const actual, unsigned long length) { ::testing::AssertionResult result = ::testing::AssertionFailure(); int errorsFound = 0; const char* separator = " "; for (unsigned long index = 0; index < length; index++) { if (fabs(expected[index] - actual[index]) > FLOAT_INEQUALITY_TOLERANCE) { if (errorsFound == 0) { result << "Differences found:"; } if (errorsFound < 3) { result << separator << expected[index] << " != " << actual[index] << " @ " << index; separator = ", "; } errorsFound++; } } if (errorsFound > 0) { result << separator << errorsFound << " differences in total"; return result; } return ::testing::AssertionSuccess(); } 

Use within the Google Testing Framework:

 EXPECT_TRUE(AreFloatingPointArraysEqual(expectedArray, actualArray, lengthToCompare)); 

In the event of an error, something like the following output is created:

 ..\MyLibraryTestMain.cpp:145: Failure Value of: AreFloatingPointArraysEqual(expectedArray, actualArray, lengthToCompare) Actual: false (Differences found: 0.86119759082794189 != 0.86119747161865234 @ 14, -0.5552707314491272 != -0.55527061223983765 @ 24, 0.047732405364513397 != 0.04773232713341713 @ 36, 339 differences in total) Expected: true 

For a detailed discussion of comparing floating point values ​​in general, see this .

+4
Oct 28 '12 at 19:29
source share
 ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; for (int i = 0; i < x.size(); ++i) { EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i; } 

Source

+4
October 10 '15 at 21:43
source share

I used the classic loop through all the elements. You can use SCOPED_TRACE to read in which the iteration of the elements of the array is different. This gives you additional information compared to some other approaches and is easy to read.

 for (int idx=0; idx<ui16DataSize; idx++) { SCOPED_TRACE(idx); //write to the console in which iteration the error occurred ASSERT_EQ(array1[idx],array2[idx]); } 
+2
Jun 03 '15 at 8:37
source share



All Articles