001: /*
002: *******************************************************************************
003: * Copyright (C) 2001-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v1.8.1 : format : IntlTestNumberFormat
010: * Source File: $ICU4CRoot/source/test/intltest/tsnmfmt.cpp
011: **/package com.ibm.icu.dev.test.format;
012:
013: import java.util.Locale;
014: import java.util.Random;
015:
016: import com.ibm.icu.text.*;
017:
018: /**
019: * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
020: * NumberFormat.
021: */
022: public class IntlTestNumberFormat extends com.ibm.icu.dev.test.TestFmwk {
023:
024: public NumberFormat fNumberFormat;
025:
026: public static void main(String[] args) throws Exception {
027: new IntlTestNumberFormat().run(args);
028: }
029:
030: /**
031: * Internal use
032: */
033: public void _testLocale(Locale locale) {
034: String localeName = locale + " (" + locale.getDisplayName()
035: + ")";
036:
037: logln("Number test " + localeName);
038: fNumberFormat = NumberFormat.getInstance(locale);
039: _testFormat();
040:
041: logln("Currency test " + localeName);
042: fNumberFormat = NumberFormat.getCurrencyInstance(locale);
043: _testFormat();
044:
045: logln("Percent test " + localeName);
046: fNumberFormat = NumberFormat.getPercentInstance(locale);
047: _testFormat();
048: }
049:
050: /**
051: * call _testFormat for currency, percent and plain number instances
052: */
053: public void TestLocale() {
054: Locale locale = Locale.getDefault();
055: String localeName = locale + " (" + locale.getDisplayName()
056: + ")";
057:
058: logln("Number test " + localeName);
059: fNumberFormat = NumberFormat.getInstance(locale);
060: _testFormat();
061:
062: logln("Currency test " + localeName);
063: fNumberFormat = NumberFormat.getCurrencyInstance(locale);
064: _testFormat();
065:
066: logln("Percent test " + localeName);
067: fNumberFormat = NumberFormat.getPercentInstance(locale);
068: _testFormat();
069: }
070:
071: /**
072: * call tryIt with many variations, called by testLocale
073: */
074: public void _testFormat() {
075:
076: if (fNumberFormat == null) {
077: errln("**** FAIL: Null format returned by createXxxInstance.");
078: return;
079: }
080: DecimalFormat s = (DecimalFormat) fNumberFormat;
081: logln("pattern :" + s.toPattern());
082:
083: tryIt(-2.02147304840132e-68);
084: tryIt(3.88057859588817e-68);
085: tryIt(-2.64651110485945e+65);
086: tryIt(9.29526819488338e+64);
087:
088: tryIt(-2.02147304840132e-100);
089: tryIt(3.88057859588817e-096);
090: tryIt(-2.64651110485945e+306);
091: tryIt(9.29526819488338e+250);
092:
093: tryIt(-9.18228054496402e+64);
094: tryIt(-9.69413034454191e+64);
095:
096: tryIt(-9.18228054496402e+255);
097: tryIt(-9.69413034454191e+273);
098:
099: tryIt(1.234e-200);
100: tryIt(-2.3e-168);
101:
102: tryIt(Double.NaN);
103: tryIt(Double.POSITIVE_INFINITY);
104: tryIt(Double.NEGATIVE_INFINITY);
105:
106: tryIt(251887531);
107: tryIt(5e-20 / 9);
108: tryIt(5e20 / 9);
109: tryIt(1.234e-50);
110: tryIt(9.99999999999996);
111: tryIt(9.999999999999996);
112:
113: tryIt(Integer.MIN_VALUE);
114: tryIt(Integer.MAX_VALUE);
115: tryIt((double) Integer.MIN_VALUE);
116: tryIt((double) Integer.MAX_VALUE);
117: tryIt((double) Integer.MIN_VALUE - 1.0);
118: tryIt((double) Integer.MAX_VALUE + 1.0);
119:
120: tryIt(5.0 / 9.0 * 1e-20);
121: tryIt(4.0 / 9.0 * 1e-20);
122: tryIt(5.0 / 9.0 * 1e+20);
123: tryIt(4.0 / 9.0 * 1e+20);
124:
125: tryIt(2147483647.);
126: tryIt(0);
127: tryIt(0.0);
128: tryIt(1);
129: tryIt(10);
130: tryIt(100);
131: tryIt(-1);
132: tryIt(-10);
133: tryIt(-100);
134: tryIt(-1913860352);
135:
136: Random random = createRandom(); // use test framework's random seed
137: for (int j = 0; j < 10; j++) {
138: double d = random.nextDouble() * 2e10 - 1e10;
139: tryIt(d);
140:
141: }
142: }
143:
144: /**
145: * Perform tests using aNumber and fNumberFormat, called in many variations
146: */
147: public void tryIt(double aNumber) {
148: final int DEPTH = 10;
149: double[] number = new double[DEPTH];
150: String[] string = new String[DEPTH];
151: int numberMatch = 0;
152: int stringMatch = 0;
153: boolean dump = false;
154: int i;
155:
156: for (i = 0; i < DEPTH; i++) {
157: if (i == 0) {
158: number[i] = aNumber;
159: } else {
160: try {
161: number[i - 1] = fNumberFormat.parse(string[i - 1])
162: .doubleValue();
163: } catch (java.text.ParseException pe) {
164: errln("**** FAIL: Parse of " + string[i - 1]
165: + " failed.");
166: dump = true;
167: break;
168: }
169: }
170:
171: string[i] = fNumberFormat.format(number[i]);
172: if (i > 0) {
173: if (numberMatch == 0 && number[i] == number[i - 1])
174: numberMatch = i;
175: else if (numberMatch > 0 && number[i] != number[i - 1]) {
176: errln("**** FAIL: Numeric mismatch after match.");
177: dump = true;
178: break;
179: }
180: if (stringMatch == 0 && string[i] == string[i - 1])
181: stringMatch = i;
182: else if (stringMatch > 0 && string[i] != string[i - 1]) {
183: errln("**** FAIL: String mismatch after match.");
184: dump = true;
185: break;
186: }
187: }
188: if (numberMatch > 0 && stringMatch > 0)
189: break;
190:
191: if (i == DEPTH)
192: --i;
193:
194: if (stringMatch > 2 || numberMatch > 2) {
195: errln("**** FAIL: No string and/or number match within 2 iterations.");
196: dump = true;
197: }
198:
199: if (dump) {
200: for (int k = 0; k <= i; ++k) {
201: logln(k + ": " + number[k] + " F> " + string[k]
202: + " P> ");
203: }
204: }
205: }
206: }
207:
208: /**
209: * perform tests using aNumber and fNumberFormat, called in many variations
210: **/
211: public void tryIt(int aNumber) {
212: long number;
213:
214: String stringNum = fNumberFormat.format(aNumber);
215: try {
216: number = fNumberFormat.parse(stringNum).longValue();
217: } catch (java.text.ParseException pe) {
218: errln("**** FAIL: Parse of " + stringNum + " failed.");
219: return;
220: }
221:
222: if (number != aNumber) {
223: errln("**** FAIL: Parse of " + stringNum + " failed. Got:"
224: + number + " Expected:" + aNumber);
225: }
226:
227: }
228:
229: /**
230: * test NumberFormat::getAvailableLocales
231: **/
232: public void TestAvailableLocales() {
233: final Locale[] locales = NumberFormat.getAvailableLocales();
234: int count = locales.length;
235: logln(count + " available locales");
236: if (count != 0) {
237: String all = "";
238: for (int i = 0; i < count; ++i) {
239: if (i != 0)
240: all += ", ";
241: all += locales[i].getDisplayName();
242: }
243: logln(all);
244: } else
245: errln("**** FAIL: Zero available locales or null array pointer");
246: }
247:
248: /**
249: * call testLocale for all locales
250: **/
251: public void TestMonster() {
252: final String SEP = "============================================================\n";
253: int count;
254: final Locale[] allLocales = NumberFormat.getAvailableLocales();
255: Locale[] locales = allLocales;
256: count = locales.length;
257: if (count != 0) {
258: if (getInclusion() < 10 && count > 6) {
259: count = 6;
260: locales = new Locale[6];
261: locales[0] = allLocales[0];
262: locales[1] = allLocales[1];
263: locales[2] = allLocales[2];
264: // In a quick test, make sure we test locales that use
265: // currency prefix, currency suffix, and choice currency
266: // logic. Otherwise bugs in these areas can slip through.
267: locales[3] = new Locale("ar", "AE", "");
268: locales[4] = new Locale("cs", "CZ", "");
269: locales[5] = new Locale("en", "IN", "");
270: }
271: for (int i = 0; i < count; ++i) {
272: String name = locales[i].getDisplayName();
273: logln(SEP);
274: _testLocale(locales[i]);
275: }
276: }
277:
278: logln(SEP);
279: }
280: }
|