2 *******************************************************************************
\r
3 * Copyright (C) 2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.localespi;
\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
15 import com.ibm.icu.dev.test.TestFmwk;
\r
17 public class NumberFormatTest extends TestFmwk {
\r
18 public static void main(String[] args) throws Exception {
\r
19 new NumberFormatTest().run(args);
\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
29 * Check if getInstance returns the ICU implementation.
\r
31 public void TestGetInstance() {
\r
32 for (Locale loc : NumberFormat.getAvailableLocales()) {
\r
33 if (TestUtil.isProblematicIBMLocale(loc)) {
\r
34 logln("Skipped " + loc);
\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
45 private void checkGetInstance(int type, Locale loc) {
\r
47 String[] method = new String[1];
\r
48 nf = getJDKInstance(type, loc, method);
\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
54 errln("FAIL: " + method[0] + " returned JDK NumberFormat for locale " + loc);
\r
58 logln("INFO: " + method[0] + " returned ICU NumberFormat for locale " + loc);
\r
60 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
\r
61 NumberFormat nfIcu = null;
\r
62 nfIcu = getJDKInstance(type, iculoc, null);
\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
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
77 private NumberFormat getJDKInstance(int type, Locale loc, String[] methodName) {
\r
78 NumberFormat nf = null;
\r
79 String method = null;
\r
83 nf = NumberFormat.getInstance(loc);
\r
84 method = "getInstance";
\r
87 nf = NumberFormat.getNumberInstance(loc);
\r
88 method = "getNumberInstance";
\r
91 nf = NumberFormat.getIntegerInstance(loc);
\r
92 method = "getIntegerInstance";
\r
95 nf = NumberFormat.getPercentInstance(loc);
\r
96 method = "getPercentInstance";
\r
99 nf = NumberFormat.getCurrencyInstance(loc);
\r
100 method = "getCurrencyInstance";
\r
103 if (methodName != null) {
\r
104 methodName[0] = method;
\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
115 icunf = com.ibm.icu.text.NumberFormat.getInstance(loc);
\r
116 method = "getInstance";
\r
119 icunf = com.ibm.icu.text.NumberFormat.getNumberInstance(loc);
\r
120 method = "getNumberInstance";
\r
123 icunf = com.ibm.icu.text.NumberFormat.getIntegerInstance(loc);
\r
124 method = "getIntegerInstance";
\r
127 icunf = com.ibm.icu.text.NumberFormat.getPercentInstance(loc);
\r
128 method = "getPercentInstance";
\r
130 case CURRENCY_TYPE:
\r
131 icunf = com.ibm.icu.text.NumberFormat.getCurrencyInstance(loc);
\r
132 method = "getCurrencyInstance";
\r
135 if (methodName != null) {
\r
136 methodName[0] = method;
\r
142 * Testing the behavior of number format between ICU instance and its
\r
143 * equivalent created via the Locale SPI framework.
\r
145 public void TestICUEquivalent() {
\r
146 Locale[] TEST_LOCALES = {
\r
147 new Locale("en", "US"),
\r
148 new Locale("de", "DE"),
\r
152 long[] TEST_LONGS = {
\r
155 112233445566778899L,
\r
158 double[] TEST_DOUBLES = {
\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
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
184 for (long l : TEST_LONGS) {
\r
186 s2 = icunf.format(l);
\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
197 } catch (ParseException e) {
\r
202 n2 = icunf.parse(s2);
\r
203 } catch (ParseException e) {
\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
219 for (double d : TEST_DOUBLES) {
\r
221 s2 = icunf.format(d);
\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
232 } catch (ParseException e) {
\r
237 n2 = icunf.parse(s2);
\r
238 } catch (ParseException e) {
\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
254 for (Object o : TEST_NUMBERS) {
\r
256 s2 = icunf.format(o);
\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
267 } catch (ParseException e) {
\r
272 n2 = icunf.parse(s2);
\r
273 } catch (ParseException e) {
\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