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