public void TestCases() {
String caseFileName = "NumberFormatTestCases.txt";
java.io.InputStream is = NumberFormatTest.class.getResourceAsStream(caseFileName);
ResourceReader reader = new ResourceReader(is, caseFileName, "utf-8");
TokenIterator tokens = new TokenIterator(reader);
Locale loc = new Locale("en", "US", "");
DecimalFormat ref = null, fmt = null;
MeasureFormat mfmt = null;
String pat = null, str = null, mloc = null;
boolean strict = false;
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));
ref.setParseStrict(strict);
logln("Setting reference pattern to:\t" + ref);
break;
case 1:
// loc= <locale>
loc = LocaleUtility.getLocaleFromName(tokens.next());
pat = ((DecimalFormat) NumberFormat.getInstance(loc)).toPattern();
logln("Setting locale to:\t" + loc + ", \tand pattern to:\t" + pat);
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));
fmt.setParseStrict(strict);
} catch (IllegalArgumentException iae) {
errln(where + "Pattern \"" + pat + '"');
iae.printStackTrace();
tokens.next(); // consume remaining tokens
//tokens.next();
if (cmd == 3) tokens.next();
continue;
}
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 parsed = fmt.parse(str);
Number exp = (Number) ref.parse(expstr);
assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")",
exp, parsed);
}
} 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);
f.setParseStrict(strict);
}
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 8: // fpc:
tok = tokens.next();
if (!tok.equals("-")) {
mloc = tok;
ULocale l = new ULocale(mloc);
try {
mfmt = MeasureFormat.getCurrencyFormat(l);
} catch (IllegalArgumentException iae) {
errln(where + "Loc \"" + tok + '"');
iae.printStackTrace();
tokens.next(); // consume remaining tokens
tokens.next();
tokens.next();
continue;
}
}
str = null;
try {
// fpc: <loc or '-'> <curr.amt> <exp. string> <exp. curr.amt>
String currAmt = tokens.next();
str = tokens.next();
CurrencyAmount n = parseCurrencyAmount(currAmt, ref, '/');
assertEquals(where + "getCurrencyFormat(" + mloc + ").format(" + currAmt + ")",
str, mfmt.format(n));
n = parseCurrencyAmount(tokens.next(), ref, '/');
assertEquals(where + "getCurrencyFormat(" + mloc + ").parse(\"" + str + "\")",
n, (CurrencyAmount) mfmt.parseObject(str));
} catch (ParseException e) {
errln(where + '"' + pat + "\".parse(\"" + str +
"\") threw an exception");
e.printStackTrace();
}
break;
case 9: // strict= true or false
strict = "true".equalsIgnoreCase(tokens.next());
logln("Setting strict to:\t" + strict);
break;
case -1:
errln("Unknown command \"" + tok + "\" at " + tokens.describePosition());
return;
}
}
} catch (java.io.IOException e) {
//#if defined(FOUNDATION10) || defined(J2SE13)