]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/NumberFormatTest.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / localespi / src / com / ibm / icu / dev / test / localespi / NumberFormatTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2008-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.localespi;
8
9 import java.math.BigDecimal;
10 import java.math.BigInteger;
11 import java.text.NumberFormat;
12 import java.text.ParseException;
13 import java.util.Locale;
14
15 import com.ibm.icu.dev.test.TestFmwk;
16 import com.ibm.icu.util.ULocale;
17
18 public class NumberFormatTest extends TestFmwk {
19     public static void main(String[] args) throws Exception {
20         new NumberFormatTest().run(args);
21     }
22
23     private static final int DEFAULT_TYPE = 0;
24     private static final int NUMBER_TYPE = 1;
25     private static final int INTEGER_TYPE  = 2;
26     private static final int PERCENT_TYPE = 3;
27     private static final int CURRENCY_TYPE = 4;
28
29     /*
30      * Check if getInstance returns the ICU implementation.
31      */
32     public void TestGetInstance() {
33         for (Locale loc : NumberFormat.getAvailableLocales()) {
34             if (TestUtil.isProblematicIBMLocale(loc)) {
35                 logln("Skipped " + loc);
36                 continue;
37             }
38             checkGetInstance(DEFAULT_TYPE, loc);
39             checkGetInstance(NUMBER_TYPE, loc);
40             checkGetInstance(INTEGER_TYPE, loc);
41             checkGetInstance(PERCENT_TYPE, loc);
42             checkGetInstance(CURRENCY_TYPE, loc);
43         }
44     }
45
46     private void checkGetInstance(int type, Locale loc) {
47         NumberFormat nf;
48         String[] method = new String[1];
49         nf = getJDKInstance(type, loc, method);
50
51         boolean isIcuImpl = (nf instanceof com.ibm.icu.impl.jdkadapter.DecimalFormatICU)
52                             || (nf instanceof com.ibm.icu.impl.jdkadapter.NumberFormatICU);
53         if (TestUtil.isICUExtendedLocale(loc)) {
54             if (!isIcuImpl) {
55                 errln("FAIL: " + method[0] + " returned JDK NumberFormat for locale " + loc);
56             }
57         } else {
58             if (isIcuImpl) {
59                 logln("INFO: " + method[0] + " returned ICU NumberFormat for locale " + loc);
60             }
61             Locale iculoc = TestUtil.toICUExtendedLocale(loc);
62             NumberFormat nfIcu = null;
63             nfIcu = getJDKInstance(type, iculoc, null);
64             if (isIcuImpl) {
65                 if (!nf.equals(nfIcu)) {
66                     errln("FAIL: " + method[0] + " returned ICU NumberFormat for locale " + loc
67                             + ", but different from the one for locale " + iculoc);
68                 }
69             } else {
70                 if (!(nfIcu instanceof com.ibm.icu.impl.jdkadapter.DecimalFormatICU)
71                         && !(nfIcu instanceof com.ibm.icu.impl.jdkadapter.NumberFormatICU)) {
72                     errln("FAIL: " + method[0] + " returned JDK NumberFormat for locale " + iculoc);
73                 }
74             }
75         }
76     }
77
78     private NumberFormat getJDKInstance(int type, Locale loc, String[] methodName) {
79         NumberFormat nf = null;
80         String method = null;
81
82         switch (type) {
83         case DEFAULT_TYPE:
84             nf = NumberFormat.getInstance(loc);
85             method = "getInstance";
86             break;
87         case NUMBER_TYPE:
88             nf = NumberFormat.getNumberInstance(loc);
89             method = "getNumberInstance";
90             break;
91         case INTEGER_TYPE:
92             nf = NumberFormat.getIntegerInstance(loc);
93             method = "getIntegerInstance";
94             break;
95         case PERCENT_TYPE:
96             nf = NumberFormat.getPercentInstance(loc);
97             method = "getPercentInstance";
98             break;
99         case CURRENCY_TYPE:
100             nf = NumberFormat.getCurrencyInstance(loc);
101             method = "getCurrencyInstance";
102             break;
103         }
104         if (methodName != null) {
105             methodName[0] = method;
106         }
107         return nf;
108     }
109
110     private com.ibm.icu.text.NumberFormat getICUInstance(int type, Locale loc, String[] methodName) {
111         com.ibm.icu.text.NumberFormat icunf = null;
112         String method = null;
113
114         switch (type) {
115         case DEFAULT_TYPE:
116             icunf = com.ibm.icu.text.NumberFormat.getInstance(loc);
117             method = "getInstance";
118             break;
119         case NUMBER_TYPE:
120             icunf = com.ibm.icu.text.NumberFormat.getNumberInstance(loc);
121             method = "getNumberInstance";
122             break;
123         case INTEGER_TYPE:
124             icunf = com.ibm.icu.text.NumberFormat.getIntegerInstance(loc);
125             method = "getIntegerInstance";
126             break;
127         case PERCENT_TYPE:
128             icunf = com.ibm.icu.text.NumberFormat.getPercentInstance(loc);
129             method = "getPercentInstance";
130             break;
131         case CURRENCY_TYPE:
132             icunf = com.ibm.icu.text.NumberFormat.getCurrencyInstance(loc);
133             method = "getCurrencyInstance";
134             break;
135         }
136         if (methodName != null) {
137             methodName[0] = method;
138         }
139         return icunf;
140     }
141
142     /*
143      * Testing the behavior of number format between ICU instance and its
144      * equivalent created via the Locale SPI framework.
145      */
146     public void TestICUEquivalent() {
147         Locale[] TEST_LOCALES = {
148                 new Locale("en", "US"),
149                 new Locale("de", "DE"),
150                 new Locale("zh"),
151         };
152
153         long[] TEST_LONGS = {
154                 40L,
155                 -1578L,
156                 112233445566778899L,
157         };
158
159         double[] TEST_DOUBLES = {
160                 0.0451D,
161                 -1.679D,
162                 124578.369D,
163         };
164
165         Object[] TEST_NUMBERS = {
166                 Byte.valueOf((byte)13),
167                 Integer.valueOf(3961),
168                 Long.valueOf(-3451237890000L),
169                 Float.valueOf(1.754F),
170                 Double.valueOf(-129.942362353D),
171                 new BigInteger("-15253545556575859505"),
172                 new BigDecimal("3.14159265358979323846264338"),
173         };
174
175         String[] methodName = new String[1];
176         for (Locale loc : TEST_LOCALES) {
177             for (int type = 0; type <= 4; type++) {
178                 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
179                 NumberFormat nf = getJDKInstance(type, iculoc, methodName);
180                 com.ibm.icu.text.NumberFormat icunf = getICUInstance(type, loc, null);
181
182                 String s1, s2;
183                 Number n1, n2;
184                 boolean pe1, pe2;
185                 for (long l : TEST_LONGS) {
186                     s1 = nf.format(l);
187                     s2 = icunf.format(l);
188
189                     if (!s1.equals(s2)) {
190                         errln("FAIL: Different results for formatting long " + l + " by NumberFormat("
191                                 + methodName[0] + ") in locale " + loc + " - JDK:" + s1 + " ICU:" + s2);
192                     }
193
194                     pe1 = false;
195                     n1 = n2 = null;
196                     try {
197                         n1 = nf.parse(s1);
198                     } catch (ParseException e) {
199                         pe1 = true;
200                     }
201                     pe2 = false;
202                     try {
203                         n2 = icunf.parse(s2);
204                     } catch (ParseException e) {
205                         pe2 = true;
206                     }
207                     if ((pe1 && !pe2) || (!pe1 && pe2)) {
208                         errln("FAIL: ParseException thrown by " + (pe1 ? "JDK" : "ICU")
209                                 + " NumberFormat(" + methodName[0] + ") for parsing long" + l
210                                 + " in locale " + loc);
211                     } else if (!pe1 && !pe2 && !n1.equals(n2)) {
212                         errln("FAIL: Different results for parsing long " + l + " by NumberFormat("
213                                 + methodName[0] + ") in locale " + loc + " - JDK:" + n1 + " ICU:" + n2);
214                     } else if (pe1 && pe2) {
215                         logln("INFO: ParseException thrown by both JDK and ICU NumberFormat("
216                                 + methodName[0] + ") for parsing long " + l + " in locale " + loc);
217                     }
218                 }
219
220                 for (double d : TEST_DOUBLES) {
221                     s1 = nf.format(d);
222                     s2 = icunf.format(d);
223
224                     if (!s1.equals(s2)) {
225                         errln("FAIL: Different results for formatting double " + d + " by NumberFormat("
226                                 + methodName[0] + ") in locale " + loc + " - JDK:" + s1 + " ICU:" + s2);
227                     }
228
229                     pe1 = false;
230                     n1 = n2 = null;
231                     try {
232                         n1 = nf.parse(s1);
233                     } catch (ParseException e) {
234                         pe1 = true;
235                     }
236                     pe2 = false;
237                     try {
238                         n2 = icunf.parse(s2);
239                     } catch (ParseException e) {
240                         pe2 = true;
241                     }
242                     if ((pe1 && !pe2) || (!pe1 && pe2)) {
243                         errln("FAIL: ParseException thrown by " + (pe1 ? "JDK" : "ICU")
244                                 + " NumberFormat(" + methodName[0] + ") for parsing double" + d
245                                 + " in locale " + loc);
246                     } else if (!pe1 && !pe2 && !n1.equals(n2)) {
247                         errln("FAIL: Different results for parsing double " + d + " by NumberFormat("
248                                 + methodName[0] + ") in locale " + loc + " - JDK:" + n1 + " ICU:" + n2);
249                     } else if (pe1 && pe2) {
250                         logln("INFO: ParseException thrown by both JDK and ICU NumberFormat("
251                                 + methodName[0] + ") for parsing double " + d + " in locale " + loc);
252                     }
253                 }
254
255                 for (Object o : TEST_NUMBERS) {
256                     s1 = nf.format(o);
257                     s2 = icunf.format(o);
258
259                     if (!s1.equals(s2)) {
260                         errln("FAIL: Different results for formatting " + o.getClass().getName() + " by NumberFormat("
261                                 + methodName[0] + ") in locale " + loc + " - JDK:" + s1 + " ICU:" + s2);
262                     }
263
264                     pe1 = false;
265                     n1 = n2 = null;
266                     try {
267                         n1 = nf.parse(s1);
268                     } catch (ParseException e) {
269                         pe1 = true;
270                     }
271                     pe2 = false;
272                     try {
273                         n2 = icunf.parse(s2);
274                     } catch (ParseException e) {
275                         pe2 = true;
276                     }
277                     if ((pe1 && !pe2) || (!pe1 && pe2)) {
278                         errln("FAIL: ParseException thrown by " + (pe1 ? "JDK" : "ICU")
279                                 + " NumberFormat(" + methodName[0] + ") for parsing " + o.getClass().getName()
280                                 + " in locale " + loc);
281                     } else if (!pe1 && !pe2 && !n1.equals(n2)) {
282                         errln("FAIL: Different results for parsing " + o.getClass().getName() + " by NumberFormat("
283                                 + methodName[0] + ") in locale " + loc + " - JDK:" + n1 + " ICU:" + n2);
284                     } else if (pe1 && pe2) {
285                         logln("INFO: ParseException thrown by both JDK and ICU NumberFormat("
286                                 + methodName[0] + ") for parsing " + o.getClass().getName() + " in locale " + loc);
287                     }
288                 }
289             }
290         }
291     }
292
293     public void TestKeywords() {
294         // ICU provider variant is appended
295         ULocale uloc0 = new ULocale("en_US_" + TestUtil.ICU_VARIANT + "@numbers=Arab;currency=EUR");
296         Locale loc = uloc0.toLocale();
297         // On Java 7+, locale extension is preserved
298         ULocale uloc = ULocale.forLocale(loc);
299         String nsType = uloc.getKeywordValue("numbers");
300         if (nsType == null) {
301             // Java 6 - skip this test
302             return;
303         }
304
305         NumberFormat jdkNfmt = NumberFormat.getCurrencyInstance(loc);
306         com.ibm.icu.text.NumberFormat icuNfmt = com.ibm.icu.text.NumberFormat.getCurrencyInstance(uloc);
307
308         final double num = 12345.67d;
309         String jdkOut = jdkNfmt.format(num);
310         String icuOut = icuNfmt.format(num);
311
312         if (!jdkOut.equals(icuOut)) {
313             errln("FAIL: JDK number format with Locale " + loc + " is " + jdkOut + ", expected: " + icuOut);
314         }
315     }
316 }