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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- How to replace file-access references for a module
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- What is a good way of cleaning up after a unit tes
-
EF6 DbSet
returns null in Moq - Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
If you want to compare a c-style array pointer to an array using Google Mock, you can go through std::vector. For example:
Google Mock's ElementsAreArray also accepts pointer and length which allow comparison of two c-style array pointers. For example:
I spent far too long trying to piece this together. Thanks to this StackOverlow post for the reminder on std::vector iterator initialization. Note that this method will copy the buffer array elements into the std::vector before the comparison.
Source
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
, andend
. 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 callEXPECT_ITERABLE_EQ( std::vector< double >, a, b)
.Hope this works for you (and that you actually check this answer two months after your question was submitted).
I used a classic loop through all elements. You can use SCOPED_TRACE to read out in which iteration the array elements differ. This provides you with additional information compared to some other approaches and is easy to read.
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
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 onlyiterators
andsize()
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.
Below is an assertion I wrote to compare [fragments of] two floating point arrays:
Usage within the Google Testing Framework is this:
In case of an error, something like the following output is produced:
For thorough discussion on comparing floating point values in general, please see this.