809N/A/*
3909N/A * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
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 *
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 *
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.
809N/A *
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
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225
809N/A * @summary tests methods in BigInteger
809N/A * @run main/timeout=400 BigIntegerTest
809N/A * @author madbot
809N/A */
809N/A
809N/Aimport java.util.Random;
809N/Aimport java.math.BigInteger;
809N/Aimport java.io.*;
809N/A
809N/A/**
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 *
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 *
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 *
809N/A */
809N/Apublic class BigIntegerTest {
809N/A static Random rnd = new Random();
809N/A static int size = 1000; // numbers per batch
809N/A static boolean failure = false;
809N/A
809N/A // Some variables for sizing test numbers in bits
809N/A private static int order1 = 100;
809N/A private static int order2 = 60;
809N/A private static int order3 = 30;
809N/A
809N/A public static void pow() {
809N/A int failCount1 = 0;
809N/A
809N/A for (int i=0; i<size; i++) {
809N/A int power = rnd.nextInt(6) +2;
809N/A BigInteger x = fetchNumber(order1);
809N/A BigInteger y = x.pow(power);
809N/A BigInteger z = x;
809N/A
809N/A for (int j=1; j<power; j++)
809N/A z = z.multiply(x);
809N/A
809N/A if (!y.equals(z))
809N/A failCount1++;
809N/A }
809N/A report("pow", failCount1);
809N/A }
809N/A
809N/A public static void arithmetic() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<size; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A while(x.compareTo(BigInteger.ZERO) != 1)
809N/A x = fetchNumber(order1);
809N/A BigInteger y = fetchNumber(order1/2);
809N/A while(x.compareTo(y) == -1)
809N/A y = fetchNumber(order1/2);
809N/A if (y.equals(BigInteger.ZERO))
809N/A y = y.add(BigInteger.ONE);
809N/A
809N/A BigInteger baz = x.divide(y);
809N/A baz = baz.multiply(y);
809N/A baz = baz.add(x.remainder(y));
809N/A baz = baz.subtract(x);
809N/A if (!baz.equals(BigInteger.ZERO))
809N/A failCount++;
809N/A }
809N/A report("Arithmetic I", failCount);
809N/A
809N/A failCount = 0;
809N/A for (int i=0; i<100; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A while(x.compareTo(BigInteger.ZERO) != 1)
809N/A x = fetchNumber(order1);
809N/A BigInteger y = fetchNumber(order1/2);
809N/A while(x.compareTo(y) == -1)
809N/A y = fetchNumber(order1/2);
809N/A if (y.equals(BigInteger.ZERO))
809N/A y = y.add(BigInteger.ONE);
809N/A
809N/A BigInteger baz[] = x.divideAndRemainder(y);
809N/A baz[0] = baz[0].multiply(y);
809N/A baz[0] = baz[0].add(baz[1]);
809N/A baz[0] = baz[0].subtract(x);
809N/A if (!baz[0].equals(BigInteger.ZERO))
809N/A failCount++;
809N/A }
809N/A report("Arithmetic II", failCount);
809N/A }
809N/A
809N/A public static void bitCount() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<size*10; i++) {
809N/A int x = rnd.nextInt();
809N/A BigInteger bigX = BigInteger.valueOf((long)x);
809N/A int bit = (x < 0 ? 0 : 1);
809N/A int tmp = x, bitCount = 0;
809N/A for (int j=0; j<32; j++) {
809N/A bitCount += ((tmp & 1) == bit ? 1 : 0);
809N/A tmp >>= 1;
809N/A }
809N/A
809N/A if (bigX.bitCount() != bitCount) {
809N/A //System.err.println(x+": "+bitCount+", "+bigX.bitCount());
809N/A failCount++;
809N/A }
809N/A }
809N/A report("Bit Count", failCount);
809N/A }
809N/A
809N/A public static void bitLength() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<size*10; i++) {
809N/A int x = rnd.nextInt();
809N/A BigInteger bigX = BigInteger.valueOf((long)x);
809N/A int signBit = (x < 0 ? 0x80000000 : 0);
809N/A int tmp = x, bitLength, j;
809N/A for (j=0; j<32 && (tmp & 0x80000000)==signBit; j++)
809N/A tmp <<= 1;
809N/A bitLength = 32 - j;
809N/A
809N/A if (bigX.bitLength() != bitLength) {
809N/A //System.err.println(x+": "+bitLength+", "+bigX.bitLength());
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A report("BitLength", failCount);
809N/A }
809N/A
809N/A public static void bitOps() {
809N/A int failCount1 = 0, failCount2 = 0, failCount3 = 0;
809N/A
809N/A for (int i=0; i<size*5; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A BigInteger y;
809N/A
809N/A /* Test setBit and clearBit (and testBit) */
809N/A if (x.signum() < 0) {
809N/A y = BigInteger.valueOf(-1);
809N/A for (int j=0; j<x.bitLength(); j++)
809N/A if (!x.testBit(j))
809N/A y = y.clearBit(j);
809N/A } else {
809N/A y = BigInteger.ZERO;
809N/A for (int j=0; j<x.bitLength(); j++)
809N/A if (x.testBit(j))
809N/A y = y.setBit(j);
809N/A }
809N/A if (!x.equals(y))
809N/A failCount1++;
809N/A
809N/A /* Test flipBit (and testBit) */
809N/A y = BigInteger.valueOf(x.signum()<0 ? -1 : 0);
809N/A for (int j=0; j<x.bitLength(); j++)
809N/A if (x.signum()<0 ^ x.testBit(j))
809N/A y = y.flipBit(j);
809N/A if (!x.equals(y))
809N/A failCount2++;
809N/A }
809N/A report("clearBit/testBit", failCount1);
809N/A report("flipBit/testBit", failCount2);
809N/A
809N/A for (int i=0; i<size*5; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A
809N/A /* Test getLowestSetBit() */
809N/A int k = x.getLowestSetBit();
809N/A if (x.signum() == 0) {
809N/A if (k != -1)
809N/A failCount3++;
809N/A } else {
809N/A BigInteger z = x.and(x.negate());
809N/A int j;
809N/A for (j=0; j<z.bitLength() && !z.testBit(j); j++)
809N/A ;
809N/A if (k != j)
809N/A failCount3++;
809N/A }
809N/A }
809N/A report("getLowestSetBit", failCount3);
809N/A }
809N/A
809N/A public static void bitwise() {
809N/A
809N/A /* Test identity x^y == x|y &~ x&y */
809N/A int failCount = 0;
809N/A for (int i=0; i<size; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A BigInteger y = fetchNumber(order1);
809N/A BigInteger z = x.xor(y);
809N/A BigInteger w = x.or(y).andNot(x.and(y));
809N/A if (!z.equals(w))
809N/A failCount++;
809N/A }
809N/A report("Logic (^ | & ~)", failCount);
809N/A
809N/A /* Test identity x &~ y == ~(~x | y) */
809N/A failCount = 0;
809N/A for (int i=0; i<size; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A BigInteger y = fetchNumber(order1);
809N/A BigInteger z = x.andNot(y);
809N/A BigInteger w = x.not().or(y).not();
809N/A if (!z.equals(w))
809N/A failCount++;
809N/A }
809N/A report("Logic (&~ | ~)", failCount);
809N/A }
809N/A
809N/A public static void shift() {
809N/A int failCount1 = 0;
809N/A int failCount2 = 0;
809N/A int failCount3 = 0;
809N/A
809N/A for (int i=0; i<100; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A int n = Math.abs(rnd.nextInt()%200);
809N/A
809N/A if (!x.shiftLeft(n).equals
809N/A (x.multiply(BigInteger.valueOf(2L).pow(n))))
809N/A failCount1++;
809N/A
809N/A BigInteger y[] =x.divideAndRemainder(BigInteger.valueOf(2L).pow(n));
809N/A BigInteger z = (x.signum()<0 && y[1].signum()!=0
809N/A ? y[0].subtract(BigInteger.ONE)
809N/A : y[0]);
809N/A
809N/A BigInteger b = x.shiftRight(n);
809N/A
809N/A if (!b.equals(z)) {
809N/A System.err.println("Input is "+x.toString(2));
809N/A System.err.println("shift is "+n);
809N/A
809N/A System.err.println("Divided "+z.toString(2));
809N/A System.err.println("Shifted is "+b.toString(2));
809N/A if (b.toString().equals(z.toString()))
809N/A System.err.println("Houston, we have a problem.");
809N/A failCount2++;
809N/A }
809N/A
809N/A if (!x.shiftLeft(n).shiftRight(n).equals(x))
809N/A failCount3++;
809N/A }
809N/A report("baz shiftLeft", failCount1);
809N/A report("baz shiftRight", failCount2);
809N/A report("baz shiftLeft/Right", failCount3);
809N/A }
809N/A
809N/A public static void divideAndRemainder() {
809N/A int failCount1 = 0;
809N/A
809N/A for (int i=0; i<size; i++) {
809N/A BigInteger x = fetchNumber(order1).abs();
809N/A while(x.compareTo(BigInteger.valueOf(3L)) != 1)
809N/A x = fetchNumber(order1).abs();
809N/A BigInteger z = x.divide(BigInteger.valueOf(2L));
809N/A BigInteger y[] = x.divideAndRemainder(x);
809N/A if (!y[0].equals(BigInteger.ONE)) {
809N/A failCount1++;
809N/A System.err.println("fail1 x :"+x);
809N/A System.err.println(" y :"+y);
809N/A }
809N/A else if (!y[1].equals(BigInteger.ZERO)) {
809N/A failCount1++;
809N/A System.err.println("fail2 x :"+x);
809N/A System.err.println(" y :"+y);
809N/A }
809N/A
809N/A y = x.divideAndRemainder(z);
809N/A if (!y[0].equals(BigInteger.valueOf(2))) {
809N/A failCount1++;
809N/A System.err.println("fail3 x :"+x);
809N/A System.err.println(" y :"+y);
809N/A }
809N/A }
809N/A report("divideAndRemainder I", failCount1);
809N/A }
809N/A
809N/A public static void stringConv() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<100; i++) {
809N/A byte xBytes[] = new byte[Math.abs(rnd.nextInt())%100+1];
809N/A rnd.nextBytes(xBytes);
809N/A BigInteger x = new BigInteger(xBytes);
809N/A
809N/A for (int radix=2; radix < 37; radix++) {
809N/A String result = x.toString(radix);
809N/A BigInteger test = new BigInteger(result, radix);
809N/A if (!test.equals(x)) {
809N/A failCount++;
809N/A System.err.println("BigInteger toString: "+x);
809N/A System.err.println("Test: "+test);
809N/A System.err.println(radix);
809N/A }
809N/A }
809N/A }
809N/A report("String Conversion", failCount);
809N/A }
809N/A
809N/A public static void byteArrayConv() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<size; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A while (x.equals(BigInteger.ZERO))
809N/A x = fetchNumber(order1);
809N/A BigInteger y = new BigInteger(x.toByteArray());
809N/A if (!x.equals(y)) {
809N/A failCount++;
809N/A System.err.println("orig is "+x);
809N/A System.err.println("new is "+y);
809N/A }
809N/A }
809N/A report("Array Conversion", failCount);
809N/A }
809N/A
809N/A public static void modInv() {
809N/A int failCount = 0, successCount = 0, nonInvCount = 0;
809N/A
809N/A for (int i=0; i<size; i++) {
809N/A BigInteger x = fetchNumber(order1);
809N/A while(x.equals(BigInteger.ZERO))
809N/A x = fetchNumber(order1);
809N/A BigInteger m = fetchNumber(order1).abs();
809N/A while(m.compareTo(BigInteger.ONE) != 1)
809N/A m = fetchNumber(order1).abs();
809N/A
809N/A try {
809N/A BigInteger inv = x.modInverse(m);
809N/A BigInteger prod = inv.multiply(x).remainder(m);
809N/A
809N/A if (prod.signum() == -1)
809N/A prod = prod.add(m);
809N/A
809N/A if (prod.equals(BigInteger.ONE))
809N/A successCount++;
809N/A else
809N/A failCount++;
809N/A } catch(ArithmeticException e) {
809N/A nonInvCount++;
809N/A }
809N/A }
809N/A report("Modular Inverse", failCount);
809N/A }
809N/A
809N/A public static void modExp() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<size/10; i++) {
809N/A BigInteger m = fetchNumber(order1).abs();
809N/A while(m.compareTo(BigInteger.ONE) != 1)
809N/A m = fetchNumber(order1).abs();
809N/A BigInteger base = fetchNumber(order2);
809N/A BigInteger exp = fetchNumber(8).abs();
809N/A
809N/A BigInteger z = base.modPow(exp, m);
809N/A BigInteger w = base.pow(exp.intValue()).mod(m);
809N/A if (!z.equals(w)) {
809N/A System.err.println("z is "+z);
809N/A System.err.println("w is "+w);
809N/A System.err.println("mod is "+m);
809N/A System.err.println("base is "+base);
809N/A System.err.println("exp is "+exp);
809N/A failCount++;
809N/A }
809N/A }
809N/A report("Exponentiation I", failCount);
809N/A }
809N/A
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 public static void modExp2() {
809N/A int failCount = 0;
809N/A
809N/A for (int i=0; i<10; i++) {
809N/A BigInteger m = new BigInteger(100, 5, rnd);
809N/A while(m.compareTo(BigInteger.ONE) != 1)
809N/A m = new BigInteger(100, 5, rnd);
809N/A BigInteger exp = m.subtract(BigInteger.ONE);
809N/A BigInteger base = fetchNumber(order1).abs();
809N/A while(base.compareTo(m) != -1)
809N/A base = fetchNumber(order1).abs();
809N/A while(base.equals(BigInteger.ZERO))
809N/A base = fetchNumber(order1).abs();
809N/A
809N/A BigInteger one = base.modPow(exp, m);
809N/A if (!one.equals(BigInteger.ONE)) {
809N/A System.err.println("m is "+m);
809N/A System.err.println("base is "+base);
809N/A System.err.println("exp is "+exp);
809N/A failCount++;
809N/A }
809N/A }
809N/A report("Exponentiation II", failCount);
809N/A }
809N/A
809N/A private static final int[] mersenne_powers = {
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
809N/A private static final long[] carmichaels = {
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 225593397919L };
809N/A
809N/A // Note: testing the larger ones takes too long.
809N/A private static final int NUM_MERSENNES_TO_TEST = 7;
809N/A // Note: this constant used for computed Carmichaels, not the array above
809N/A private static final int NUM_CARMICHAELS_TO_TEST = 5;
809N/A
809N/A private static final String[] customer_primes = {
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
809N/A private static final BigInteger ZERO = BigInteger.ZERO;
809N/A private static final BigInteger ONE = BigInteger.ONE;
809N/A private static final BigInteger TWO = new BigInteger("2");
809N/A private static final BigInteger SIX = new BigInteger("6");
809N/A private static final BigInteger TWELVE = new BigInteger("12");
809N/A private static final BigInteger EIGHTEEN = new BigInteger("18");
809N/A
809N/A public static void prime() {
809N/A BigInteger p1, p2, c1;
809N/A int failCount = 0;
809N/A
809N/A // Test consistency
809N/A for(int i=0; i<10; i++) {
809N/A p1 = BigInteger.probablePrime(100, rnd);
809N/A if (!p1.isProbablePrime(100)) {
809N/A System.err.println("Consistency "+p1.toString(16));
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A // Test some known Mersenne primes (2^n)-1
809N/A // The array holds the exponents, not the numbers being tested
809N/A for (int i=0; i<NUM_MERSENNES_TO_TEST; i++) {
809N/A p1 = new BigInteger("2");
809N/A p1 = p1.pow(mersenne_powers[i]);
809N/A p1 = p1.subtract(BigInteger.ONE);
809N/A if (!p1.isProbablePrime(100)) {
809N/A System.err.println("Mersenne prime "+i+ " failed.");
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A // Test some primes reported by customers as failing in the past
809N/A for (int i=0; i<customer_primes.length; i++) {
809N/A p1 = new BigInteger(customer_primes[i]);
809N/A if (!p1.isProbablePrime(100)) {
809N/A System.err.println("Customer prime "+i+ " failed.");
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A // Test some known Carmichael numbers.
809N/A for (int i=0; i<carmichaels.length; i++) {
809N/A c1 = BigInteger.valueOf(carmichaels[i]);
809N/A if(c1.isProbablePrime(100)) {
809N/A System.err.println("Carmichael "+i+ " reported as prime.");
809N/A failCount++;
809N/A }
809N/A }
809N/A
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 int found = 0;
809N/A BigInteger f1 = new BigInteger(40, 100, rnd);
809N/A while (found < NUM_CARMICHAELS_TO_TEST) {
809N/A BigInteger k = null;
809N/A BigInteger f2, f3;
809N/A f1 = f1.nextProbablePrime();
809N/A BigInteger[] result = f1.subtract(ONE).divideAndRemainder(SIX);
809N/A if (result[1].equals(ZERO)) {
809N/A k = result[0];
809N/A f2 = k.multiply(TWELVE).add(ONE);
809N/A if (f2.isProbablePrime(100)) {
809N/A f3 = k.multiply(EIGHTEEN).add(ONE);
809N/A if (f3.isProbablePrime(100)) {
809N/A c1 = f1.multiply(f2).multiply(f3);
809N/A if (c1.isProbablePrime(100)) {
809N/A System.err.println("Computed Carmichael "
809N/A +c1.toString(16));
809N/A failCount++;
809N/A }
809N/A found++;
809N/A }
809N/A }
809N/A }
809N/A f1 = f1.add(TWO);
809N/A }
809N/A
809N/A // Test some composites that are products of 2 primes
809N/A for (int i=0; i<50; i++) {
809N/A p1 = BigInteger.probablePrime(100, rnd);
809N/A p2 = BigInteger.probablePrime(100, rnd);
809N/A c1 = p1.multiply(p2);
809N/A if (c1.isProbablePrime(100)) {
809N/A System.err.println("Composite failed "+c1.toString(16));
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A for (int i=0; i<4; i++) {
809N/A p1 = BigInteger.probablePrime(600, rnd);
809N/A p2 = BigInteger.probablePrime(600, rnd);
809N/A c1 = p1.multiply(p2);
809N/A if (c1.isProbablePrime(100)) {
809N/A System.err.println("Composite failed "+c1.toString(16));
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A report("Prime", failCount);
809N/A }
809N/A
809N/A private static final long[] primesTo100 = {
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 };
809N/A
809N/A private static final long[] aPrimeSequence = {
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 };
809N/A
809N/A public static void nextProbablePrime() throws Exception {
809N/A int failCount = 0;
809N/A BigInteger p1, p2, p3;
809N/A p1 = p2 = p3 = ZERO;
809N/A
809N/A // First test nextProbablePrime on the low range starting at zero
809N/A for (int i=0; i<primesTo100.length; i++) {
809N/A p1 = p1.nextProbablePrime();
809N/A if (p1.longValue() != primesTo100[i]) {
809N/A System.err.println("low range primes failed");
809N/A System.err.println("p1 is "+p1);
809N/A System.err.println("expected "+primesTo100[i]);
809N/A failCount++;
809N/A }
809N/A }
809N/A
809N/A // Test nextProbablePrime on a relatively small, known prime sequence
809N/A p1 = BigInteger.valueOf(aPrimeSequence[0]);
809N/A for (int i=1; i<aPrimeSequence.length; i++) {
809N/A p1 = p1.nextProbablePrime();
809N/A if (p1.longValue() != aPrimeSequence[i]) {
809N/A System.err.println("prime sequence failed");
809N/A failCount++;
809N/A }
809N/A }
809N/A
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 p1 = BigInteger.probablePrime(50 + i, rnd);
809N/A p2 = p1.add(ONE);
809N/A p3 = p1.nextProbablePrime();
809N/A while(p2.compareTo(p3) < 0) {
809N/A if (p2.isProbablePrime(100)){
809N/A System.err.println("nextProbablePrime failed");
809N/A System.err.println("along range "+p1.toString(16));
809N/A System.err.println("to "+p3.toString(16));
809N/A failCount++;
809N/A break;
809N/A }
809N/A p2 = p2.add(ONE);
809N/A }
809N/A }
809N/A
809N/A report("nextProbablePrime", failCount);
809N/A }
809N/A
809N/A public static void serialize() throws Exception {
809N/A int failCount = 0;
809N/A
809N/A String bitPatterns[] = {
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 };
809N/A
809N/A for(int i = 0; i < bitPatterns.length; i++) {
809N/A BigInteger b1 = new BigInteger(bitPatterns[i], 16);
2030N/A BigInteger b2 = null;
809N/A
809N/A File f = new File("serialtest");
3646N/A
3646N/A try (FileOutputStream fos = new FileOutputStream(f)) {
3646N/A try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
2030N/A oos.writeObject(b1);
2030N/A oos.flush();
2030N/A }
809N/A
3646N/A try (FileInputStream fis = new FileInputStream(f);
3646N/A ObjectInputStream ois = new ObjectInputStream(fis))
3646N/A {
3646N/A b2 = (BigInteger)ois.readObject();
2030N/A }
2030N/A
2030N/A if (!b1.equals(b2) ||
2030N/A !b1.equals(b1.or(b2))) {
2030N/A failCount++;
2030N/A System.err.println("Serialized failed for hex " +
2030N/A b1.toString(16));
2030N/A }
809N/A }
809N/A f.delete();
809N/A }
809N/A
809N/A for(int i=0; i<10; i++) {
809N/A BigInteger b1 = fetchNumber(rnd.nextInt(100));
2030N/A BigInteger b2 = null;
809N/A File f = new File("serialtest");
3646N/A try (FileOutputStream fos = new FileOutputStream(f)) {
3646N/A try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
2030N/A oos.writeObject(b1);
2030N/A oos.flush();
2030N/A }
2030N/A
3646N/A try (FileInputStream fis = new FileInputStream(f);
3646N/A ObjectInputStream ois = new ObjectInputStream(fis))
3646N/A {
3646N/A b2 = (BigInteger)ois.readObject();
2030N/A }
2030N/A }
809N/A
809N/A if (!b1.equals(b2) ||
809N/A !b1.equals(b1.or(b2)))
809N/A failCount++;
809N/A f.delete();
809N/A }
809N/A
809N/A report("Serialize", failCount);
809N/A }
809N/A
809N/A /**
809N/A * Main to interpret arguments and run several tests.
809N/A *
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 *
809N/A */
809N/A public static void main(String[] args) throws Exception {
809N/A
809N/A if (args.length >0)
809N/A order1 = (int)((Integer.parseInt(args[0]))* 3.333);
809N/A if (args.length >1)
809N/A order2 = (int)((Integer.parseInt(args[1]))* 3.333);
809N/A if (args.length >2)
809N/A order3 = (int)((Integer.parseInt(args[2]))* 3.333);
809N/A
809N/A prime();
809N/A nextProbablePrime();
809N/A
809N/A arithmetic();
809N/A divideAndRemainder();
809N/A pow();
809N/A
809N/A bitCount();
809N/A bitLength();
809N/A bitOps();
809N/A bitwise();
809N/A
809N/A shift();
809N/A
809N/A byteArrayConv();
809N/A
809N/A modInv();
809N/A modExp();
809N/A modExp2();
809N/A
809N/A stringConv();
809N/A serialize();
809N/A
809N/A if (failure)
809N/A throw new RuntimeException("Failure in BigIntegerTest.");
809N/A }
809N/A
809N/A /*
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 *
809N/A * If order is less than 2, order is changed to 2.
809N/A */
809N/A private static BigInteger fetchNumber(int order) {
809N/A boolean negative = rnd.nextBoolean();
809N/A int numType = rnd.nextInt(6);
809N/A BigInteger result = null;
809N/A if (order < 2) order = 2;
809N/A
809N/A switch (numType) {
809N/A case 0: // Empty
809N/A result = BigInteger.ZERO;
809N/A break;
809N/A
809N/A case 1: // One
809N/A result = BigInteger.ONE;
809N/A break;
809N/A
809N/A case 2: // All bits set in number
809N/A int numBytes = (order+7)/8;
809N/A byte[] fullBits = new byte[numBytes];
809N/A for(int i=0; i<numBytes; i++)
809N/A fullBits[i] = (byte)0xff;
809N/A int excessBits = 8*numBytes - order;
809N/A fullBits[0] &= (1 << (8-excessBits)) - 1;
809N/A result = new BigInteger(1, fullBits);
809N/A break;
809N/A
809N/A case 3: // One bit in number
809N/A result = BigInteger.ONE.shiftLeft(rnd.nextInt(order));
809N/A break;
809N/A
809N/A case 4: // Random bit density
809N/A int iterations = rnd.nextInt(order-1);
809N/A result = BigInteger.ONE.shiftLeft(rnd.nextInt(order));
809N/A for(int i=0; i<iterations; i++) {
809N/A BigInteger temp = BigInteger.ONE.shiftLeft(
809N/A rnd.nextInt(order));
809N/A result = result.or(temp);
809N/A }
809N/A break;
809N/A
809N/A default: // random bits
809N/A result = new BigInteger(order, rnd);
809N/A }
809N/A
809N/A if (negative)
809N/A result = result.negate();
809N/A
809N/A return result;
809N/A }
809N/A
809N/A static void report(String testName, int failCount) {
809N/A System.err.println(testName+": " +
809N/A (failCount==0 ? "Passed":"Failed("+failCount+")"));
809N/A if (failCount > 0)
809N/A failure = true;
809N/A }
809N/A}