/*
**********************************************************************
-* Copyright (C) 1997-2011, International Business Machines
+* Copyright (C) 1997-2012, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*
// 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
CASE(51,TestLenientParse);
CASE(52,TestAvailableNumberingSystems);
CASE(53,TestRoundingPattern);
+ CASE(54,Test9087);
default: name = ""; break;
}
}
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 */