Google Test Check for Float Not Equality

Viewed 523

Is there an expectation for a float value not being equal to the second value?

E.g the opposite of EXPECT_FLOAT_EQ(val1,val2)

2 Answers

I don't think GTest has this functionality, they explain in their documentation

Some floating-point operations are useful, but not that often used. In order to avoid an explosion of new macros, we provide them as predicate-format functions that can be used in predicate assertion macros (e.g. EXPECT_PRED_FORMAT2, etc).

You can define a predicate, as suggested by @idclev 463035818, and pass it into the EXPECT_PRED_FORMAT2(pred, val1, val2). The predicate must conform to the signature:

testing::AssertionResult pred(const char* expr1, const char* expr2,
                                    double val1, double val2);

Where expr1 & expr2 are the string representation of the float arguments that will be used in the assertion messages.

You can use Google Mock's floating point matchers. They can be easily composed into more complex assertions:

EXPECT_THAT(val1, Not(DoubleEq(val2)))
Related