1 /*
2 * Copyright (c) 2013, 1025, 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 * @ignore This test has huge memory requirements
27 * @library ..
28 * @run main/timeout=180/othervm -Xmx8g SymmetricRangeTests
29 * @bug 6910473 8021204 8021203 9005933 8074460
30 * @summary Test range of BigInteger values (use -Dseed=X to set PRNG seed)
31 * @author Dmitry Nadezhin
32 * @key randomness
33 */
34 import java.io.ByteArrayInputStream;
35 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.io.ObjectInputStream;
38 import java.io.ObjectOutputStream;
39 import java.util.Arrays;
40 import java.math.BigInteger;
41
42 public class SymmetricRangeTests {
43
44 private static final BigInteger MAX_VALUE = makeMaxValue();
45 private static final BigInteger MIN_VALUE = MAX_VALUE.negate();
46
47 private static BigInteger makeMaxValue() {
48 byte[] ba = new byte[1 << 28];
49 Arrays.fill(ba, (byte) 0xFF);
50 ba[0] = (byte) 0x7F;
51 return new BigInteger(ba);
52 }
53
54 private static void check(String msg, BigInteger actual, BigInteger expected) {
55 if (!actual.equals(expected)) {
56 throw new RuntimeException(msg + ".bitLength()=" + actual.bitLength());
57 }
58 }
59
60 private static void check(String msg, double actual, double expected) {
98 StringBuilder sb = new StringBuilder();
99 sb.append('1');
100 for (int i = 0; i < (1 << 30) - 1; i++) {
101 sb.append('0');
102 }
103 sb.append('1');
104 String s = sb.toString();
105 sb = null;
106 try {
107 BigInteger actual = new BigInteger(s, 16);
108 throw new RuntimeException("new BigInteger(\"1000...001\").bitLength()=" + actual.bitLength());
109 } catch (ArithmeticException e) {
110 // expected
111 }
112 }
113
114 private static void testOverflowInBitSieve() {
115 System.out.println("Testing overflow in BitSieve.sieveSingle");
116 int bitLength = (5 << 27) - 1;
117 try {
118 RandomSeed rndSeed = new RandomSeed(false);
119 System.out.println("Random number generator seed = " + rndSeed.getSeed());
120 BigInteger actual = new BigInteger(bitLength, 0, rndSeed.getRandom());
121 throw new RuntimeException("new BigInteger(bitLength, 0, null).bitLength()=" + actual.bitLength());
122 } catch (ArithmeticException e) {
123 // expected
124 }
125 try {
126 BigInteger bi = BigInteger.ONE.shiftLeft(bitLength - 1).subtract(BigInteger.ONE);
127 BigInteger actual = bi.nextProbablePrime();
128 throw new RuntimeException("bi.nextActualPrime().bitLength()=" + actual.bitLength());
129 } catch (ArithmeticException e) {
130 // expected
131 }
132 }
133
134 private static void testAdd() {
135 System.out.println("Testing BigInteger.add");
136 try {
137 BigInteger actual = MAX_VALUE.add(BigInteger.ONE);
138 throw new RuntimeException("BigInteger.MAX_VALUE.add(BigInteger.ONE).bitLength()=" + actual.bitLength());
139 } catch (ArithmeticException e) {
140 // expected
|
1 /*
2 * Copyright (c) 2013, 2015, 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 * @ignore This test has huge memory requirements
27 * @library /lib/testlibrary/
28 * @build jdk.testlibrary.*
29 * @run main/timeout=180/othervm -Xmx8g SymmetricRangeTests
30 * @bug 6910473 8021204 8021203 9005933 8074460 8078672
31 * @summary Test range of BigInteger values (use -Dseed=X to set PRNG seed)
32 * @author Dmitry Nadezhin
33 * @key randomness
34 */
35 import java.io.ByteArrayInputStream;
36 import java.io.ByteArrayOutputStream;
37 import java.io.IOException;
38 import java.io.ObjectInputStream;
39 import java.io.ObjectOutputStream;
40 import java.util.Arrays;
41 import java.math.BigInteger;
42 import java.util.Random;
43
44 public class SymmetricRangeTests {
45
46 private static final BigInteger MAX_VALUE = makeMaxValue();
47 private static final BigInteger MIN_VALUE = MAX_VALUE.negate();
48
49 private static BigInteger makeMaxValue() {
50 byte[] ba = new byte[1 << 28];
51 Arrays.fill(ba, (byte) 0xFF);
52 ba[0] = (byte) 0x7F;
53 return new BigInteger(ba);
54 }
55
56 private static void check(String msg, BigInteger actual, BigInteger expected) {
57 if (!actual.equals(expected)) {
58 throw new RuntimeException(msg + ".bitLength()=" + actual.bitLength());
59 }
60 }
61
62 private static void check(String msg, double actual, double expected) {
100 StringBuilder sb = new StringBuilder();
101 sb.append('1');
102 for (int i = 0; i < (1 << 30) - 1; i++) {
103 sb.append('0');
104 }
105 sb.append('1');
106 String s = sb.toString();
107 sb = null;
108 try {
109 BigInteger actual = new BigInteger(s, 16);
110 throw new RuntimeException("new BigInteger(\"1000...001\").bitLength()=" + actual.bitLength());
111 } catch (ArithmeticException e) {
112 // expected
113 }
114 }
115
116 private static void testOverflowInBitSieve() {
117 System.out.println("Testing overflow in BitSieve.sieveSingle");
118 int bitLength = (5 << 27) - 1;
119 try {
120 Random random = RandomFactory.getRandom();
121 BigInteger actual = new BigInteger(bitLength, 0, random);
122 throw new RuntimeException("new BigInteger(bitLength, 0, null).bitLength()=" + actual.bitLength());
123 } catch (ArithmeticException e) {
124 // expected
125 }
126 try {
127 BigInteger bi = BigInteger.ONE.shiftLeft(bitLength - 1).subtract(BigInteger.ONE);
128 BigInteger actual = bi.nextProbablePrime();
129 throw new RuntimeException("bi.nextActualPrime().bitLength()=" + actual.bitLength());
130 } catch (ArithmeticException e) {
131 // expected
132 }
133 }
134
135 private static void testAdd() {
136 System.out.println("Testing BigInteger.add");
137 try {
138 BigInteger actual = MAX_VALUE.add(BigInteger.ONE);
139 throw new RuntimeException("BigInteger.MAX_VALUE.add(BigInteger.ONE).bitLength()=" + actual.bitLength());
140 } catch (ArithmeticException e) {
141 // expected
|