]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatRoundTripTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / format / NumberFormatRoundTripTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 /** 
9  * Porting From: ICU4C v1.8.1 : format : NumberFormatRoundTripTest
10  * Source File: $ICU4CRoot/source/test/intltest/nmfmtrt.cpp
11  **/
12
13 package com.ibm.icu.dev.test.format;
14
15 import java.util.Locale;
16 import java.util.Random;
17
18 import com.ibm.icu.text.DecimalFormat;
19 import com.ibm.icu.text.NumberFormat;
20
21 /** 
22  * Performs round-trip tests for NumberFormat
23  **/
24 public class NumberFormatRoundTripTest extends com.ibm.icu.dev.test.TestFmwk {
25     
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;
34     
35     public static void main(String[] args) throws Exception {
36         new NumberFormatRoundTripTest().run(args);
37     }
38     
39     public void TestNumberFormatRoundTrip() {
40     
41         NumberFormat fmt = null;
42     
43         logln("Default Locale");
44         
45         logln("Default Number format");
46         fmt = NumberFormat.getInstance();
47         _test(fmt);
48     
49         logln("Currency Format");
50         fmt = NumberFormat.getCurrencyInstance();
51         _test(fmt);
52     
53         logln("Percent Format");
54         fmt = NumberFormat.getPercentInstance();
55         _test(fmt);
56     
57     
58         int locCount = 0;
59         final Locale[] loc = NumberFormat.getAvailableLocales();
60         if(quick) {
61             if(locCount > 5)
62                 locCount = 5;
63             logln("Quick mode: only _testing first 5 Locales");
64         }
65         for(int i = 0; i < locCount; ++i) {
66             logln(loc[i].getDisplayName());
67     
68             fmt = NumberFormat.getInstance(loc[i]);
69             _test(fmt);
70         
71             fmt = NumberFormat.getCurrencyInstance(loc[i]);
72             _test(fmt);
73         
74             fmt = NumberFormat.getPercentInstance(loc[i]);
75             _test(fmt);
76         }
77     
78         logln("Numeric error " + min_numeric_error + " to " + max_numeric_error);
79     }
80     
81     /**
82      * Return a random value from -range..+range.
83      */
84     private Random random;
85     public double randomDouble(double range) {
86         if (random == null) {
87             random = createRandom(); // use test framework's random seed
88         }
89         return  random.nextDouble() * range;
90     } 
91     
92     public void _test(NumberFormat fmt) {
93     
94         _test(fmt, Double.NaN);
95         _test(fmt, Double.POSITIVE_INFINITY);
96         _test(fmt, Double.NEGATIVE_INFINITY);
97     
98         _test(fmt, 500);
99         _test(fmt, 0);
100         _test(fmt, -0);
101         _test(fmt, 0.0);
102         double negZero = 0.0;
103         negZero /= -1.0;
104         _test(fmt, negZero);
105         _test(fmt, 9223372036854775808.0d);
106         _test(fmt, -9223372036854775809.0d);
107         //_test(fmt, 6.936065876100493E74d);
108         
109     //    _test(fmt, 6.212122845281909E48d);
110         for (int i = 0; i < 10; ++i) {
111     
112             _test(fmt, randomDouble(1));
113             
114             _test(fmt, randomDouble(10000));
115     
116             _test(fmt, Math.floor((randomDouble(10000))));
117     
118             _test(fmt, randomDouble(1e50));
119     
120             _test(fmt, randomDouble(1e-50));
121     
122             _test(fmt, randomDouble(1e100));
123     
124             _test(fmt, randomDouble(1e75));
125     
126             _test(fmt, randomDouble(1e308) / ((DecimalFormat) fmt).getMultiplier());
127     
128             _test(fmt, randomDouble(1e75) / ((DecimalFormat) fmt).getMultiplier());
129     
130             _test(fmt, randomDouble(1e65) / ((DecimalFormat) fmt).getMultiplier());
131     
132             _test(fmt, randomDouble(1e-292));
133     
134             _test(fmt, randomDouble(1e-78));
135     
136             _test(fmt, randomDouble(1e-323));
137     
138             _test(fmt, randomDouble(1e-100));
139     
140             _test(fmt, randomDouble(1e-78));
141         }
142     }
143     
144     public void _test(NumberFormat fmt, double value) {
145         _test(fmt, new Double(value));
146     }
147     
148     public void _test(NumberFormat fmt, long value) {
149         _test(fmt, new Long(value));
150     }
151     
152     public void _test(NumberFormat fmt, Number value) {
153         logln("test data = " + value);
154         fmt.setMaximumFractionDigits(999);
155         String s, s2;
156         if (value.getClass().getName().equalsIgnoreCase("java.lang.Double"))
157             s = fmt.format(value.doubleValue());
158         else
159             s = fmt.format(value.longValue());
160     
161         Number n = new Double(0);
162         boolean show = verbose;
163         if (DEBUG)
164             logln(
165             /*value.getString(temp) +*/ " F> " + s);
166         try {
167             n = fmt.parse(s);
168         } catch (java.text.ParseException e) {
169             System.out.println(e);
170         }
171     
172         if (DEBUG)
173             logln(s + " P> " /*+ n.getString(temp)*/);
174     
175         if (value.getClass().getName().equalsIgnoreCase("java.lang.Double"))
176             s2 = fmt.format(n.doubleValue());
177         else
178             s2 = fmt.format(n.longValue());
179     
180         if (DEBUG)
181             logln(/*n.getString(temp) +*/ " F> " + s2);
182     
183         if (STRING_COMPARE) {
184             if (!s.equals(s2)) {
185                 errln("*** STRING ERROR \"" + s + "\" != \"" + s2 + "\"");
186                 show = true;
187             }
188         }
189     
190         if (EXACT_NUMERIC_COMPARE) {
191             if (value != n) {
192                 errln("*** NUMERIC ERROR");
193                 show = true;
194             }
195         } else {
196             // Compute proportional error
197             double error = proportionalError(value, n);
198     
199             if (error > MAX_ERROR) {
200                 errln("*** NUMERIC ERROR " + error);
201                 show = true;
202             }
203     
204             if (error > max_numeric_error)
205                 max_numeric_error = error;
206             if (error < min_numeric_error)
207                 min_numeric_error = error;
208         }
209     
210         if (show)
211             logln(
212             /*value.getString(temp) +*/ value.getClass().getName() + " F> " + s + " P> " +
213             /*n.getString(temp) +*/ n.getClass().getName() + " F> " + s2);
214     
215     }
216         
217     public double proportionalError(Number a, Number b) {
218         double aa,bb;
219         
220         if(a.getClass().getName().equalsIgnoreCase("java.lang.Double"))
221             aa = a.doubleValue();
222         else
223             aa = a.longValue();
224     
225         if(a.getClass().getName().equalsIgnoreCase("java.lang.Double"))
226             bb = b.doubleValue();
227         else
228             bb = b.longValue();
229     
230         double error = aa - bb;
231         if(aa != 0 && bb != 0) 
232             error /= aa;
233            
234         return Math.abs(error);
235     }   
236 }