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