From: Shane Carr Date: Thu, 26 Apr 2018 01:33:59 +0000 (+0000) Subject: ICU-13725 Fixing various number test failures in MSVC. X-Git-Tag: release-62-rc~189 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e77603e3c3a8d6c1ab09b9cb0e92a8e6b830766d;p=icu ICU-13725 Fixing various number test failures in MSVC. X-SVN-Rev: 41281 --- diff --git a/icu4c/source/i18n/number_decimalquantity.cpp b/icu4c/source/i18n/number_decimalquantity.cpp index bb8bd8f2911..dd6f5ca23c6 100644 --- a/icu4c/source/i18n/number_decimalquantity.cpp +++ b/icu4c/source/i18n/number_decimalquantity.cpp @@ -380,7 +380,7 @@ DecimalQuantity &DecimalQuantity::setToDouble(double n) { setBcdToZero(); flags = 0; // signbit() from handles +0.0 vs -0.0 - if (std::signbit(n) != 0) { + if (std::signbit(n)) { flags |= NEGATIVE_FLAG; n = -n; } diff --git a/icu4c/source/i18n/numparse_parsednumber.cpp b/icu4c/source/i18n/numparse_parsednumber.cpp index f0e063b8b85..98da4e83192 100644 --- a/icu4c/source/i18n/numparse_parsednumber.cpp +++ b/icu4c/source/i18n/numparse_parsednumber.cpp @@ -11,6 +11,7 @@ #include "numparse_types.h" #include "number_decimalquantity.h" +#include "putilimp.h" #include using namespace icu; @@ -57,7 +58,9 @@ double ParsedNumber::getDouble() const { // Check for NaN, infinity, and -0.0 if (sawNaN) { - return NAN; + // Can't use NAN or std::nan because the byte pattern is platform-dependent; + // MSVC sets the sign bit, but Clang and GCC do not + return uprv_getNaN(); } if (sawInfinity) { if (0 != (flags & FLAG_NEGATIVE)) { @@ -85,7 +88,9 @@ void ParsedNumber::populateFormattable(Formattable& output, parse_flags_t parseF // Check for NaN, infinity, and -0.0 if (sawNaN) { - output.setDouble(NAN); + // Can't use NAN or std::nan because the byte pattern is platform-dependent; + // MSVC sets the sign bit, but Clang and GCC do not + output.setDouble(uprv_getNaN()); return; } if (sawInfinity) { diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 411cbdb4e33..2dbc30ddaed 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -450,7 +450,7 @@ UBool NumberFormatTestDataDriven::isParsePass( } return TRUE; } else if (tuple.output == "-0.0") { - if (std::signbit(result.getDouble()) == 0 || result.getDouble() != 0) { + if (!std::signbit(result.getDouble()) || result.getDouble() != 0) { appendErrorMessage.append(UnicodeString("Expected -0.0, but got: ") + result.getDouble()); return FALSE; } @@ -660,6 +660,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n TESTCASE_AUTO(TestParsePercentRegression); TESTCASE_AUTO(TestMultiplierWithScale); TESTCASE_AUTO(TestFastFormatInt32); + TESTCASE_AUTO(TestParseNaN); TESTCASE_AUTO_END; } @@ -8313,7 +8314,7 @@ void NumberFormatTest::TestCurrencyUsage() { UnicodeString original; fmt->format(agent,original); - assertEquals("Test Currency Usage 1", UnicodeString("PKR\u00A0124"), original); + assertEquals("Test Currency Usage 1", u"PKR\u00A0124", original); // test the getter here UCurrencyUsage curUsage = fmt->getCurrencyUsage(); @@ -8333,7 +8334,7 @@ void NumberFormatTest::TestCurrencyUsage() { UnicodeString cash_currency; fmt->format(agent,cash_currency); - assertEquals("Test Currency Usage 2", UnicodeString("PKR\u00A0124"), cash_currency); + assertEquals("Test Currency Usage 2", u"PKR\u00A0124", cash_currency); delete fmt; } @@ -8350,7 +8351,7 @@ void NumberFormatTest::TestCurrencyUsage() { UnicodeString original_rounding; fmt->format(agent, original_rounding); - assertEquals("Test Currency Usage 3", UnicodeString("CA$123.57"), original_rounding); + assertEquals("Test Currency Usage 3", u"CA$123.57", original_rounding); fmt->setCurrencyUsage(UCURR_USAGE_CASH, &status); }else{ fmt = (DecimalFormat *) NumberFormat::createInstance(enUS_CAD, UNUM_CASH_CURRENCY, status); @@ -8361,7 +8362,7 @@ void NumberFormatTest::TestCurrencyUsage() { UnicodeString cash_rounding_currency; fmt->format(agent, cash_rounding_currency); - assertEquals("Test Currency Usage 4", UnicodeString("CA$123.55"), cash_rounding_currency); + assertEquals("Test Currency Usage 4", u"CA$123.55", cash_rounding_currency); delete fmt; } @@ -8386,14 +8387,14 @@ void NumberFormatTest::TestCurrencyUsage() { UnicodeString cur_original; fmt->setCurrencyUsage(UCURR_USAGE_STANDARD, &status); fmt->format(agent, cur_original); - assertEquals("Test Currency Usage 5", UnicodeString("CA$123.57"), cur_original); + assertEquals("Test Currency Usage 5", u"CA$123.57", cur_original); fmt->setCurrency(CUR_PKR, status); assertSuccess("Set currency to PKR", status); UnicodeString PKR_changed; fmt->format(agent, PKR_changed); - assertEquals("Test Currency Usage 6", UnicodeString("PKR\u00A0124"), PKR_changed); + assertEquals("Test Currency Usage 6", u"PKR\u00A0124", PKR_changed); delete fmt; } } @@ -9120,4 +9121,17 @@ void NumberFormatTest::TestFastFormatInt32() { } } +void NumberFormatTest::TestParseNaN() { + IcuTestErrorCode status(*this, "TestParseNaN"); + + DecimalFormat df("0", { "en", status }, status); + Formattable parseResult; + df.parse(u"NaN", parseResult, status); + assertEquals("NaN should parse successfully", NAN, parseResult.getDouble()); + assertFalse("Result NaN should be positive", std::signbit(parseResult.getDouble())); + UnicodeString formatResult; + df.format(parseResult.getDouble(), formatResult); + assertEquals("NaN should round-trip", u"NaN", formatResult); +} + #endif /* #if !UCONFIG_NO_FORMATTING */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index 7c463a79d54..3239619bfcc 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -225,6 +225,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void TestParsePercentRegression(); void TestMultiplierWithScale(); void TestFastFormatInt32(); + void TestParseNaN(); private: UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f); diff --git a/icu4c/source/test/intltest/tsdcfmsy.cpp b/icu4c/source/test/intltest/tsdcfmsy.cpp index d033754f1d7..b4a31ac161c 100644 --- a/icu4c/source/test/intltest/tsdcfmsy.cpp +++ b/icu4c/source/test/intltest/tsdcfmsy.cpp @@ -201,15 +201,15 @@ void IntlTestDecimalFormatSymbols::testSymbols(/* char *par */) DecimalFormatSymbols sym(Locale::getUS(), status); UnicodeString customDecSeperator("S"); - Verify(34.5, (UnicodeString)"00.00", sym, (UnicodeString)"34.50"); + Verify(34.5, u"00.00", sym, u"34.50"); sym.setSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol, customDecSeperator); - Verify(34.5, (UnicodeString)"00.00", sym, (UnicodeString)"34S50"); - sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, (UnicodeString)"P"); - Verify(34.5, (UnicodeString)"00 %", sym, (UnicodeString)"3450 P"); - sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, (UnicodeString)"D"); - Verify(34.5, CharsToUnicodeString("\\u00a4##.##"), sym, (UnicodeString)"D 34.50"); - sym.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, (UnicodeString)"|"); - Verify(3456.5, (UnicodeString)"0,000.##", sym, (UnicodeString)"3|456S5"); + Verify(34.5, u"00.00", sym, u"34S50"); + sym.setSymbol(DecimalFormatSymbols::kPercentSymbol, u"P"); + Verify(34.5, u"00 %", sym, u"3450 P"); + sym.setSymbol(DecimalFormatSymbols::kCurrencySymbol, u"D"); + Verify(34.5, u"\u00a4##.##", sym, u"D\u00a034.50"); + sym.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, u"|"); + Verify(3456.5, u"0,000.##", sym, u"3|456S5"); }