ICU-9287 merge of r31888,r31889 #9258 - fix for U_INT64_MIN formatting

X-SVN-Rev: 31890
This commit is contained in:
Steven R. Loomis 2012-05-30 23:33:55 +00:00
parent 1f2a99f86c
commit 9299168445
2 changed files with 96 additions and 6 deletions

View file

@ -1078,7 +1078,7 @@ DecimalFormat::_format(int64_t number,
printf("fastpath? [%d]\n", number);
#endif
if( data.fFastpathStatus==kFastpathYES ) {
if( data.fFastpathStatus==kFastpathYES) {
#define kZero 0x0030
const int32_t MAX_IDX = MAX_DIGITS+2;
@ -1087,14 +1087,17 @@ DecimalFormat::_format(int64_t number,
outputStr[--destIdx] = 0; // term
int64_t n = number;
if (number < 0) { // Negative numbers are slightly larger than a postive
//outputStr[--destIdx] = (char)(-(n % 10) + kZero);
n *= -1;
if (number < 1) {
// Negative numbers are slightly larger than positive
// output the first digit (or the leading zero)
outputStr[--destIdx] = (-(n % 10) + kZero);
n /= -10;
}
do {
// get any remaining digits
while (n > 0) {
outputStr[--destIdx] = (n % 10) + kZero;
n /= 10;
} while (n > 0);
}
// Slide the number to the start of the output str

View file

@ -6587,6 +6587,93 @@ void NumberFormatTest::TestFormatFastpaths() {
#else
infoln("NOTE: UCONFIG_FORMAT_FASTPATHS not set, test skipped.");
#endif
// get some additional case
{
UErrorCode status=U_ZERO_ERROR;
DecimalFormat df(UnicodeString("0000",""),status);
int64_t long_number = 1;
UnicodeString expect = "0001";
UnicodeString result;
FieldPosition pos;
df.format(long_number, result, pos);
if(U_FAILURE(status)||expect!=result) {
errln("FAIL: expected '"+expect+"' got '"+result+"' status "+UnicodeString(u_errorName(status),""));
} else {
logln("OK: got expected '"+result+"' status "+UnicodeString(u_errorName(status),""));
}
}
{
UErrorCode status=U_ZERO_ERROR;
DecimalFormat df(UnicodeString("0000000000000000000",""),status);
int64_t long_number = U_INT64_MIN; // -9223372036854775808L;
// uint8_t bits[8];
// memcpy(bits,&long_number,8);
// for(int i=0;i<8;i++) {
// logln("bits: %02X", (unsigned int)bits[i]);
// }
UnicodeString expect = "-9223372036854775808";
UnicodeString result;
FieldPosition pos;
df.format(long_number, result, pos);
if(U_FAILURE(status)||expect!=result) {
errln("FAIL: expected '"+expect+"' got '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on -9223372036854775808");
} else {
logln("OK: got expected '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on -9223372036854775808");
}
}
{
UErrorCode status=U_ZERO_ERROR;
DecimalFormat df(UnicodeString("0000000000000000000",""),status);
int64_t long_number = U_INT64_MAX; // -9223372036854775808L;
// uint8_t bits[8];
// memcpy(bits,&long_number,8);
// for(int i=0;i<8;i++) {
// logln("bits: %02X", (unsigned int)bits[i]);
// }
UnicodeString expect = "9223372036854775807";
UnicodeString result;
FieldPosition pos;
df.format(long_number, result, pos);
if(U_FAILURE(status)||expect!=result) {
errln("FAIL: expected '"+expect+"' got '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on U_INT64_MAX");
} else {
logln("OK: got expected '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on U_INT64_MAX");
}
}
{
UErrorCode status=U_ZERO_ERROR;
DecimalFormat df(UnicodeString("0000000000000000000",""),status);
int64_t long_number = 0;
// uint8_t bits[8];
// memcpy(bits,&long_number,8);
// for(int i=0;i<8;i++) {
// logln("bits: %02X", (unsigned int)bits[i]);
// }
UnicodeString expect = "0000000000000000000";
UnicodeString result;
FieldPosition pos;
df.format(long_number, result, pos);
if(U_FAILURE(status)||expect!=result) {
errln("FAIL: expected '"+expect+"' got '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on 0");
} else {
logln("OK: got expected '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on 0");
}
}
{
UErrorCode status=U_ZERO_ERROR;
DecimalFormat df(UnicodeString("0000000000000000000",""),status);
int64_t long_number = U_INT64_MIN + 1;
UnicodeString expect = "-9223372036854775807";
UnicodeString result;
FieldPosition pos;
df.format(long_number, result, pos);
if(U_FAILURE(status)||expect!=result) {
errln("FAIL: expected '"+expect+"' got '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on -9223372036854775807");
} else {
logln("OK: got expected '"+result+"' status "+UnicodeString(u_errorName(status),"")+" on -9223372036854775807");
}
}
}
#endif /* #if !UCONFIG_NO_FORMATTING */