001: /*
002: *******************************************************************************
003: * Copyright (C) 2001-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Porting From: ICU4C v1.8.1 : format : NumberFormatRoundTripTest
010: * Source File: $ICU4CRoot/source/test/intltest/nmfmtrt.cpp
011: **/package com.ibm.icu.dev.test.format;
012:
013: import com.ibm.icu.text.*;
014: import java.util.Locale;
015: import java.util.Random;
016:
017: /**
018: * Performs round-trip tests for NumberFormat
019: **/
020: public class NumberFormatRoundTripTest extends
021: com.ibm.icu.dev.test.TestFmwk {
022:
023: public double MAX_ERROR = 1e-14;
024: public double max_numeric_error = 0.0;
025: public double min_numeric_error = 1.0;
026: public boolean verbose = false;
027: public boolean STRING_COMPARE = false;
028: public boolean EXACT_NUMERIC_COMPARE = false;
029: public boolean DEBUG = false;
030: public boolean quick = true;
031:
032: public static void main(String[] args) throws Exception {
033: new NumberFormatRoundTripTest().run(args);
034: }
035:
036: public void TestNumberFormatRoundTrip() {
037:
038: NumberFormat fmt = null;
039:
040: logln("Default Locale");
041:
042: logln("Default Number format");
043: fmt = NumberFormat.getInstance();
044: _test(fmt);
045:
046: logln("Currency Format");
047: fmt = NumberFormat.getCurrencyInstance();
048: _test(fmt);
049:
050: logln("Percent Format");
051: fmt = NumberFormat.getPercentInstance();
052: _test(fmt);
053:
054: int locCount = 0;
055: final Locale[] loc = NumberFormat.getAvailableLocales();
056: if (quick) {
057: if (locCount > 5)
058: locCount = 5;
059: logln("Quick mode: only _testing first 5 Locales");
060: }
061: for (int i = 0; i < locCount; ++i) {
062: logln(loc[i].getDisplayName());
063:
064: fmt = NumberFormat.getInstance(loc[i]);
065: _test(fmt);
066:
067: fmt = NumberFormat.getCurrencyInstance(loc[i]);
068: _test(fmt);
069:
070: fmt = NumberFormat.getPercentInstance(loc[i]);
071: _test(fmt);
072: }
073:
074: logln("Numeric error " + min_numeric_error + " to "
075: + max_numeric_error);
076: }
077:
078: /**
079: * Return a random value from -range..+range.
080: */
081: private Random random;
082:
083: public double randomDouble(double range) {
084: if (random == null) {
085: random = createRandom(); // use test framework's random seed
086: }
087: return random.nextDouble() * range;
088: }
089:
090: public void _test(NumberFormat fmt) {
091:
092: _test(fmt, Double.NaN);
093: _test(fmt, Double.POSITIVE_INFINITY);
094: _test(fmt, Double.NEGATIVE_INFINITY);
095:
096: _test(fmt, 500);
097: _test(fmt, 0);
098: _test(fmt, -0);
099: _test(fmt, 0.0);
100: double negZero = 0.0;
101: negZero /= -1.0;
102: _test(fmt, negZero);
103: _test(fmt, 9223372036854775808.0d);
104: _test(fmt, -9223372036854775809.0d);
105: //_test(fmt, 6.936065876100493E74d);
106:
107: // _test(fmt, 6.212122845281909E48d);
108: for (int i = 0; i < 10; ++i) {
109:
110: _test(fmt, randomDouble(1));
111:
112: _test(fmt, randomDouble(10000));
113:
114: _test(fmt, Math.floor((randomDouble(10000))));
115:
116: _test(fmt, randomDouble(1e50));
117:
118: _test(fmt, randomDouble(1e-50));
119:
120: _test(fmt, randomDouble(1e100));
121:
122: _test(fmt, randomDouble(1e75));
123:
124: _test(fmt, randomDouble(1e308)
125: / ((DecimalFormat) fmt).getMultiplier());
126:
127: _test(fmt, randomDouble(1e75)
128: / ((DecimalFormat) fmt).getMultiplier());
129:
130: _test(fmt, randomDouble(1e65)
131: / ((DecimalFormat) fmt).getMultiplier());
132:
133: _test(fmt, randomDouble(1e-292));
134:
135: _test(fmt, randomDouble(1e-78));
136:
137: _test(fmt, randomDouble(1e-323));
138:
139: _test(fmt, randomDouble(1e-100));
140:
141: _test(fmt, randomDouble(1e-78));
142: }
143: }
144:
145: public void _test(NumberFormat fmt, double value) {
146: _test(fmt, new Double(value));
147: }
148:
149: public void _test(NumberFormat fmt, long value) {
150: _test(fmt, new Long(value));
151: }
152:
153: public void _test(NumberFormat fmt, Number value) {
154: logln("test data = " + value);
155: fmt.setMaximumFractionDigits(999);
156: String s, s2;
157: if (value.getClass().getName().equalsIgnoreCase(
158: "java.lang.Double"))
159: s = fmt.format(value.doubleValue());
160: else
161: s = fmt.format(value.longValue());
162:
163: Number n = new Double(0);
164: boolean show = verbose;
165: if (DEBUG)
166: logln(
167: /*value.getString(temp) +*/" F> " + s);
168: try {
169: n = fmt.parse(s);
170: } catch (java.text.ParseException e) {
171: System.out.println(e);
172: }
173:
174: if (DEBUG)
175: logln(s + " P> " /*+ n.getString(temp)*/);
176:
177: if (value.getClass().getName().equalsIgnoreCase(
178: "java.lang.Double"))
179: s2 = fmt.format(n.doubleValue());
180: else
181: s2 = fmt.format(n.longValue());
182:
183: if (DEBUG)
184: logln(/*n.getString(temp) +*/" F> " + s2);
185:
186: if (STRING_COMPARE) {
187: if (!s.equals(s2)) {
188: errln("*** STRING ERROR \"" + s + "\" != \"" + s2
189: + "\"");
190: show = true;
191: }
192: }
193:
194: if (EXACT_NUMERIC_COMPARE) {
195: if (value != n) {
196: errln("*** NUMERIC ERROR");
197: show = true;
198: }
199: } else {
200: // Compute proportional error
201: double error = proportionalError(value, n);
202:
203: if (error > MAX_ERROR) {
204: errln("*** NUMERIC ERROR " + error);
205: show = true;
206: }
207:
208: if (error > max_numeric_error)
209: max_numeric_error = error;
210: if (error < min_numeric_error)
211: min_numeric_error = error;
212: }
213:
214: if (show)
215: logln(
216: /*value.getString(temp) +*/value.getClass().getName()
217: + " F> " + s + " P> " +
218: /*n.getString(temp) +*/n.getClass().getName()
219: + " F> " + s2);
220:
221: }
222:
223: public double proportionalError(Number a, Number b) {
224: double aa, bb;
225:
226: if (a.getClass().getName().equalsIgnoreCase("java.lang.Double"))
227: aa = a.doubleValue();
228: else
229: aa = a.longValue();
230:
231: if (a.getClass().getName().equalsIgnoreCase("java.lang.Double"))
232: bb = b.doubleValue();
233: else
234: bb = b.longValue();
235:
236: double error = aa - bb;
237: if (aa != 0 && bb != 0)
238: error /= aa;
239:
240: return Math.abs(error);
241: }
242: }
|