author | malenkov |
Fri, 16 May 2014 15:51:57 +0400 | |
changeset 25088 | 8d4b058368f0 |
parent 24030 | 1b54fabeee2e |
permissions | -rw-r--r-- |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
1 |
/* |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
4 |
* |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
8 |
* |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
13 |
* accompanied this code). |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
14 |
* |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
18 |
* |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
21 |
* questions. |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
22 |
*/ |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
23 |
|
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
24 |
/* |
24030 | 25 |
* @test |
26 |
* @ignore This test has huge memory requirements |
|
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
27 |
* @bug 8021204 |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
28 |
* @summary Test constructor BigInteger(String val, int radix) on very long string |
24030 | 29 |
* @run main/othervm -Xshare:off -Xmx8g StringConstructorOverflow |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
30 |
* @author Dmitry Nadezhin |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
31 |
*/ |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
32 |
import java.math.BigInteger; |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
33 |
|
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
34 |
public class StringConstructorOverflow { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
35 |
|
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
36 |
// String with hexadecimal value pow(2,pow(2,34))+1 |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
37 |
private static String makeLongHexString() { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
38 |
StringBuilder sb = new StringBuilder(); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
39 |
sb.append('1'); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
40 |
for (int i = 0; i < (1 << 30) - 1; i++) { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
41 |
sb.append('0'); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
42 |
} |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
43 |
sb.append('1'); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
44 |
return sb.toString(); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
45 |
} |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
46 |
|
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
47 |
public static void main(String[] args) { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
48 |
try { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
49 |
BigInteger bi = new BigInteger(makeLongHexString(), 16); |
24030 | 50 |
if (bi.compareTo(BigInteger.ONE) <= 0) { |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
51 |
throw new RuntimeException("Incorrect result " + bi.toString()); |
24030 | 52 |
} |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
53 |
} catch (ArithmeticException e) { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
54 |
// expected |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
55 |
System.out.println("Overflow is reported by ArithmeticException, as expected"); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
56 |
} catch (OutOfMemoryError e) { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
57 |
// possible |
24030 | 58 |
System.err.println("StringConstructorOverflow skipped: OutOfMemoryError"); |
59 |
System.err.println("Run jtreg with -javaoption:-Xmx8g"); |
|
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
60 |
} |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
61 |
} |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
62 |
} |