2 *******************************************************************************
3 * Copyright (C) 2004-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.format;
9 import java.util.Locale;
11 import com.ibm.icu.dev.test.TestFmwk;
12 import com.ibm.icu.text.RuleBasedNumberFormat;
13 import com.ibm.icu.util.ULocale;
15 public class RBNFParseTest extends TestFmwk {
16 public static void main(String[] args) {
17 new RBNFParseTest().run(args);
20 public void TestParse() {
22 // these rules make no sense but behave rationally
51 String[] exceptrules = {
65 "10:", // formatting any value with a one's digit will fail
66 "11: << x", // formating a multiple of 10 causes rollback rule to fail
67 "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;",
70 String[][] allrules = {
75 for (int j = 0; j < allrules.length; ++j) {
76 String[] tests = allrules[j];
77 boolean except = tests == exceptrules;
78 for (int i = 0; i < tests.length; ++i) {
80 logln("rules: '" + tests[i] + "'");
81 boolean caughtException = false;
83 RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(tests[i], Locale.US);
84 logln("1.23: " + fmt.format(20));
85 logln("-123: " + fmt.format(-123));
86 logln(".123: " + fmt.format(.123));
87 logln(" 123: " + fmt.format(123));
91 errln("Unexpected exception: " + e.getMessage());
93 caughtException = true;
96 if (except && !caughtException) {
97 errln("expected exception but didn't get one!");
103 private void parseFormat(RuleBasedNumberFormat rbnf, String s, String target) {
105 Number n = rbnf.parse(s);
106 String t = rbnf.format(n);
107 assertEquals(rbnf.getLocale(ULocale.ACTUAL_LOCALE) + ": " + s + " : " + n, target, t);
108 } catch (java.text.ParseException e){
109 fail("exception:" + e);
113 private void parseList(RuleBasedNumberFormat rbnf_en, RuleBasedNumberFormat rbnf_fr, String[][] lists) {
114 for (int i = 0; i < lists.length; ++i) {
115 String[] list = lists[i];
117 String target_en = list[1];
118 String target_fr = list[2];
120 parseFormat(rbnf_en, s, target_en);
121 parseFormat(rbnf_fr, s, target_fr);
125 public void TestLenientParse() throws Exception {
126 RuleBasedNumberFormat rbnf_en, rbnf_fr;
128 // TODO: this still passes, but setLenientParseMode should have no effect now.
129 // Did it ever test what it was supposed to?
130 rbnf_en = new RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
131 rbnf_en.setLenientParseMode(true);
132 rbnf_fr = new RuleBasedNumberFormat(Locale.FRENCH, RuleBasedNumberFormat.SPELLOUT);
133 rbnf_fr.setLenientParseMode(true);
135 Number n = rbnf_en.parse("1,2 million");
139 { "1,2", "twelve", "un virgule deux" },
140 { "1,2 million", "twelve million", "un virgule deux" },
141 { "1,2 millions", "twelve million", "un million deux cent mille" },
142 { "1.2", "one point two", "douze" },
143 { "1.2 million", "one million two hundred thousand", "douze" },
144 { "1.2 millions", "one million two hundred thousand", "douze millions" },
147 Locale.setDefault(Locale.FRANCE);
148 logln("Default locale:" + Locale.getDefault());
149 logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName());
150 logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName());
151 parseList(rbnf_en, rbnf_fr, lists);
153 Locale.setDefault(Locale.US);
154 logln("Default locale:" + Locale.getDefault());
155 logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName());
156 logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName());
157 parseList(rbnf_en, rbnf_fr, lists);