Checking if floating-point arrays are nearly equal

3,321 views
Skip to first unread message

Daniel P. Volpato

unread,
Jul 5, 2013, 9:40:11 AM7/5/13
to googl...@googlegroups.com
Hi everyone,

I couldn't find a way to check if two floating-point arrays matches (they don't need to be exactly equal, just nearly equal each other). For integer arrays and other sorts of arrays that can use operator ==, I was successfull using ElementsAreArray() like this:

const int expected_array[kArraySize] = { ... };
int actual_array[kArraySize];
// call method that fills actual_array
EXPECT_THAT(expected_array, ::testing::ElementsAreArray(actual_array, array_size));

But if the arrays are floating-point (float or double), ElementsAreArray also uses matcher Eq(), so my test fails. How can I check if two FP arrays are nearly equal? Is there any built-in way to apply matcher FloatEq() to all elements of an array?

Thanks,
Daniel P. Volpato

Zhanyong Wan (λx.x x)

unread,
Jul 7, 2013, 6:52:58 PM7/7/13
to Daniel P. Volpato, Google C++ Mocking Framework
You can give ElementsAreArray() an array of matchers instead of an array of values, so one way to do it is:

const Matcher<double> expected_array[kArraySize] = {
  DoubleEq(1.0),
  ...
  DoubleEq(4.2)
};
...
EXPECT_THAT(expected_array, ElementsAreArray(expected_array));

Another way is to define a Matcher<tr1::tuple<double, double> > that succeeds when the argument is a pair of almost equal doubles (you can define it in terms of DoubleEq(x)).  You can then use it in Pointwise():

const double expected_array[kArraySize] = { 1.0, ..., 4.2 };
...
EXPECT_THAT(actual_array, Pointwise(DoubleEqual(), expected_array));

Thanks,
Daniel P. Volpato

--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Zhanyong

Daniel P. Volpato

unread,
Jul 10, 2013, 8:16:24 AM7/10/13
to Zhanyong Wan (λx.x x), Google C++ Mocking Framework
On Sun, Jul 7, 2013 at 7:52 PM, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
But if the arrays are floating-point (float or double), ElementsAreArray also uses matcher Eq(), so my test fails. How can I check if two FP arrays are nearly equal? Is there any built-in way to apply matcher FloatEq() to all elements of an array?

You can give ElementsAreArray() an array of matchers instead of an array of values, so one way to do it is:

const Matcher<double> expected_array[kArraySize] = {
  DoubleEq(1.0),
  ...
  DoubleEq(4.2)
};
...
EXPECT_THAT(expected_array, ElementsAreArray(expected_array));

Another way is to define a Matcher<tr1::tuple<double, double> > that succeeds when the argument is a pair of almost equal doubles (you can define it in terms of DoubleEq(x)).  You can then use it in Pointwise():

const double expected_array[kArraySize] = { 1.0, ..., 4.2 };
...
EXPECT_THAT(actual_array, Pointwise(DoubleEqual(), expected_array));
 
-- 
Zhanyong

Thanks.
It never occurred to me to use an array of matchers. That worked really well.

--
Daniel

László Veréb

unread,
Aug 27, 2013, 7:25:48 AM8/27/13
to googl...@googlegroups.com, Daniel P. Volpato

Hi!
Could you give me an example on how to define  a Matcher<tr1::tuple<double, double> >? Don't know how to google it, and could not find out from headers.
Thanks.
Reply all
Reply to author
Forward
0 new messages