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