From: Hugo van der Merwe <17109322+hugovdm@users.noreply.github.com> Date: Thu, 30 Apr 2020 10:54:11 +0000 (+0200) Subject: Update code for correct NaN/Inf handling. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8cc7c94138e0a71c1ddd337bcfc8c149436fbbc8;p=icu Update code for correct NaN/Inf handling. --- diff --git a/icu4c/source/test/intltest/intltest.cpp b/icu4c/source/test/intltest/intltest.cpp index d222d46d883..9f4ae348ab9 100644 --- a/icu4c/source/test/intltest/intltest.cpp +++ b/icu4c/source/test/intltest/intltest.cpp @@ -2026,8 +2026,7 @@ UBool IntlTest::assertEquals(const char* message, double expected, double actual) { bool bothNaN = std::isnan(expected) && std::isnan(actual); - bool bothInf = std::isinf(expected) && std::isinf(actual); - if (expected != actual && !bothNaN && !bothInf) { + if (expected != actual && !bothNaN) { errln((UnicodeString)"FAIL: " + message + "; got " + actual + "; expected " + expected); @@ -2045,9 +2044,14 @@ UBool IntlTest::assertEquals(const char* message, double expected, double actual, double delta) { + if (std::isnan(delta) || std::isinf(delta)) { + errln((UnicodeString)("FAIL: ") + message + "; nonsensical delta " + delta + + " - delta may not be NaN or Inf"); + return FALSE; + } bool bothNaN = std::isnan(expected) && std::isnan(actual); - bool bothInf = std::isinf(expected) && std::isinf(actual); - if (abs(expected - actual) > delta && !bothNaN && !bothInf) { + double difference = abs(expected - actual); + if (expected != actual && (difference > delta || std::isnan(difference)) && !bothNaN) { errln((UnicodeString)("FAIL: ") + message + "; got " + actual + "; expected " + expected + "; acceptable delta " + delta); return FALSE; diff --git a/icu4c/source/test/intltest/intltest.h b/icu4c/source/test/intltest/intltest.h index b3eec2ba01b..ada0c0bfd6d 100644 --- a/icu4c/source/test/intltest/intltest.h +++ b/icu4c/source/test/intltest/intltest.h @@ -303,8 +303,8 @@ public: * Asserts that two doubles are equal to within a positive delta. Returns * false if they are not. * - * If the expected value is infinity then the delta value is ignored. NaNs - * are considered equal: assertEquals(msg, NaN, NaN, *) passes. + * NaNs are considered equal: assertEquals(msg, NaN, NaN, *) passes. + * Infs are considered equal: assertEquals(msg, inf, inf, *) passes. * * @param message - the identifying message for the AssertionError. * @param expected - expected value. @@ -339,8 +339,8 @@ public: * Asserts that two doubles are equal to within a positive delta. Returns * false if they are not. * - * If the expected value is infinity then the delta value is ignored. NaNs - * are considered equal: assertEquals(msg, NaN, NaN, *) passes. + * NaNs are considered equal: assertEquals(msg, NaN, NaN, *) passes. + * Infs are considered equal: assertEquals(msg, inf, inf, *) passes. * * @param message - the identifying message for the AssertionError. * @param expected - expected value.