ICU-9087 Add special case for +-infinity in DigitList::set()

X-SVN-Rev: 31403
This commit is contained in:
John Emmons 2012-02-17 04:00:49 +00:00
parent 7d681bcd8d
commit b8281ee69e
3 changed files with 53 additions and 2 deletions

View 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

View 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 */

View File

@ -155,6 +155,7 @@ class NumberFormatTest: public CalendarTimeZoneTest {
void TestExponentParse();
void TestExplicitParents();
void TestAvailableNumberingSystems();
void Test9087();
private: