From dc44c2bdd9a5352a06b9d15052b9655251808ad3 Mon Sep 17 00:00:00 2001 From: "Steven R. Loomis" Date: Wed, 30 May 2012 18:21:49 +0000 Subject: [PATCH] ICU-9258 fix int64 format of U_INT64_MIN; // -9223372036854775808L X-SVN-Rev: 31888 --- icu4c/source/i18n/decimfmt.cpp | 17 +++-- icu4c/source/test/intltest/numfmtst.cpp | 87 +++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/icu4c/source/i18n/decimfmt.cpp b/icu4c/source/i18n/decimfmt.cpp index ff15a14765a..1a31b876fa1 100644 --- a/icu4c/source/i18n/decimfmt.cpp +++ b/icu4c/source/i18n/decimfmt.cpp @@ -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; - } - do { + 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; + } + // 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 diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index 734465c6f3c..ee8aa3dfd19 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -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 = -9223372036854775807L; + 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 */ -- 2.40.0