2 *******************************************************************************
\r
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v1.8.1 : format : IntlTestNumberFormat
\r
10 * Source File: $ICU4CRoot/source/test/intltest/tsnmfmt.cpp
\r
13 package com.ibm.icu.dev.test.format;
\r
14 import java.util.Locale;
\r
15 import java.util.Random;
\r
17 import com.ibm.icu.text.DecimalFormat;
\r
18 import com.ibm.icu.text.NumberFormat;
\r
21 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
\r
24 public class IntlTestNumberFormat extends com.ibm.icu.dev.test.TestFmwk {
\r
26 public NumberFormat fNumberFormat;
\r
28 public static void main(String[] args) throws Exception {
\r
29 new IntlTestNumberFormat().run(args);
\r
35 public void _testLocale(Locale locale) {
\r
36 String localeName = locale + " (" + locale.getDisplayName() + ")";
\r
38 logln("Number test " + localeName);
\r
39 fNumberFormat = NumberFormat.getInstance(locale);
\r
42 logln("Currency test " + localeName);
\r
43 fNumberFormat = NumberFormat.getCurrencyInstance(locale);
\r
46 logln("Percent test " + localeName);
\r
47 fNumberFormat = NumberFormat.getPercentInstance(locale);
\r
52 * call _testFormat for currency, percent and plain number instances
\r
54 public void TestLocale() {
\r
55 Locale locale = Locale.getDefault();
\r
56 String localeName = locale + " (" + locale.getDisplayName() + ")";
\r
58 logln("Number test " + localeName);
\r
59 fNumberFormat = NumberFormat.getInstance(locale);
\r
62 logln("Currency test " + localeName);
\r
63 fNumberFormat = NumberFormat.getCurrencyInstance(locale);
\r
66 logln("Percent test " + localeName);
\r
67 fNumberFormat = NumberFormat.getPercentInstance(locale);
\r
72 * call tryIt with many variations, called by testLocale
\r
74 public void _testFormat() {
\r
76 if (fNumberFormat == null){
\r
77 errln("**** FAIL: Null format returned by createXxxInstance.");
\r
80 DecimalFormat s = (DecimalFormat)fNumberFormat;
\r
81 logln("pattern :" + s.toPattern());
\r
83 tryIt(-2.02147304840132e-68);
\r
84 tryIt(3.88057859588817e-68);
\r
85 tryIt(-2.64651110485945e+65);
\r
86 tryIt(9.29526819488338e+64);
\r
88 tryIt(-2.02147304840132e-100);
\r
89 tryIt(3.88057859588817e-096);
\r
90 tryIt(-2.64651110485945e+306);
\r
91 tryIt(9.29526819488338e+250);
\r
93 tryIt(-9.18228054496402e+64);
\r
94 tryIt(-9.69413034454191e+64);
\r
96 tryIt(-9.18228054496402e+255);
\r
97 tryIt(-9.69413034454191e+273);
\r
104 tryIt(Double.POSITIVE_INFINITY);
\r
105 tryIt(Double.NEGATIVE_INFINITY);
\r
111 tryIt(9.99999999999996);
\r
112 tryIt(9.999999999999996);
\r
114 tryIt(Integer.MIN_VALUE);
\r
115 tryIt(Integer.MAX_VALUE);
\r
116 tryIt((double)Integer.MIN_VALUE);
\r
117 tryIt((double)Integer.MAX_VALUE);
\r
118 tryIt((double)Integer.MIN_VALUE - 1.0);
\r
119 tryIt((double)Integer.MAX_VALUE + 1.0);
\r
121 tryIt(5.0 / 9.0 * 1e-20);
\r
122 tryIt(4.0 / 9.0 * 1e-20);
\r
123 tryIt(5.0 / 9.0 * 1e+20);
\r
124 tryIt(4.0 / 9.0 * 1e+20);
\r
126 tryIt(2147483647.);
\r
135 tryIt(-1913860352);
\r
137 Random random = createRandom(); // use test framework's random seed
\r
138 for (int j = 0; j < 10; j++) {
\r
139 double d = random.nextDouble()*2e10 - 1e10;
\r
146 * Perform tests using aNumber and fNumberFormat, called in many variations
\r
148 public void tryIt(double aNumber) {
\r
149 final int DEPTH = 10;
\r
150 double[] number = new double[DEPTH];
\r
151 String[] string = new String[DEPTH];
\r
152 int numberMatch = 0;
\r
153 int stringMatch = 0;
\r
154 boolean dump = false;
\r
157 for (i = 0; i < DEPTH; i++) {
\r
159 number[i] = aNumber;
\r
162 number[i - 1] = fNumberFormat.parse(string[i - 1]).doubleValue();
\r
163 } catch(java.text.ParseException pe) {
\r
164 errln("**** FAIL: Parse of " + string[i-1] + " failed.");
\r
170 string[i] = fNumberFormat.format(number[i]);
\r
173 if (numberMatch == 0 && number[i] == number[i-1])
\r
175 else if (numberMatch > 0 && number[i] != number[i-1])
\r
177 errln("**** FAIL: Numeric mismatch after match.");
\r
181 if (stringMatch == 0 && string[i] == string[i-1])
\r
183 else if (stringMatch > 0 && string[i] != string[i-1])
\r
185 errln("**** FAIL: String mismatch after match.");
\r
190 if (numberMatch > 0 && stringMatch > 0)
\r
196 if (stringMatch > 2 || numberMatch > 2)
\r
198 errln("**** FAIL: No string and/or number match within 2 iterations.");
\r
204 for (int k=0; k<=i; ++k)
\r
206 logln(k + ": " + number[k] + " F> " +
\r
207 string[k] + " P> ");
\r
214 * perform tests using aNumber and fNumberFormat, called in many variations
\r
216 public void tryIt(int aNumber) {
\r
219 String stringNum = fNumberFormat.format(aNumber);
\r
221 number = fNumberFormat.parse(stringNum).longValue();
\r
222 } catch (java.text.ParseException pe) {
\r
223 errln("**** FAIL: Parse of " + stringNum + " failed.");
\r
227 if (number != aNumber) {
\r
228 errln("**** FAIL: Parse of " + stringNum + " failed. Got:" + number
\r
229 + " Expected:" + aNumber);
\r
235 * test NumberFormat::getAvailableLocales
\r
237 public void TestAvailableLocales() {
\r
238 final Locale[] locales = NumberFormat.getAvailableLocales();
\r
239 int count = locales.length;
\r
240 logln(count + " available locales");
\r
244 for (int i = 0; i< count; ++i)
\r
248 all += locales[i].getDisplayName();
\r
253 errln("**** FAIL: Zero available locales or null array pointer");
\r
257 * call testLocale for all locales
\r
259 public void TestMonster() {
\r
260 final String SEP = "============================================================\n";
\r
262 final Locale[] allLocales = NumberFormat.getAvailableLocales();
\r
263 Locale[] locales = allLocales;
\r
264 count = locales.length;
\r
267 if (getInclusion() < 10 && count > 6) {
\r
269 locales = new Locale[6];
\r
270 locales[0] = allLocales[0];
\r
271 locales[1] = allLocales[1];
\r
272 locales[2] = allLocales[2];
\r
273 // In a quick test, make sure we test locales that use
\r
274 // currency prefix, currency suffix, and choice currency
\r
275 // logic. Otherwise bugs in these areas can slip through.
\r
276 locales[3] = new Locale("ar", "AE", "");
\r
277 locales[4] = new Locale("cs", "CZ", "");
\r
278 locales[5] = new Locale("en", "IN", "");
\r
280 for (int i=0; i<count; ++i)
\r
283 _testLocale(locales[i]);
\r