• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package libcore.java.text;
19 
20 import java.math.BigDecimal;
21 import java.math.BigInteger;
22 import java.text.AttributedCharacterIterator;
23 import java.text.DecimalFormat;
24 import java.text.DecimalFormatSymbols;
25 import java.text.FieldPosition;
26 import java.text.NumberFormat;
27 import java.text.ParseException;
28 import java.text.ParsePosition;
29 import java.util.BitSet;
30 import java.util.Locale;
31 import junit.framework.TestCase;
32 import tests.support.Support_DecimalFormat;
33 
34 public class OldDecimalFormatTest extends TestCase {
35 
test_formatToCharacterIterator()36     public void test_formatToCharacterIterator() throws Exception {
37         AttributedCharacterIterator iterator;
38         int[] runStarts;
39         int[] runLimits;
40         String result;
41         char current;
42 
43         // For BigDecimal with multiplier test.
44         DecimalFormat df = new DecimalFormat();
45         df.setMultiplier(10);
46         iterator = df.formatToCharacterIterator(new BigDecimal("12345678901234567890"));
47         result = "123,456,789,012,345,678,900";
48         current = iterator.current();
49         for (int i = 0; i < result.length(); i++) {
50             assertEquals("wrong char @" + i, result.charAt(i), current);
51             current = iterator.next();
52         }
53 
54         // For BigDecimal with multiplier test.
55         df = new DecimalFormat();
56         df.setMultiplier(-1);
57         df.setMaximumFractionDigits(20);
58         iterator = df.formatToCharacterIterator(new BigDecimal("1.23456789012345678901"));
59         result = "-1.23456789012345678901";
60         current = iterator.current();
61         for (int i = 0; i < result.length(); i++) {
62             assertEquals("wrong char @" + i, result.charAt(i), current);
63             current = iterator.next();
64         }
65 
66         iterator = new DecimalFormat()
67                 .formatToCharacterIterator(new BigDecimal("1.23456789E1234"));
68         runStarts = new int[] {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14};
69         runLimits = new int[] {2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15};
70         result = "12,345,678,900,"; // 000,000,000,000....
71         current = iterator.current();
72         for (int i = 0; i < runStarts.length; i++) {
73             assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
74             assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
75             assertEquals("wrong char @" + i, result.charAt(i), current);
76             current = iterator.next();
77         }
78         assertEquals(0, iterator.getBeginIndex());
79         assertEquals(1646, iterator.getEndIndex());
80 
81         iterator = new DecimalFormat()
82                 .formatToCharacterIterator(new BigDecimal("1.23456789E301"));
83         runStarts = new int[] {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14};
84         runLimits = new int[] {2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15};
85         result = "12,345,678,900,"; // 000,000,000,000....
86         current = iterator.current();
87         for (int i = 0; i < runStarts.length; i++) {
88             assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
89             assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
90             assertEquals("wrong char @" + i, result.charAt(i), current);
91             current = iterator.next();
92         }
93         assertEquals(0, iterator.getBeginIndex());
94         assertEquals(402, iterator.getEndIndex());
95 
96         iterator = new DecimalFormat()
97                 .formatToCharacterIterator(new BigDecimal("1.2345678E4"));
98         runStarts = new int[] {0, 0, 2, 3, 3, 3, 6, 7, 7, 7};
99         runLimits = new int[] {2, 2, 3, 6, 6, 6, 7, 10, 10, 10};
100         result = "12,345.678";
101         current = iterator.current();
102         for (int i = 0; i < runStarts.length; i++) {
103             assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
104             assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
105             assertEquals("wrong char @" + i, result.charAt(i), current);
106             current = iterator.next();
107         }
108         assertEquals(0, iterator.getBeginIndex());
109         assertEquals(10, iterator.getEndIndex());
110 
111         iterator = new DecimalFormat()
112                 .formatToCharacterIterator(new BigInteger("123456789"));
113         runStarts = new int[] {0, 0, 0, 3, 4, 4, 4, 7, 8, 8, 8};
114         runLimits = new int[] {3, 3, 3, 4, 7, 7, 7, 8, 11, 11, 11};
115         result = "123,456,789";
116         current = iterator.current();
117         for (int i = 0; i < runStarts.length; i++) {
118             assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart());
119             assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit());
120             assertEquals("wrong char @" + i, result.charAt(i), current);
121             current = iterator.next();
122         }
123         assertEquals(0, iterator.getBeginIndex());
124         assertEquals(11, iterator.getEndIndex());
125     }
126 
127     /*
128      * Test the getter and setter of parseBigDecimal and parseIntegerOnly and
129      * test the default value of them.
130      */
test_isParseBigDecimalLjava_lang_Boolean_isParseIntegerOnlyLjava_lang_Boolean()131     public void test_isParseBigDecimalLjava_lang_Boolean_isParseIntegerOnlyLjava_lang_Boolean() {
132 
133         // parseBigDecimal default to false
134         DecimalFormat form = (DecimalFormat) DecimalFormat
135                 .getInstance(Locale.US);
136         assertFalse(form.isParseBigDecimal());
137         form.setParseBigDecimal(true);
138         assertTrue(form.isParseBigDecimal());
139 
140         try {
141             Number result = form.parse("123.123");
142             assertEquals(new BigDecimal("123.123"), result);
143         } catch (ParseException e) {
144             fail("ParseException was thrown.");
145         }
146 
147         form.setParseBigDecimal(false);
148         assertFalse(form.isParseBigDecimal());
149 
150         try {
151             Number result = form.parse("123.123");
152             assertFalse(result instanceof BigDecimal);
153         } catch (ParseException e) {
154             fail("ParseException was thrown.");
155         }
156 
157         // parseIntegerOnly default to false
158         assertFalse(form.isParseIntegerOnly());
159     }
160 
test_isParseIntegerOnly()161     public void test_isParseIntegerOnly() {
162 
163             DecimalFormat format = new DecimalFormat();
164             assertFalse("Default value of isParseIntegerOnly is true",
165                     format.isParseIntegerOnly());
166 
167             format.setParseIntegerOnly(true);
168             assertTrue(format.isParseIntegerOnly());
169             try {
170                 Number result = format.parse("123.123");
171                 assertEquals(new Long("123"), result);
172             } catch (ParseException e) {
173                 fail("ParseException was thrown.");
174             }
175 
176             format.setParseIntegerOnly(false);
177             assertFalse(format.isParseIntegerOnly());
178             try {
179                 Number result = format.parse("123.123");
180                 assertEquals(new Double("123.123"), result);
181             } catch (ParseException e) {
182                 fail("ParseException was thrown.");
183             }
184     }
185 
test_isGroupingUsed()186     public void test_isGroupingUsed() {
187         String [] patterns = {"####.##", "######.######", "000000.000000",
188                 "######.000000", "000000.######", " ###.###", "$#####.######",
189                 "$$####.######"};
190 
191         for(String pattern:patterns) {
192             DecimalFormat format = new DecimalFormat(pattern);
193             assertFalse(format.isGroupingUsed());
194         }
195 
196         DecimalFormat format = new DecimalFormat("###,####");
197         assertTrue(format.isGroupingUsed());
198     }
199 
200     // Test the type of the returned object
test_parseLjava_lang_String_Ljava_text_ParsePosition()201     public void test_parseLjava_lang_String_Ljava_text_ParsePosition() {
202         DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
203         form.setParseIntegerOnly(true);
204         form.setParseBigDecimal(true);
205 
206         final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408,"
207             + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077,"
208             + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653,"
209             + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152,"
210             + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806,"
211             + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476,"
212             + "354,361,838,599,762,500,808,052,368,249,716,736";
213         Number number = form.parse(doubleMax2, new ParsePosition(0));
214         assertTrue(number instanceof BigDecimal);
215         BigDecimal result = (BigDecimal)number;
216         assertEquals(new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(
217                 Double.MAX_VALUE)), result);
218     }
219 
220     // AndroidOnly: Difference to RI
test_getMaximumIntegerDigits_AndroidOnly()221     public void test_getMaximumIntegerDigits_AndroidOnly() {
222         final int maxIntDigit = 309;
223 
224         // When use default locale, in this case zh_CN
225         // the returned instance of NumberFormat is a DecimalFormat
226         DecimalFormat form = new DecimalFormat("00.###E0");
227         NumberFormat nform = DecimalFormat.getInstance(Locale.US);
228         nform = DecimalFormat.getInstance(Locale.US);
229         form = null;
230         if (nform instanceof DecimalFormat) {
231             form = (DecimalFormat) nform;
232         }
233         // getMaximumIntegerDigits from NumberFormat default to 309
234         // getMaximumIntegerDigits from DecimalFormat default to 309
235         // the following 2 assertions will fail on RI implementation, since the
236         // implementation of ICU and RI are not identical. RI does not give
237         // DecimalFormat an initial bound about its maximumIntegerDigits
238         // (default to Integer.MAX_VALUE: 2147483647 )
239         assertEquals(maxIntDigit, nform.getMaximumIntegerDigits());
240         assertEquals(maxIntDigit, form.getMaximumIntegerDigits());
241     }
242 
243     // AndroidOnly: second 0 needs to be quoted in icu.
244     // (quoting special characters in prefix and suffix necessary)
test_getMaximumIntegerDigits2()245     public void test_getMaximumIntegerDigits2() {
246         // regression test for HARMONY-878
247         assertTrue(new DecimalFormat("0\t'0'").getMaximumIntegerDigits() > 0);
248     }
249 
test_setPositivePrefixLjava_lang_String()250     public void test_setPositivePrefixLjava_lang_String() {
251         DecimalFormat format = new DecimalFormat();
252         assertEquals("", format.getPositivePrefix());
253 
254         format.setPositivePrefix("PosPrf");
255         assertEquals("PosPrf", format.getPositivePrefix());
256         try {
257             assertTrue(format.parse("PosPrf123.45").doubleValue() == 123.45);
258         } catch(java.text.ParseException pe) {
259             fail("ParseException was thrown.");
260         }
261 
262         format.setPositivePrefix("");
263         assertEquals("", format.getPositivePrefix());
264 
265         format.setPositivePrefix(null);
266         assertNull(format.getPositivePrefix());
267     }
test_setPositiveSuffixLjava_lang_String()268     public void test_setPositiveSuffixLjava_lang_String() {
269         DecimalFormat format = new DecimalFormat();
270         assertEquals("", format.getPositiveSuffix());
271 
272         format.setPositiveSuffix("PosSfx");
273         assertEquals("PosSfx", format.getPositiveSuffix());
274         try {
275             assertTrue(format.parse("123.45PosSfx").doubleValue() == 123.45);
276         } catch(java.text.ParseException pe) {
277             fail("ParseException was thrown.");
278         }
279 
280         format.setPositiveSuffix("");
281         assertEquals("", format.getPositiveSuffix());
282 
283         format.setPositiveSuffix(null);
284         assertNull(format.getPositiveSuffix());
285     }
test_setNegativePrefixLjava_lang_String()286     public void test_setNegativePrefixLjava_lang_String() {
287         DecimalFormat format = new DecimalFormat();
288         assertEquals("-", format.getNegativePrefix());
289 
290         format.setNegativePrefix("NegPrf");
291         assertEquals("NegPrf", format.getNegativePrefix());
292         try {
293             assertTrue(format.parse("NegPrf123.45").doubleValue() == -123.45);
294         } catch(java.text.ParseException pe) {
295             fail("ParseException was thrown.");
296         }
297         format.setNegativePrefix("");
298         assertEquals("", format.getNegativePrefix());
299 
300         format.setNegativePrefix(null);
301         assertNull(format.getNegativePrefix());
302     }
test_setNegativeSuffixLjava_lang_String()303     public void test_setNegativeSuffixLjava_lang_String() {
304         DecimalFormat format = new DecimalFormat();
305         assertEquals("", format.getNegativeSuffix());
306 
307         format.setNegativeSuffix("NegSfx");
308         assertEquals("NegSfx", format.getNegativeSuffix());
309         try {
310             assertTrue(format.parse("123.45NegPfx").doubleValue() == 123.45);
311         } catch(java.text.ParseException pe) {
312             fail("ParseException was thrown.");
313         }
314 
315         format.setNegativeSuffix("");
316         assertEquals("", format.getNegativeSuffix());
317 
318         format.setNegativeSuffix(null);
319         assertNull(format.getNegativeSuffix());
320     }
321 
test_toLocalizedPattern()322     public void test_toLocalizedPattern() {
323         DecimalFormat format = new DecimalFormat();
324         format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
325         try {
326             format.applyLocalizedPattern("#.#");
327             assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern());
328             format.applyLocalizedPattern("#.");
329             assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern());
330             format.applyLocalizedPattern("#");
331             assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern());
332             format.applyLocalizedPattern(".#");
333             assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern());
334         } catch (Exception e) {
335             fail("Unexpected exception " + e.toString());
336         }
337     }
338 
test_toPattern()339     public void test_toPattern() {
340         DecimalFormat format = new DecimalFormat();
341         try {
342             format.applyPattern("#.#");
343             assertEquals("Wrong pattern 1", "#0.#", format.toPattern());
344             format.applyPattern("#.");
345             assertEquals("Wrong pattern 2", "#0.", format.toPattern());
346             format.applyPattern("#");
347             assertEquals("Wrong pattern 3", "#", format.toPattern());
348             format.applyPattern(".#");
349             assertEquals("Wrong pattern 4", "#.0", format.toPattern());
350         } catch (Exception e) {
351             fail("Unexpected exception " + e.toString());
352         }
353     }
test_setGroupingUse()354     public void test_setGroupingUse() {
355         DecimalFormat format = new DecimalFormat();
356 
357         StringBuffer buf = new StringBuffer();
358         format.setGroupingUsed(false);
359         format.format(new Long(1970), buf, new FieldPosition(0));
360         assertEquals("1970", buf.toString());
361         assertFalse(format.isGroupingUsed());
362         format.format(new Long(1970), buf, new FieldPosition(0));
363         assertEquals("19701970", buf.toString());
364         assertFalse(format.isGroupingUsed());
365 
366         format.setGroupingUsed(true);
367         format.format(new Long(1970), buf, new FieldPosition(0));
368         assertEquals("197019701,970", buf.toString());
369         assertTrue(format.isGroupingUsed());
370     }
371 
test_Constructor()372     public void test_Constructor() {
373         // Test for method java.text.DecimalFormat()
374         // the constructor form that specifies a pattern is equal to the form
375         // constructed with no pattern and applying that pattern using the
376         // applyPattern call
377         try {
378             DecimalFormat format1 = new DecimalFormat();
379             format1.applyPattern("'$'1000.0000");
380             DecimalFormat format2 = new DecimalFormat();
381             format2.applyPattern("'$'1000.0000");
382             assertTrue(
383                     "Constructed format did not match applied format object",
384                     format2.equals(format1));
385             DecimalFormat format3 = new DecimalFormat("'$'1000.0000");
386             assertTrue(
387                     "Constructed format did not match applied format object",
388                     format3.equals(format1));
389             DecimalFormat format4 = new DecimalFormat("'$'8000.0000");
390             assertTrue(
391                     "Constructed format did not match applied format object",
392                     !format4.equals(format1));
393         } catch (Exception e) {
394             fail("Unexpected exception " + e.toString());
395         }
396     }
397 
test_ConstructorLjava_lang_String()398     public void test_ConstructorLjava_lang_String() {
399         // Test for method java.text.DecimalFormat(java.lang.String)
400         // the constructor form that specifies a pattern is equal to the form
401         // constructed with no pattern and applying that pattern using the
402         // applyPattern call
403         DecimalFormat format = new DecimalFormat("'$'0000.0000");
404         DecimalFormat format1 = new DecimalFormat();
405         format1.applyPattern("'$'0000.0000");
406         assertTrue("Constructed format did not match applied format object",
407                 format.equals(format1));
408 
409         String [] patterns = {"####.##", "######.######", "000000.000000",
410                 "######.000000", "000000.######", " ###.###", "$#####.######",
411                 "$$####.######", "%#,##,###,####", "#,##0.00;(#,##0.00)"};
412 
413         for(String str:patterns) {
414             new DecimalFormat(str);
415         }
416 
417         try {
418             new DecimalFormat(null);
419             fail("NullPointerException wasn't thrown.");
420         } catch(NullPointerException npe){
421             //expected
422         }
423 
424         String [] incPatterns = {"%#,##,###,####'", "#.##0.00"};
425         for(String str:incPatterns) {
426             try {
427                 new DecimalFormat(str);
428                 fail("IllegalArgumentException wasn't thrown for pattern: " + str);
429             } catch(IllegalArgumentException iae){
430                 //expected
431             }
432         }
433     }
434 
435     /**
436      * Case 1: Try to construct object using correct pattern and fromat symbols.
437      * Case 2: Try to construct object using null arguments.
438      * Case 3: Try to construct object using incorrect pattern.
439      */
test_ConstructorLjava_lang_StringLjava_text_DecimalFormatSymbols()440     public void test_ConstructorLjava_lang_StringLjava_text_DecimalFormatSymbols() {
441         try {
442             // case 1: Try to construct object using correct pattern and fromat
443             // symbols.
444             DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.CANADA);
445             DecimalFormat format1 = new DecimalFormat("'$'1000.0000", dfs);
446             DecimalFormat format2 = new DecimalFormat();
447             format2.applyPattern("'$'1000.0000");
448             format2.setDecimalFormatSymbols(dfs);
449             assertTrue(
450                     "Constructed format did not match applied format object",
451                     format2.equals(format1));
452             assertTrue(
453                     "Constructed format did not match applied format object",
454                     !format1.equals(new DecimalFormat("'$'1000.0000",
455                             new DecimalFormatSymbols(Locale.CHINA))));
456 
457             // case 2: Try to construct object using null arguments.
458             try {
459                 new DecimalFormat("'$'1000.0000", (DecimalFormatSymbols) null);
460                 fail("Expected NullPointerException was not thrown");
461             } catch (NullPointerException e) {
462                 // expected
463             }
464             try {
465                 new DecimalFormat(null, new DecimalFormatSymbols());
466                 fail("Expected NullPointerException was not thrown");
467             } catch (NullPointerException e) {
468                 // expected
469             }
470             try {
471                 new DecimalFormat(null, (DecimalFormatSymbols) null);
472                 fail("Expected NullPointerException was not thrown");
473             } catch (NullPointerException e) {
474                 // expected
475             }
476 
477             // case 3: Try to construct object using incorrect pattern.
478             try {
479                 new DecimalFormat("$'", new DecimalFormatSymbols());
480                 fail("Expected IllegalArgumentException was not thrown");
481             } catch (IllegalArgumentException e) {
482                 // expected
483             }
484         } catch (Exception e) {
485             fail("Unexpected exception " + e.toString());
486         }
487     }
488 
489     /**
490      * Case 1: Try to apply correct variants of pattern.
491      * Case 2: Try to apply malformed patten. Case 3: Try to apply null pattern.
492      */
test_applyLocalizedPatternLjava_lang_String()493     public void test_applyLocalizedPatternLjava_lang_String() {
494         DecimalFormat format = new DecimalFormat();
495         try {
496             // case 1: Try to apply correct variants of pattern.
497             format.applyLocalizedPattern("#.#");
498             assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern());
499             format.applyLocalizedPattern("#.");
500             assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern());
501             format.applyLocalizedPattern("#");
502             assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern());
503             format.applyLocalizedPattern(".#");
504             assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern());
505 
506             // case 2: Try to apply malformed patten.
507             try {
508                 format.applyLocalizedPattern("'#,#:#0.0#;(#)");
509                 fail("Expected IllegalArgumentException was not thrown");
510             } catch (IllegalArgumentException e) {
511                 // expected
512             }
513 
514             // case 3: Try to apply null patern.
515             try {
516                 format.applyLocalizedPattern((String) null);
517                 fail("Expected NullPointerException was not thrown");
518             } catch (NullPointerException e) {
519                 // expected
520             }
521         } catch (Exception e) {
522             fail("Unexpected exception " + e.toString());
523         }
524     }
525 
test_applyPatternLjava_lang_String()526     public void test_applyPatternLjava_lang_String() {
527         DecimalFormat format = new DecimalFormat("#.#");
528         assertEquals("Wrong pattern 1", "#0.#", format.toPattern());
529         format = new DecimalFormat("#.");
530         assertEquals("Wrong pattern 2", "#0.", format.toPattern());
531         format = new DecimalFormat("#");
532         assertEquals("Wrong pattern 3", "#", format.toPattern());
533         format = new DecimalFormat(".#");
534         assertEquals("Wrong pattern 4", "#.0", format.toPattern());
535 
536         DecimalFormat decFormat = new DecimalFormat("#.#");
537 
538         try {
539             decFormat.applyPattern(null);
540             fail("NullPointerException was not thrown.");
541         } catch(NullPointerException npe) {
542             //expected
543         }
544 
545         String [] incPatterns = {"%#,##,###,####'", "#.##0.00"};
546         for(String str:incPatterns) {
547             try {
548                 decFormat.applyPattern(str);
549                 fail("IllegalArgumentException was not thrown for pattern: " +
550                         str);
551             } catch(IllegalArgumentException  iae) {
552                 //expected
553             }
554         }
555     }
556 
557     // AndroidOnly: icu supports 2 grouping sizes
test_applyPatternLjava_lang_String2()558     public void test_applyPatternLjava_lang_String2() {
559         DecimalFormat decFormat = new DecimalFormat("#.#");
560         String [] patterns = {"####.##", "######.######", "000000.000000",
561                 "######.000000", "000000.######", " ###.###", "$#####.######",
562                 "$$####.######", "%#,##,###,####", "#,##0.00;(#,##0.00)",
563                  "##.##-E"};
564 
565         String [] expResult = {"#0.##", "#0.######", "#000000.000000",
566                 "#.000000", "#000000.######", " #0.###", "$#0.######",
567                 "$$#0.######",
568                 "%#,###,####", // icu only. icu supports two grouping sizes
569                 "#,##0.00;(#,##0.00)",
570                 "#0.##-'E'"}; // icu only. E in the suffix does not need to be
571                               // quoted. This is done automatically.
572 
573         for (int i = 0; i < patterns.length; i++) {
574             decFormat.applyPattern(patterns[i]);
575             String result = decFormat.toPattern();
576             assertEquals("Failed to apply following pattern: " + patterns[i] +
577                     " expected: " + expResult[i] + " returned: " + result,
578                     expResult[i], result);
579         }
580     }
581 
test_clone()582     public void test_clone() {
583         DecimalFormat format = (DecimalFormat) DecimalFormat
584                 .getInstance(Locale.US);
585         DecimalFormat cloned = (DecimalFormat) format.clone();
586         assertEquals(cloned.getDecimalFormatSymbols(), format
587                 .getDecimalFormatSymbols());
588 
589         format = new DecimalFormat("'$'0000.0000");
590         DecimalFormat format1 = (DecimalFormat) (format.clone());
591         // make sure the objects are equal
592         assertTrue("Object's clone isn't equal!", format.equals(format1));
593         // change the content of the clone and make sure it's not equal anymore
594         // verifies that it's data is now distinct from the original
595         format1.applyPattern("'$'0000.####");
596         assertTrue("Object's changed clone should not be equal!", !format
597                 .equals(format1));
598     }
599 
compare(String testName, String format, String expected)600     private void compare(String testName, String format, String expected) {
601         assertTrue(testName + " got: " + format + " expected: " + expected,
602                 format.equals(expected));
603     }
604 
compare(int count, String format, String expected)605     private boolean compare(int count, String format, String expected) {
606         boolean result = format.equals(expected);
607         if (!result)
608             System.out.println("Failure test: " + count + " got: " + format
609                     + " expected: " + expected);
610         return result;
611     }
612 
test_formatDLjava_lang_StringBufferLjava_text_FieldPosition()613     public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition() {
614         new Support_DecimalFormat(
615                 "test_formatDLjava_lang_StringBufferLjava_text_FieldPosition")
616                 .t_format_with_FieldPosition();
617 
618         int failCount = 0;
619         BitSet failures = new BitSet();
620 
621         final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
622 
623         DecimalFormat df = new DecimalFormat("00.0#E0", dfs);
624         compare("00.0#E0: 0.0", df.format(0.0), "00.0E0");
625         compare("00.0#E0: 1.0", df.format(1.0), "10.0E-1");
626         compare("00.0#E0: 12.0", df.format(12.0), "12.0E0");
627         compare("00.0#E0: 123.0", df.format(123.0), "12.3E1");
628         compare("00.0#E0: 1234.0", df.format(1234.0), "12.34E2");
629         compare("00.0#E0: 12346.0", df.format(12346.0), "12.35E3");
630         compare("00.0#E0: 99999.0", df.format(99999.0), "10.0E4");
631         compare("00.0#E0: 1.2", df.format(1.2), "12.0E-1");
632         compare("00.0#E0: 12.3", df.format(12.3), "12.3E0");
633         compare("00.0#E0: 123.4", df.format(123.4), "12.34E1");
634         compare("00.0#E0: 1234.6", df.format(1234.6), "12.35E2");
635         compare("00.0#E0: 9999.9", df.format(9999.9), "10.0E3");
636         compare("00.0#E0: 0.1", df.format(0.1), "10.0E-2");
637         compare("00.0#E0: 0.12", df.format(0.12), "12.0E-2");
638         compare("00.0#E0: 0.123", df.format(0.123), "12.3E-2");
639         compare("00.0#E0: 0.1234", df.format(0.1234), "12.34E-2");
640         compare("00.0#E0: 0.12346", df.format(0.12346), "12.35E-2");
641         compare("00.0#E0: 0.99999", df.format(0.99999), "10.0E-1");
642         compare("00.0#E0: -0.0", df.format(-0.0), "-00.0E0");
643         compare("00.0#E0: -1.0", df.format(-1.0), "-10.0E-1");
644         compare("00.0#E0: -12.0", df.format(-12.0), "-12.0E0");
645         compare("00.0#E0: -123.0", df.format(-123.0), "-12.3E1");
646         compare("00.0#E0: -1234.0", df.format(-1234.0), "-12.34E2");
647         compare("00.0#E0: -12346.0", df.format(-12346.0), "-12.35E3");
648         compare("00.0#E0: -99999.0", df.format(-99999.0), "-10.0E4");
649 
650         df = new DecimalFormat("##0.0E0", dfs);
651         compare("##0.0E0: -0.0", df.format(-0.0), "-0.0E0");
652         compare("##0.0E0: 0.0", df.format(0.0), "0.0E0");
653         compare("##0.0E0: 1.0", df.format(1.0), "1.0E0");
654         compare("##0.0E0: 12.0", df.format(12.0), "12E0");
655         compare("##0.0E0: 123.0", df.format(123.0), "123E0");  // Android fails, here!
656         compare("##0.0E0: 1234.0", df.format(1234.0), "1.234E3");
657         compare("##0.0E0: 12346.0", df.format(12346.0), "12.35E3");
658         // Fails in JDK 1.2.2
659         if (!compare(failCount, df.format(99999.0), "100E3"))
660             failures.set(failCount);
661         failCount++;
662         compare("##0.0E0: 999999.0", df.format(999999.0), "1.0E6");
663 
664         df = new DecimalFormat("#00.0##E0", dfs);
665         compare("#00.0##E0: 0.1", df.format(0.1), "100E-3");
666         compare("#00.0##E0: 0.12", df.format(0.12), "120E-3");
667         compare("#00.0##E0: 0.123", df.format(0.123), "123E-3");
668         compare("#00.0##E0: 0.1234", df.format(0.1234), "123.4E-3");
669         compare("#00.0##E0: 0.1234567", df.format(0.1234567), "123.457E-3");
670         compare("#00.0##E0: 0.01", df.format(0.01), "10.0E-3");
671         compare("#00.0##E0: 0.012", df.format(0.012), "12.0E-3");
672         compare("#00.0##E0: 0.0123", df.format(0.0123), "12.3E-3");
673         compare("#00.0##E0: 0.01234", df.format(0.01234), "12.34E-3");
674         compare("#00.0##E0: 0.01234567", df.format(0.01234567), "12.3457E-3");
675         compare("#00.0##E0: 0.001", df.format(0.001), "1.00E-3");
676         compare("#00.0##E0: 0.0012", df.format(0.0012), "1.20E-3");
677         compare("#00.0##E0: 0.00123", df.format(0.00123), "1.23E-3");
678         compare("#00.0##E0: 0.001234", df.format(0.001234), "1.234E-3");
679         compare("#00.0##E0: 0.001234567", df.format(0.001234567), "1.23457E-3");
680         compare("#00.0##E0: 0.0001", df.format(0.0001), "100E-6");
681         compare("#00.0##E0: 0.00012", df.format(0.00012), "120E-6");
682         compare("#00.0##E0: 0.000123", df.format(0.000123), "123E-6");
683         compare("#00.0##E0: 0.0001234", df.format(0.0001234), "123.4E-6");
684         compare("#00.0##E0: 0.0001234567", df.format(0.0001234567),
685                 "123.457E-6");
686 
687         // Fails in JDK 1.2.2
688         if (!compare(failCount, df.format(0.0), "0.00E0"))
689             failures.set(failCount);
690         failCount++;
691         compare("#00.0##E0: 1.0", df.format(1.0), "1.00E0");
692         compare("#00.0##E0: 12.0", df.format(12.0), "12.0E0");
693         compare("#00.0##E0: 123.0", df.format(123.0), "123E0");
694         compare("#00.0##E0: 1234.0", df.format(1234.0), "1.234E3");
695         compare("#00.0##E0: 12345.0", df.format(12345.0), "12.345E3");
696         compare("#00.0##E0: 123456.0", df.format(123456.0), "123.456E3");
697         compare("#00.0##E0: 1234567.0", df.format(1234567.0), "1.23457E6");
698         compare("#00.0##E0: 12345678.0", df.format(12345678.0), "12.3457E6");
699         compare("#00.0##E0: 99999999.0", df.format(99999999.0), "100E6");
700 
701         df = new DecimalFormat("#.0E0", dfs);
702         compare("#.0E0: -0.0", df.format(-0.0), "-.0E0");
703         compare("#.0E0: 0.0", df.format(0.0), ".0E0");
704         compare("#.0E0: 1.0", df.format(1.0), ".1E1");
705         compare("#.0E0: 12.0", df.format(12.0), ".12E2");
706         compare("#.0E0: 123.0", df.format(123.0), ".12E3");
707         compare("#.0E0: 1234.0", df.format(1234.0), ".12E4");
708         compare("#.0E0: 9999.0", df.format(9999.0), ".1E5");
709 
710         df = new DecimalFormat("0.#E0", dfs);
711         compare("0.#E0: -0.0", df.format(-0.0), "-0E0");
712         compare("0.#E0: 0.0", df.format(0.0), "0E0");
713         compare("0.#E0: 1.0", df.format(1.0), "1E0");
714         compare("0.#E0: 12.0", df.format(12.0), "1.2E1");
715         compare("0.#E0: 123.0", df.format(123.0), "1.2E2");
716         compare("0.#E0: 1234.0", df.format(1234.0), "1.2E3");
717         compare("0.#E0: 9999.0", df.format(9999.0), "1E4");
718 
719         df = new DecimalFormat(".0E0", dfs);
720         compare(".0E0: -0.0", df.format(-0.0), "-.0E0");
721         compare(".0E0: 0.0", df.format(0.0), ".0E0");
722         compare(".0E0: 1.0", df.format(1.0), ".1E1");
723         compare(".0E0: 12.0", df.format(12.0), ".1E2");
724         compare(".0E0: 123.0", df.format(123.0), ".1E3");
725         compare(".0E0: 1234.0", df.format(1234.0), ".1E4");
726         compare(".0E0: 9999.0", df.format(9999.0), ".1E5");
727 
728         df = new DecimalFormat("0.E0", dfs);
729         // Fails in JDK 1.2.2
730         if (!compare(failCount, df.format(0.0), "0.E0"))
731             failures.set(failCount);
732         failCount++;
733         if (!compare(failCount, df.format(1.0), "1.E0"))
734             failures.set(failCount);
735         failCount++;
736         if (!compare(failCount, df.format(12.0), "1.E1"))
737             failures.set(failCount);
738         failCount++;
739         if (!compare(failCount, df.format(123.0), "1.E2"))
740             failures.set(failCount);
741         failCount++;
742         if (!compare(failCount, df.format(1234.0), "1.E3"))
743             failures.set(failCount);
744         failCount++;
745         if (!compare(failCount, df.format(9999.0), "1.E4"))
746             failures.set(failCount);
747         failCount++;
748 
749         df = new DecimalFormat("##0.00#E0", dfs);
750         compare("##0.00#E0: 0.1", df.format(0.1), "100E-3");
751         compare("##0.00#E0: 0.1234567", df.format(0.1234567), "123.457E-3");
752         compare("##0.00#E0: 0.9999999", df.format(0.9999999), "1.00E0");
753         compare("##0.00#E0: 0.01", df.format(0.01), "10.0E-3");
754         compare("##0.00#E0: 0.01234567", df.format(0.01234567), "12.3457E-3");
755         compare("##0.00#E0: 0.09999999", df.format(0.09999999), "100E-3");
756         compare("##0.00#E0: 0.001", df.format(0.001), "1.00E-3");
757         compare("##0.00#E0: 0.001234567", df.format(0.001234567), "1.23457E-3");
758         compare("##0.00#E0: 0.009999999", df.format(0.009999999), "10.0E-3");
759         compare("##0.00#E0: 0.0001", df.format(0.0001), "100E-6");
760         compare("##0.00#E0: 0.0001234567", df.format(0.0001234567),
761                 "123.457E-6");
762         compare("##0.00#E0: 0.0009999999", df.format(0.0009999999), "1.00E-3");
763 
764         df = new DecimalFormat("###0.00#E0", dfs);
765         compare("###0.00#E0: 0.1", df.format(0.1), "1000E-4");
766         compare("###0.00#E0: 0.12345678", df.format(0.12345678), "1234.568E-4");
767         compare("###0.00#E0: 0.99999999", df.format(0.99999999), "1.00E0");
768         compare("###0.00#E0: 0.01", df.format(0.01), "100E-4");
769         compare("###0.00#E0: 0.012345678", df.format(0.012345678),
770                 "123.4568E-4");
771         compare("###0.00#E0: 0.099999999", df.format(0.099999999), "1000E-4");
772         compare("###0.00#E0: 0.001", df.format(0.001), "10.0E-4");
773         compare("###0.00#E0: 0.0012345678", df.format(0.0012345678),
774                 "12.34568E-4");
775         compare("###0.00#E0: 0.0099999999", df.format(0.0099999999), "100E-4");
776         compare("###0.00#E0: 0.0001", df.format(0.0001), "1.00E-4");
777         compare("###0.00#E0: 0.00012345678", df.format(0.00012345678),
778                 "1.234568E-4");
779         compare("###0.00#E0: 0.00099999999", df.format(0.00099999999),
780                 "10.0E-4");
781         // Fails in JDK 1.2.2
782         if (!compare(failCount, df.format(0.00001), "1000E-8"))
783             failures.set(failCount);
784         failCount++;
785         compare("###0.00#E0: 0.000012345678", df.format(0.000012345678),
786                 "1234.568E-8");
787         compare("###0.00#E0: 0.000099999999", df.format(0.000099999999),
788                 "1.00E-4");
789 
790         df = new DecimalFormat("###0.0#E0", dfs);
791         compare("###0.0#E0: 0.1", df.format(0.1), "1000E-4");
792         compare("###0.0#E0: 0.1234567", df.format(0.1234567), "1234.57E-4");
793         compare("###0.0#E0: 0.9999999", df.format(0.9999999), "1.0E0");
794         // Fails in JDK 1.2.2
795         if (!compare(failCount, df.format(0.01), "100E-4"))
796             failures.set(failCount);
797         failCount++;
798         compare("###0.0#E0: 0.01234567", df.format(0.01234567), "123.457E-4");
799         compare("###0.0#E0: 0.09999999", df.format(0.09999999), "1000E-4");
800         compare("###0.0#E0: 0.001", df.format(0.001), "10E-4");
801         compare("###0.0#E0: 0.001234567", df.format(0.001234567), "12.3457E-4");
802         // Fails in JDK 1.2.2
803         if (!compare(failCount, df.format(0.009999999), "100E-4"))
804             failures.set(failCount);
805         failCount++;
806         compare("###0.0#E0: 0.0001", df.format(0.0001), "1.0E-4");
807         compare("###0.0#E0: 0.0001234567", df.format(0.0001234567),
808                 "1.23457E-4");
809         compare("###0.0#E0: 0.0009999999", df.format(0.0009999999), "10E-4");
810         // Fails in JDK 1.2.2
811         if (!compare(failCount, df.format(0.00001), "1000E-8"))
812             failures.set(failCount);
813         failCount++;
814         compare("###0.0#E0: 0.00001234567", df.format(0.00001234567),
815                 "1234.57E-8");
816         compare("###0.0#E0: 0.00009999999", df.format(0.00009999999), "1.0E-4");
817 
818         assertTrue("Failed " + failures + " of " + failCount,
819                 failures.length() == 0);
820 
821         String formatString = "##0.#";
822         df = new DecimalFormat(formatString, dfs);
823         df.setMinimumFractionDigits(30);
824         compare(formatString + ": 0.000000000000000000000000000000", df
825                 .format(0.0), "0.000000000000000000000000000000");
826         compare(formatString + ": -0.000000000000000000000000000000", df
827                 .format(-0.0), "-0.000000000000000000000000000000");
828         compare(formatString + ": 1.000000000000000000000000000000", df
829                 .format(1.0), "1.000000000000000000000000000000");
830         compare(formatString + ": -1.000000000000000000000000000000", df
831                 .format(-1.0), "-1.000000000000000000000000000000");
832 
833         df = new DecimalFormat(formatString);
834         df.setMaximumFractionDigits(30);
835         compare(formatString + ": 0", df.format(0.0), "0");
836         compare(formatString + ": -0", df.format(-0.0), "-0");
837         compare(formatString + ": 1", df.format(1.0), "1");
838         compare(formatString + ": -1", df.format(-1.0), "-1");
839     }
840 
test_formatD()841     public void test_formatD() {
842         DecimalFormat format = (DecimalFormat) NumberFormat
843                 .getInstance(Locale.ENGLISH);
844         format.setGroupingUsed(false);
845         format.setMaximumFractionDigits(400);
846         assertEquals("123456789012345", format.format(123456789012345.));
847         assertEquals("1", "12345678901234.5", format.format(12345678901234.5));
848         assertEquals("2", "1234567890123.25", format.format(1234567890123.25));
849         assertEquals("3", "999999999999.375", format.format(999999999999.375));
850         assertEquals("4", "99999999999.0625", format.format(99999999999.0625));
851         assertEquals("5", "9999999999.03125", format.format(9999999999.03125));
852         assertEquals("6", "999999999.015625", format.format(999999999.015625));
853         assertEquals("7", "99999999.0078125", format.format(99999999.0078125));
854         assertEquals("8", "9999999.00390625", format.format(9999999.00390625));
855         assertEquals("9", "999999.001953125", format.format(999999.001953125));
856         assertEquals("10", "9999.00048828125", format.format(9999.00048828125));
857         assertEquals("11", "999.000244140625", format.format(999.000244140625));
858         assertEquals("12", "99.0001220703125", format.format(99.0001220703125));
859         assertEquals("13", "9.00006103515625", format.format(9.00006103515625));
860         assertEquals("14", "0.000030517578125", format.format(0.000030517578125));
861     }
862 
863 
test_getNegativePrefix()864     public void test_getNegativePrefix() {
865         DecimalFormat df = new DecimalFormat();
866         try {
867             df.setNegativePrefix("--");
868             assertTrue("Incorrect negative prefix", df.getNegativePrefix()
869                     .equals("--"));
870         } catch (Exception e) {
871             fail("Unexpected exception " + e.toString());
872         }
873     }
874 
test_getNegativeSuffix()875     public void test_getNegativeSuffix() {
876         DecimalFormat df = new DecimalFormat();
877         try {
878             df.setNegativeSuffix("&");
879             assertTrue("Incorrect negative suffix", df.getNegativeSuffix()
880                     .equals("&"));
881         } catch (Exception e) {
882             fail("Unexpected exception " + e.toString());
883         }
884     }
885 
test_getPositivePrefix()886     public void test_getPositivePrefix() {
887         DecimalFormat df = new DecimalFormat();
888         try {
889             df.setPositivePrefix("++");
890             assertTrue("Incorrect positive prefix", df.getPositivePrefix()
891                     .equals("++"));
892         } catch (Exception e) {
893             fail("Unexpected exception " + e.toString());
894         }
895     }
896 
test_getPositiveSuffix()897     public void test_getPositiveSuffix() {
898         DecimalFormat df = new DecimalFormat();
899         try {
900             df.setPositiveSuffix("%");
901             assertTrue("Incorrect positive prefix", df.getPositiveSuffix()
902                     .equals("%"));
903         } catch (Exception e) {
904             fail("Unexpected exception " + e.toString());
905         }
906     }
907 
test_hashCode()908     public void test_hashCode() {
909         try {
910             DecimalFormat df1 = new DecimalFormat();
911             DecimalFormat df2 = (DecimalFormat) df1.clone();
912             assertTrue("Hash codes of equals object are not equal", df2
913                     .hashCode() == df1.hashCode());
914         } catch (Exception e) {
915             fail("Unexpected exception " + e.toString());
916         }
917     }
918 
test_parseLjava_lang_StringLjava_text_ParsePosition()919     public void test_parseLjava_lang_StringLjava_text_ParsePosition() {
920         DecimalFormat format = (DecimalFormat) NumberFormat
921                 .getNumberInstance(Locale.ENGLISH);
922         ParsePosition pos = new ParsePosition(0);
923         Number result = format.parse("9223372036854775807", pos);
924         assertTrue("Wrong result type for Long.MAX_VALUE",
925                 result.getClass() == Long.class);
926         assertEquals("Wrong result Long.MAX_VALUE",
927                 Long.MAX_VALUE, result.longValue());
928         pos = new ParsePosition(0);
929         result = format.parse("-9223372036854775808", pos);
930         assertTrue("Wrong result type for Long.MIN_VALUE",
931                 result.getClass() == Long.class);
932         assertTrue("Wrong result Long.MIN_VALUE: " + result.longValue(), result
933                 .longValue() == Long.MIN_VALUE);
934         pos = new ParsePosition(0);
935         result = format.parse("9223372036854775808", pos);
936         assertTrue("Wrong result type for Long.MAX_VALUE+1",
937                 result.getClass() == Double.class);
938         assertEquals("Wrong result Long.MAX_VALUE + 1",
939                 (double) Long.MAX_VALUE + 1, result.doubleValue());
940         pos = new ParsePosition(0);
941         result = format.parse("-9223372036854775809", pos);
942         assertTrue("Wrong result type for Long.MIN_VALUE - 1",
943                 result.getClass() == Double.class);
944         assertEquals("Wrong result Long.MIN_VALUE - 1",
945                 (double) Long.MIN_VALUE - 1, result.doubleValue());
946 
947         pos = new ParsePosition(0);
948         result = format.parse("18446744073709551629", pos);
949         assertTrue("Wrong result type for overflow",
950                 result.getClass() == Double.class);
951         assertEquals("Wrong result for overflow",
952                 18446744073709551629d, result.doubleValue());
953 
954         pos = new ParsePosition(0);
955         result = format.parse("42325917317067571199", pos);
956         assertTrue("Wrong result type for overflow a: " + result, result
957                 .getClass() == Double.class);
958         assertTrue("Wrong result for overflow a: " + result, result
959                 .doubleValue() == 42325917317067571199d);
960         pos = new ParsePosition(0);
961         result = format.parse("4232591731706757119E1", pos);
962         assertTrue("Wrong result type for overflow b: " + result, result
963                 .getClass() == Double.class);
964         assertEquals("Wrong result for overflow b: " + result,
965                 42325917317067571190d, result.doubleValue());
966         pos = new ParsePosition(0);
967         result = format.parse(".42325917317067571199E20", pos);
968         assertTrue("Wrong result type for overflow c: " + result, result
969                 .getClass() == Double.class);
970         assertTrue("Wrong result for overflow c: " + result, result
971                 .doubleValue() == 42325917317067571199d);
972         pos = new ParsePosition(0);
973         result = format.parse("922337203685477580.9E1", pos);
974         assertTrue("Wrong result type for overflow d: " + result, result
975                 .getClass() == Double.class);
976         assertTrue("Wrong result for overflow d: " + result, result
977                 .doubleValue() == 9223372036854775809d);
978         pos = new ParsePosition(0);
979         result = format.parse("9.223372036854775809E18", pos);
980         assertTrue("Wrong result type for overflow e: " + result, result
981                 .getClass() == Double.class);
982         assertTrue("Wrong result for overflow e: " + result, result
983                 .doubleValue() == 9223372036854775809d);
984 
985         // test parse with multipliers
986         format.setMultiplier(100);
987         result = format.parse("9223372036854775807", new ParsePosition(0));
988         assertEquals("Wrong result type multiplier 100: " + result, Long.class, result.getClass());
989         // RI on windows and linux both answer with a slightly rounded result
990         assertTrue("Wrong result for multiplier 100: " + result, result
991                 .longValue() == 92233720368547760L);
992         format.setMultiplier(1000);
993         result = format.parse("9223372036854775807", new ParsePosition(0));
994         assertTrue("Wrong result type multiplier 1000: " + result, result
995                 .getClass() == Long.class);
996         assertTrue("Wrong result for multiplier 1000: " + result, result
997                 .longValue() == 9223372036854776L);
998 
999         format.setMultiplier(10000);
1000         result = format.parse("9223372036854775807", new ParsePosition(0));
1001         assertTrue("Wrong result type multiplier 10000: " + result, result
1002                 .getClass() == Double.class);
1003         assertTrue("Wrong result for multiplier 10000: " + result, result
1004                 .doubleValue() == 922337203685477.5807d);
1005 
1006     }
1007 }
1008