]> granicus.if.org Git - icu/commitdiff
ICU-13725 Fixing various number test failures in MSVC.
authorShane Carr <shane@unicode.org>
Thu, 26 Apr 2018 01:33:59 +0000 (01:33 +0000)
committerShane Carr <shane@unicode.org>
Thu, 26 Apr 2018 01:33:59 +0000 (01:33 +0000)
X-SVN-Rev: 41281

icu4c/source/i18n/number_decimalquantity.cpp
icu4c/source/i18n/numparse_parsednumber.cpp
icu4c/source/test/intltest/numfmtst.cpp
icu4c/source/test/intltest/numfmtst.h
icu4c/source/test/intltest/tsdcfmsy.cpp

index bb8bd8f29113d060288208c6c80bd0c87bd5bca8..dd6f5ca23c6f0bde82f10d40b54205970c5dc8c6 100644 (file)
@@ -380,7 +380,7 @@ DecimalQuantity &DecimalQuantity::setToDouble(double n) {
     setBcdToZero();
     flags = 0;
     // signbit() from <math.h> handles +0.0 vs -0.0
-    if (std::signbit(n) != 0) {
+    if (std::signbit(n)) {
         flags |= NEGATIVE_FLAG;
         n = -n;
     }
index f0e063b8b85c35e8ad044ac7ba27bff8d31341df..98da4e8319227a662a2a3e35459cb9c1f6193d67 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "numparse_types.h"
 #include "number_decimalquantity.h"
+#include "putilimp.h"
 #include <cmath>
 
 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) {
index 411cbdb4e33f1d9fb537cbab3a172cc39015bbe2..2dbc30ddaed9e83b4bb7691351c06738c2225710 100644 (file)
@@ -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 */
index 7c463a79d54312c581a247a529026787f41f4baf..3239619bfcc74efbd9c816d6e375cc7861c41b02 100644 (file)
@@ -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);
index d033754f1d7a046b95c884614d4a67b8ef440475..b4a31ac161c1b7e7b549bd2a522ec68a0fc51820 100644 (file)
@@ -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");
 
 }