]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/core/src/com/ibm/icu/dev/test/format/RbnfRoundTripTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / core / src / com / ibm / icu / dev / test / format / RbnfRoundTripTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.dev.test.format;\r
8 \r
9 import java.util.Locale;\r
10 \r
11 import com.ibm.icu.dev.test.TestFmwk;\r
12 import com.ibm.icu.text.RuleBasedNumberFormat;\r
13 \r
14 public class RbnfRoundTripTest extends TestFmwk {\r
15     public static void main(String[] args) {\r
16         RbnfRoundTripTest test = new RbnfRoundTripTest();\r
17 \r
18         try {\r
19             test.run(args);\r
20         }\r
21         catch (Throwable e) {\r
22             System.out.println("Entire test failed because of exception: "\r
23                             + e.toString());\r
24             e.printStackTrace();\r
25         }\r
26     }\r
27 \r
28     /**\r
29      * Perform an exhaustive round-trip test on the English spellout rules\r
30      */\r
31     public void TestEnglishSpelloutRT() {\r
32         RuleBasedNumberFormat formatter\r
33                         = new RuleBasedNumberFormat(Locale.US,\r
34                         RuleBasedNumberFormat.SPELLOUT);\r
35 \r
36         doTest(formatter, -12345678, 12345678);\r
37     }\r
38 \r
39     /**\r
40      * Perform an exhaustive round-trip test on the duration-formatting rules\r
41      */\r
42     public void TestDurationsRT() {\r
43         RuleBasedNumberFormat formatter\r
44                         = new RuleBasedNumberFormat(Locale.US,\r
45                         RuleBasedNumberFormat.DURATION);\r
46 \r
47         doTest(formatter, 0, 12345678);\r
48     }\r
49 \r
50     /**\r
51      * Perform an exhaustive round-trip test on the Spanish spellout rules\r
52      */\r
53     public void TestSpanishSpelloutRT() {\r
54         RuleBasedNumberFormat formatter\r
55                         = new RuleBasedNumberFormat(new Locale("es", "es",\r
56                         ""), RuleBasedNumberFormat.SPELLOUT);\r
57 \r
58         doTest(formatter, -12345678, 12345678);\r
59     }\r
60 \r
61     /**\r
62      * Perform an exhaustive round-trip test on the French spellout rules\r
63      */\r
64     public void TestFrenchSpelloutRT() {\r
65         RuleBasedNumberFormat formatter\r
66                         = new RuleBasedNumberFormat(Locale.FRANCE,\r
67                         RuleBasedNumberFormat.SPELLOUT);\r
68 \r
69         doTest(formatter, -12345678, 12345678);\r
70     }\r
71 \r
72     /**\r
73      * Perform an exhaustive round-trip test on the Swiss French spellout rules\r
74      */\r
75     public void TestSwissFrenchSpelloutRT() {\r
76         RuleBasedNumberFormat formatter\r
77                         = new RuleBasedNumberFormat(new Locale("fr", "CH",\r
78                         ""), RuleBasedNumberFormat.SPELLOUT);\r
79 \r
80         doTest(formatter, -12345678, 12345678);\r
81     }\r
82 \r
83     /**\r
84      * Perform an exhaustive round-trip test on the Italian spellout rules\r
85      */\r
86     public void TestItalianSpelloutRT() {\r
87         RuleBasedNumberFormat formatter\r
88                         = new RuleBasedNumberFormat(Locale.ITALIAN,\r
89                         RuleBasedNumberFormat.SPELLOUT);\r
90 \r
91         doTest(formatter, -999999, 999999);\r
92     }\r
93 \r
94     /**\r
95      * Perform an exhaustive round-trip test on the German spellout rules\r
96      */\r
97     public void TestGermanSpelloutRT() {\r
98         RuleBasedNumberFormat formatter\r
99                         = new RuleBasedNumberFormat(Locale.GERMANY,\r
100                         RuleBasedNumberFormat.SPELLOUT);\r
101 \r
102         doTest(formatter, 0, 12345678);\r
103     }\r
104 \r
105     /**\r
106      * Perform an exhaustive round-trip test on the Swedish spellout rules\r
107      */\r
108     public void TestSwedishSpelloutRT() {\r
109         RuleBasedNumberFormat formatter\r
110                         = new RuleBasedNumberFormat(new Locale("sv", "SE",\r
111                         ""), RuleBasedNumberFormat.SPELLOUT);\r
112 \r
113         doTest(formatter, 0, 12345678);\r
114     }\r
115 \r
116     /**\r
117      * Perform an exhaustive round-trip test on the Dutch spellout rules\r
118      */\r
119     public void TestDutchSpelloutRT() {\r
120         RuleBasedNumberFormat formatter\r
121                         = new RuleBasedNumberFormat(new Locale("nl", "NL",\r
122                         ""), RuleBasedNumberFormat.SPELLOUT);\r
123 \r
124         doTest(formatter, -12345678, 12345678);\r
125     }\r
126 \r
127     /**\r
128      * Perform an exhaustive round-trip test on the Japanese spellout rules\r
129      */\r
130     public void TestJapaneseSpelloutRT() {\r
131         RuleBasedNumberFormat formatter\r
132                         = new RuleBasedNumberFormat(Locale.JAPAN,\r
133                         RuleBasedNumberFormat.SPELLOUT);\r
134 \r
135         doTest(formatter, 0, 12345678);\r
136     }\r
137 \r
138     /**\r
139      * Perform an exhaustive round-trip test on the Russian spellout rules\r
140      */\r
141     public void TestRussianSpelloutRT() {\r
142         RuleBasedNumberFormat formatter\r
143                         = new RuleBasedNumberFormat(new Locale("ru", "RU",\r
144                         ""), RuleBasedNumberFormat.SPELLOUT);\r
145 \r
146         doTest(formatter, 0, 12345678);\r
147     }\r
148 \r
149     /**\r
150      * Perform an exhaustive round-trip test on the Greek spellout rules\r
151      */\r
152     public void TestGreekSpelloutRT() {\r
153         RuleBasedNumberFormat formatter\r
154                         = new RuleBasedNumberFormat(new Locale("el", "GR",\r
155                         ""), RuleBasedNumberFormat.SPELLOUT);\r
156 \r
157         doTest(formatter, 0, 12345678);\r
158     }\r
159 \r
160     /**\r
161      * Perform an exhaustive round-trip test on the Greek spellout rules\r
162      */\r
163     public void TestHebrewNumberingRT() {\r
164         RuleBasedNumberFormat formatter\r
165                         = new RuleBasedNumberFormat(new Locale("he", "IL",\r
166                         ""), RuleBasedNumberFormat.NUMBERING_SYSTEM);\r
167 \r
168         formatter.setDefaultRuleSet("%hebrew");\r
169         doTest(formatter, 0, 12345678);\r
170     }\r
171 \r
172     void doTest(RuleBasedNumberFormat formatter,  long lowLimit,\r
173                     long highLimit) {\r
174         try {\r
175             long count = 0;\r
176             long increment = 1;\r
177             for (long i = lowLimit; i <= highLimit; i += increment) {\r
178                 if (count % 1000 == 0)\r
179                     logln(Long.toString(i));\r
180 \r
181                 if (Math.abs(i) < 5000)\r
182                     increment = 1;\r
183                 else if (Math.abs(i) < 500000)\r
184                     increment = 2737;\r
185                 else\r
186                     increment = 267437;\r
187 \r
188                 String text = formatter.format(i);\r
189                 long rt = formatter.parse(text).longValue();\r
190 \r
191                 if (rt != i) {\r
192                     errln("Round-trip failed: " + i + " -> " + text +\r
193                                     " -> " + rt);\r
194                 }\r
195 \r
196                 ++count;\r
197             }\r
198 \r
199             if (lowLimit < 0) {\r
200                 double d = 1.234;\r
201                 while (d < 1000) {\r
202                     String text = formatter.format(d);\r
203                     double rt = formatter.parse(text).doubleValue();\r
204 \r
205                     if (rt != d) {\r
206                         errln("Round-trip failed: " + d + " -> " + text +\r
207                                         " -> " + rt);\r
208                     }\r
209                     d *= 10;\r
210                 }\r
211             }\r
212         }\r
213         catch (Throwable e) {\r
214             errln("Test failed with exception: " + e.toString());\r
215             e.printStackTrace();\r
216         }\r
217     }\r
218 }\r
219 \r