author | malenkov |
Fri, 16 May 2014 15:51:57 +0400 | |
changeset 25088 | 8d4b058368f0 |
parent 24128 | 36469eed1629 |
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 |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
26 |
* @bug 6910473 |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
27 |
* @summary Test that bitLength() is not negative |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
28 |
* @author Dmitry Nadezhin |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
29 |
*/ |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
30 |
import java.math.BigInteger; |
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 |
public class BitLengthOverflow { |
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 static void main(String[] args) { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
35 |
try { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
36 |
BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE); // x = pow(2,Integer.MAX_VALUE) |
24030 | 37 |
if (x.bitLength() != (1L << 31)) { |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
38 |
throw new RuntimeException("Incorrect bitLength() " + x.bitLength()); |
24030 | 39 |
} |
21420
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
40 |
System.out.println("Surprisingly passed with correct bitLength() " + x.bitLength()); |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
41 |
} catch (ArithmeticException e) { |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
42 |
// expected |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
43 |
System.out.println("Overflow is reported by ArithmeticException, as expected"); |
24128
36469eed1629
8041683: Catch OutOfMemoryError in BitLengthOverflow and DoubleValueOverflow
bpb
parents:
24030
diff
changeset
|
44 |
} catch (OutOfMemoryError e) { |
36469eed1629
8041683: Catch OutOfMemoryError in BitLengthOverflow and DoubleValueOverflow
bpb
parents:
24030
diff
changeset
|
45 |
// possible |
36469eed1629
8041683: Catch OutOfMemoryError in BitLengthOverflow and DoubleValueOverflow
bpb
parents:
24030
diff
changeset
|
46 |
System.err.println("BitLengthOverflow skipped: OutOfMemoryError"); |
36469eed1629
8041683: Catch OutOfMemoryError in BitLengthOverflow and DoubleValueOverflow
bpb
parents:
24030
diff
changeset
|
47 |
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
|
48 |
} |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
49 |
} |
a56f40ab71ce
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff
changeset
|
50 |
} |