]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/format/IntlTestNumberFormatAPI.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / core / src / com / ibm / icu / dev / test / format / IntlTestNumberFormatAPI.java
1 /*****************************************************************************************
2  *
3  *   Copyright (C) 1996-2009, International Business Machines
4  *   Corporation and others.  All Rights Reserved.
5  **/
6 /** 
7  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestNumberFormatAPI
8  * Source File: java/text/format/IntlTestNumberFormatAPI.java
9  **/
10
11 /*
12     @test 1.4 98/03/06
13     @summary test International Number Format API
14 */
15
16 package com.ibm.icu.dev.test.format;
17
18 import java.math.BigInteger;
19 import java.text.FieldPosition;
20 import java.text.ParseException;
21 import java.text.ParsePosition;
22 import java.util.Locale;
23
24 import com.ibm.icu.text.NumberFormat;
25 import com.ibm.icu.util.ULocale;
26
27 public class IntlTestNumberFormatAPI extends com.ibm.icu.dev.test.TestFmwk
28 {
29     public static void main(String[] args) throws Exception {
30         new IntlTestNumberFormatAPI().run(args);
31     }
32
33     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
34     public void TestAPI()
35     {
36         logln("NumberFormat API test---"); logln("");
37         Locale.setDefault(Locale.ENGLISH);
38
39         // ======= Test constructors
40
41         logln("Testing NumberFormat constructors");
42
43         NumberFormat def = NumberFormat.getInstance();
44
45         NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH);
46
47         NumberFormat cur = NumberFormat.getCurrencyInstance();
48
49         NumberFormat cur_fr = NumberFormat.getCurrencyInstance(Locale.FRENCH);
50
51         NumberFormat per = NumberFormat.getPercentInstance();
52
53         NumberFormat per_fr = NumberFormat.getPercentInstance(Locale.FRENCH);
54         
55         NumberFormat integer = NumberFormat.getIntegerInstance();
56         
57         NumberFormat int_fr = NumberFormat.getIntegerInstance(Locale.FRENCH);
58         
59         //Fix "The variable is never used" compilation warnings
60         logln("Currency : " + cur.format(1234.5));
61         logln("Percent : " + per.format(1234.5));
62         logln("Integer : " + integer.format(1234.5));
63         logln("Int_fr : " + int_fr.format(1234.5));
64         
65         // ======= Test equality
66
67         logln("Testing equality operator");
68
69         if( per_fr.equals(cur_fr) ) {
70             errln("ERROR: == failed");
71         }
72
73         // ======= Test various format() methods
74
75         logln("Testing various format() methods");
76
77 //        final double d = -10456.0037; // this appears as -10456.003700000001 on NT
78 //        final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
79         final double d = -10456.00370000000000; // this works!
80         final long l = 100000000;
81
82         String res1 = new String();
83         String res2 = new String();
84         StringBuffer res3 = new StringBuffer();
85         StringBuffer res4 = new StringBuffer();
86         StringBuffer res5 = new StringBuffer();
87         StringBuffer res6 = new StringBuffer();
88         FieldPosition pos1 = new FieldPosition(0);
89         FieldPosition pos2 = new FieldPosition(0);
90         FieldPosition pos3 = new FieldPosition(0);
91         FieldPosition pos4 = new FieldPosition(0);
92
93         res1 = cur_fr.format(d);
94         logln( "" + d + " formatted to " + res1);
95
96         res2 = cur_fr.format(l);
97         logln("" + l + " formatted to " + res2);
98
99         res3 = cur_fr.format(d, res3, pos1);
100         logln( "" + d + " formatted to " + res3);
101
102         res4 = cur_fr.format(l, res4, pos2);
103         logln("" + l + " formatted to " + res4);
104
105         res5 = cur_fr.format(d, res5, pos3);
106         logln("" + d + " formatted to " + res5);
107
108         res6 = cur_fr.format(l, res6, pos4);
109         logln("" + l + " formatted to " + res6);
110
111
112         // ======= Test parse()
113
114         logln("Testing parse()");
115
116 //        String text = new String("-10,456.0037");
117         String text = new String("-10456,0037");
118         ParsePosition pos = new ParsePosition(0);
119         ParsePosition pos01 = new ParsePosition(0);
120         double d1 = ((Number)fr.parseObject(text, pos)).doubleValue();
121         if(d1 != d) {
122             errln("ERROR: Roundtrip failed (via parse()) for " + text);
123         }
124         logln(text + " parsed into " + d1);
125
126         double d2 = fr.parse(text, pos01).doubleValue();
127         if(d2 != d) {
128             errln("ERROR: Roundtrip failed (via parse()) for " + text);
129         }
130         logln(text + " parsed into " + d2);
131
132         double d3 = 0;
133         try {
134             d3 = fr.parse(text).doubleValue();
135         }
136         catch (ParseException e) {
137             errln("ERROR: parse() failed");
138         }
139         if(d3 != d) {
140             errln("ERROR: Roundtrip failed (via parse()) for " + text);
141         }
142         logln(text + " parsed into " + d3);
143
144
145         // ======= Test getters and setters
146
147         logln("Testing getters and setters");
148
149         final Locale[] locales = NumberFormat.getAvailableLocales();
150         long count = locales.length;
151         logln("Got " + count + " locales" );
152         for(int i = 0; i < count; i++) {
153             String name;
154             name = locales[i].getDisplayName();
155             logln(name);
156         }
157
158         fr.setParseIntegerOnly( def.isParseIntegerOnly() );
159         if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) {
160                 errln("ERROR: setParseIntegerOnly() failed");
161         }
162
163         fr.setGroupingUsed( def.isGroupingUsed() );
164         if(fr.isGroupingUsed() != def.isGroupingUsed() ) {
165                 errln("ERROR: setGroupingUsed() failed");
166         }
167
168         fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() );
169         if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) {
170                 errln("ERROR: setMaximumIntegerDigits() failed");
171         }
172
173         fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() );
174         if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) {
175                 errln("ERROR: setMinimumIntegerDigits() failed");
176         }
177
178         fr.setMaximumFractionDigits( def.getMaximumFractionDigits() );
179         if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) {
180                 errln("ERROR: setMaximumFractionDigits() failed");
181         }
182
183         fr.setMinimumFractionDigits( def.getMinimumFractionDigits() );
184         if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) {
185                 errln("ERROR: setMinimumFractionDigits() failed");
186         }
187
188         // ======= Test getStaticClassID()
189
190 //        logln("Testing instanceof()");
191
192 //        try {
193 //            NumberFormat test = new DecimalFormat();
194
195 //            if (! (test instanceof DecimalFormat)) {
196 //                errln("ERROR: instanceof failed");
197 //            }
198 //        }
199 //        catch (Exception e) {
200 //            errln("ERROR: Couldn't create a DecimalFormat");
201 //        }
202     }
203     
204     // Jitterbug 4451, for coverage
205     public void TestCoverage(){
206         class StubNumberFormat extends NumberFormat{
207             /**
208              * For serialization
209              */
210             private static final long serialVersionUID = 3768385020503005993L;
211             public void run(){
212                 String p = NumberFormat.getPattern(ULocale.getDefault().toLocale(),0);
213                 if (!p.equals(NumberFormat.getPattern(ULocale.getDefault(),0))){
214                     errln("NumberFormat.getPattern(Locale, int) should delegate to (ULocale,)");
215                 }
216             }
217             public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
218             public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
219             public StringBuffer format(BigInteger number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
220             public StringBuffer format(java.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
221             public StringBuffer format(com.ibm.icu.math.BigDecimal number, StringBuffer toAppendTo, FieldPosition pos) {return null;}
222             public Number parse(String text, ParsePosition parsePosition) {return null;}
223         }
224         new StubNumberFormat().run();
225     }
226 }