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) { diff --git a/tests/auto/testlib/selftests/expected_float.lightxml b/tests/auto/testlib/selftests/expected_float.lightxml index 458f2277e6..54d6eabdbd 100644 --- a/tests/auto/testlib/selftests/expected_float.lightxml +++ b/tests/auto/testlib/selftests/expected_float.lightxml @@ -8,15 +8,15 @@ - - - + + + - - - + + + +static void nonFinite_data(F zero, F one) +{ + using Bounds = std::numeric_limits; + + // QCOMPARE special-cases non-finite values + if (Bounds::has_quiet_NaN) { + const F nan = Bounds::quiet_NaN(); + QTest::newRow("should PASS: NaN == NaN") << nan << nan; + QTest::newRow("should FAIL: NaN != 0") << nan << zero; + QTest::newRow("should FAIL: 0 != NaN") << zero << nan; + QTest::newRow("should FAIL: NaN != 1") << nan << one; + QTest::newRow("should FAIL: 1 != NaN") << one << nan; + } + + if (Bounds::has_infinity) { + const F uge = Bounds::infinity(); + QTest::newRow("should PASS: inf == inf") << uge << uge; + QTest::newRow("should PASS: -inf == -inf") << -uge << -uge; + QTest::newRow("should FAIL: inf != -inf") << uge << -uge; + QTest::newRow("should FAIL: -inf != inf") << -uge << uge; + if (Bounds::has_quiet_NaN) { + const F nan = Bounds::quiet_NaN(); + QTest::newRow("should FAIL: inf != nan") << uge << nan; + QTest::newRow("should FAIL: nan != inf") << nan << uge; + QTest::newRow("should FAIL: -inf != nan") << -uge << nan; + QTest::newRow("should FAIL: nan != -inf") << nan << -uge; + } + QTest::newRow("should FAIL: inf != 0") << uge << zero; + QTest::newRow("should FAIL: 0 != inf") << zero << uge; + QTest::newRow("should FAIL: -inf != 0") << -uge << zero; + QTest::newRow("should FAIL: 0 != -inf") << zero << -uge; + QTest::newRow("should FAIL: inf != 1") << uge << one; + QTest::newRow("should FAIL: 1 != inf") << one << uge; + QTest::newRow("should FAIL: -inf != 1") << -uge << one; + QTest::newRow("should FAIL: 1 != -inf") << one << -uge; + + const F big = Bounds::max(); + QTest::newRow("should FAIL: inf != max") << uge << big; + QTest::newRow("should FAIL: inf != -max") << uge << -big; + QTest::newRow("should FAIL: max != inf") << big << uge; + QTest::newRow("should FAIL: -max != inf") << -big << uge; + QTest::newRow("should FAIL: -inf != max") << -uge << big; + QTest::newRow("should FAIL: -inf != -max") << -uge << -big; + QTest::newRow("should FAIL: max != -inf") << big << -uge; + QTest::newRow("should FAIL: -max != -inf") << -big << -uge; + } +} + void tst_float::doubleComparisons() const { QFETCH(double, operandLeft); @@ -58,65 +107,26 @@ void tst_float::doubleComparisons_data() const { QTest::addColumn("operandLeft"); QTest::addColumn("operandRight"); + double zero(0.), one(1.); - QTest::newRow("should PASS 1") << 0. << 0.; - QTest::newRow("should FAIL 1") << 1.00000 << 3.00000; - QTest::newRow("should FAIL 2") << 1.00000e-7 << 3.00000e-7; + QTest::newRow("should FAIL 1") << one << 3.; + QTest::newRow("should PASS 1") << zero << zero; + QTest::newRow("should FAIL 2") << 1.e-7 << 3.e-7; // QCOMPARE for doubles uses qFuzzyCompare(), which succeeds if the numbers // differ by no more than 1e-12 times the smaller value. Thus // QCOMPARE(1e12-2, 1e12-1) should fail, while QCOMPARE(1e12+1, 1e12+2) // should pass. - QTest::newRow("should PASS 2") << 1e12 + 1. << 1e12 + 2.; - QTest::newRow("should FAIL 3") << 1e12 - 1. << 1e12 - 2.; + QTest::newRow("should PASS 2") << 1e12 + one << 1e12 + 2.; + QTest::newRow("should FAIL 3") << 1e12 - one << 1e12 - 2.; // ... but rounding makes that a bit unrelaible when scaled close to the bounds. QTest::newRow("should PASS 3") << 1e-310 + 1e-322 << 1e-310 + 2e-322; QTest::newRow("should FAIL 4") << 1e-310 - 1e-322 << 1e-310 - 3e-322; QTest::newRow("should PASS 4") << 1e307 + 1e295 << 1e307 + 2e295; QTest::newRow("should FAIL 5") << 1e307 - 1e295 << 1e307 - 3e295; - // QCOMPARE special-cases non-finite values - if (std::numeric_limits::has_quiet_NaN) { - const double nan = std::numeric_limits::quiet_NaN(); - QTest::newRow("should PASS: NaN == NaN") << nan << nan; - QTest::newRow("should FAIL: NaN != 0") << nan << 0.; - QTest::newRow("should FAIL: 0 != NaN") << 0. << nan; - QTest::newRow("should FAIL: NaN != 1") << nan << 1.; - QTest::newRow("should FAIL: 1 != NaN") << 1. << nan; - } - if (std::numeric_limits::has_infinity) { - const double uge = std::numeric_limits::infinity(); - QTest::newRow("should PASS: inf == inf") << uge << uge; - QTest::newRow("should PASS: -inf == -inf") << -uge << -uge; - QTest::newRow("should FAIL: inf != -inf") << uge << -uge; - QTest::newRow("should FAIL: -inf != inf") << -uge << uge; - if (std::numeric_limits::has_quiet_NaN) { - const double nan = std::numeric_limits::quiet_NaN(); - QTest::newRow("should FAIL: inf != nan") << uge << nan; - QTest::newRow("should FAIL: nan != inf") << nan << uge; - QTest::newRow("should FAIL: -inf != nan") << -uge << nan; - QTest::newRow("should FAIL: nan != -inf") << nan << -uge; - } - QTest::newRow("should FAIL: inf != 0") << uge << 0.; - QTest::newRow("should FAIL: 0 != inf") << 0. << uge; - QTest::newRow("should FAIL: -inf != 0") << -uge << 0.; - QTest::newRow("should FAIL: 0 != -inf") << 0. << -uge; - QTest::newRow("should FAIL: inf != 1") << uge << 1.; - QTest::newRow("should FAIL: 1 != inf") << 1. << uge; - QTest::newRow("should FAIL: -inf != 1") << -uge << 1.; - QTest::newRow("should FAIL: 1 != -inf") << 1. << -uge; - - const double big = std::numeric_limits::max(); - QTest::newRow("should FAIL: inf != max") << uge << big; - QTest::newRow("should FAIL: inf != -max") << uge << -big; - QTest::newRow("should FAIL: max != inf") << big << uge; - QTest::newRow("should FAIL: -max != inf") << -big << uge; - QTest::newRow("should FAIL: -inf != max") << -uge << big; - QTest::newRow("should FAIL: -inf != -max") << -uge << -big; - QTest::newRow("should FAIL: max != -inf") << big << -uge; - QTest::newRow("should FAIL: -max != -inf") << -big << -uge; - } + nonFinite_data(zero, one); } void tst_float::floatComparisons() const @@ -131,65 +141,26 @@ void tst_float::floatComparisons_data() const { QTest::addColumn("operandLeft"); QTest::addColumn("operandRight"); + float zero(0.f), one(1.f); - QTest::newRow("should FAIL 1") << 1.00000f << 3.00000f; - QTest::newRow("should PASS 1") << 0.f << 0.f; - QTest::newRow("should FAIL 2") << 1.00000e-7f << 3.00000e-7f; + QTest::newRow("should FAIL 1") << one << 3.f; + QTest::newRow("should PASS 1") << zero << zero; + QTest::newRow("should FAIL 2") << 1.e-7f << 3.e-7f; // QCOMPARE for floats uses qFuzzyCompare(), which succeeds if the numbers // differ by no more than 1e-5 times the smaller value. Thus // QCOMPARE(1e5-2, 1e5-1) should fail, while QCOMPARE(1e5+1, 1e5+2) // should pass. - QTest::newRow("should PASS 2") << 1e5f + 1.f << 1e5f + 2.f; - QTest::newRow("should FAIL 3") << 1e5f - 1.f << 1e5f - 2.f; + QTest::newRow("should PASS 2") << 1e5f + one << 1e5f + 2.f; + QTest::newRow("should FAIL 3") << 1e5f - one << 1e5f - 2.f; // ... but rounding makes that a bit unrelaible when scaled close to the bounds. QTest::newRow("should PASS 3") << 1e-39f + 1e-44f << 1e-39f + 2e-44f; QTest::newRow("should FAIL 4") << 1e-39f - 1e-44f << 1e-39f - 3e-44f; QTest::newRow("should PASS 4") << 1e38f + 1e33f << 1e38f + 2e33f; QTest::newRow("should FAIL 5") << 1e38f - 1e33f << 1e38f - 3e33f; - // QCOMPARE special-cases non-finite values - if (std::numeric_limits::has_quiet_NaN) { - const float nan = std::numeric_limits::quiet_NaN(); - QTest::newRow("should PASS: NaN == NaN") << nan << nan; - QTest::newRow("should FAIL: NaN != 0") << nan << 0.f; - QTest::newRow("should FAIL: 0 != NaN") << 0.f << nan; - QTest::newRow("should FAIL: NaN != 1") << nan << 1.f; - QTest::newRow("should FAIL: 1 != NaN") << 1.f << nan; - } - if (std::numeric_limits::has_infinity) { - const float uge = std::numeric_limits::infinity(); - QTest::newRow("should PASS: inf == inf") << uge << uge; - QTest::newRow("should PASS: -inf == -inf") << -uge << -uge; - QTest::newRow("should FAIL: inf != -inf") << uge << -uge; - QTest::newRow("should FAIL: -inf != inf") << -uge << uge; - if (std::numeric_limits::has_quiet_NaN) { - const float nan = std::numeric_limits::quiet_NaN(); - QTest::newRow("should FAIL: inf != nan") << uge << nan; - QTest::newRow("should FAIL: nan != inf") << nan << uge; - QTest::newRow("should FAIL: -inf != nan") << -uge << nan; - QTest::newRow("should FAIL: nan != -inf") << nan << -uge; - } - QTest::newRow("should FAIL: inf != 0") << uge << 0.f; - QTest::newRow("should FAIL: 0 != inf") << 0.f << uge; - QTest::newRow("should FAIL: -inf != 0") << -uge << 0.f; - QTest::newRow("should FAIL: 0 != -inf") << 0.f << -uge; - QTest::newRow("should FAIL: inf != 1") << uge << 1.f; - QTest::newRow("should FAIL: 1 != inf") << 1.f << uge; - QTest::newRow("should FAIL: -inf != 1") << -uge << 1.f; - QTest::newRow("should FAIL: 1 != -inf") << 1.f << -uge; - - const float big = std::numeric_limits::max(); - QTest::newRow("should FAIL: inf != max") << uge << big; - QTest::newRow("should FAIL: inf != -max") << uge << -big; - QTest::newRow("should FAIL: max != inf") << big << uge; - QTest::newRow("should FAIL: -max != inf") << -big << uge; - QTest::newRow("should FAIL: -inf != max") << -uge << big; - QTest::newRow("should FAIL: -inf != -max") << -uge << -big; - QTest::newRow("should FAIL: max != -inf") << big << -uge; - QTest::newRow("should FAIL: -max != -inf") << -big << -uge; - } + nonFinite_data(zero, one); } void tst_float::float16Comparisons() const