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