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