2 *******************************************************************************
3 * Copyright (C) 2008-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.localespi;
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;
15 import com.ibm.icu.dev.test.TestFmwk;
16 import com.ibm.icu.util.ULocale;
18 public class NumberFormatTest extends TestFmwk {
19 public static void main(String[] args) throws Exception {
20 new NumberFormatTest().run(args);
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;
30 * Check if getInstance returns the ICU implementation.
32 public void TestGetInstance() {
33 for (Locale loc : NumberFormat.getAvailableLocales()) {
34 if (TestUtil.isProblematicIBMLocale(loc)) {
35 logln("Skipped " + loc);
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);
46 private void checkGetInstance(int type, Locale loc) {
48 String[] method = new String[1];
49 nf = getJDKInstance(type, loc, method);
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)) {
55 errln("FAIL: " + method[0] + " returned JDK NumberFormat for locale " + loc);
59 logln("INFO: " + method[0] + " returned ICU NumberFormat for locale " + loc);
61 Locale iculoc = TestUtil.toICUExtendedLocale(loc);
62 NumberFormat nfIcu = null;
63 nfIcu = getJDKInstance(type, iculoc, null);
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);
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);
78 private NumberFormat getJDKInstance(int type, Locale loc, String[] methodName) {
79 NumberFormat nf = null;
84 nf = NumberFormat.getInstance(loc);
85 method = "getInstance";
88 nf = NumberFormat.getNumberInstance(loc);
89 method = "getNumberInstance";
92 nf = NumberFormat.getIntegerInstance(loc);
93 method = "getIntegerInstance";
96 nf = NumberFormat.getPercentInstance(loc);
97 method = "getPercentInstance";
100 nf = NumberFormat.getCurrencyInstance(loc);
101 method = "getCurrencyInstance";
104 if (methodName != null) {
105 methodName[0] = method;
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;
116 icunf = com.ibm.icu.text.NumberFormat.getInstance(loc);
117 method = "getInstance";
120 icunf = com.ibm.icu.text.NumberFormat.getNumberInstance(loc);
121 method = "getNumberInstance";
124 icunf = com.ibm.icu.text.NumberFormat.getIntegerInstance(loc);
125 method = "getIntegerInstance";
128 icunf = com.ibm.icu.text.NumberFormat.getPercentInstance(loc);
129 method = "getPercentInstance";
132 icunf = com.ibm.icu.text.NumberFormat.getCurrencyInstance(loc);
133 method = "getCurrencyInstance";
136 if (methodName != null) {
137 methodName[0] = method;
143 * Testing the behavior of number format between ICU instance and its
144 * equivalent created via the Locale SPI framework.
146 public void TestICUEquivalent() {
147 Locale[] TEST_LOCALES = {
148 new Locale("en", "US"),
149 new Locale("de", "DE"),
153 long[] TEST_LONGS = {
159 double[] TEST_DOUBLES = {
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"),
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);
185 for (long l : TEST_LONGS) {
187 s2 = icunf.format(l);
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);
198 } catch (ParseException e) {
203 n2 = icunf.parse(s2);
204 } catch (ParseException e) {
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);
220 for (double d : TEST_DOUBLES) {
222 s2 = icunf.format(d);
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);
233 } catch (ParseException e) {
238 n2 = icunf.parse(s2);
239 } catch (ParseException e) {
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);
255 for (Object o : TEST_NUMBERS) {
257 s2 = icunf.format(o);
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);
268 } catch (ParseException e) {
273 n2 = icunf.parse(s2);
274 } catch (ParseException e) {
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);
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
305 NumberFormat jdkNfmt = NumberFormat.getCurrencyInstance(loc);
306 com.ibm.icu.text.NumberFormat icuNfmt = com.ibm.icu.text.NumberFormat.getCurrencyInstance(uloc);
308 final double num = 12345.67d;
309 String jdkOut = jdkNfmt.format(num);
310 String icuOut = icuNfmt.format(num);
312 if (!jdkOut.equals(icuOut)) {
313 errln("FAIL: JDK number format with Locale " + loc + " is " + jdkOut + ", expected: " + icuOut);