2 *******************************************************************************
3 * Copyright (C) 2001-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Porting From: ICU4C v1.8.1 : format : NumberFormatRoundTripTest
10 * Source File: $ICU4CRoot/source/test/intltest/nmfmtrt.cpp
13 package com.ibm.icu.dev.test.format;
15 import java.util.Locale;
16 import java.util.Random;
18 import com.ibm.icu.text.DecimalFormat;
19 import com.ibm.icu.text.NumberFormat;
22 * Performs round-trip tests for NumberFormat
24 public class NumberFormatRoundTripTest extends com.ibm.icu.dev.test.TestFmwk {
26 public double MAX_ERROR = 1e-14;
27 public double max_numeric_error = 0.0;
28 public double min_numeric_error = 1.0;
29 public boolean verbose = false;
30 public boolean STRING_COMPARE = false;
31 public boolean EXACT_NUMERIC_COMPARE = false;
32 public boolean DEBUG = false;
33 public boolean quick = true;
35 public static void main(String[] args) throws Exception {
36 new NumberFormatRoundTripTest().run(args);
39 public void TestNumberFormatRoundTrip() {
41 NumberFormat fmt = null;
43 logln("Default Locale");
45 logln("Default Number format");
46 fmt = NumberFormat.getInstance();
49 logln("Currency Format");
50 fmt = NumberFormat.getCurrencyInstance();
53 logln("Percent Format");
54 fmt = NumberFormat.getPercentInstance();
59 final Locale[] loc = NumberFormat.getAvailableLocales();
63 logln("Quick mode: only _testing first 5 Locales");
65 for(int i = 0; i < locCount; ++i) {
66 logln(loc[i].getDisplayName());
68 fmt = NumberFormat.getInstance(loc[i]);
71 fmt = NumberFormat.getCurrencyInstance(loc[i]);
74 fmt = NumberFormat.getPercentInstance(loc[i]);
78 logln("Numeric error " + min_numeric_error + " to " + max_numeric_error);
82 * Return a random value from -range..+range.
84 private Random random;
85 public double randomDouble(double range) {
87 random = createRandom(); // use test framework's random seed
89 return random.nextDouble() * range;
92 public void _test(NumberFormat fmt) {
94 _test(fmt, Double.NaN);
95 _test(fmt, Double.POSITIVE_INFINITY);
96 _test(fmt, Double.NEGATIVE_INFINITY);
102 double negZero = 0.0;
105 _test(fmt, 9223372036854775808.0d);
106 _test(fmt, -9223372036854775809.0d);
107 //_test(fmt, 6.936065876100493E74d);
109 // _test(fmt, 6.212122845281909E48d);
110 for (int i = 0; i < 10; ++i) {
112 _test(fmt, randomDouble(1));
114 _test(fmt, randomDouble(10000));
116 _test(fmt, Math.floor((randomDouble(10000))));
118 _test(fmt, randomDouble(1e50));
120 _test(fmt, randomDouble(1e-50));
122 _test(fmt, randomDouble(1e100));
124 _test(fmt, randomDouble(1e75));
126 _test(fmt, randomDouble(1e308) / ((DecimalFormat) fmt).getMultiplier());
128 _test(fmt, randomDouble(1e75) / ((DecimalFormat) fmt).getMultiplier());
130 _test(fmt, randomDouble(1e65) / ((DecimalFormat) fmt).getMultiplier());
132 _test(fmt, randomDouble(1e-292));
134 _test(fmt, randomDouble(1e-78));
136 _test(fmt, randomDouble(1e-323));
138 _test(fmt, randomDouble(1e-100));
140 _test(fmt, randomDouble(1e-78));
144 public void _test(NumberFormat fmt, double value) {
145 _test(fmt, new Double(value));
148 public void _test(NumberFormat fmt, long value) {
149 _test(fmt, new Long(value));
152 public void _test(NumberFormat fmt, Number value) {
153 logln("test data = " + value);
154 fmt.setMaximumFractionDigits(999);
156 if (value.getClass().getName().equalsIgnoreCase("java.lang.Double"))
157 s = fmt.format(value.doubleValue());
159 s = fmt.format(value.longValue());
161 Number n = new Double(0);
162 boolean show = verbose;
165 /*value.getString(temp) +*/ " F> " + s);
168 } catch (java.text.ParseException e) {
169 System.out.println(e);
173 logln(s + " P> " /*+ n.getString(temp)*/);
175 if (value.getClass().getName().equalsIgnoreCase("java.lang.Double"))
176 s2 = fmt.format(n.doubleValue());
178 s2 = fmt.format(n.longValue());
181 logln(/*n.getString(temp) +*/ " F> " + s2);
183 if (STRING_COMPARE) {
185 errln("*** STRING ERROR \"" + s + "\" != \"" + s2 + "\"");
190 if (EXACT_NUMERIC_COMPARE) {
192 errln("*** NUMERIC ERROR");
196 // Compute proportional error
197 double error = proportionalError(value, n);
199 if (error > MAX_ERROR) {
200 errln("*** NUMERIC ERROR " + error);
204 if (error > max_numeric_error)
205 max_numeric_error = error;
206 if (error < min_numeric_error)
207 min_numeric_error = error;
212 /*value.getString(temp) +*/ value.getClass().getName() + " F> " + s + " P> " +
213 /*n.getString(temp) +*/ n.getClass().getName() + " F> " + s2);
217 public double proportionalError(Number a, Number b) {
220 if(a.getClass().getName().equalsIgnoreCase("java.lang.Double"))
221 aa = a.doubleValue();
225 if(a.getClass().getName().equalsIgnoreCase("java.lang.Double"))
226 bb = b.doubleValue();
230 double error = aa - bb;
231 if(aa != 0 && bb != 0)
234 return Math.abs(error);