Path: blob/master/test/jdk/java/math/BigDecimal/StringConstructor.java
41149 views
/*1* Copyright (c) 1999, 2017, 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* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main StringConstructor28* @bug 4103117 4331084 4488017 4490929 6255285 6268365 8074460 807867229* @summary Tests the BigDecimal string constructor (use -Dseed=X to set PRNG seed).30* @key randomness31*/3233import java.math.*;34import java.util.Random;35import jdk.test.lib.RandomFactory;3637public class StringConstructor {3839public static void main(String[] args) throws Exception {40constructWithError("");41constructWithError("+");42constructWithError("-");43constructWithError("+e");44constructWithError("-e");45constructWithError("e+");46constructWithError("1.-0");47constructWithError(".-123");48constructWithError("-");49constructWithError("--1.1");50constructWithError("-+1.1");51constructWithError("+-1.1");52constructWithError("1-.1");53constructWithError("1+.1");54constructWithError("1.111+1");55constructWithError("1.111-1");56constructWithError("11.e+");57constructWithError("11.e-");58constructWithError("11.e+-");59constructWithError("11.e-+");60constructWithError("11.e-+1");61constructWithError("11.e+-1");6263// Range checks64constructWithError("1e"+Integer.MIN_VALUE);65constructWithError("10e"+Integer.MIN_VALUE);66constructWithError("0.01e"+Integer.MIN_VALUE);67constructWithError("1e"+((long)Integer.MIN_VALUE-1));68constructWithError("1e"+((long)Integer.MAX_VALUE + 1));6970leadingExponentZeroTest();71nonAsciiZeroTest();7273// Roundtrip tests74Random random = RandomFactory.getRandom();75for (int i=0; i<100; i++) {76int size = random.nextInt(100) + 1;77BigInteger bi = new BigInteger(size, random);78if (random.nextBoolean())79bi = bi.negate();80int decimalLength = bi.toString().length();81int scale = random.nextInt(decimalLength);82BigDecimal bd = new BigDecimal(bi, scale);83String bdString = bd.toString();84// System.err.println("bi" + bi.toString() + "\tscale " + scale);85// System.err.println("bd string: " + bdString);86BigDecimal bdDoppel = new BigDecimal(bdString);87if (!bd.equals(bdDoppel)) {88System.err.println("bd string: scale: " + bd.scale() +89"\t" + bdString);90System.err.println("bd doppel: scale: " + bdDoppel.scale() +91"\t" + bdDoppel.toString());92throw new RuntimeException("String constructor failure.");93}94}95}969798/*99* Verify precision is set properly if the significand has100* non-ASCII leading zeros.101*/102private static void nonAsciiZeroTest() {103String values[] = {104"00004e5",105"\u0660\u0660\u0660\u06604e5",106};107108BigDecimal expected = new BigDecimal("4e5");109110for(String s : values) {111BigDecimal tmp = new BigDecimal(s);112// System.err.println("Testing " + s);113if (! expected.equals(tmp) || tmp.precision() != 1) {114System.err.println("Bad conversion of " + s + "got " +115tmp + "precision = " + tmp.precision());116throw new RuntimeException("String constructor failure.");117}118}119120}121122private static void leadingExponentZeroTest() {123BigDecimal twelve = new BigDecimal("12");124BigDecimal onePointTwo = new BigDecimal("1.2");125126String start = "1.2e0";127String end = "1";128String middle = "";129130// Test with more excess zeros than the largest number of131// decimal digits needed to represent a long132int limit = ((int)Math.log10(Long.MAX_VALUE)) + 6;133for(int i = 0; i < limit; i++, middle += "0") {134String t1 = start + middle;135String t2 = t1 + end;136137// System.out.println(i + "\t" + t1 + "\t" + t2);138testString(t1, onePointTwo);139testString(t2, twelve);140}141}142143private static void testString(String s, BigDecimal expected) {144testString0(s, expected);145testString0(switchZero(s), expected);146}147148private static void testString0(String s, BigDecimal expected) {149if (!expected.equals(new BigDecimal(s)))150throw new RuntimeException(s + " is not equal to " + expected);151}152153private static String switchZero(String s) {154return s.replace('0', '\u0660'); // Arabic-Indic zero155}156157private static void constructWithError(String badString) {158try {159BigDecimal d = new BigDecimal(badString);160throw new RuntimeException(badString + " accepted");161} catch(NumberFormatException e) {162}163}164}165166167