From ab53f0f24e6ed01124b189a285686087a337b31b Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 21 Feb 2019 20:53:15 +0100 Subject: QtTestLib: unify handling of float and double using suitable templates The QTest::qCompare() implementations were almost duplicates; pull the common code out into a templated version. Tweaked the QTest::toString() specialization for float and double (a macro) and fixed a bous modifier in double's format. The doubleComparisons and floatComparisons tests in the tst_float.cpp selftest shared a large block of tests in common, aside from the difference of type. Break this out into a templated static function to save duplication. This prepares the way for using the same templated code for qfloat16. Change-Id: I2823fd006910c5ff88335d625d1fa05cb7753513 Reviewed-by: Thiago Macieira --- src/testlib/qtestcase.cpp | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'src/testlib') diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 3ee8094ddf..0911c64e57 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2485,6 +2485,20 @@ bool QTest::compare_helper(bool success, const char *failureMsg, return QTestResult::compare(success, failureMsg, val1, val2, actual, expected, file, line); } +template +static bool floatingCompare(const T &t1, const T &t2) +{ + switch (std::fpclassify(t1)) + { + case FP_INFINITE: + return (t1 < 0) == (t2 < 0) && std::fpclassify(t2) == FP_INFINITE; + case FP_NAN: + return std::fpclassify(t2) == FP_NAN; + default: + return qFuzzyCompare(t1, t2); + } +} + /*! \fn bool QTest::qCompare(const qfloat16 &t1, const qfloat16 &t2, const char *actual, const char *expected, const char *file, int line) \internal */ @@ -2501,16 +2515,8 @@ bool QTest::qCompare(qfloat16 const &t1, qfloat16 const &t2, const char *actual, bool QTest::qCompare(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line) { - 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 floats are not the same (fuzzy compare)", + return compare_helper(floatingCompare(t1, t2), + "Compared floats are not the same (fuzzy compare)", toString(t1), toString(t2), actual, expected, file, line); } @@ -2520,16 +2526,8 @@ 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) { - 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)", + return compare_helper(floatingCompare(t1, t2), + "Compared doubles are not the same (fuzzy compare)", toString(t1), toString(t2), actual, expected, file, line); } @@ -2607,7 +2605,7 @@ template <> Q_TESTLIB_EXPORT char *QTest::toString(const TYPE &t) \ qstrncpy(msg, "nan", 128); \ break; \ default: \ - qsnprintf(msg, 128, #FORMAT, t); \ + qsnprintf(msg, 128, #FORMAT, double(t)); \ massageExponent(msg); \ break; \ } \ @@ -2615,7 +2613,7 @@ template <> Q_TESTLIB_EXPORT char *QTest::toString(const TYPE &t) \ } TO_STRING_FLOAT(float, %g) -TO_STRING_FLOAT(double, %.12lg) +TO_STRING_FLOAT(double, %.12g) template <> Q_TESTLIB_EXPORT char *QTest::toString(const qfloat16 &t) { -- cgit v1.2.3