]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/format/RBNFParseTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / format / RBNFParseTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2004-2013, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.format;
8
9 import java.util.Locale;
10
11 import com.ibm.icu.dev.test.TestFmwk;
12 import com.ibm.icu.text.RuleBasedNumberFormat;
13 import com.ibm.icu.util.ULocale;
14
15 public class RBNFParseTest extends TestFmwk {
16     public static void main(String[] args) {
17         new RBNFParseTest().run(args);
18     }
19
20     public void TestParse() {
21
22         // these rules make no sense but behave rationally
23         String[] okrules = {
24             "random text",
25             "%foo:bar",
26             "%foo: bar",
27             "0:",
28             "0::",
29             "%%foo:;",
30             "-",
31             "-1",
32             "-:",
33             ".",
34             ".1",
35             "[",
36             "]",
37             "[]",
38             "[foo]",
39             "[[]",
40             "[]]",
41             "[[]]",
42             "[][]",
43             "<",
44             ">",
45             "=",
46             "==",
47             "===",
48             "=foo=",
49         };
50
51         String[] exceptrules = {
52             "",
53             ";",
54             ";;",
55             ":",
56             "::",
57             ":1",
58             ":;",
59             ":;:;",
60             "<<",
61             "<<<",
62             "10:;9:;",
63             ">>",
64             ">>>",
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=;",
68         };
69
70         String[][] allrules = {
71             okrules,
72             exceptrules,
73         };
74
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) {
79                 logln("----------");
80                 logln("rules: '" + tests[i] + "'");
81                 boolean caughtException = false;
82                 try {
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));
88                 }
89                 catch (Exception e) {
90                     if (!except) {
91                         errln("Unexpected exception: " + e.getMessage());
92                     } else {
93                         caughtException = true;
94                     }
95                 }
96                 if (except && !caughtException) {
97                     errln("expected exception but didn't get one!");
98                 }
99             }
100         }
101     }
102
103     private void parseFormat(RuleBasedNumberFormat rbnf, String s, String target) {
104         try {
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);
110         }
111     }
112
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];
116             String s = list[0];
117             String target_en = list[1];
118             String target_fr = list[2];
119
120             parseFormat(rbnf_en, s, target_en);
121             parseFormat(rbnf_fr, s, target_fr);
122         }
123     }
124
125     public void TestLenientParse() throws Exception {
126         RuleBasedNumberFormat rbnf_en, rbnf_fr;
127
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);
134
135         Number n = rbnf_en.parse("1,2 million");
136         logln(n.toString());
137
138         String[][] lists = {
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" },
145         };
146
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);
152
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);
158     }
159 }