From b8281ee69ee7c83505a9ca8702f3a5199bdcd045 Mon Sep 17 00:00:00 2001 From: John Emmons Date: Fri, 17 Feb 2012 04:00:49 +0000 Subject: [PATCH] ICU-9087 Add special case for +-infinity in DigitList::set() X-SVN-Rev: 31403 --- icu4c/source/i18n/digitlst.cpp | 14 +++++++-- icu4c/source/test/intltest/numfmtst.cpp | 40 +++++++++++++++++++++++++ icu4c/source/test/intltest/numfmtst.h | 1 + 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/icu4c/source/i18n/digitlst.cpp b/icu4c/source/i18n/digitlst.cpp index db7a148a4df..b885a17cb21 100644 --- a/icu4c/source/i18n/digitlst.cpp +++ b/icu4c/source/i18n/digitlst.cpp @@ -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 diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 7685bc83cf3..8ea62c014ba 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -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 */ diff --git a/icu4c/source/test/intltest/numfmtst.h b/icu4c/source/test/intltest/numfmtst.h index 5cf7aec86c8..da44791fb19 100644 --- a/icu4c/source/test/intltest/numfmtst.h +++ b/icu4c/source/test/intltest/numfmtst.h @@ -155,6 +155,7 @@ class NumberFormatTest: public CalendarTimeZoneTest { void TestExponentParse(); void TestExplicitParents(); void TestAvailableNumberingSystems(); + void Test9087(); private: -- 2.40.0