]> granicus.if.org Git - icu/commitdiff
ICU-5186 Make DecimalFormat::operator== much more complete
authorPeter Edberg <pedberg@unicode.org>
Tue, 4 Mar 2014 05:54:16 +0000 (05:54 +0000)
committerPeter Edberg <pedberg@unicode.org>
Tue, 4 Mar 2014 05:54:16 +0000 (05:54 +0000)
X-SVN-Rev: 35312

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

index 4ac3f640bb76c55e2f8672574900e2fd4b942285..eb9e1481c53a8c82c7bc8c7f902687fcc77ff1ec 100644 (file)
@@ -814,7 +814,7 @@ DecimalFormat::operator=(const DecimalFormat& rhs)
         fUseSignificantDigits = rhs.fUseSignificantDigits;
         fFormatPattern = rhs.fFormatPattern;
         fStyle = rhs.fStyle;
-        fCurrencySignCount = rhs.fCurrencySignCount;
+        //fCurrencySignCount = rhs.fCurrencySignCount; // duplicates line above 
         _clone_ptr(&fCurrencyPluralInfo, rhs.fCurrencyPluralInfo);
         deleteHashForAffixPattern();
         if (rhs.fAffixPatternsForCurrency) {
@@ -926,6 +926,10 @@ DecimalFormat::operator==(const Format& that) const
         if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
         debug("Rounding Increment !=");
               }
+    if (fRoundingMode != other->fRoundingMode) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        printf("Rounding Mode %d != %d", (int)fRoundingMode, (int)other->fRoundingMode);
+    }
     if (getMultiplier() != other->getMultiplier()) {
         if (first) { printf("[ "); first = FALSE; }
         printf("Multiplier %ld != %ld", getMultiplier(), other->getMultiplier());
@@ -940,16 +944,25 @@ DecimalFormat::operator==(const Format& that) const
     }
     if (fDecimalSeparatorAlwaysShown != other->fDecimalSeparatorAlwaysShown) {
         if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
-        printf("Dec Sep Always %d != %d", fDecimalSeparatorAlwaysShown, other->fDecimalSeparatorAlwaysShown);
+        printf("fDecimalSeparatorAlwaysShown %d != %d", fDecimalSeparatorAlwaysShown, other->fDecimalSeparatorAlwaysShown);
     }
     if (fUseExponentialNotation != other->fUseExponentialNotation) {
         if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
-        debug("Use Exp !=");
+        debug("fUseExponentialNotation !=");
+    }
+    if (fUseExponentialNotation &&
+        fMinExponentDigits != other->fMinExponentDigits) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fMinExponentDigits !=");
+    }
+    if (fUseExponentialNotation &&
+        fExponentSignAlwaysShown != other->fExponentSignAlwaysShown) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fExponentSignAlwaysShown !=");
     }
-    if (!(!fUseExponentialNotation ||
-          fMinExponentDigits != other->fMinExponentDigits)) {
+    if (fBoolFlags.getAll() != other->fBoolFlags.getAll()) {
         if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
-        debug("Exp Digits !=");
+        debug("fBoolFlags !=");
     }
     if (*fSymbols != *(other->fSymbols)) {
         if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
@@ -957,16 +970,41 @@ DecimalFormat::operator==(const Format& that) const
     }
     // TODO Add debug stuff for significant digits here
     if (fUseSignificantDigits != other->fUseSignificantDigits) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
         debug("fUseSignificantDigits !=");
     }
     if (fUseSignificantDigits &&
         fMinSignificantDigits != other->fMinSignificantDigits) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
         debug("fMinSignificantDigits !=");
     }
     if (fUseSignificantDigits &&
         fMaxSignificantDigits != other->fMaxSignificantDigits) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
         debug("fMaxSignificantDigits !=");
     }
+    if (fFormatWidth != other->fFormatWidth) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fFormatWidth !=");
+    }
+    if (fPad != other->fPad) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fPad !=");
+    }
+    if (fPadPosition != other->fPadPosition) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fPadPosition !=");
+    }
+    if (fStyle == UNUM_CURRENCY_PLURAL &&
+        fStyle != other->fStyle)
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fStyle !=");
+    }
+    if (fStyle == UNUM_CURRENCY_PLURAL &&
+        fFormatPattern != other->fFormatPattern) {
+        if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
+        debug("fFormatPattern !=");
+    }
 
     if (!first) { printf(" ]"); }
     if (fCurrencySignCount != other->fCurrencySignCount) {
@@ -1015,23 +1053,32 @@ DecimalFormat::operator==(const Format& that) const
              || (fRoundingIncrement != NULL &&
                  other->fRoundingIncrement != NULL &&
                  *fRoundingIncrement == *other->fRoundingIncrement)) &&
+        fRoundingMode == other->fRoundingMode &&
         getMultiplier() == other->getMultiplier() &&
         fGroupingSize == other->fGroupingSize &&
         fGroupingSize2 == other->fGroupingSize2 &&
         fDecimalSeparatorAlwaysShown == other->fDecimalSeparatorAlwaysShown &&
         fUseExponentialNotation == other->fUseExponentialNotation &&
         (!fUseExponentialNotation ||
-         fMinExponentDigits == other->fMinExponentDigits) &&
+            (fMinExponentDigits == other->fMinExponentDigits && fExponentSignAlwaysShown == other->fExponentSignAlwaysShown)) &&
+        fBoolFlags.getAll() == other->fBoolFlags.getAll() &&
         *fSymbols == *(other->fSymbols) &&
         fUseSignificantDigits == other->fUseSignificantDigits &&
         (!fUseSignificantDigits ||
-         (fMinSignificantDigits == other->fMinSignificantDigits &&
-          fMaxSignificantDigits == other->fMaxSignificantDigits)) &&
+            (fMinSignificantDigits == other->fMinSignificantDigits && fMaxSignificantDigits == other->fMaxSignificantDigits)) &&
+        fFormatWidth == other->fFormatWidth &&
+        fPad == other->fPad &&
+        fPadPosition == other->fPadPosition &&
+        (fStyle != UNUM_CURRENCY_PLURAL ||
+            (fStyle == other->fStyle && fFormatPattern == other->fFormatPattern)) &&
         fCurrencySignCount == other->fCurrencySignCount &&
         ((fCurrencyPluralInfo == other->fCurrencyPluralInfo &&
           fCurrencyPluralInfo == NULL) ||
          (fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo != NULL &&
          *fCurrencyPluralInfo == *(other->fCurrencyPluralInfo))));
+        // depending on other settings we may also need to compare
+        // fCurrencyChoice (mostly deprecated?),
+        // fAffixesForCurrency & fPluralAffixesForCurrency (only relevant in some cases)
 }
 
 //------------------------------------------------------------------------------
@@ -3075,12 +3122,12 @@ UnicodeString& DecimalFormat::trimMarksFromAffix(const UnicodeString& affix, Uni
     for (affixPos = 0; affixPos < affixLen; affixPos++) {
         UChar c = affix.charAt(affixPos);
         if (!IS_BIDI_MARK(c)) {
-               if (trimLen < TRIM_BUFLEN) {
-                       trimBuf[trimLen++] = c;
-               } else {
-                       trimLen = 0;
-                       break;
-               }
+            if (trimLen < TRIM_BUFLEN) {
+                trimBuf[trimLen++] = c;
+            } else {
+                trimLen = 0;
+                break;
+            }
         }
     }
     return (trimLen > 0)? trimmedAffix.setTo(trimBuf, trimLen): trimmedAffix.setTo(affix);
@@ -3373,8 +3420,8 @@ int32_t DecimalFormat::compareComplexAffix(const UnicodeString& affixPat,
                         UChar effectiveCurr[4];
                         getEffectiveCurrency(effectiveCurr, ec);
                         if ( U_FAILURE(ec) || u_strncmp(curr,effectiveCurr,4) != 0 ) {
-                               pos = -1;
-                               continue;
+                            pos = -1;
+                            continue;
                         }
                     }
                     pos = ppos.getIndex();
index 5c5c401355445c438859407f9cecef1d188bb056..1abb1b36c86d7825b8a0dd5985df4da876856bdf 100644 (file)
@@ -133,6 +133,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n
   TESTCASE_AUTO(TestRoundingScientific10542);
   TESTCASE_AUTO(TestZeroScientific10547);
   TESTCASE_AUTO(TestAccountingCurrency);
+  TESTCASE_AUTO(TestEquality);
   TESTCASE_AUTO_END;
 }
 
@@ -7600,4 +7601,29 @@ void NumberFormatTest::TestAccountingCurrency() {
         (Formattable)-23456.7, UnicodeString("-23.456,70\\u00A0\\u20AC").unescape(), TRUE, status);
 }
 
+// for #5186
+void NumberFormatTest::TestEquality() {
+    UErrorCode status = U_ZERO_ERROR;
+    DecimalFormatSymbols* symbols = new DecimalFormatSymbols(Locale("root"), status);
+    if (U_FAILURE(status)) {
+       dataerrln("Fail: can't create DecimalFormatSymbols for root");
+       return;
+    }
+    UnicodeString pattern("#,##0.###");
+    DecimalFormat* fmtBase = new DecimalFormat(pattern, symbols, status);
+    if (U_FAILURE(status)) {
+       dataerrln("Fail: can't create DecimalFormat using root symbols");
+       return;
+    }
+
+    DecimalFormat* fmtClone = (DecimalFormat*)fmtBase->clone();
+    fmtClone->setFormatWidth(fmtBase->getFormatWidth() + 32);
+    if (*fmtClone == *fmtBase) {
+        errln("Error: DecimalFormat == does not distinguish objects that differ only in FormatWidth");
+    }
+    delete fmtClone;
+
+    delete fmtBase;
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
index 171995fb7c2591a89f37cad15536790c1b200947..f25a4250a69b77efa9526d43c5426a0fe9068f8e 100644 (file)
@@ -180,6 +180,7 @@ class NumberFormatTest: public CalendarTimeZoneTest {
     void TestRoundingScientific10542();
     void TestZeroScientific10547();
     void TestAccountingCurrency();
+    void TestEquality();
 
  private:
     UBool testFormattableAsUFormattable(const char *file, int line, Formattable &f);