views:

266

answers:

3

I am looking to compare two arrays in google test. In UnitTest++ this is done through CHECK_ARRAY_EQUAL. How do you do it in google test?

A: 

http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#How%5Fto%5FWrite%5FValue-Parameterized%5FTests

More specific:

ValuesIn(container) and ValuesIn(begin, end): Yields values from a C-style array, an STL-style container, or an iterator range [begin, end).

See if this helps you

coelhudo
ValuesIn seems to generate parameters for Value-Parameterized tests. I cannot see that you could use it to compare arrays.
Tobbe
Yes, sorry. I didn't find similar funcion/macro in google test too. If I had to test two arrays, or I will use std::vector (or std::array, -std=c++0x for gcc) instead or put ASSERT_EQ inside a while/for loop.Sorry again for the mistake.
coelhudo
+3  A: 

I had the exact same question, so I wrote a couple of macros that do comparisons between two generic containers. It's extensible to ANY container that has const_iterator, begin, and end. If it fails, it will display a verbose message of where the array went wrong and will do so for every element that fails; it will make sure they're the same length; and the location in your code that it reports as failing 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 actually check this answer two months after your question was submitted).

Seth Johnson
That's a great approach! Maybe you could provide this to google so they add it to the framework?
Tobbe
They said (http://code.google.com/p/googletest/issues/detail?id=231) that they discourage adding macros, and this functionality is available to some extent in the Google Mock framework.
Seth Johnson
+2  A: 

I would really suggest looking at Google C++ Mocking Framework. Even if you don't want to mock anything, it allows you to write rather complicated assertions with ease.

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's plenty of matchers for every possible situations, and you can combine them to achieve almost anything.

Did I told you that ElementsAre needs only iterators and size() method on a class to work? So it not only works with any container from STL but with custom containers also.

Google Mock claims to be almost as portable as Google Test and frankly I don't see why you wouldn't use it. It is just purely awesome.

vava
I do use google mock. And I agree that it is awesome. I never expected to see something like it for C++.
Tobbe