ICU-2334 add text file driven test cases

X-SVN-Rev: 14749
This commit is contained in:
Alan Liu 2004-03-24 18:40:35 +00:00
parent 8558fa0d35
commit 15fce7a190
3 changed files with 236 additions and 6 deletions

View file

@ -6,8 +6,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/build.xml,v $
* $Date: 2004/02/19 00:30:53 $
* $Revision: 1.89 $
* $Date: 2004/03/24 18:40:35 $
* $Revision: 1.90 $
*
*******************************************************************************
* This is the ant build file for ICU4J. See readme.html for more information.
@ -215,6 +215,8 @@
<fileset dir="${src.dir}/com/ibm/icu/dev/data"
includes="*.spp"/>
</copy>
<copy file="${src.dir}/com/ibm/icu/dev/test/format/NumberFormatTestCases.txt"
todir="${build.dir}/com/ibm/icu/dev/test/format"/>
</target>
<!-- builds richedit and richedit tests -->

View file

@ -1,11 +1,11 @@
/*
*******************************************************************************
* Copyright (C) 2001-2003, International Business Machines Corporation and *
* Copyright (C) 2001-2004, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/format/NumberFormatTest.java,v $
* $Date: 2004/02/20 19:40:31 $
* $Revision: 1.23 $
* $Date: 2004/03/24 18:39:47 $
* $Revision: 1.24 $
*
*****************************************************************************************
*/
@ -21,7 +21,9 @@ import com.ibm.icu.dev.test.TestUtil;
import com.ibm.icu.text.*;
import com.ibm.icu.text.NumberFormat.*;
import com.ibm.icu.util.*;
import com.ibm.icu.impl.LocaleUtility;
import com.ibm.icu.impl.data.ResourceReader;
import com.ibm.icu.impl.data.TokenIterator;
import java.math.BigInteger;
import java.text.FieldPosition;
import java.text.ParsePosition;
@ -971,6 +973,162 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk {
}
}
/**
* Return an integer representing the next token from this
* iterator. The integer will be an index into the given list, or
* -1 if there are no more tokens, or -2 if the token is not on
* the list.
*/
private static int keywordIndex(String tok) {
for (int i=0; i<KEYWORDS.length; ++i) {
if (tok.equals(KEYWORDS[i])) {
return i;
}
}
return -1;
}
private static final String KEYWORDS[] = {
/*0*/ "ref=", // <reference pattern to parse numbers>
/*1*/ "loc=", // <locale for formats>
/*2*/ "f:", // <pattern or '-'> <number> <exp. string>
/*3*/ "fp:", // <pattern or '-'> <number> <exp. string> <exp. number>
/*4*/ "rt:", // <pattern or '-'> <(exp.) number> <(exp.) string>
/*5*/ "p:", // <pattern or '-'> <string> <exp. number>
/*6*/ "perr:", // <pattern or '-'> <invalid string>
/*7*/ "pat:", // <pattern or '-'> <exp. toPattern or '-' or 'err'>
};
public void TestCases() {
ResourceReader reader = new ResourceReader(NumberFormatTest.class,
"NumberFormatTestCases.txt");
TokenIterator tokens = new TokenIterator(reader);
Locale loc = new Locale("en", "US", "");
DecimalFormat ref = null, fmt = null;
String pat = null;
try {
for (;;) {
String tok = tokens.next();
if (tok == null) {
break;
}
String where = "(" + tokens.getLineNumber() + ") ";
int cmd = keywordIndex(tok);
switch (cmd) {
case 0:
// ref= <reference pattern>
ref = new DecimalFormat(tokens.next(),
new DecimalFormatSymbols(Locale.US));
break;
case 1:
// loc= <locale>
loc = LocaleUtility.getLocaleFromName(tokens.next());
break;
case 2: // f:
case 3: // fp:
case 4: // rt:
case 5: // p:
tok = tokens.next();
if (!tok.equals("-")) {
pat = tok;
try {
fmt = new DecimalFormat(pat, new DecimalFormatSymbols(loc));
} catch (IllegalArgumentException iae) {
errln(where + "Pattern \"" + pat + '"');
iae.printStackTrace();
tokens.next(); // consume remaining tokens
tokens.next();
if (cmd == 3) tokens.next();
continue;
}
}
String str = null;
try {
if (cmd == 2 || cmd == 3 || cmd == 4) {
// f: <pattern or '-'> <number> <exp. string>
// fp: <pattern or '-'> <number> <exp. string> <exp. number>
// rt: <pattern or '-'> <number> <string>
String num = tokens.next();
str = tokens.next();
Number n = (Number) ref.parse(num);
assertEquals(where + '"' + pat + "\".format(" + num + ")",
str, fmt.format(n));
if (cmd == 3) { // fp:
n = (Number) ref.parse(tokens.next());
}
if (cmd != 2) { // != f:
assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")",
n, fmt.parse(str));
}
}
// p: <pattern or '-'> <string to parse> <exp. number>
else {
str = tokens.next();
String expstr = tokens.next();
Number exp = (Number) ref.parse(expstr);
assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")",
exp, fmt.parse(str));
}
} catch (ParseException e) {
errln(where + '"' + pat + "\".parse(\"" + str +
"\") threw an exception");
e.printStackTrace();
}
break;
case 6:
// perr: <pattern or '-'> <invalid string>
errln("Under construction");
return;
case 7:
// pat: <pattern> <exp. toPattern, or '-' or 'err'>
String testpat = tokens.next();
String exppat = tokens.next();
boolean err = exppat.equals("err");
if (testpat.equals("-")) {
if (err) {
errln("Invalid command \"pat: - err\" at " + tokens.describePosition());
continue;
}
testpat = pat;
}
if (exppat.equals("-")) exppat = testpat;
try {
DecimalFormat f = null;
if (testpat == pat) { // [sic]
f = fmt;
} else {
f = new DecimalFormat(testpat);
}
if (err) {
errln(where + "Invalid pattern \"" + testpat +
"\" was accepted");
} else {
assertEquals(where + '"' + testpat + "\".toPattern()",
exppat, f.toPattern());
}
} catch (IllegalArgumentException iae2) {
if (err) {
logln("Ok: " + where + "Invalid pattern \"" + testpat +
"\" threw an exception");
} else {
errln(where + "Valid pattern \"" + testpat +
"\" threw an exception");
iae2.printStackTrace();
}
}
break;
case -1:
errln("Unknown command \"" + tok + "\" at " + tokens.describePosition());
return;
}
}
} catch (java.io.IOException e) {
throw new RuntimeException(e);
}
}
//------------------------------------------------------------------
// Support methods
//------------------------------------------------------------------

