diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2018-01-09 18:21:45 -0800 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2018-03-21 14:23:13 +0000 |
commit | 79493a3ee129c014298c6ef694415b8f0fffd74b (patch) | |
tree | e6e3041f01f07eb9b9aca43c72ee738e1cf3d11e /src | |
parent | 6e8e9979d0786d7d730528c2888e0e1af4fb96ca (diff) |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/testlib/qtestcase.cpp | 12 |
1 files changed, 11 insertions, 1 deletions
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 <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); } |