]> granicus.if.org Git - icu/commitdiff
ICU-9087 Add special case for +-infinity in DigitList::set()
authorJohn Emmons <emmo@us.ibm.com>
Fri, 17 Feb 2012 04:00:49 +0000 (04:00 +0000)
committerJohn Emmons <emmo@us.ibm.com>
Fri, 17 Feb 2012 04:00:49 +0000 (04:00 +0000)
X-SVN-Rev: 31403

icu4c/source/i18n/digitlst.cpp
icu4c/source/test/intltest/numfmtst.cpp
icu4c/source/test/intltest/numfmtst.h

index db7a148a4dfcefdd861eb810993f5eecf4721b4d..b885a17cb212075d045fff5a76ad5322f17168ec 100644 (file)
@@ -1,6 +1,6 @@
 /*
 **********************************************************************
-*   Copyright (C) 1997-2011, International Business Machines
+*   Copyright (C) 1997-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 **********************************************************************
 *
@@ -754,7 +754,17 @@ DigitList::set(double source)
 
     // Generate a representation of the form /[+-][0-9].[0-9]+e[+-][0-9]+/
     // Can also generate /[+-]nan/ or /[+-]inf/
-    sprintf(rep, "%+1.*e", MAX_DBL_DIGITS - 1, source);
+    // TODO: Use something other than sprintf() here, since it's behavior is somewhat platform specific.
+    //       That is why infinity is special cased here.
+    if (uprv_isInfinite(source)) {
+        if (uprv_isNegativeInfinity(source)) {
+            uprv_strcpy(rep,"-inf"); // Handle negative infinity
+        } else {
+            uprv_strcpy(rep,"inf");
+        }
+    } else {
+        sprintf(rep, "%+1.*e", MAX_DBL_DIGITS - 1, source);
+    }
     U_ASSERT(uprv_strlen(rep) < sizeof(rep));
 
     // uprv_decNumberFromString() will parse the string expecting '.' as a
index 7685bc83cf375448b3bc40a7bea5892fe702c424..8ea62c014ba7c531dad1e64a4ad0c4589e2f3c67 100644 (file)
@@ -117,6 +117,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n
         CASE(51,TestLenientParse);
         CASE(52,TestAvailableNumberingSystems);
         CASE(53,TestRoundingPattern);
+        CASE(54,Test9087);
         default: name = ""; break;
     }
 }
@@ -6530,4 +6531,43 @@ void NumberFormatTest::TestAvailableNumberingSystems() {
 
     delete availableNumberingSystems;
 }
+
+void
+NumberFormatTest::Test9087(void)
+{
+    U_STRING_DECL(pattern,"#",1);
+    U_STRING_INIT(pattern,"#",1);
+    
+    U_STRING_DECL(infstr,"INF",3);
+    U_STRING_INIT(infstr,"INF",3);
+
+    U_STRING_DECL(nanstr,"NAN",3);
+    U_STRING_INIT(nanstr,"NAN",3);
+    
+    UChar outputbuf[50] = {0};
+    UErrorCode status = U_ZERO_ERROR;
+    UNumberFormat* fmt = unum_open(UNUM_PATTERN_DECIMAL,pattern,1,NULL,NULL,&status);
+    if ( U_FAILURE(status) ) {
+        errln("FAIL: error in unum_open()");
+    }
+
+    unum_setSymbol(fmt,UNUM_INFINITY_SYMBOL,infstr,3,&status);
+    unum_setSymbol(fmt,UNUM_NAN_SYMBOL,nanstr,3,&status);
+    if ( U_FAILURE(status) ) {
+        errln("FAIL: error setting symbols");
+    }
+
+    double inf = uprv_getInfinity();
+
+    unum_setAttribute(fmt,UNUM_ROUNDING_MODE,UNUM_ROUND_HALFEVEN);
+    unum_setDoubleAttribute(fmt,UNUM_ROUNDING_INCREMENT,0);
+
+    UFieldPosition position;
+    unum_formatDouble(fmt,inf,outputbuf,50,&position,&status);
+    
+    if ( u_strcmp(infstr, outputbuf)) {
+        errln((UnicodeString)"FAIL: unexpected result for infinity - expected " + infstr + " got " + outputbuf);
+    };
+
+}
 #endif /* #if !UCONFIG_NO_FORMATTING */
index 5cf7aec86c834180b3ce295b9a96f7e0d99c0cff..da44791fb19b36afea3c892d228f6a5a07cbdab1 100644 (file)
@@ -155,6 +155,7 @@ class NumberFormatTest: public CalendarTimeZoneTest {
     void TestExponentParse();
     void TestExplicitParents();
     void TestAvailableNumberingSystems();
+    void Test9087();
 
 
  private: