ICU-5186 Make DecimalFormat::operator== much more complete

X-SVN-Rev: 35312
This commit is contained in:
Peter Edberg 2014-03-04 05:54:16 +00:00
parent 3997d2d085
commit 24a8cc84f4
3 changed files with 91 additions and 17 deletions

View 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 (fUseExponentialNotation &&
fMinExponentDigits != other->fMinExponentDigits) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Exp Digits !=");
debug("fMinExponentDigits !=");
}
if (fUseExponentialNotation &&
fExponentSignAlwaysShown != other->fExponentSignAlwaysShown) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("fExponentSignAlwaysShown !=");
}
if (fBoolFlags.getAll() != other->fBoolFlags.getAll()) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
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();

View 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 */

View 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);