]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/format/IntlTestDecimalFormatAPI.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / format / IntlTestDecimalFormatAPI.java
1 //##header\r
2 /*****************************************************************************************\r
3  *\r
4  *   Copyright (C) 1996-2009, International Business Machines\r
5  *   Corporation and others.  All Rights Reserved.\r
6  **/\r
7 \r
8 /** \r
9  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI\r
10  * Source File: java/text/format/IntlTestDecimalFormatAPI.java\r
11  **/\r
12  \r
13 /*\r
14     @test 1.4 98/03/06\r
15     @summary test International Decimal Format API\r
16 */\r
17 \r
18 package com.ibm.icu.dev.test.format;\r
19 \r
20 import com.ibm.icu.text.*;\r
21 import com.ibm.icu.math.BigDecimal;\r
22 import com.ibm.icu.math.MathContext;\r
23 import java.util.Locale;\r
24 import java.text.ParsePosition;\r
25 import java.text.Format;\r
26 import java.text.FieldPosition;\r
27 import java.text.ParseException;\r
28 import com.ibm.icu.text.DecimalFormat;\r
29 \r
30 public class IntlTestDecimalFormatAPI extends com.ibm.icu.dev.test.TestFmwk\r
31 {\r
32     public static void main(String[] args)  throws Exception {\r
33         new IntlTestDecimalFormatAPI().run(args);\r
34     }\r
35     \r
36     /**\r
37      * Problem 1: simply running \r
38      * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work \r
39      * as decF4.setRoundingIncrement(.0001) must also be run.\r
40      * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected. \r
41      * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in \r
42      * order for this to work as expected.\r
43      * Problem 3: There seems to be no way to set half up to be the default \r
44      * rounding mode.\r
45      * We solved the problem with the code at the bottom of this page however \r
46      * this is not quite general purpose enough to include in icu4j. A static\r
47      * setDefaultRoundingMode function would solve the problem nicely. Also \r
48      * decimal places past 20 are not handled properly. A small ammount of work \r
49      * would make bring this up to snuff.\r
50      */\r
51     public void testJB1871()\r
52     {\r
53         // problem 2\r
54         double number = 8.88885;\r
55         String expected = "8.8889";\r
56         \r
57         String pat = ",##0.0000";\r
58         DecimalFormat dec = new DecimalFormat(pat);\r
59         dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);\r
60         double roundinginc = 0.0001;\r
61         dec.setRoundingIncrement(roundinginc);\r
62         String str = dec.format(number);\r
63         if (!str.equals(expected)) {\r
64             errln("Fail: " + number + " x \"" + pat + "\" = \"" +\r
65                   str + "\", expected \"" + expected + "\"");\r
66         }   \r
67 \r
68         pat = ",##0.0001";\r
69         dec = new DecimalFormat(pat);\r
70         dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);\r
71         str = dec.format(number);\r
72         if (!str.equals(expected)) {\r
73             errln("Fail: " + number + " x \"" + pat + "\" = \"" +\r
74                   str + "\", expected \"" + expected + "\"");\r
75         }  \r
76         \r
77         // testing 20 decimal places\r
78         pat = ",##0.00000000000000000001";\r
79         dec = new DecimalFormat(pat);\r
80         BigDecimal bignumber = new BigDecimal("8.888888888888888888885");\r
81         expected = "8.88888888888888888889";\r
82         \r
83         dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);\r
84         str = dec.format(bignumber); \r
85         if (!str.equals(expected)) {\r
86             errln("Fail: " + bignumber + " x \"" + pat + "\" = \"" +\r
87                   str + "\", expected \"" + expected + "\"");\r
88         }   \r
89         \r
90     }\r
91 \r
92     /** \r
93      * This test checks various generic API methods in DecimalFormat to achieve \r
94      * 100% API coverage.\r
95      */\r
96     public void TestAPI()\r
97     {\r
98         logln("DecimalFormat API test---"); logln("");\r
99         Locale.setDefault(Locale.ENGLISH);\r
100 \r
101         // ======= Test constructors\r
102 \r
103         logln("Testing DecimalFormat constructors");\r
104 \r
105         DecimalFormat def = new DecimalFormat();\r
106 \r
107         final String pattern = new String("#,##0.# FF");\r
108         DecimalFormat pat = null;\r
109         try {\r
110             pat = new DecimalFormat(pattern);\r
111         }\r
112         catch (IllegalArgumentException e) {\r
113             errln("ERROR: Could not create DecimalFormat (pattern)");\r
114         }\r
115 \r
116         DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);\r
117 \r
118         DecimalFormat cust1 = new DecimalFormat(pattern, symbols);\r
119 \r
120         // ======= Test clone(), assignment, and equality\r
121 \r
122         logln("Testing clone() and equality operators");\r
123 \r
124         Format clone = (Format) def.clone();\r
125         if( ! def.equals(clone)) {\r
126             errln("ERROR: Clone() failed");\r
127         }\r
128 \r
129         // ======= Test various format() methods\r
130 \r
131         logln("Testing various format() methods");\r
132 \r
133 //        final double d = -10456.0037; // this appears as -10456.003700000001 on NT\r
134 //        final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT\r
135         final double d = -10456.00370000000000; // this works!\r
136         final long l = 100000000;\r
137         logln("" + d + " is the double value");\r
138 \r
139         StringBuffer res1 = new StringBuffer();\r
140         StringBuffer res2 = new StringBuffer();\r
141         StringBuffer res3 = new StringBuffer();\r
142         StringBuffer res4 = new StringBuffer();\r
143         FieldPosition pos1 = new FieldPosition(0);\r
144         FieldPosition pos2 = new FieldPosition(0);\r
145         FieldPosition pos3 = new FieldPosition(0);\r
146         FieldPosition pos4 = new FieldPosition(0);\r
147 \r
148         res1 = def.format(d, res1, pos1);\r
149         logln("" + d + " formatted to " + res1);\r
150 \r
151         res2 = pat.format(l, res2, pos2);\r
152         logln("" + l + " formatted to " + res2);\r
153 \r
154         res3 = cust1.format(d, res3, pos3);\r
155         logln("" + d + " formatted to " + res3);\r
156 \r
157         res4 = cust1.format(l, res4, pos4);\r
158         logln("" + l + " formatted to " + res4);\r
159 \r
160         // ======= Test parse()\r
161 \r
162         logln("Testing parse()");\r
163 \r
164         String text = new String("-10,456.0037");\r
165         ParsePosition pos = new ParsePosition(0);\r
166         String patt = new String("#,##0.#");\r
167         pat.applyPattern(patt);\r
168         double d2 = pat.parse(text, pos).doubleValue();\r
169         if(d2 != d) {\r
170             errln("ERROR: Roundtrip failed (via parse(" + d2 + " != " + d + ")) for " + text);\r
171         }\r
172         logln(text + " parsed into " + (long) d2);\r
173 \r
174         // ======= Test getters and setters\r
175 \r
176         logln("Testing getters and setters");\r
177 \r
178         final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();\r
179         def.setDecimalFormatSymbols(syms);\r
180         if( ! pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {\r
181             errln("ERROR: set DecimalFormatSymbols() failed");\r
182         }\r
183 \r
184         String posPrefix;\r
185         pat.setPositivePrefix("+");\r
186         posPrefix = pat.getPositivePrefix();\r
187         logln("Positive prefix (should be +): " + posPrefix);\r
188         if(posPrefix != "+") {\r
189             errln("ERROR: setPositivePrefix() failed");\r
190         }\r
191 \r
192         String negPrefix;\r
193         pat.setNegativePrefix("-");\r
194         negPrefix = pat.getNegativePrefix();\r
195         logln("Negative prefix (should be -): " + negPrefix);\r
196         if(negPrefix != "-") {\r
197             errln("ERROR: setNegativePrefix() failed");\r
198         }\r
199 \r
200         String posSuffix;\r
201         pat.setPositiveSuffix("_");\r
202         posSuffix = pat.getPositiveSuffix();\r
203         logln("Positive suffix (should be _): " + posSuffix);\r
204         if(posSuffix != "_") {\r
205             errln("ERROR: setPositiveSuffix() failed");\r
206         }\r
207 \r
208         String negSuffix;\r
209         pat.setNegativeSuffix("~");\r
210         negSuffix = pat.getNegativeSuffix();\r
211         logln("Negative suffix (should be ~): " + negSuffix);\r
212         if(negSuffix != "~") {\r
213             errln("ERROR: setNegativeSuffix() failed");\r
214         }\r
215 \r
216         long multiplier = 0;\r
217         pat.setMultiplier(8);\r
218         multiplier = pat.getMultiplier();\r
219         logln("Multiplier (should be 8): " + multiplier);\r
220         if(multiplier != 8) {\r
221             errln("ERROR: setMultiplier() failed");\r
222         }\r
223 \r
224         int groupingSize = 0;\r
225         pat.setGroupingSize(2);\r
226         groupingSize = pat.getGroupingSize();\r
227         logln("Grouping size (should be 2): " + (long) groupingSize);\r
228         if(groupingSize != 2) {\r
229             errln("ERROR: setGroupingSize() failed");\r
230         }\r
231 \r
232         pat.setDecimalSeparatorAlwaysShown(true);\r
233         boolean tf = pat.isDecimalSeparatorAlwaysShown();\r
234         logln("DecimalSeparatorIsAlwaysShown (should be true) is " +  (tf ? "true" : "false"));\r
235         if(tf != true) {\r
236             errln("ERROR: setDecimalSeparatorAlwaysShown() failed");\r
237         }\r
238 \r
239         String funkyPat;\r
240         funkyPat = pat.toPattern();\r
241         logln("Pattern is " + funkyPat);\r
242 \r
243         String locPat;\r
244         locPat = pat.toLocalizedPattern();\r
245         logln("Localized pattern is " + locPat);\r
246 \r
247         // ======= Test applyPattern()\r
248 \r
249         logln("Testing applyPattern()");\r
250 \r
251         String p1 = new String("#,##0.0#;(#,##0.0#)");\r
252         logln("Applying pattern " + p1);\r
253         pat.applyPattern(p1);\r
254         String s2;\r
255         s2 = pat.toPattern();\r
256         logln("Extracted pattern is " + s2);\r
257         if( ! s2.equals(p1) ) {\r
258             errln("ERROR: toPattern() result did not match pattern applied");\r
259         }\r
260 \r
261         String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");\r
262         logln("Applying pattern " + p2);\r
263         pat.applyLocalizedPattern(p2);\r
264         String s3;\r
265         s3 = pat.toLocalizedPattern();\r
266         logln("Extracted pattern is " + s3);\r
267         if( ! s3.equals(p2) ) {\r
268             errln("ERROR: toLocalizedPattern() result did not match pattern applied");\r
269         }\r
270     }\r
271 \r
272 //#if defined(FOUNDATION10) || defined(J2SE13)\r
273 //#else\r
274     public void testJB6134()\r
275     {\r
276         DecimalFormat decfmt = new DecimalFormat();\r
277         StringBuffer buf = new StringBuffer();\r
278 \r
279         FieldPosition fposByInt = new FieldPosition(NumberFormat.INTEGER_FIELD);\r
280         decfmt.format(123, buf, fposByInt);\r
281 \r
282         buf.setLength(0);\r
283         FieldPosition fposByField = new FieldPosition(NumberFormat.Field.INTEGER);\r
284         decfmt.format(123, buf, fposByField);\r
285 \r
286         if (fposByInt.getEndIndex() != fposByField.getEndIndex())\r
287         {\r
288             errln("ERROR: End index for integer field - fposByInt:" + fposByInt.getEndIndex() +\r
289                 " / fposByField: " + fposByField.getEndIndex());\r
290         }\r
291     }\r
292 //#endif\r
293 \r
294     public void testJB4971()\r
295     {\r
296         DecimalFormat decfmt = new DecimalFormat();\r
297         MathContext resultICU;\r
298 \r
299         MathContext comp1 = new MathContext(0, MathContext.PLAIN);\r
300         resultICU = decfmt.getMathContextICU();\r
301         if ((comp1.getDigits() != resultICU.getDigits()) ||\r
302             (comp1.getForm() != resultICU.getForm()) ||\r
303             (comp1.getLostDigits() != resultICU.getLostDigits()) ||\r
304             (comp1.getRoundingMode() != resultICU.getRoundingMode()))\r
305         {\r
306             errln("ERROR: Math context 1 not equal - result: " + resultICU.toString() +\r
307                 " / expected: " + comp1.toString());\r
308         }\r
309 \r
310         MathContext comp2 = new MathContext(5, MathContext.ENGINEERING);\r
311         decfmt.setMathContextICU(comp2);\r
312         resultICU = decfmt.getMathContextICU();\r
313         if ((comp2.getDigits() != resultICU.getDigits()) ||\r
314             (comp2.getForm() != resultICU.getForm()) ||\r
315             (comp2.getLostDigits() != resultICU.getLostDigits()) ||\r
316             (comp2.getRoundingMode() != resultICU.getRoundingMode()))\r
317         {\r
318             errln("ERROR: Math context 2 not equal - result: " + resultICU.toString() +\r
319                 " / expected: " + comp2.toString());\r
320         }\r
321 \r
322 //#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)\r
323 //#else\r
324 \r
325         java.math.MathContext result;\r
326 \r
327         java.math.MathContext comp3 = new java.math.MathContext(3, java.math.RoundingMode.DOWN);\r
328         decfmt.setMathContext(comp3);\r
329         result = decfmt.getMathContext();\r
330         if ((comp3.getPrecision() != result.getPrecision()) ||\r
331             (comp3.getRoundingMode() != result.getRoundingMode()))\r
332         {\r
333             errln("ERROR: Math context 3 not equal - result: " + result.toString() +\r
334                 " / expected: " + comp3.toString());\r
335         }\r
336 \r
337 //#endif\r
338 \r
339     }\r
340 \r
341     public void testJB6354()\r
342     {\r
343         DecimalFormat pat = new DecimalFormat("#,##0.00");\r
344 \r
345         // get default rounding increment\r
346 //#if defined(FOUNDATION10)\r
347 //##        com.ibm.icu.math.BigDecimal r1 = pat.getRoundingIncrement();\r
348 //#else\r
349         java.math.BigDecimal r1 = pat.getRoundingIncrement();\r
350 //#endif\r
351 \r
352         // set rounding mode with zero increment.  Rounding \r
353         // increment should be set by this operation\r
354         pat.setRoundingMode(BigDecimal.ROUND_UP);\r
355 //#if defined(FOUNDATION10)\r
356 //##        com.ibm.icu.math.BigDecimal r2 = pat.getRoundingIncrement();\r
357 //#else\r
358         java.math.BigDecimal r2 = pat.getRoundingIncrement();\r
359 //#endif\r
360 \r
361         // check for different values\r
362         if ((r1 != null) && (r2 != null))\r
363         {\r
364             if (r1.compareTo(r2) == 0)\r
365             {\r
366                 errln("ERROR: Rounding increment did not change");\r
367             }\r
368         }\r
369     }\r
370     \r
371     public void testJB6648()\r
372     {\r
373         DecimalFormat df = new DecimalFormat();\r
374         df.setParseStrict(true);\r
375         \r
376         String numstr = new String();\r
377         \r
378         String[] patterns = {\r
379             "0",\r
380             "00",\r
381             "000",\r
382             "0,000",\r
383             "0.0",\r
384             "#000.0"          \r
385         };\r
386         \r
387         for(int i=0; i < patterns.length; i++) {\r
388             df.applyPattern(patterns[i]);\r
389             numstr = df.format(5);        \r
390             try {\r
391                 Number n = df.parse(numstr);\r
392                 logln("INFO: Parsed " + numstr + " -> " + n);\r
393             } catch (ParseException pe) {\r
394                 errln("ERROR: Failed round trip with strict parsing.");\r
395             }           \r
396         }\r
397         \r
398         df.applyPattern(patterns[1]);\r
399         numstr = "005";        \r
400         try {\r
401             Number n = df.parse(numstr);\r
402             errln("ERROR: Expected round trip failure not encountered: numstr -> " + n);\r
403         } catch (ParseException pe) {\r
404             logln("INFO: Expected ParseExpection for " + numstr + " with strick parse enabled");\r
405         }  \r
406         \r
407     }\r
408 }\r