BigIntegerTest.java revision 3646
2362N/A * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved. 809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 809N/A * This code is free software; you can redistribute it and/or modify it 809N/A * under the terms of the GNU General Public License version 2 only, as 809N/A * published by the Free Software Foundation. 809N/A * This code is distributed in the hope that it will be useful, but WITHOUT 809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 809N/A * version 2 for more details (a copy is included in the LICENSE file that 809N/A * accompanied this code). 809N/A * You should have received a copy of the GNU General Public License version 809N/A * 2 along with this work; if not, write to the Free Software Foundation, 809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 809N/A * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 809N/A * @summary tests methods in BigInteger 809N/A * This is a simple test class created to ensure that the results 809N/A * generated by BigInteger adhere to certain identities. Passing 809N/A * this test is a strong assurance that the BigInteger operations 809N/A * are working correctly. 809N/A * Three arguments may be specified which give the number of 809N/A * decimal digits you desire in the three batches of test numbers. 809N/A * The tests are performed on arrays of random numbers which are 809N/A * generated by a Random class as well as special cases which 809N/A * throw in boundary numbers such as 0, 1, maximum sized, etc. 809N/A static int size =
1000;
// numbers per batch 809N/A // Some variables for sizing test numbers in bits 809N/A for (
int i=
0; i<
100; i++) {
809N/A for (
int j=
0; j<
32; j++) {
809N/A //System.err.println(x+": "+bitCount+", "+bigX.bitCount()); 809N/A //System.err.println(x+": "+bitLength+", "+bigX.bitLength()); 809N/A /* Test setBit and clearBit (and testBit) */ 809N/A /* Test flipBit (and testBit) */ 809N/A /* Test getLowestSetBit() */ 809N/A /* Test identity x^y == x|y &~ x&y */ 809N/A /* Test identity x &~ y == ~(~x | y) */ 809N/A for (
int i=
0; i<
100; i++) {
809N/A for (
int i=
0; i<
100; i++) {
809N/A // This test is based on Fermat's theorem 809N/A // which is not ideal because base must not be multiple of modulus 809N/A // and modulus must be a prime or pseudoprime (Carmichael number) 809N/A for (
int i=
0; i<
10; i++) {
809N/A 521,
607,
1279,
2203,
2281,
3217,
4253,
4423,
9689,
9941,
11213,
19937,
809N/A 21701,
23209,
44497,
86243,
110503,
132049,
216091,
756839,
859433,
809N/A 1257787,
1398269,
2976221,
3021377,
6972593,
13466917 };
809N/A 561,
1105,
1729,
2465,
2821,
6601,
8911,
10585,
15841,
29341,
41041,
46657,
52633,
809N/A 62745,
63973,
75361,
101101,
115921,
126217,
162401,
172081,
188461,
252601,
809N/A 278545,
294409,
314821,
334153,
340561,
399001,
410041,
449065,
488881,
512461,
809N/A // Note: testing the larger ones takes too long. 809N/A // Note: this constant used for computed Carmichaels, not the array above 809N/A "120000000000000000000000000000000019",
809N/A "633825300114114700748351603131",
809N/A "1461501637330902918203684832716283019651637554291",
809N/A "779626057591079617852292862756047675913380626199",
809N/A "857591696176672809403750477631580323575362410491",
809N/A "910409242326391377348778281801166102059139832131",
809N/A "929857869954035706722619989283358182285540127919",
809N/A "961301750640481375785983980066592002055764391999",
809N/A "1267617700951005189537696547196156120148404630231",
809N/A "1326015641149969955786344600146607663033642528339" };
809N/A for(
int i=
0; i<
10; i++) {
809N/A // Test some known Mersenne primes (2^n)-1 809N/A // The array holds the exponents, not the numbers being tested 809N/A // Test some primes reported by customers as failing in the past 809N/A // Test some known Carmichael numbers. 809N/A // Test some computed Carmichael numbers. 809N/A // Numbers of the form (6k+1)(12k+1)(18k+1) are Carmichael numbers if 809N/A // each of the factors is prime 809N/A // Test some composites that are products of 2 primes 809N/A for (
int i=
0; i<
50; i++) {
809N/A for (
int i=
0; i<
4; i++) {
809N/A 2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97 809N/A 1999999003L,
1999999013L,
1999999049L,
1999999061L,
1999999081L,
809N/A 1999999087L,
1999999093L,
1999999097L,
1999999117L,
1999999121L,
809N/A 1999999151L,
1999999171L,
1999999207L,
1999999219L,
1999999271L,
809N/A 1999999321L,
1999999373L,
1999999423L,
1999999439L,
1999999499L,
809N/A 1999999553L,
1999999559L,
1999999571L,
1999999609L,
1999999613L,
809N/A 1999999621L,
1999999643L,
1999999649L,
1999999657L,
1999999747L,
809N/A 1999999763L,
1999999777L,
1999999811L,
1999999817L,
1999999829L,
809N/A 1999999853L,
1999999861L,
1999999871L,
1999999873 809N/A // First test nextProbablePrime on the low range starting at zero 809N/A // Test nextProbablePrime on a relatively small, known prime sequence 809N/A // Next, pick some large primes, use nextProbablePrime to find the 809N/A // next one, and make sure there are no primes in between 809N/A for (
int i=
0; i<
100; i+=
10) {
809N/A "ffffffff00000000ffffffff00000000ffffffff00000000",
809N/A "ffffffffffffffffffffffff000000000000000000000000",
809N/A "ffffffff0000000000000000000000000000000000000000",
809N/A "10000000ffffffffffffffffffffffffffffffffffffffff",
809N/A "100000000000000000000000000000000000000000000000",
809N/A "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
809N/A "-ffffffff00000000ffffffff00000000ffffffff00000000",
809N/A "-ffffffffffffffffffffffff000000000000000000000000",
809N/A "-ffffffff0000000000000000000000000000000000000000",
809N/A "-10000000ffffffffffffffffffffffffffffffffffffffff",
809N/A "-100000000000000000000000000000000000000000000000",
809N/A "-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 809N/A for(
int i=
0; i<
10; i++) {
809N/A * Main to interpret arguments and run several tests. 809N/A * Up to three arguments may be given to specify the size of BigIntegers 809N/A * used for call parameters 1, 2, and 3. The size is interpreted as 809N/A * the maximum number of decimal digits that the parameters will have. 809N/A * Get a random or boundary-case number. This is designed to provide 809N/A * a lot of numbers that will find failure points, such as max sized 809N/A * numbers, empty BigIntegers, etc. 809N/A * If order is less than 2, order is changed to 2. 809N/A case 2:
// All bits set in number 809N/A case 3:
// One bit in number 809N/A case 4:
// Random bit density 809N/A default:
// random bits