1 /*
2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4018937 8008577
27  * @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected.
28  * @library /java/text/testlib
29  * @run main/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalParse
30  */
31 
32 package test.java.text.Format.NumberFormat;
33 
34 import java.math.BigDecimal;
35 import java.text.*;
36 import java.util.*;
37 
38 import test.java.text.testlib.IntlTest;
39 
40 public class BigDecimalParse extends IntlTest {
41 
main(String[] args)42     public static void main(String[] args) throws Exception {
43         Locale loc = Locale.getDefault();
44         try {
45             Locale.setDefault(Locale.US);
46             new BigDecimalParse().run(args);
47         } finally {
48             // restore the reserved locale
49             Locale.setDefault(loc);
50         }
51     }
52 
53     static final String nonsep_int =
54         "123456789012345678901234567890123456789012345678901234567890" +
55         "123456789012345678901234567890123456789012345678901234567890" +
56         "123456789012345678901234567890123456789012345678901234567890" +
57         "123456789012345678901234567890123456789012345678901234567890" +
58         "123456789012345678901234567890123456789012345678901234567890" +
59         "123456789012345678901234567890123456789012345678901234567890";
60 
61     static final String sep_int =
62         "123,456,789,012,345,678,901,234,567,890," +
63         "123,456,789,012,345,678,901,234,567,890," +
64         "123,456,789,012,345,678,901,234,567,890," +
65         "123,456,789,012,345,678,901,234,567,890," +
66         "123,456,789,012,345,678,901,234,567,890," +
67         "123,456,789,012,345,678,901,234,567,890," +
68         "123,456,789,012,345,678,901,234,567,890," +
69         "123,456,789,012,345,678,901,234,567,890," +
70         "123,456,789,012,345,678,901,234,567,890," +
71         "123,456,789,012,345,678,901,234,567,890," +
72         "123,456,789,012,345,678,901,234,567,890," +
73         "123,456,789,012,345,678,901,234,567,890";
74 
75     static final String nonsep_zero =
76         "000000000000000000000000000000000000000000000000000000000000" +
77         "000000000000000000000000000000000000000000000000000000000000" +
78         "000000000000000000000000000000000000000000000000000000000000" +
79         "000000000000000000000000000000000000000000000000000000000000" +
80         "000000000000000000000000000000000000000000000000000000000000" +
81         "000000000000000000000000000000000000000000000000000000000000";
82 
83     static final String sep_zero =
84         "000,000,000,000,000,000,000,000,000,000," +
85         "000,000,000,000,000,000,000,000,000,000," +
86         "000,000,000,000,000,000,000,000,000,000," +
87         "000,000,000,000,000,000,000,000,000,000," +
88         "000,000,000,000,000,000,000,000,000,000," +
89         "000,000,000,000,000,000,000,000,000,000," +
90         "000,000,000,000,000,000,000,000,000,000," +
91         "000,000,000,000,000,000,000,000,000,000," +
92         "000,000,000,000,000,000,000,000,000,000," +
93         "000,000,000,000,000,000,000,000,000,000," +
94         "000,000,000,000,000,000,000,000,000,000," +
95         "000,000,000,000,000,000,000,000,000,000";
96 
97     static final String fra =
98         "012345678901234567890123456789012345678901234567890123456789" +
99         "012345678901234567890123456789012345678901234567890123456789" +
100         "012345678901234567890123456789012345678901234567890123456789" +
101         "012345678901234567890123456789012345678901234567890123456789" +
102         "012345678901234567890123456789012345678901234567890123456789" +
103         "012345678901234567890123456789012345678901234567890123456789";
104 
105 
106     Number parsed = null;
107     ParsePosition pp;
108     boolean exceptionOccurred;
109     String msg;
110     DecimalFormat df;
111 
112     /**
113      * Test for normal big numbers which have the fraction part
114      */
test_Parse_in_DecimalFormat_BigDecimal()115     void test_Parse_in_DecimalFormat_BigDecimal() {
116         df = new DecimalFormat();
117         df.setParseBigDecimal(true);
118 
119         // From: 1234...7890.012...789
120         // To:   BigDecimal 1234...7890.012...789
121         check(nonsep_int + "." + fra, new BigDecimal(nonsep_int + "." + fra));
122 
123         // From: -1,234...7,890.012...789
124         // To:   BigDecimal -1234...7890.012...789
125         check("-" + sep_int    + "." + fra,
126               new BigDecimal("-" + nonsep_int + "." + fra));
127 
128         // From: 000...0000.0...0
129         // To:   BigDecimal 0E-360
130         check(nonsep_zero + "." + nonsep_zero,
131               new BigDecimal(nonsep_zero + "." + nonsep_zero));
132 
133         // From: 0.000...0000123...789E370
134         // To:   BigDecimal 0.0123...789
135         check("0.0000000000" + nonsep_zero + fra + "E370",
136               new BigDecimal("0.0000000000" + nonsep_zero + fra + "E370"));
137 
138         // From: 0.1123...890E-360
139         // To:   BigDecimal 1.123...890E-361
140         check("0.1" + nonsep_int + "E-360",
141               new BigDecimal("0.1" + nonsep_int + "E-360"));
142 
143         // From: 000...0000.0...0123...7890
144         // To:   BigDecimal 1.234...890E-361
145         check(nonsep_zero + "." + nonsep_zero + nonsep_int,
146               new BigDecimal(nonsep_zero + "." + nonsep_zero + nonsep_int));
147 
148         // From: 0.123...890E360
149         // To:   BigDecimal 123...890
150         check("0." + nonsep_int + "E360",
151               new BigDecimal("0." + nonsep_int + "E360"));
152     }
153 
154     /**
155      * Test for normal big numbers which have the fraction part with multiplier
156      */
test_Parse_in_DecimalFormat_BigDecimal_usingMultiplier()157     void test_Parse_in_DecimalFormat_BigDecimal_usingMultiplier() {
158         df = new DecimalFormat();
159         df.setParseBigDecimal(true);
160 
161         // From: 250,0...0,000.000...000
162         // To:   1000...0000.000...000
163         df.setMultiplier(250000000);
164         check("250,000,000," + sep_zero + "." + nonsep_zero,
165               new BigDecimal("1" + nonsep_zero + "." + nonsep_zero));
166 
167         // From: -250,0...0,000.000...000
168         // To:   -1000...0000.000...000
169         check("-250,000,000," + sep_zero + "." + nonsep_zero,
170               new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));
171 
172         // From: 250,0...0,000.000...000
173         // To:   -1000...0000.000...000
174         df.setMultiplier(-250000000);
175         check("250,000,000," + sep_zero + "." + nonsep_zero,
176               new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));
177 
178         // From: -250,0...0,000.000...000
179         // To:   1000...0000.000...000
180         check("-250,000,000," + sep_zero + "." + nonsep_zero,
181               new BigDecimal("1" + nonsep_zero + "." + nonsep_zero));
182 
183         // Confirm that ArithmeticException is handled properly
184         // From: 1000.000
185         // To:   333.333
186         df.setMultiplier(3);
187         check("1000.000", new BigDecimal("333.333"));
188 
189         // Confirm that ArithmeticException is handled properly
190         // From: 10000.0000
191         // To:   303.0303
192         df.setMultiplier(33);
193         check("10000.0000", new BigDecimal("303.0303"));
194     }
195 
196     /**
197      * Test for division by zero (BigDecimal)
198      */
test_Parse_in_DecimalFormat_BigDecimal_DivisionByZero()199     void test_Parse_in_DecimalFormat_BigDecimal_DivisionByZero() {
200         df = new DecimalFormat();
201         df.setParseBigDecimal(true);
202         df.setMultiplier(0);
203 
204         // From: 1000.000
205         // To:   Double.POSITIVE_INFINITY
206         check("1000.000", Double.POSITIVE_INFINITY);
207 
208         // From: -1000
209         // To:   Double.NEGATIVE_INFINITY
210         check("-1000", Double.NEGATIVE_INFINITY);
211 
212         // From: -0.00
213         // To:   Double.NaN
214         check("-0.00", Double.NaN);
215     }
216 
217     /**
218      * Test for division by zero (Double)
219      */
test_Parse_in_DecimalFormat_Double_DivisionByZero()220     void test_Parse_in_DecimalFormat_Double_DivisionByZero() {
221         df = new DecimalFormat();
222         df.setParseBigDecimal(false);
223         df.setMultiplier(0);
224 
225         // From: 1000.000
226         // To:   Double.POSITIVE_INFINITY
227         check("1000.000", Double.POSITIVE_INFINITY);
228 
229         // From: -1000.000
230         // To:   Double.NEGATIVE_INFINITY
231         check("-1000.000", Double.NEGATIVE_INFINITY);
232 
233         // From: 0.0
234         // To:   Double.NaN
235         check("0.0", Double.NaN);
236 
237         // From: -0.0 (Double)
238         // To:   Double.NaN
239         check("-0.0", Double.NaN);
240 
241         // From: Double.NaN
242         // To:   Double.NaN
243         check("\ufffd", Double.NaN);
244 
245         // From: Double.POSITIVE_INFINITY
246         // To:   Double.NaN
247         check("\u221e", Double.POSITIVE_INFINITY);
248 
249         // From: Double.NEGATIVE_INFINITY
250         // To:   Double.NaN
251         check("-\u221e", Double.NEGATIVE_INFINITY);
252     }
253 
254     /**
255      * Test for division by zero (Long)
256      */
test_Parse_in_DecimalFormat_Long_DivisionByZero()257     void test_Parse_in_DecimalFormat_Long_DivisionByZero() {
258         df = new DecimalFormat();
259         df.setParseBigDecimal(false);
260         df.setMultiplier(0);
261 
262         // From: 1000
263         // To:   Double.POSITIVE_INFINITY
264         check("1000", Double.POSITIVE_INFINITY);
265 
266         // From: -1000
267         // To:   Double.NEGATIVE_INFINITY
268         check("-1000", Double.NEGATIVE_INFINITY);
269 
270         // From: -000 (Long)
271         // To:   Double.NaN
272         check("-000", Double.NaN);
273     }
274 
275     /**
276      * Test for normal big numbers which don't have the fraction part
277      */
test_Parse_in_DecimalFormat_BigInteger()278     void test_Parse_in_DecimalFormat_BigInteger() {
279         df = new DecimalFormat();
280         df.setParseBigDecimal(true);
281 
282         // From: 123...890
283         // To:   BigDecimal 123...890
284         check(nonsep_int + nonsep_int, new BigDecimal(nonsep_int + nonsep_int));
285 
286         // From: 123,4...7,890
287         // To:   BigDecimal 1234...7890
288         check(sep_int + "," + sep_int, new BigDecimal(nonsep_int + nonsep_int));
289 
290         // From: -000...000123...890
291         // To:   BigDecimal -123...890
292         check("-" + nonsep_zero + nonsep_int, new BigDecimal("-" + nonsep_int));
293 
294         // From: -000,0...0,000,123,4...7,890
295         // To:   BigDecimal -123...890
296         check("-" + sep_zero + "," + sep_int, new BigDecimal("-" + nonsep_int));
297     }
298 
299     /**
300      * Test for normal big numbers which don't have the fraction part with
301      * multiplier
302      */
test_Parse_in_DecimalFormat_BigInteger_usingMultiplier()303     void test_Parse_in_DecimalFormat_BigInteger_usingMultiplier() {
304         df = new DecimalFormat();
305         df.setParseBigDecimal(true);
306 
307         // From: 250,0...0,000
308         // To:   1000...0000
309         df.setMultiplier(250000000);
310         check("250,000,000," + sep_zero, new BigDecimal("1" + nonsep_zero));
311 
312         // From: -250,0...0,000
313         // To:   -1000...0000
314         check("-250,000,000," + sep_zero, new BigDecimal("-1" + nonsep_zero));
315 
316         // From: 250,0...0,000
317         // To:   -1000...0000
318         df.setMultiplier(-250000000);
319         check("250,000,000," + sep_zero, new BigDecimal("-1" + nonsep_zero));
320 
321         // From: -250,0...0,000
322         // To:   1000...0000
323         check("-250,000,000," + sep_zero, new BigDecimal("1" + nonsep_zero));
324 
325         // From: 250,0...0,000E-360
326         // To:   -1000...0000.000...000
327         check("250,000,000," + sep_zero + "," + sep_zero + "E-360",
328               new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));
329 
330         // Confirm that a division which results in a irrational number is done
331         // properly
332         // From: 1000
333         // To:   333
334         df.setMultiplier(3);
335         check("1000", new BigDecimal("333"));
336     }
337 
338     /**
339      * Test for special numbers
340      *    Double.NaN
341      *    Double.POSITIVE_INFINITY
342      *    Double.NEGATIVE_INFINITY
343      */
test_Parse_in_DecimalFormat_SpecialNumber()344     void test_Parse_in_DecimalFormat_SpecialNumber() {
345         df = new DecimalFormat();
346         df.setParseBigDecimal(true);
347 
348         String[] numbers = {
349             "0", "0.0", "25", "25.0", "25.5", "\u221e", "\ufffd",
350             "-0", "-0.0", "-25", "-25.0", "-25.5", "-\u221e",
351         };
352         int multipliers[] = {5, -5};
353         Number[][] expected = {
354             {
355                 new BigDecimal("0"), new BigDecimal("0.0"), new BigDecimal("5"),
356                 new BigDecimal("5.0"), new BigDecimal("5.1"),
357                 Double.POSITIVE_INFINITY, Double.NaN,
358                 new BigDecimal("0"), new BigDecimal("0.0"),
359                 new BigDecimal("-5"), new BigDecimal("-5.0"),
360                 new BigDecimal("-5.1"),
361                 Double.NEGATIVE_INFINITY, Double.NaN,
362             },
363             {
364                 new BigDecimal("0"), new BigDecimal("0.0"),
365                 new BigDecimal("-5"), new BigDecimal("-5.0"),
366                 new BigDecimal("-5.1"),
367                 Double.NEGATIVE_INFINITY, Double.NaN,
368                 new BigDecimal("0"), new BigDecimal("0.0"), new BigDecimal("5"),
369                 new BigDecimal("5.0"), new BigDecimal("5.1"),
370                 Double.POSITIVE_INFINITY,
371             },
372         };
373 
374         for (int i = 0; i < multipliers.length; i++) {
375             df.setMultiplier(multipliers[i]);
376             for (int j = 0; j < numbers.length; j++) {
377                 check(String.valueOf(numbers[j]), expected[i][j]);
378             }
379         }
380     }
381 
382     /**
383      * Test for special numbers
384      */
test_Parse_in_DecimalFormat_Other()385     void test_Parse_in_DecimalFormat_Other() {
386         df = new DecimalFormat();
387         df.setParseBigDecimal(true);
388 
389         String[] numbers = {
390             "-9223372036854775808",     // Long.MIN_VALUE
391         };
392         int multipliers[] = {1, -1};
393         String[][] expected = {
394             {"-9223372036854775808"},   // Long.MIN_VALUE
395             {"9223372036854775808"},    // Long.MAX_VALUE+1 = abs(MIN_VALUE)
396         };
397 
398         for (int i = 0; i < multipliers.length; i++) {
399             df.setMultiplier(multipliers[i]);
400             for (int j = 0; j < numbers.length; j++) {
401                 check(String.valueOf(numbers[j]),
402                       new BigDecimal(expected[i][j]));
403             }
404         }
405     }
406 
407     static final String[] patterns = {
408         "  {0, number}  ",
409         "  {0, number}  ",
410         "  {0, number, currency}  ",
411         "  {0, number, currency}  ",
412         "  {0, number, percent}  ",
413         "  {0, number, percent}  ",
414         "  {0, number,#,##0.###E0}  ",
415         "  {0, number,#,##0.###E0}  ",
416 
417         "  {0, number}  ",
418         "  {0, number}  ",
419         "  {0, number, integer}  ",
420         "  {0, number, integer}  ",
421         "  {0, number, currency}  ",
422         "  {0, number, currency}  ",
423         "  {0, number, percent}  ",
424         "  {0, number, percent}  ",
425         "  {0, number,#,##0.###E0}  ",
426         "  {0, number,#,##0.###E0}  ",
427     };
428     static final String[] from = {
429         "  12,345,678,901,234,567,890.98765432109876543210987654321  ",
430         "  -12,345,678,901,234,567,890.98765432109876543210987654321  ",
431         "  $12,345,678,901,234,567,890.98765432109876543210987654321  ",
432         "  ($12,345,678,901,234,567,890.98765432109876543210987654321)  ",
433         "  1,234,567,890,123,456,789,098.76543210987654321098765432100%  ",
434         "  -1,234,567,890,123,456,789,098.76543210987654321098765432100%  ",
435         "  12,345,678,901,234,567,890.98765432109876543210987654321E-20  ",
436         "  -12,345,678,901,234,567,890.98765432109876543210987654321E-20  ",
437 
438         "  9,876,543,210,987,654,321,098,765,432,109,876,543,210  ",
439         "  -9,876,543,210,987,654,321,098,765,432,109,876,543,210  ",
440         "  9,876,543,210,987,654,321,098,765,432,109,876,543,210E5  ",
441         "  -9,876,543,210,987,654,321,098,765,432,109,876,543,210E-5  ",
442         "  $9,876,543,210,987,654,321,098,765,432,109,876,543,210.00  ",
443         "  ($9,876,543,210,987,654,321,098,765,432,109,876,543,210.00)  ",
444         "  987,654,321,098,765,432,109,876,543,210,987,654,321,012%  ",
445         "  -987,654,321,098,765,432,109,876,543,210,987,654,321,012%  ",
446         "  98,765,432,109,876,543,210.98765432109876543210E20  ",
447         "  -987,654,321,098,765,432,109,876,543,210,987,654,321,000,000,000,000,000,000,000E-20  ",
448     };
449 
450     static final String[] expected1 = { // isParseIntegerOnly() == false
451         "12345678901234567890.98765432109876543210987654321",
452         "-12345678901234567890.98765432109876543210987654321",
453         "12345678901234567890.98765432109876543210987654321",
454         "-12345678901234567890.98765432109876543210987654321",
455         "12345678901234567890.98765432109876543210987654321",
456         "-12345678901234567890.98765432109876543210987654321",
457         "0.1234567890123456789098765432109876543210987654321",
458         "-0.1234567890123456789098765432109876543210987654321",
459 
460         "9876543210987654321098765432109876543210",
461         "-9876543210987654321098765432109876543210",
462         "9.876543210987654321098765432109876543210E44",
463         "-98765432109876543210987654321098765.43210",
464         "9876543210987654321098765432109876543210.00",
465         "-9876543210987654321098765432109876543210.00",
466         "9876543210987654321098765432109876543210.12",
467         "-9876543210987654321098765432109876543210.12",
468         "9876543210987654321098765432109876543210",
469         "-9876543210987654321098765432109876543210.00000000000000000000",
470     };
471     static final int[] parsePosition1 = {
472         60, 61, 61, 63, 64, 65, 64, 65,
473         57, 58, 59, 61, 61, 63, 60, 61, 54, 88,
474     };
475 
476     /**
477      * Test for MessageFormat: setParseIntegerOnly(false)
478      */
test_Parse_in_MessageFormat_NotParseIntegerOnly()479     void test_Parse_in_MessageFormat_NotParseIntegerOnly() {
480         for (int i=0; i < patterns.length; i++) {
481             pp = new ParsePosition(0);
482             Object[] parsed = null;
483 
484             try {
485                 MessageFormat mf = new MessageFormat(patterns[i]);
486                 Format[] formats = mf.getFormats();
487                 for (int j=0; j < formats.length; j++) {
488                     ((DecimalFormat)formats[j]).setParseBigDecimal(true);
489                 }
490 
491                 parsed = mf.parse(from[i], pp);
492 
493                 if (pp.getErrorIndex() != -1) {
494                     errln("Case" + (i+1) +
495                           ": getErrorIndex() returns wrong value. expected:-1, got:"+
496                           pp.getErrorIndex() + " for " + from[i]);
497                 }
498                 if (pp.getIndex() != parsePosition1[i]) {
499                     errln("Case" + (i+1) +
500                           ": getIndex() returns wrong value. expected:" +
501                           parsePosition1[i] + ", got:"+ pp.getIndex() +
502                           " for " + from[i]);
503                 }
504             }
505             catch(Exception e) {
506                 errln("Unexpected exception: " + e.getMessage());
507             }
508 
509             checkType(from[i], getType(new BigDecimal(expected1[i])),
510                       getType((Number)parsed[0]));
511             checkParse(from[i], new BigDecimal(expected1[i]),
512                        (Number)parsed[0]);
513         }
514     }
515 
516     static final String[] expected2 = { // isParseIntegerOnly() == true
517         "12345678901234567890",
518         "-12345678901234567890",
519         "12345678901234567890",
520         "-12345678901234567890",
521         "12345678901234567890",
522         "-12345678901234567890",
523         "0",
524         "0",
525 
526         "9876543210987654321098765432109876543210",
527         "-9876543210987654321098765432109876543210",
528         "9.876543210987654321098765432109876543210E44",
529         "-98765432109876543210987654321098765.43210",
530         "9876543210987654321098765432109876543210",
531         "-9876543210987654321098765432109876543210",
532         "9876543210987654321098765432109876543210.12",
533         "-9876543210987654321098765432109876543210.12",
534         "9876543210987654321098765432109876543210",
535         "-9876543210987654321098765432109876543210.00000000000000000000",
536     };
537     static final int[][] parsePosition2 = {     // {errorIndex, index}
538         /*
539          * Should keep in mind that the expected result is different from
540          * DecimalFormat.parse() for some cases.
541          */
542         {28, 0},        // parsing stopped at '.'
543         {29, 0},        // parsing stopped at '.'
544         {29, 0},        // parsing stopped at '.'
545         {2, 0},         // parsing stopped at '(' because cannot find ')'
546         {2, 0},         // parsing stopped at the first numeric
547                         // because cannot find '%'
548         {2, 0},         // parsing stopped at the first numeric
549                         // because cannot find '%'
550         {28, 0},        // parsing stopped at '.'
551         {29, 0},        // parsing stopped at '.'
552 
553         {-1, 57}, {-1, 58}, {-1, 59}, {-1, 61},
554         {56, 0},        // parsing stopped at '.'
555                         // because cannot find '%'
556         {2, 0},         // parsing stopped at '(' because cannot find ')'
557         {-1, 60}, {-1, 61},
558         {28, 0},        // parsing stopped at '.'
559         {-1, 88},
560     };
561 
562     /**
563      * Test for MessageFormat: setParseIntegerOnly(true)
564      */
test_Parse_in_MessageFormat_ParseIntegerOnly()565     void test_Parse_in_MessageFormat_ParseIntegerOnly() {
566         for (int i=0; i < patterns.length; i++) {
567             pp = new ParsePosition(0);
568             Object[] parsed = null;
569 
570             try {
571                 MessageFormat mf = new MessageFormat(patterns[i]);
572                 Format[] formats = mf.getFormats();
573                 for (int j=0; j < formats.length; j++) {
574                     ((DecimalFormat)formats[j]).setParseBigDecimal(true);
575                     ((DecimalFormat)formats[j]).setParseIntegerOnly(true);
576                 }
577 
578                 parsed = mf.parse(from[i], pp);
579 
580                 if (pp.getErrorIndex() != parsePosition2[i][0]) {
581                     errln("Case" + (i+1) +
582                           ": getErrorIndex() returns wrong value. expected:" +
583                           parsePosition2[i][0] + ", got:"+ pp.getErrorIndex() +
584                           " for " + from[i]);
585                 }
586                 if (pp.getIndex() != parsePosition2[i][1]) {
587                     errln("Case" + (i+1) +
588                           ": getIndex() returns wrong value. expected:" +
589                           parsePosition2[i][1] + ", got:"+ pp.getIndex() +
590                           " for " + from[i]);
591                 }
592             }
593             catch(Exception e) {
594                 errln("Unexpected exception: " + e.getMessage());
595             }
596 
597             if (parsePosition2[i][0] == -1) {
598                 checkType(from[i], getType(new BigDecimal(expected2[i])),
599                           getType((Number)parsed[0]));
600                 checkParse(from[i], new BigDecimal(expected2[i]),
601                            (Number)parsed[0]);
602             }
603         }
604     }
605 
606     static final String[] from3 = {
607         "12,345,678,901,234,567,890.98765432109876543210987654321",
608         "-12,345,678,901,234,567,890.98765432109876543210987654321",
609         "9,876,543,210,987,654,321,098,765,432,109,876,543,210",
610         "-9,876,543,210,987,654,321,098,765,432,109,876,543,210",
611         "1234556790000E-8",
612     };
613     static final String[] expected3 = {
614         "12345678901234567890",
615         "-12345678901234567890",
616         "9876543210987654321098765432109876543210",
617         "-9876543210987654321098765432109876543210",
618         "12345.56790000",
619     };
620     static final int[][] parsePosition3 = {     // {errorIndex, index}
621         {-1, 26},
622         {-1, 27},
623         {-1, 53},
624         {-1, 54},
625         {-1, 16},
626     };
627 
628     /**
629      * Test for DecimalFormat: setParseIntegerOnly(true)
630      */
test_Parse_in_DecimalFormat_ParseIntegerOnly()631     void test_Parse_in_DecimalFormat_ParseIntegerOnly() {
632         DecimalFormat df = (DecimalFormat)NumberFormat.getIntegerInstance();
633         df.setParseBigDecimal(true);
634 
635         for (int i=0; i < from3.length; i++) {
636             pp = new ParsePosition(0);
637             Number parsed = null;
638 
639             try {
640                 parsed = df.parse(from3[i], pp);
641 
642                 if (pp.getErrorIndex() != parsePosition3[i][0]) {
643                     errln("Case" + (i+1) +
644                           ": getErrorIndex() returns wrong value. expected:" +
645                           parsePosition3[i][0] + ", got:"+ pp.getErrorIndex() +
646                           " for " + from3[i]);
647                 }
648                 if (pp.getIndex() != parsePosition3[i][1]) {
649                     errln("Case" + (i+1) +
650                           ": getIndex() returns wrong value. expected:" +
651                           parsePosition3[i][1] + ", got:"+ pp.getIndex() +
652                           " for " + from3[i]);
653                 }
654             }
655             catch(Exception e) {
656                 errln("Unexpected exception: " + e.getMessage());
657             }
658 
659             if (parsePosition3[i][0] == -1) {
660                 checkType(from3[i], getType(new BigDecimal(expected3[i])),
661                           getType(parsed));
662                 checkParse(from3[i], new BigDecimal(expected3[i]), parsed);
663             }
664         }
665     }
666 
check(String from, Number to)667     protected void check(String from, Number to) {
668         pp = new ParsePosition(0);
669         try {
670             parsed = df.parse(from, pp);
671         }
672         catch(Exception e) {
673             exceptionOccurred = true;
674             errln(e.getMessage());
675         }
676         if (!exceptionOccurred) {
677             checkParse(from, to, parsed);
678             checkType(from, getType(to), getType(parsed));
679             checkParsePosition(from, from.length(), pp.getIndex());
680         }
681     }
682 
checkParse(String orig, Number expected, Number got)683     private void checkParse(String orig, Number expected, Number got) {
684         if (!expected.equals(got)) {
685             errln("Parsing... failed." +
686                   "\n   original: " + orig +
687                   "\n   parsed:   " + got +
688                   "\n   expected: " + expected + "\n");
689         }
690     }
691 
checkType(String orig, String expected, String got)692     private void checkType(String orig, String expected, String got) {
693         if (!expected.equals(got)) {
694             errln("Parsing... unexpected Class returned." +
695                   "\n   original: " + orig +
696                   "\n   got:      " + got +
697                   "\n   expected: " + expected + "\n");
698         }
699     }
700 
checkParsePosition(String orig, int expected, int got)701     private void checkParsePosition(String orig, int expected, int got) {
702         if (expected != got) {
703             errln("Parsing... wrong ParsePosition returned." +
704                   "\n   original: " + orig +
705                   "\n   got:      " + got +
706                   "\n   expected: " + expected + "\n");
707         }
708     }
709 
getType(Number number)710     private String getType(Number number) {
711         return number.getClass().getName();
712     }
713 }
714