Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/math/BigDecimal/StringConstructor.java
38811 views
/*1* Copyright (c) 1999, 2005, 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 4103117 4331084 4488017 4490929 6255285 626836526* @summary Tests the BigDecimal string constructor.27* @key randomness28*/2930import java.math.*;31import java.util.Random;3233public class StringConstructor {3435private static int seed = new Random().nextInt();36private static Random rnd = new Random(seed);3738public static void main(String[] args) throws Exception {39constructWithError("");40constructWithError("+");41constructWithError("-");42constructWithError("+e");43constructWithError("-e");44constructWithError("e+");45constructWithError("1.-0");46constructWithError(".-123");47constructWithError("-");48constructWithError("--1.1");49constructWithError("-+1.1");50constructWithError("+-1.1");51constructWithError("1-.1");52constructWithError("1+.1");53constructWithError("1.111+1");54constructWithError("1.111-1");55constructWithError("11.e+");56constructWithError("11.e-");57constructWithError("11.e+-");58constructWithError("11.e-+");59constructWithError("11.e-+1");60constructWithError("11.e+-1");6162// Range checks63constructWithError("1e"+Integer.MIN_VALUE);64constructWithError("10e"+Integer.MIN_VALUE);65constructWithError("0.01e"+Integer.MIN_VALUE);66constructWithError("1e"+((long)Integer.MIN_VALUE-1));67constructWithError("1e"+((long)Integer.MAX_VALUE + 1));6869leadingExponentZeroTest();70nonAsciiZeroTest();7172// Roundtrip tests73for (int i=0; i<100; i++) {74int size = rnd.nextInt(100) + 1;75BigInteger bi = new BigInteger(size, rnd);76if (rnd.nextBoolean())77bi = bi.negate();78int decimalLength = bi.toString().length();79int scale = rnd.nextInt(decimalLength);80BigDecimal bd = new BigDecimal(bi, scale);81String bdString = bd.toString();82// System.err.println("bi" + bi.toString() + "\tscale " + scale);83// System.err.println("bd string: " + bdString);84BigDecimal bdDoppel = new BigDecimal(bdString);85if (!bd.equals(bdDoppel)) {86System.err.println("Random number seed = " + seed);87System.err.println("bd string: scale: " + bd.scale() +88"\t" + bdString);89System.err.println("bd doppel: scale: " + bdDoppel.scale() +90"\t" + bdDoppel.toString());91throw new RuntimeException("String constructor failure.");92}93}94}959697/*98* Verify precision is set properly if the significand has99* non-ASCII leading zeros.100*/101private static void nonAsciiZeroTest() {102String values[] = {103"00004e5",104"\u0660\u0660\u0660\u06604e5",105};106107BigDecimal expected = new BigDecimal("4e5");108109for(String s : values) {110BigDecimal tmp = new BigDecimal(s);111// System.err.println("Testing " + s);112if (! expected.equals(tmp) || tmp.precision() != 1) {113System.err.println("Bad conversion of " + s + "got " +114tmp + "precision = " + tmp.precision());115throw new RuntimeException("String constructor failure.");116}117}118119}120121private static void leadingExponentZeroTest() {122BigDecimal twelve = new BigDecimal("12");123BigDecimal onePointTwo = new BigDecimal("1.2");124125String start = "1.2e0";126String end = "1";127String middle = "";128129// Test with more excess zeros than the largest number of130// decimal digits needed to represent a long131int limit = ((int)Math.log10(Long.MAX_VALUE)) + 6;132for(int i = 0; i < limit; i++, middle += "0") {133String t1 = start + middle;134String t2 = t1 + end;135136// System.out.println(i + "\t" + t1 + "\t" + t2);137testString(t1, onePointTwo);138testString(t2, twelve);139}140}141142private static void testString(String s, BigDecimal expected) {143testString0(s, expected);144testString0(switchZero(s), expected);145}146147private static void testString0(String s, BigDecimal expected) {148if (!expected.equals(new BigDecimal(s)))149throw new RuntimeException(s + " is not equal to " + expected);150}151152private static String switchZero(String s) {153return s.replace('0', '\u0660'); // Arabic-Indic zero154}155156private static void constructWithError(String badString) {157try {158BigDecimal d = new BigDecimal(badString);159throw new RuntimeException(badString + " accepted");160} catch(NumberFormatException e) {161}162}163}164165166