]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/format/NumberFormatRegressionTest.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / format / NumberFormatRegressionTest.java
1 //##header J2SE15
2 /*
3  *******************************************************************************
4  * Copyright (C) 2001-2009, International Business Machines Corporation and    *
5  * others. All Rights Reserved.                                                *
6  *******************************************************************************
7  */
8
9 /** 
10  * Port From:   ICU4C v1.8.1 : format : NumberFormatRegressionTest
11  * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp
12  **/
13
14 package com.ibm.icu.dev.test.format;
15
16 import com.ibm.icu.text.*;
17 import com.ibm.icu.util.*;
18 import java.util.Locale;
19 import java.util.Date;
20 import java.text.ParseException;
21 import java.io.*;
22
23 /** 
24  * Performs regression test for MessageFormat
25  **/
26 public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk {
27     
28     public static void main(String[] args) throws Exception{
29         new NumberFormatRegressionTest().run(args);
30     }
31     
32     /**
33      * alphaWorks upgrade
34      */
35     public void Test4161100() {
36         NumberFormat nf = NumberFormat.getInstance(Locale.US);
37         nf.setMinimumFractionDigits(1);
38         nf.setMaximumFractionDigits(1);
39         double a = -0.09;
40         String s = nf.format(a);
41         logln(a + " x " +
42               ((DecimalFormat) nf).toPattern() + " = " + s);
43         if (!s.equals("-0.1")) {
44             errln("FAIL");
45         }
46     }
47     
48     /**
49      * DateFormat should call setIntegerParseOnly(TRUE) on adopted
50      * NumberFormat objects.
51      */
52     public void TestJ691() {
53         
54         Locale loc = new Locale("fr", "CH");
55     
56         // set up the input date string & expected output
57         String udt = "11.10.2000";
58         String exp = "11.10.00";
59     
60         // create a Calendar for this locale
61         Calendar cal = Calendar.getInstance(loc);
62     
63         // create a NumberFormat for this locale
64         NumberFormat nf = NumberFormat.getInstance(loc);
65     
66         // *** Here's the key: We don't want to have to do THIS:
67         //nf.setParseIntegerOnly(true);
68     
69         // create the DateFormat
70         DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc);
71     
72         df.setCalendar(cal);
73         df.setNumberFormat(nf);
74     
75         // set parsing to lenient & parse
76         Date ulocdat = new Date();
77         df.setLenient(true);
78         try {
79             ulocdat = df.parse(udt);
80         } catch (java.text.ParseException pe) {
81             errln(pe.getMessage());
82         }
83         // format back to a string
84         String outString = df.format(ulocdat);
85     
86         if (!outString.equals(exp)) {
87             errln("FAIL: " + udt + " => " + outString);
88         }
89     }
90     
91     /**
92      * Test getIntegerInstance();
93      */
94     public void Test4408066() {
95         
96         NumberFormat nf1 = NumberFormat.getIntegerInstance();
97         NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
98     
99         //test isParseIntegerOnly
100         if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) {
101             errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)");
102         }
103     
104         //Test format
105         {
106             double[] data = {
107                 -3.75, -2.5, -1.5, 
108                 -1.25, 0,    1.0, 
109                 1.25,  1.5,  2.5, 
110                 3.75,  10.0, 255.5
111                 };
112             String[] expected = {
113                 "-4", "-2", "-2",
114                 "-1", "0",  "1",
115                 "1",  "2",  "2",
116                 "4",  "10", "256"
117                 };
118     
119             for (int i = 0; i < data.length; ++i) {
120                 String result = nf1.format(data[i]);
121                 if (!result.equals(expected[i])) {
122                     errln("Failed => Source: " + Double.toString(data[i]) 
123                         + ";Formatted : " + result
124                         + ";but expectted: " + expected[i]);
125                 }
126             }
127         }
128         //Test parse, Parsing should stop at "."
129         {
130             String data[] = {
131                 "-3.75", "-2.5", "-1.5", 
132                 "-1.25", "0",    "1.0", 
133                 "1.25",  "1.5",  "2.5", 
134                 "3.75",  "10.0", "255.5"
135                 };
136             long[] expected = {
137                 -3, -2, -1,
138                 -1, 0,  1,
139                 1,  1,  2,
140                 3,  10, 255
141                 };
142             
143             for (int i = 0; i < data.length; ++i) {
144                 Number n = null;
145                 try {
146                     n = nf1.parse(data[i]);
147                 } catch (ParseException e) {
148                     errln("Failed: " + e.getMessage());
149                 }
150                 if (!(n instanceof Long) || (n instanceof Integer)) {
151                     errln("Failed: Integer Number Format should parse string to Long/Integer");
152                 }
153                 if (n.longValue() != expected[i]) {
154                     errln("Failed=> Source: " + data[i] 
155                         + ";result : " + n.toString()
156                         + ";expected :" + Long.toString(expected[i]));
157                 }
158             }
159         }
160     }
161     
162     //Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1)
163     public void TestSerialization() throws IOException{
164 //#if defined(FOUNDATION10) || defined(J2SE13)
165 //#else
166         byte[][] contents = NumberFormatSerialTestData.getContent();
167         double data = 1234.56;
168         String[] expected = {
169             "1,234.56", "$1,234.56", "123,456%", "1.23456E3"};
170         for (int i = 0; i < 4; ++i) {
171             ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(contents[i]));
172             try {
173                 NumberFormat format = (NumberFormat) ois.readObject();
174                 String result = format.format(data);
175                 if (result.equals(expected[i])) {
176                     logln("OK: Deserialized bogus NumberFormat(new version read old version)");
177                 } else {
178                     errln("FAIL: the test data formats are not euqal");
179                 }
180             } catch (Exception e) {
181                 warnln("FAIL: " + e.getMessage());
182             }
183         }
184 //#endif
185     }
186
187     /*
188      * Test case for JB#5509, strict parsing issue
189      */
190     public void TestJB5509() {
191         String[] data = {
192                 "1,2",
193                 "1.2",
194                 "1,2.5",
195                 "1,23.5",
196                 "1,234.5",
197                 "1,234",
198                 "1,234,567",
199                 "1,234,567.8",
200                 "1,234,5",
201                 "1,234,5.6",
202                 "1,234,56.7"
203         };
204         boolean[] expected = { // false for expected parse failure
205                 false,
206                 true,
207                 false,
208                 false,
209                 true,
210                 true,
211                 true,
212                 true,
213                 false,
214                 false,
215                 false,
216                 false
217         };
218
219         DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US")));
220         df.setParseStrict(true);
221         for (int i = 0; i < data.length; i++) {
222             try {
223                 df.parse(data[i]);
224                 if (!expected[i]) {
225                     errln("Failed: ParseException must be thrown for string " + data[i]);
226                 }
227             } catch (ParseException pe) {
228                 if (expected[i]) {
229                     errln("Failed: ParseException must not be thrown for string " + data[i]);
230                 }
231             }
232         }
233     }
234
235     /*
236      * Test case for ticket#5698 - parsing extremely large/small values
237      */
238     public void TestT5698() {
239         final String[] data = {
240                 "12345679E66666666666666666",
241                 "-12345679E66666666666666666",
242                 ".1E2147483648", // exponent > max int
243                 ".1E2147483647", // exponent == max int
244                 ".1E-2147483648", // exponent == min int
245                 ".1E-2147483649", // exponent < min int
246                 "1.23E350", // value > max double
247                 "1.23E300", // value < max double
248                 "-1.23E350", // value < min double
249                 "-1.23E300", // value > min double
250                 "4.9E-324", // value = smallest non-zero double
251                 "1.0E-325", // 0 < value < smallest non-zero positive double0
252                 "-1.0E-325", // 0 > value > largest non-zero negative double
253         };
254         final double[] expected = {
255                 Double.POSITIVE_INFINITY,
256                 Double.NEGATIVE_INFINITY,
257                 Double.POSITIVE_INFINITY,
258                 Double.POSITIVE_INFINITY,
259                 0.0,
260                 0.0,
261                 Double.POSITIVE_INFINITY,
262                 1.23e300d,
263                 Double.NEGATIVE_INFINITY,
264                 -1.23e300d,
265                 4.9e-324d,
266                 0.0,
267                 -0.0,
268         };
269
270         NumberFormat nfmt = NumberFormat.getInstance();
271
272         for (int i = 0; i < data.length; i++) {
273             try {
274                 Number n = nfmt.parse(data[i]);
275                 if (expected[i] != n.doubleValue()) {
276                     errln("Failed: Parsed result for " + data[i] + ": " 
277                             + n.doubleValue() + " / expected: " + expected[i]);
278                 }
279             } catch (ParseException pe) {
280                 errln("Failed: ParseException is thrown for " + data[i]);
281             }
282         }
283     }
284     
285     void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber) {
286         String myString = nf.format(myNumber);
287         
288             double aNumber;
289             try {
290                 aNumber = nf.parse(myString).doubleValue();
291             } catch (ParseException e) {
292                 // TODO Auto-generated catch block
293                 errln("FAIL: " + testcase +" - failed to parse. " + e.toString());
294                 return;
295             }
296             if(Math.abs(aNumber-myNumber)>.001) {
297             errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");
298         } else {
299             logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n");
300         }
301     }
302
303     void checkNBSPPatternRT(String testcase, NumberFormat nf) {
304         checkNBSPPatternRtNum(testcase, nf, 12345.);
305         checkNBSPPatternRtNum(testcase, nf, -12345.);
306     }
307
308     public void TestNBSPInPattern() {
309     NumberFormat nf = null;
310     String testcase;
311     
312     
313     testcase="ar_AE UNUM_CURRENCY";
314     nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE"));
315     checkNBSPPatternRT(testcase, nf);
316     // if we don't have CLDR 1.6 data, bring out the problem anyways 
317     
318     String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00";
319     testcase = "ar_AE special pattern: " + SPECIAL_PATTERN;
320     nf = new DecimalFormat();
321     ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN);
322     checkNBSPPatternRT(testcase, nf);
323     
324     }
325     
326 }