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
  27  * @summary Confirm that DecimalFormat.parse() parses BigDecimal and BigInteger as expected.
  28  */
  29 
  30 import java.math.*;
  31 import java.text.*;
  32 import java.util.*;
  33 
  34 public class BigDecimalCompatibilityTest {
  35 
  36     static boolean err = false;
  37 
  38     static final String[] input_data = {
  39         "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  40         "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
  41     };
  42     static final String[] exponents = {
  43         "E-100", "E100", "E-900", "E900", ""
  44     };
  45     static final int[] multipliers = {
  46         -1, 1, -100, 100, -9999, 9999
  47     };
  48 
  49     public static void main(String[] args) throws Exception {
  50         Locale loc = Locale.getDefault();
  51         Locale.setDefault(Locale.US);
  52 
  53         testBigDecimal();
  54         testBigInteger();
  55 
  56         Locale.setDefault(loc);
  57 
  58         if (err) {
  59             throw new RuntimeException("Error: Unexpected value");
  60         }
  61     }
  62 
  63     static private void testBigDecimal() {
  64         DecimalFormat df = new DecimalFormat();
  65         df.setParseBigDecimal(true);
  66         df.setMaximumFractionDigits(Integer.MAX_VALUE);
  67 
  68         for (int i = 0; i < input_data.length; i++) {
  69             for (int j = 0; j < input_data.length; j++) {
  70                 for (int k = 0; k < input_data.length; k++) {
  71                     for (int l = 0; l < input_data.length; l++) {
  72                         for (int m = 0; m < exponents.length; m++) {
  73                             String s = input_data[i] + input_data[j] + '.' +
  74                                        input_data[k] + input_data[l] +
  75                                        exponents[m];
  76                             for (int n = 0; n < multipliers.length; n++) {
  77                                 test(df, s, multipliers[n]);
  78                                 test(df, '-'+s, multipliers[n]);
  79                             }
  80                         }
  81                     }
  82                 }
  83             }
  84         }
  85     }
  86 
  87     static private void testBigInteger() {
  88         DecimalFormat df = new DecimalFormat();
  89         df.setParseBigDecimal(true);
  90         df.setMaximumFractionDigits(Integer.MAX_VALUE);
  91 
  92         for (int i = 0; i < input_data.length; i++) {
  93             for (int j = 0; j < input_data.length; j++) {
  94                 String s = input_data[i] + input_data[j];
  95                 for (int k = 0; k < multipliers.length; k++) {
  96                     test(df, s, multipliers[k]);
  97                     test(df, '-'+s, multipliers[k]);
  98                 }
  99             }
 100         }
 101     }
 102 
 103     static void test(DecimalFormat df, String s, int multiplier) {
 104         df.setMultiplier(multiplier);
 105 
 106         Number num = null;
 107         try {
 108             num = df.parse(s);
 109         }
 110         catch (ParseException e) {
 111             err = true;
 112             System.err.println("Failed: Exception occurred: " + e.getMessage());
 113             return;
 114         }
 115 
 116         BigDecimal bd = new BigDecimal(s);
 117         try {
 118            bd = bd.divide(new BigDecimal(multiplier));
 119         }
 120         catch (ArithmeticException e) {
 121            bd = bd.divide(new BigDecimal(multiplier), RoundingMode.HALF_EVEN);
 122         }
 123         check(num, bd, multiplier);
 124     }
 125 
 126     static void check(Number got, BigDecimal expected, int multiplier) {
 127         if (!got.equals(expected)) {
 128             err = true;
 129             System.err.println("Failed: got:" + got +
 130                                ", expected: " + expected +
 131                                ", multiplier=" + multiplier);
 132         }
 133     }
 134 }