]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/format/BigNumberFormatTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / format / BigNumberFormatTest.java
1 //##header\r
2 /*\r
3  *******************************************************************************\r
4  * Copyright (C) 1996-2009, International Business Machines Corporation and    *\r
5  * others. All Rights Reserved.                                                *\r
6  *******************************************************************************\r
7  */\r
8 package com.ibm.icu.dev.test.format;\r
9 \r
10 import com.ibm.icu.dev.test.*;\r
11 import com.ibm.icu.text.*;\r
12 import java.text.ParseException;\r
13 import java.util.*;\r
14 import com.ibm.icu.impl.Utility;\r
15 \r
16 /**\r
17  * @test\r
18  * General test of Big NumberFormat\r
19  */\r
20 public class BigNumberFormatTest extends TestFmwk {\r
21 \r
22     static final int ILLEGAL = -1;\r
23 \r
24     public static void main(String[] args) throws Exception {\r
25         new BigNumberFormatTest().run(args);\r
26     }\r
27         \r
28     public void TestExponent() {\r
29         DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
30         DecimalFormat fmt1 = new DecimalFormat("0.###E0", US);\r
31         DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US);\r
32         Number n = new Long(1234);\r
33         expect(fmt1, n, "1.234E3");\r
34         expect(fmt2, n, "1.234E+3");\r
35         expect(fmt1, "1.234E3", n);\r
36         expect(fmt1, "1.234E+3", n); // Either format should parse "E+3"\r
37         expect(fmt2, "1.234E+3", n);\r
38     }\r
39 \r
40     /**\r
41      * Test the functioning of the secondary grouping value.\r
42      */\r
43     public void TestSecondaryGrouping() {\r
44         DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
45         DecimalFormat f = new DecimalFormat("#,##,###", US);\r
46         expect(f, new Long(123456789), "12,34,56,789");\r
47         expectPat(f, "#,##,###");\r
48         f.applyPattern("#,###");\r
49         f.setSecondaryGroupingSize(4);\r
50         expect(f, new Long(123456789), "12,3456,789");\r
51         expectPat(f, "#,####,###");\r
52 \r
53         // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit,\r
54         // but on IBM JDK 1.2-1.3, the locale uses U+0966.\r
55         f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN"));\r
56         String str = transmute("1,87,65,43,210",\r
57                                f.getDecimalFormatSymbols().getZeroDigit());\r
58         expect(f, new Long(1876543210), str);\r
59     }\r
60 \r
61     private void expectPad(DecimalFormat fmt, String pat, int pos) {\r
62         expectPad(fmt, pat, pos, 0, (char)0);\r
63     }\r
64 \r
65     private void expectPad(DecimalFormat fmt, String pat,\r
66                            int pos, int width, char pad) {\r
67         int apos = 0, awidth = 0;\r
68         char apad = 0;\r
69         try {\r
70             fmt.applyPattern(pat);\r
71             apos = fmt.getPadPosition();\r
72             awidth = fmt.getFormatWidth();\r
73             apad = fmt.getPadCharacter();\r
74         } catch (IllegalArgumentException e) {\r
75             apos = -1;\r
76             awidth = width;\r
77             apad = pad;\r
78         }\r
79         if (apos == pos && awidth == width && apad == pad) {\r
80             logln("Ok   \"" + pat + "\" pos=" + apos +\r
81                   ((pos == -1) ? "" : " width=" + awidth + " pad=" + apad));\r
82         } else {\r
83             logln("FAIL \"" + pat + "\" pos=" + apos +\r
84                   " width=" + awidth + " pad=" + apad +\r
85                   ", expected " + pos + " " + width + " " + pad);\r
86         }\r
87     }\r
88 \r
89     /**\r
90      */\r
91     public void TestPatterns() {\r
92         DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
93         DecimalFormat fmt = new DecimalFormat("#", US);\r
94 \r
95         expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^');\r
96         expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^');\r
97         expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^');\r
98         expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^');\r
99         expectPad(fmt, "$*^$#", ILLEGAL);\r
100         expectPad(fmt, "#$*^$", ILLEGAL);\r
101         expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX,\r
102                   12, 'x');\r
103         expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX,\r
104                   3, 'x');\r
105         expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX,\r
106                   10, 'a');\r
107 \r
108         fmt.applyPattern("AA#,##0.00ZZ");\r
109         fmt.setPadCharacter('^');\r
110 \r
111         fmt.setFormatWidth(10);\r
112 \r
113         fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX);\r
114         expectPat(fmt, "*^AA#,##0.00ZZ");\r
115 \r
116         fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX);\r
117         expectPat(fmt, "AA#,##0.00*^ZZ");\r
118 \r
119         fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX);\r
120         expectPat(fmt, "AA#,##0.00ZZ*^");\r
121 \r
122         //            12  3456789012\r
123         String exp = "AA*^#,##0.00ZZ";\r
124         fmt.setFormatWidth(12);\r
125         fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX);\r
126         expectPat(fmt, exp);\r
127 \r
128         fmt.setFormatWidth(13);\r
129         //              12  34567890123\r
130         expectPat(fmt, "AA*^##,##0.00ZZ");\r
131 \r
132         fmt.setFormatWidth(14);\r
133         //              12  345678901234\r
134         expectPat(fmt, "AA*^###,##0.00ZZ");\r
135 \r
136         fmt.setFormatWidth(15);\r
137         //              12  3456789012345\r
138         expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case\r
139 \r
140         fmt.setFormatWidth(16);\r
141         //              12  34567890123456\r
142         expectPat(fmt, "AA*^#,###,##0.00ZZ");\r
143     }\r
144 \r
145     private void expectPat(DecimalFormat fmt, String exp) {\r
146         String pat = fmt.toPattern();\r
147         if (pat.equals(exp)) {\r
148             logln("Ok   \"" + pat + '"');\r
149         } else {\r
150             errln("FAIL \"" + pat + "\", expected \"" + exp + '"');\r
151         }\r
152     }\r
153 \r
154     /**\r
155      * Test the handling of the AlphaWorks BigDecimal\r
156      */\r
157     public void TestAlphaBigDecimal() {\r
158         DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
159         /*For ICU compatibility [Richard/GCL]*/\r
160         expect(NumberFormat.getScientificInstance(Locale.US),\r
161                new Number[] { new com.ibm.icu.math.BigDecimal("12345.678901"),\r
162                            },\r
163                "1.2345678901E4");\r
164         expect(new DecimalFormat("##0.####E0", US),\r
165                new Number[] { new com.ibm.icu.math.BigDecimal("12345.4999"),\r
166                               new com.ibm.icu.math.BigDecimal("12344.5001"),\r
167                             },\r
168                "12.345E3");\r
169         expect(new DecimalFormat("##0.####E0", US),\r
170                new Number[] { new com.ibm.icu.math.BigDecimal("12345.5000"),\r
171                               new com.ibm.icu.math.BigDecimal("12346.5000"),\r
172                             },\r
173                "12.346E3");\r
174     }\r
175 \r
176     /**\r
177      */\r
178     public void TestScientific() {\r
179         DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
180         /*For ICU compatibility [Richard/GCL]*/\r
181         expect(NumberFormat.getScientificInstance(Locale.US),\r
182                new Number[] { new Double(12345.678901),\r
183                               new java.math.BigDecimal("12345.678901"),\r
184                             },\r
185                "1.2345678901E4");\r
186         expect(new DecimalFormat("##0.###E0", US),\r
187                new Double(12345),\r
188                "12.34E3");\r
189         expect(new DecimalFormat("##0.###E0", US),\r
190                new Double(12345.00001),\r
191                "12.35E3");\r
192         expect(new DecimalFormat("##0.####E0", US),\r
193                new Number[] { new Integer(12345),\r
194                               new Long(12345),\r
195                               new java.math.BigDecimal("12345.4999"),\r
196                               new java.math.BigDecimal("12344.5001"),\r
197                             },\r
198                "12.345E3");\r
199         expect(new DecimalFormat("##0.####E0", US),\r
200                new Number[] { new java.math.BigDecimal("12345.5000"),\r
201                               new java.math.BigDecimal("12346.5000"),\r
202                             },\r
203                "12.346E3");\r
204         /*For ICU compatibility [Richard/GCL]*/\r
205         expect(NumberFormat.getScientificInstance(Locale.FRANCE),\r
206                new Double(12345.678901),\r
207                "1,2345678901E4");\r
208         expect(new DecimalFormat("##0.####E0", US),\r
209                new Double(789.12345e-9),\r
210                "789.12E-9");\r
211         expect(new DecimalFormat("##0.####E0", US),\r
212                new Double(780.e-9),\r
213                "780E-9");\r
214         expect(new DecimalFormat(".###E0", US),\r
215                new Double(45678),\r
216                ".457E5");\r
217         expect(new DecimalFormat(".###E0", US),\r
218                new Long(0),\r
219                ".0E0");\r
220         expect(new DecimalFormat[] { new DecimalFormat("#E0", US),\r
221                                      new DecimalFormat("##E0", US),\r
222                                      new DecimalFormat("####E0", US),\r
223                                      new DecimalFormat("0E0", US),    \r
224                                      new DecimalFormat("00E0", US),   \r
225                                      new DecimalFormat("000E0", US), \r
226                                    },\r
227                new Long(45678000),\r
228                new String[] { "4.5678E7",\r
229                               "45.678E6",\r
230                               "4567.8E4",\r
231                               "5E7",\r
232                               "46E6",  \r
233                               "457E5",\r
234                             }\r
235                );\r
236         expect(new DecimalFormat("###E0", US),\r
237                new Object[] { new Double(0.0000123), "12.3E-6",\r
238                               new Double(0.000123), "123E-6",\r
239                               new java.math.BigDecimal("0.00123"), "1.23E-3", // Cafe VM messes up Double(0.00123)\r
240                               new Double(0.0123), "12.3E-3",\r
241                               new Double(0.123), "123E-3",\r
242                               new Double(1.23), "1.23E0",\r
243                               new Double(12.3), "12.3E0",\r
244                               new Double(123), "123E0",\r
245                               new Double(1230), "1.23E3",\r
246                              });\r
247         expect(new DecimalFormat("0.#E+00", US),\r
248                new Object[] { new Double(0.00012), "1.2E-04",\r
249                               new Long(12000),     "1.2E+04",\r
250                              });\r
251     }\r
252 \r
253     /**\r
254      */\r
255     public void TestPad() {\r
256         DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US);\r
257         expect(new DecimalFormat("*^##.##", US),\r
258                new Object[] { new Long(0),      "^^^^0",\r
259                               new Double(-1.3), "^-1.3",\r
260                             }\r
261                );\r
262         expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US),\r
263                new Object[] { new Long(0),       "0.0E0______ g-m/s^2",\r
264                               new Double(1.0/3), "333.333E-3_ g-m/s^2",\r
265                             }\r
266                );\r
267         expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US),\r
268                new Object[] { new Long(0),       "0.0______ g-m/s^2",\r
269                               new Double(1.0/3), "0.33333__ g-m/s^2",\r
270                             }\r
271                );\r
272         expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US),\r
273                new Object[] {\r
274                    new Long(-100),        "xxxxxxxx(100.00)",\r
275                    new Long(-1000),       "xxxxxx(1,000.00)",\r
276                    new Long(-1000000),    "xx(1,000,000.00)",\r
277                    new Long(-1000000000), "(1,000,000,000.00)",\r
278                });\r
279     }\r
280     \r
281     private void expect(NumberFormat fmt, Object[] data) {\r
282         for (int i=0; i<data.length; i+=2) {\r
283             expect(fmt, (Number) data[i], (String) data[i+1]);\r
284         }\r
285     }\r
286     \r
287     private void expect(Object fmto, Object numo, Object expo) {\r
288         NumberFormat fmt = null, fmts[] = null;\r
289         Number num = null, nums[] = null;\r
290         String exp = null, exps[] = null;\r
291         if (fmto instanceof NumberFormat[]) {\r
292             fmts = (NumberFormat[]) fmto;\r
293         } else {\r
294             fmt = (NumberFormat) fmto;\r
295         }\r
296         if (numo instanceof Number[]) {\r
297             nums = (Number[]) numo;\r
298         } else {\r
299             num = (Number) numo;\r
300         }\r
301         if (expo instanceof String[]) {\r
302             exps = (String[]) expo;\r
303         } else {\r
304             exp = (String) expo;\r
305         }\r
306         int n = 1;\r
307         if (fmts != null) {\r
308             n = Math.max(n, fmts.length);\r
309         }\r
310         if (nums != null) {\r
311             n = Math.max(n, nums.length);\r
312         }\r
313         if (exps != null) {\r
314             n = Math.max(n, exps.length);\r
315         }\r
316         for (int i=0; i<n; ++i) {\r
317             expect(fmts == null ? fmt : fmts[i],\r
318                    nums == null ? num : nums[i],\r
319                    exps == null ? exp : exps[i]);\r
320         }\r
321     }\r
322 \r
323     private static String showNumber(Number n) {\r
324         String cls = n.getClass().getName();\r
325         if (!(n instanceof com.ibm.icu.math.BigDecimal\r
326               || n instanceof java.math.BigDecimal)) {\r
327             int i = cls.lastIndexOf('.');\r
328             cls = cls.substring(i+1);\r
329         }\r
330         return n.toString() + " (" + cls + ')';\r
331     }\r
332 \r
333     private void expect(NumberFormat fmt, Number n, String exp) {\r
334         String saw = fmt.format(n);\r
335         String pat = ((DecimalFormat) fmt).toPattern();\r
336         if (saw.equals(exp)) {\r
337             logln("Ok   " + showNumber(n) + " x " +\r
338                   pat + " = " +\r
339                   Utility.escape(saw));\r
340         } else {\r
341             errln("FAIL " + showNumber(n) + " x " +\r
342                   pat + " = \"" +\r
343                   Utility.escape(saw) + ", expected " + Utility.escape(exp));\r
344         }\r
345     }\r
346 \r
347     private void expect(NumberFormat fmt, String str, Number exp) {\r
348         Number saw = null;\r
349         try {\r
350             saw = fmt.parse(str);\r
351         } catch (ParseException e) {\r
352             saw = null;\r
353         }\r
354         String pat = ((DecimalFormat) fmt).toPattern();\r
355         if (saw.equals(exp)) {\r
356             logln("Ok   \"" + str + "\" x " +\r
357                   pat + " = " +\r
358                   showNumber(saw));\r
359         } else {\r
360             errln("FAIL \"" + str + "\" x " +\r
361                   pat + " = " +\r
362                   showNumber(saw) + ", expected " + showNumber(exp));\r
363         }\r
364     }\r
365 \r
366     /**\r
367      * Given a string composed of [0-9] and other chars, convert the\r
368      * [0-9] chars to be offsets 0..9 from 'zero'.\r
369      */\r
370     private static String transmute(String str, char zero) {\r
371         StringBuffer buf = new StringBuffer();\r
372         for (int i=0; i<str.length(); ++i) {\r
373             char c = str.charAt(i);\r
374             if (c >= '0' && c <= '9') {\r
375                 c = (char) (c - '0' + zero);\r
376             }\r
377             buf.append(c);\r
378         }\r
379         return buf.toString();\r
380     }\r
381 \r
382     public void Test4161100() {\r
383         NumberFormat f = NumberFormat.getInstance();\r
384         f.setMinimumFractionDigits(1);\r
385         f.setMaximumFractionDigits(1);\r
386         double a = -0.09;\r
387         String s = f.format(a);\r
388         logln(a + " x " +\r
389               ((DecimalFormat) f).toPattern() + " = " +\r
390               s);\r
391         if (!s.equals("-0.1")) {\r
392             errln("FAIL");\r
393         }\r
394     }        \r
395 \r
396     public void TestBigDecimalJ28() {\r
397         String[] DATA = {\r
398             "1", "1E0",\r
399             "-1", "-1E0",\r
400             "0", "0E0",\r
401             "12e34", "1.2E35",\r
402             "-12.3e-45", "-1.23E-44",\r
403             "0.73e-7", "7.3E-8",\r
404         };\r
405         NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US);\r
406         logln("Pattern: " + ((DecimalFormat)fmt).toPattern());\r
407         for (int i=0; i<DATA.length; i+=2) {\r
408             String input = DATA[i];\r
409             String exp = DATA[i+1];\r
410             com.ibm.icu.math.BigDecimal bd = new com.ibm.icu.math.BigDecimal(input);\r
411             String output = fmt.format(bd);\r
412             if (output.equals(exp)) {\r
413                 logln("input=" + input + " num=" + bd + " output=" + output);\r
414             } else {\r
415                 errln("FAIL: input=" + input + " num=" + bd + " output=" + output +\r
416                       " expected=" + exp);\r
417             }\r
418         }\r
419     }\r
420 //#if defined(FOUNDATION10)\r
421 //#else\r
422     public void TestBigDecimalRounding() {\r
423         // jb 3657\r
424         java.text.DecimalFormat jdkFormat=new java.text.DecimalFormat("###,###,###,##0");\r
425         com.ibm.icu.text.DecimalFormat icuFormat=new com.ibm.icu.text.DecimalFormat("###,###,###,##0");\r
426         String[] values = {\r
427             "-1.74", "-1.24", "-0.74", "-0.24", "0.24", "0.74", "1.24", "1.74"\r
428         };\r
429         for (int i = 0; i < values.length; ++i) {\r
430             String val = values[i];\r
431             java.math.BigDecimal bd = new java.math.BigDecimal(val);\r
432             String jdk = jdkFormat.format(bd);\r
433             String icu = icuFormat.format(bd);\r
434             logln("Format of BigDecimal " + val + " by JDK is " + jdk);\r
435             logln("Format of BigDecimal " + val + " by ICU is " + icu);\r
436             if (!jdk.equals(icu)) {\r
437                 errln("BigDecimal jdk: " + jdk + " != icu: " + icu);\r
438             }\r
439 \r
440             double d = bd.doubleValue();\r
441             jdk = jdkFormat.format(d);\r
442             icu = icuFormat.format(d);\r
443             logln("Format of double " + val + " by JDK is " + jdk);\r
444             logln("Format of double " + val + " by ICU is " + icu);\r
445             if (!jdk.equals(icu)) {\r
446                 errln("double jdk: " + jdk + " != icu: " + icu);\r
447             }\r
448         }\r
449     }\r
450 //#endif\r
451 }\r