1 /* 2 * Copyright (c) 1999, 2017, 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 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run main StringConstructor 29 * @bug 4103117 4331084 4488017 4490929 6255285 6268365 8074460 8078672 30 * @summary Tests the BigDecimal string constructor (use -Dseed=X to set PRNG seed). 31 * @key randomness 32 */ 33 34 import java.math.*; 35 import java.util.Random; 36 import jdk.test.lib.RandomFactory; 37 38 public class StringConstructor { 39 40 public static void main(String[] args) throws Exception { 41 constructWithError(""); 42 constructWithError("+"); 43 constructWithError("-"); 44 constructWithError("+e"); 45 constructWithError("-e"); 46 constructWithError("e+"); 47 constructWithError("1.-0"); 48 constructWithError(".-123"); 49 constructWithError("-"); 50 constructWithError("--1.1"); 51 constructWithError("-+1.1"); 52 constructWithError("+-1.1"); 53 constructWithError("1-.1"); 54 constructWithError("1+.1"); 55 constructWithError("1.111+1"); 56 constructWithError("1.111-1"); 57 constructWithError("11.e+"); 58 constructWithError("11.e-"); 59 constructWithError("11.e+-"); 60 constructWithError("11.e-+"); 61 constructWithError("11.e-+1"); 62 constructWithError("11.e+-1"); 63 64 // Range checks 65 constructWithError("1e"+Integer.MIN_VALUE); 66 constructWithError("10e"+Integer.MIN_VALUE); 67 constructWithError("0.01e"+Integer.MIN_VALUE); 68 constructWithError("1e"+((long)Integer.MIN_VALUE-1)); 69 constructWithError("1e"+((long)Integer.MAX_VALUE + 1)); 70 71 leadingExponentZeroTest(); 72 nonAsciiZeroTest(); 73 74 // Roundtrip tests 75 Random random = RandomFactory.getRandom(); 76 for (int i=0; i<100; i++) { 77 int size = random.nextInt(100) + 1; 78 BigInteger bi = new BigInteger(size, random); 79 if (random.nextBoolean()) 80 bi = bi.negate(); 81 int decimalLength = bi.toString().length(); 82 int scale = random.nextInt(decimalLength); 83 BigDecimal bd = new BigDecimal(bi, scale); 84 String bdString = bd.toString(); 85 // System.err.println("bi" + bi.toString() + "\tscale " + scale); 86 // System.err.println("bd string: " + bdString); 87 BigDecimal bdDoppel = new BigDecimal(bdString); 88 if (!bd.equals(bdDoppel)) { 89 System.err.println("bd string: scale: " + bd.scale() + 90 "\t" + bdString); 91 System.err.println("bd doppel: scale: " + bdDoppel.scale() + 92 "\t" + bdDoppel.toString()); 93 throw new RuntimeException("String constructor failure."); 94 } 95 } 96 } 97 98 99 /* 100 * Verify precision is set properly if the significand has 101 * non-ASCII leading zeros. 102 */ 103 private static void nonAsciiZeroTest() { 104 String values[] = { 105 "00004e5", 106 "\u0660\u0660\u0660\u06604e5", 107 }; 108 109 BigDecimal expected = new BigDecimal("4e5"); 110 111 for(String s : values) { 112 BigDecimal tmp = new BigDecimal(s); 113 // System.err.println("Testing " + s); 114 if (! expected.equals(tmp) || tmp.precision() != 1) { 115 System.err.println("Bad conversion of " + s + "got " + 116 tmp + "precision = " + tmp.precision()); 117 throw new RuntimeException("String constructor failure."); 118 } 119 } 120 121 } 122 123 private static void leadingExponentZeroTest() { 124 BigDecimal twelve = new BigDecimal("12"); 125 BigDecimal onePointTwo = new BigDecimal("1.2"); 126 127 String start = "1.2e0"; 128 String end = "1"; 129 String middle = ""; 130 131 // Test with more excess zeros than the largest number of 132 // decimal digits needed to represent a long 133 int limit = ((int)Math.log10(Long.MAX_VALUE)) + 6; 134 for(int i = 0; i < limit; i++, middle += "0") { 135 String t1 = start + middle; 136 String t2 = t1 + end; 137 138 // System.out.println(i + "\t" + t1 + "\t" + t2); 139 testString(t1, onePointTwo); 140 testString(t2, twelve); 141 } 142 } 143 144 private static void testString(String s, BigDecimal expected) { 145 testString0(s, expected); 146 testString0(switchZero(s), expected); 147 } 148 149 private static void testString0(String s, BigDecimal expected) { 150 if (!expected.equals(new BigDecimal(s))) 151 throw new RuntimeException(s + " is not equal to " + expected); 152 } 153 154 private static String switchZero(String s) { 155 return s.replace('0', '\u0660'); // Arabic-Indic zero 156 } 157 158 private static void constructWithError(String badString) { 159 try { 160 BigDecimal d = new BigDecimal(badString); 161 throw new RuntimeException(badString + " accepted"); 162 } catch(NumberFormatException e) { 163 } 164 } 165 }