c++googletest

Google Test Check for Float Not Equality


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)


Solution

  • 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.