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