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 <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2018-01-09 18:21:45 -08:00
parent 6e8e9979d0
commit 79493a3ee1

View File

@ -75,6 +75,7 @@
#include <QtTest/private/qtestutil_macos_p.h>
#endif
#include <cmath>
#include <numeric>
#include <algorithm>
@ -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);
}