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