]> granicus.if.org Git - icu/commitdiff
ICU-9258 fix int64 format of U_INT64_MIN; // -9223372036854775808L
authorSteven R. Loomis <srl@icu-project.org>
Wed, 30 May 2012 18:21:49 +0000 (18:21 +0000)
committerSteven R. Loomis <srl@icu-project.org>
Wed, 30 May 2012 18:21:49 +0000 (18:21 +0000)
X-SVN-Rev: 31888

icu4c/source/i18n/decimfmt.cpp
icu4c/source/test/intltest/numfmtst.cpp

index ff15a14765af0f073c9497e0c9c5d55dc38e4825..1a31b876fa1e69ebe3e6102d9a1f408baa1c0a2e 100644 (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;
-    }
-    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
index 734465c6f3c703918f8afc8f0634787ca02d71d1..ee8aa3dfd19568c2408df431cc9837c26fd41ec6 100644 (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 = -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 */