View file

@ -0,0 +1,70 @@
######################################################################
# Copyright (c) 2004, International Business Machines
# Corporation and others. All Rights Reserved.
######################################################################
# Author: Alan Liu
# Created: March 15 2004
# Since: ICU 3.0
######################################################################
ref= "#.#"
rt: "0.###" 1.0 "1"
# Basics
fp: "0.####" 0.10005 "0.1" 0.1
fp: - 0.10006 "0.1001" 0.1001
pat: - "#0.####"
fp: "#.####" 0.10005 "0.1" 0.1
pat: - "#0.####"
rt: "0" 1234 "1234"
pat: - "#0"
# Significant digits
fp: "@@@" 1.234567 "1.23" 1.23
fp: - 1234567 "1230000" 1230000
fp: - 0.012345 "0.0123" 0.0123
pat: - -
fp: "#,@@@" 1234567 "1,230,000" 1230000
pat: - "#,@@@"
rt: "@@@@" 0.0012 "0.001200"
fp: - 0.99999 "1.000" 1
pat: - -
rt: "@###" 0.00123 "0.00123"
rt: - 123000 "123000"
fp: - 123456 "123500" 123500
fp: - 12.3456 "12.35" 12.35
fp: - 0.0123456 "0.01235" 0.01235
pat: - -
fp: "@,###" 27182 "27,180" 27180
rt: - 123000 "123,000"
fp: - 0.99999 "1" 1
rt: - 0.9999 "0.9999"
pat: - -
rt: "@##E0" 20000 "2E4"
rt: - 27000 "2.7E4"
rt: - 27100 "2.71E4"
fp: - 27182 "2.72E4" 27200
pat: - -
rt: "@@@E0" 20000 "2.00E4"
rt: - 27000 "2.70E4"
rt: - 27100 "2.71E4"
fp: - 27182 "2.72E4" 27200
pat: - -
fp: "#,@@##" 314156 "31,4200" 314200
rt: - 3 "3.0"
rt: - 5000 "5000"
rt: - 0.005 "0.0050"
pat: - -
pat: "##,@@##" "#,@@##"
pat: "##@@##" "@@##"
pat: "@@.@@" err # decimal sep. disallowed in sig. digits
pat: "@#@" err # only one cluster of sig. digits
pat: "@@0" err # either @ or 0, not both
# NumberRegression/Test4140009
rt: "" 123.456 "123.456"
rt: "" -123.456 "-123.456"