From 79493a3ee129c014298c6ef694415b8f0fffd74b Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 9 Jan 2018 18:21:45 -0800 Subject: Make QCOMPARE(-inf, -inf) and QCOMPARE(NaN, NaN) succeed This will make two floating points containing NaN compare as equal, instead of the regular nan != nan IEEE behavior (which isn't very useful in a unit-test framework). Note that this does not apply to indirect comparisons, for example via QVariant. Change-Id: I39332e0a867442d58082fffd150851acfdd18c23 Reviewed-by: Lars Knoll --- src/testlib/qtestcase.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index d03eb4c2c0..adf4b9e1ef 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -75,6 +75,7 @@ #include #endif +#include #include #include @@ -2443,7 +2444,16 @@ bool QTest::qCompare(float const &t1, float const &t2, const char *actual, const bool QTest::qCompare(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) { - return compare_helper(qFuzzyCompare(t1, t2), "Compared doubles are not the same (fuzzy compare)", + bool equal = false; + int cl1 = std::fpclassify(t1); + int cl2 = std::fpclassify(t2); + if (cl1 == FP_INFINITE) + equal = ((t1 < 0) == (t2 < 0)) && cl2 == FP_INFINITE; + else if (cl1 == FP_NAN) + equal = (cl2 == FP_NAN); + else + equal = qFuzzyCompare(t1, t2); + return compare_helper(equal, "Compared doubles are not the same (fuzzy compare)", toString(t1), toString(t2), actual, expected, file, line); } -- cgit v1.2.3