ICU-3089 add generic legal/illegal pattern test; disallow unquoted specials in suffix

X-SVN-Rev: 14556
This commit is contained in:
Alan Liu 2004-02-20 19:40:31 +00:00
parent d0393ac474
commit c01a6bdc37
5 changed files with 93 additions and 26 deletions

View File

@ -66,6 +66,7 @@ void NumberFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &n
CASE(24,TestInt64);
CASE(25,TestPerMill);
CASE(26,TestIllegalPatterns);
default: name = ""; break;
}
@ -1026,13 +1027,13 @@ void NumberFormatTest::TestPad(void) {
int32_t(0), "^^^^0", status);
expect2(new DecimalFormat("*^##.##", US, status),
-1.3, "^-1.3", status);
expect2(new DecimalFormat("##0.0####E0*_ g-m/s^2", US, status),
expect2(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US, status),
int32_t(0), "0.0E0______ g-m/s^2", status);
expect(new DecimalFormat("##0.0####E0*_ g-m/s^2", US, status),
expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US, status),
1.0/3, "333.333E-3_ g-m/s^2", status);
expect2(new DecimalFormat("##0.0####*_ g-m/s^2", US, status),
expect2(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US, status),
int32_t(0), "0.0______ g-m/s^2", status);
expect(new DecimalFormat("##0.0####*_ g-m/s^2", US, status),
expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US, status),
1.0/3, "0.33333__ g-m/s^2", status);
// Test padding before a sign
@ -1218,16 +1219,16 @@ void NumberFormatTest::TestSurrogateSupport(void) {
expect2(new DecimalFormat("##.##", custom, status),
-1.3, " minus 1decimal3", status);
status = U_ZERO_ERROR;
expect2(new DecimalFormat("##0.0####E0 g'-'m/s^2", custom, status),
expect2(new DecimalFormat("##0.0####E0 'g-m/s^2'", custom, status),
int32_t(0), "0decimal0exponent0 g-m/s^2", status);
status = U_ZERO_ERROR;
expect(new DecimalFormat("##0.0####E0 g'-'m/s^2", custom, status),
expect(new DecimalFormat("##0.0####E0 'g-m/s^2'", custom, status),
1.0/3, "333decimal333exponent minus 3 g-m/s^2", status);
status = U_ZERO_ERROR;
expect2(new DecimalFormat("##0.0#### g'-'m/s^2", custom, status),
expect2(new DecimalFormat("##0.0#### 'g-m/s^2'", custom, status),
int32_t(0), "0decimal0 g-m/s^2", status);
status = U_ZERO_ERROR;
expect(new DecimalFormat("##0.0#### g'-'m/s^2", custom, status),
expect(new DecimalFormat("##0.0#### 'g-m/s^2'", custom, status),
1.0/3, "0decimal33333 g-m/s^2", status);
UnicodeString zero((UChar32)0x10000);
@ -1490,6 +1491,36 @@ void NumberFormatTest::TestPerMill() {
"485.7m", fmt2.format(0.4857, str));
}
/**
* Generic test for patterns that should be legal/illegal.
*/
void NumberFormatTest::TestIllegalPatterns() {
// Test cases:
// Prefix with "-:" for illegal patterns
// Prefix with "+:" for legal patterns
const char* DATA[] = {
// Unquoted special characters in the suffix are illegal
"-:000.000|###",
"+:000.000'|###'",
0
};
for (int32_t i=0; DATA[i]; ++i) {
const char* pat=DATA[i];
UBool valid = (*pat) == '+';
pat += 2;
UErrorCode ec = U_ZERO_ERROR;
DecimalFormat fmt(pat, ec); // locale doesn't matter here
if (U_SUCCESS(ec) == valid) {
logln("Ok: pattern \"%s\": %s",
pat, u_errorName(ec));
} else {
errln("FAIL: pattern \"%s\" should have %s; got %s",
pat, (valid?"succeeded":"failed"),
u_errorName(ec));
}
}
}
//----------------------------------------------------------------------
// Support methods
//----------------------------------------------------------------------

View File

@ -106,6 +106,8 @@ class NumberFormatTest: public CalendarTimeZoneTest {
void TestPerMill(void);
void TestIllegalPatterns(void);
private:
static UBool equalValue(const Formattable& a, const Formattable& b);

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/format/BigNumberFormatTest.java,v $
* $Date: 2003/06/03 18:49:29 $
* $Revision: 1.16 $
* $Date: 2004/02/20 19:40:16 $
* $Revision: 1.17 $
*
*****************************************************************************************
*/
@ -264,12 +264,12 @@ public class BigNumberFormatTest extends TestFmwk {
new Double(-1.3), "^-1.3",
}
);
expect(new DecimalFormat("##0.0####E0*_ g-m/s^2", US),
expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
new Object[] { new Long(0), "0.0E0______ g-m/s^2",
new Double(1.0/3), "333.333E-3_ g-m/s^2",
}
);
expect(new DecimalFormat("##0.0####*_ g-m/s^2", US),
expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),
new Object[] { new Long(0), "0.0______ g-m/s^2",
new Double(1.0/3), "0.33333__ g-m/s^2",
}

View File

@ -4,8 +4,8 @@
* others. All Rights Reserved. *
*******************************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/format/NumberFormatTest.java,v $
* $Date: 2004/02/12 01:10:19 $
* $Revision: 1.22 $
* $Date: 2004/02/20 19:40:31 $
* $Revision: 1.23 $
*
*****************************************************************************************
*/
@ -336,7 +336,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
double aNumber = 0l;
try {
aNumber = format.parse(arg).doubleValue();
} catch (java.text.ParseException e) {
} catch (ParseException e) {
System.out.println(e);
}
logln("parse(" + arg + ") = " + aNumber);
@ -573,16 +573,16 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
expect2(new DecimalFormat("*^##.##", US), 0, "^^^^0");
expect2(new DecimalFormat("*^##.##", US), -1.3, "^-1.3");
expect2(
new DecimalFormat("##0.0####E0*_ g-m/s^2", US),
new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
0,
"0.0E0______ g-m/s^2");
expect(
new DecimalFormat("##0.0####E0*_ g-m/s^2", US),
new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),
1.0 / 3,
"333.333E-3_ g-m/s^2");
expect2(new DecimalFormat("##0.0####*_ g-m/s^2", US), 0, "0.0______ g-m/s^2");
expect2(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US), 0, "0.0______ g-m/s^2");
expect(
new DecimalFormat("##0.0####*_ g-m/s^2", US),
new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),
1.0 / 3,
"0.33333__ g-m/s^2");
@ -941,6 +941,36 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
"485.7m", fmt2.format(0.4857));
}
public void TestIllegalPatterns() {
// Test cases:
// Prefix with "-:" for illegal patterns
// Prefix with "+:" for legal patterns
String DATA[] = {
// Unquoted special characters in the suffix are illegal
"-:000.000|###",
"+:000.000'|###'",
};
for (int i=0; i<DATA.length; ++i) {
String pat=DATA[i];
boolean valid = pat.charAt(0) == '+';
pat = pat.substring(2);
IllegalArgumentException e = null;
try {
// locale doesn't matter here
new DecimalFormat(pat);
} catch (IllegalArgumentException e1) {
e = e1;
}
String msg = (e==null) ? "success" : e.getMessage();
if ((e==null) == valid) {
logln("Ok: pattern \"" + pat + "\": " + msg);
} else {
errln("FAIL: pattern \"" + pat + "\" should have " +
(valid?"succeeded":"failed") + "; got " + msg);
}
}
}
//------------------------------------------------------------------
// Support methods
//------------------------------------------------------------------
@ -984,7 +1014,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
errln("FAIL \"" + exp + "\" => " + n2 +
" => \"" + saw2 + '"');
}
} catch (java.text.ParseException e) {
} catch (ParseException e) {
errln(e.getMessage());
return;
}
@ -1016,7 +1046,7 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
Number num = null;
try {
num = (Number) fmt.parse(str);
} catch (java.text.ParseException e) {
} catch (ParseException e) {
errln(e.getMessage());
return;
}

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/DecimalFormat.java,v $
* $Date: 2004/02/19 01:11:46 $
* $Revision: 1.44 $
* $Date: 2004/02/20 19:40:16 $
* $Revision: 1.45 $
*
*****************************************************************************************
*/
@ -3109,6 +3109,10 @@ public class DecimalFormat extends NumberFormat {
// Process the prefix / suffix characters
// Process unquoted characters seen in prefix or suffix
// subpart.
// Several syntax characters implicitly begins the
// next subpart if we are in the prefix; otherwise
// they are illegal if unquoted.
if (ch == digit ||
ch == groupingSeparator ||
ch == decimalSeparator ||
@ -3131,13 +3135,13 @@ public class DecimalFormat extends NumberFormat {
if ((pos+1) < pattern.length() &&
pattern.charAt(pos+1) == QUOTE) {
++pos;
// Fall through to append(ch)
affix.append(ch);
} else {
subpart += 2; // open quote
continue;
}
continue;
}
// Fall through to append(ch)
throw new IllegalArgumentException("Unquoted special character");
} else if (ch == CURRENCY_SIGN) {
// Use lookahead to determine if the currency sign is
// doubled or not.