Path: blob/master/test/jdk/java/text/Format/NumberFormat/BigDecimalCompatibilityTest.java
41152 views
/*1* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 401893726* @summary Confirm that DecimalFormat.parse() parses BigDecimal and BigInteger as expected.27*/2829import java.math.*;30import java.text.*;31import java.util.*;3233public class BigDecimalCompatibilityTest {3435static boolean err = false;3637static final String[] input_data = {38"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",39"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"40};41static final String[] exponents = {42"E-100", "E100", "E-900", "E900", ""43};44static final int[] multipliers = {45-1, 1, -100, 100, -9999, 999946};4748public static void main(String[] args) throws Exception {49Locale loc = Locale.getDefault();50Locale.setDefault(Locale.US);5152testBigDecimal();53testBigInteger();5455Locale.setDefault(loc);5657if (err) {58throw new RuntimeException("Error: Unexpected value");59}60}6162static private void testBigDecimal() {63DecimalFormat df = new DecimalFormat();64df.setParseBigDecimal(true);65df.setMaximumFractionDigits(Integer.MAX_VALUE);6667for (int i = 0; i < input_data.length; i++) {68for (int j = 0; j < input_data.length; j++) {69for (int k = 0; k < input_data.length; k++) {70for (int l = 0; l < input_data.length; l++) {71for (int m = 0; m < exponents.length; m++) {72String s = input_data[i] + input_data[j] + '.' +73input_data[k] + input_data[l] +74exponents[m];75for (int n = 0; n < multipliers.length; n++) {76test(df, s, multipliers[n]);77test(df, '-'+s, multipliers[n]);78}79}80}81}82}83}84}8586static private void testBigInteger() {87DecimalFormat df = new DecimalFormat();88df.setParseBigDecimal(true);89df.setMaximumFractionDigits(Integer.MAX_VALUE);9091for (int i = 0; i < input_data.length; i++) {92for (int j = 0; j < input_data.length; j++) {93String s = input_data[i] + input_data[j];94for (int k = 0; k < multipliers.length; k++) {95test(df, s, multipliers[k]);96test(df, '-'+s, multipliers[k]);97}98}99}100}101102static void test(DecimalFormat df, String s, int multiplier) {103df.setMultiplier(multiplier);104105Number num = null;106try {107num = df.parse(s);108}109catch (ParseException e) {110err = true;111System.err.println("Failed: Exception occurred: " + e.getMessage());112return;113}114115BigDecimal bd = new BigDecimal(s);116try {117bd = bd.divide(new BigDecimal(multiplier));118}119catch (ArithmeticException e) {120bd = bd.divide(new BigDecimal(multiplier), RoundingMode.HALF_EVEN);121}122check(num, bd, multiplier);123}124125static void check(Number got, BigDecimal expected, int multiplier) {126if (!got.equals(expected)) {127err = true;128System.err.println("Failed: got:" + got +129", expected: " + expected +130", multiplier=" + multiplier);131}132}133}134135136