ICU-5101 Test and prevent code from crashing when prefix and suffix are changed on a currency format. The prefix and suffix are still not settable when a currency is used, and the applyPattern must still be used when using currency formats.

X-SVN-Rev: 21145
This commit is contained in:
George Rhoten 2007-03-02 00:28:09 +00:00
parent a5d3c1ebfc
commit 4706047498
2 changed files with 79 additions and 15 deletions

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1997-2006, International Business Machines Corporation and *
* Copyright (C) 1997-2007, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -1628,24 +1628,49 @@ int32_t DecimalFormat::compareAffix(const UnicodeString& text,
int32_t pos,
UBool isNegative,
UBool isPrefix,
UChar* currency) const {
UChar* currency) const
{
const UnicodeString *patternToCompare;
if (fCurrencyChoice != NULL || currency != NULL) {
if (isPrefix) {
return compareComplexAffix(isNegative ? *fNegPrefixPattern : *fPosPrefixPattern,
text, pos, currency);
} else {
return compareComplexAffix(isNegative ? *fNegSuffixPattern : *fPosSuffixPattern,
text, pos, currency);
if (isNegative) {
if (isPrefix) {
patternToCompare = fNegPrefixPattern;
}
else {
patternToCompare = fNegSuffixPattern;
}
}
else {
if (isPrefix) {
patternToCompare = fPosPrefixPattern;
}
else {
patternToCompare = fPosSuffixPattern;
}
}
if (patternToCompare != NULL) {
return compareComplexAffix(*patternToCompare, text, pos, currency);
}
/* else the caller modified the pattern. Fallback to normal behavior. */
}
if (isPrefix) {
return compareSimpleAffix(isNegative ? fNegativePrefix : fPositivePrefix,
text, pos);
} else {
return compareSimpleAffix(isNegative ? fNegativeSuffix : fPositiveSuffix,
text, pos);
if (isNegative) {
if (isPrefix) {
patternToCompare = &fNegativePrefix;
}
else {
patternToCompare = &fNegativeSuffix;
}
}
else {
if (isPrefix) {
patternToCompare = &fPositivePrefix;
}
else {
patternToCompare = &fPositiveSuffix;
}
}
return compareSimpleAffix(*patternToCompare, text, pos);
}
/**

View File

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2005-2006, International Business Machines Corporation and
* Copyright (c) 2005-2007, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "unicode/utypes.h"
@ -189,6 +189,44 @@ static void TestFractionDigitOverride(void) {
unum_close(fmt);
}
static void TestPrefixSuffix(void) {
int32_t pos;
UErrorCode status;
double result1 = 0.0, result2 = 0.0;
UNumberFormat* parser;
UChar buffer[4];
static const UChar TEST_NUMBER[] = {0x0024,0x0031,0x0032,0x002E,0x0030,0x0030,0}; /* $12.00 */
static const UChar NEG_PREFIX[] = {0x005B,0}; /* "[" */
static const UChar NEG_SUFFIX[] = {0x005D,0}; /* "]" */
status = U_ZERO_ERROR;
parser = unum_open(UNUM_CURRENCY, NULL, -1, "en_US", NULL, &status);
if (U_FAILURE(status)) {
log_err("Error: unum_open returned %s\n", u_errorName(status));
return;
}
pos = 0;
status = U_ZERO_ERROR;
result1 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status);
unum_setTextAttribute(parser, UNUM_NEGATIVE_SUFFIX, NEG_SUFFIX, -1, &status);
unum_setTextAttribute(parser, UNUM_NEGATIVE_PREFIX, NEG_PREFIX, -1, &status);
if (U_FAILURE(status)) {
log_err("Error: unum_setTextAttribute returned %s\n", u_errorName(status));
return;
}
pos = 0;
result2 = unum_parseDoubleCurrency(parser, TEST_NUMBER, -1, &pos, buffer, &status);
if (result1 != result2 || U_FAILURE(status)) {
log_err("Error: unum_parseDoubleCurrency didn't return the same value for same string %f %f %s\n",
result1, result2, u_errorName(status));
}
unum_close(parser);
}
void addCurrencyTest(TestNode** root);
#define TESTCASE(x) addTest(root, &x, "tsformat/currtest/" #x)
@ -199,6 +237,7 @@ void addCurrencyTest(TestNode** root)
TESTCASE(TestEnumListReset);
TESTCASE(TestEnumListCount);
TESTCASE(TestFractionDigitOverride);
TESTCASE(TestPrefixSuffix);
}
#endif /* #if !UCONFIG_NO_FORMATTING */