Re: [googletest] Is there an easy way of getting the functionality of EXPECT_DOUBLE_EQ from a custom predicate?

262 views
Skip to first unread message

Vlad Losev

unread,
Mar 25, 2013, 10:18:08 AM3/25/13
to Google C++ Testing Framework
I don't think there is a ready made solution for this case. You can roll your own comparison predicate as described in https://code.google.com/p/googletest/wiki/AdvancedGuide#Using_a_Predicate-Formatter and then define an EXPECT_POINT_EQ macro using it. The algorithm to perform ULP comparison is shown at http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm.

HTH,
Vlad

On Thu, Mar 21, 2013 at 4:23 PM, Mike McNees <mmc...@gmail.com> wrote:
In the tests I am working on I very often compare two variables that are structs with doubles in them ie:

typedef struct
{
double i;
double j;
} point;


Instead of always using:

EXPECT_DOUBLE_EQ(point1.i, point2.i);
EXPECT_DOUBLE_EQ(point1.j, point2.j);

I would like to use:

EXPECT_POINT_EQ(point1, point2);

and have a predicate that does the compare, but I would like to have the same 4 ULP bounds. So I am wondering if there is an easy way to use EXPECT_DOUBLE_EQ or even EXPECT_NEAR inside of a predicate? Or will I need to implement that myself? Any ideas?

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

Zhanyong Wan (λx.x x)

unread,
Mar 25, 2013, 12:16:49 PM3/25/13
to Vlad Losev, Google C++ Testing Framework
Or, you can use googlemock and define a custom matcher using the stock DoubleEq() matcher, which does the same ULP-based comparison as googletest.


using testing::DoubleEq;
using testing::Value;

MATCHER_P(PointEq, rhs, "") {
  return Value(arg.i, DoubleEq(rhs.i)) && Value(arg.j, DoubleEq(rhs.j));
}

...
EXPECT_THAT(point1, PointEq(point2));
--
Zhanyong
Reply all
Reply to author
Forward
0 new messages