--- old/test/java/math/BigInteger/BigIntegerTest.java 2013-06-19 09:00:22.000000000 -0700 +++ new/test/java/math/BigInteger/BigIntegerTest.java 2013-06-19 09:00:22.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,15 +23,19 @@ /* * @test - * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 + * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 * @summary tests methods in BigInteger * @run main/timeout=400 BigIntegerTest * @author madbot */ -import java.util.Random; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.math.BigInteger; -import java.io.*; +import java.util.Random; /** * This is a simple test class created to ensure that the results @@ -48,21 +52,42 @@ * */ public class BigIntegerTest { + // + // Bit large number thresholds based on the int thresholds + // defined in BigInteger itself: + // + // KARATSUBA_THRESHOLD = 50 ints = 1600 bits + // TOOM_COOK_THRESHOLD = 75 ints = 2400 bits + // KARATSUBA_SQUARE_THRESHOLD = 90 ints = 2880 bits + // TOOM_COOK_SQUARE_THRESHOLD = 140 ints = 4480 bits + // + static final int BITS_KARATSUBA = 1600; + static final int BITS_TOOM_COOK = 2400; + static final int BITS_KARATSUBA_SQUARE = 2880; + static final int BITS_TOOM_COOK_SQUARE = 4480; + + static final int ORDER_SMALL = 60; + static final int ORDER_MEDIUM = 100; + // #bits for testing Karatsuba and Burnikel-Ziegler + static final int ORDER_KARATSUBA = 1800; + // #bits for testing Toom-Cook + static final int ORDER_TOOM_COOK = 3000; + // #bits for testing Karatsuba squaring + static final int ORDER_KARATSUBA_SQUARE = 3200; + // #bits for testing Toom-Cook squaring + static final int ORDER_TOOM_COOK_SQUARE = 4600; + static Random rnd = new Random(); static int size = 1000; // numbers per batch static boolean failure = false; - // Some variables for sizing test numbers in bits - private static int order1 = 100; - private static int order2 = 60; - private static int order3 = 30; - - public static void pow() { + public static void pow(int order) { int failCount1 = 0; for (int i=0; i + * (u << a)*(v << b) = (u*v) << (a + b) + * + * For Karatsuba multiplication, the right hand expression will be evaluated + * using the standard naive algorithm, and the left hand expression using + * the Karatsuba algorithm. For 3-way Toom-Cook multiplication, the right + * hand expression will be evaluated using Karatsuba multiplication, and the + * left hand expression using 3-way Toom-Cook multiplication. + */ + public static void multiplyLarge() { + int failCount = 0; + + BigInteger base = BigInteger.ONE.shiftLeft(BITS_KARATSUBA - 32 - 1); + for (int i=0; ibigInteger.multiply(bigInteger) tests whether + * the parameter is the same instance on which the method is being invoked + * and calls square() accordingly. + */ + public static void squareLarge() { + int failCount = 0; + + BigInteger base = BigInteger.ONE.shiftLeft(BITS_KARATSUBA_SQUARE - 32 - 1); + for (int i=0; i0) order1 = (int)((Integer.parseInt(args[0]))* 3.333); if (args.length >1) order2 = (int)((Integer.parseInt(args[1]))* 3.333); if (args.length >2) order3 = (int)((Integer.parseInt(args[2]))* 3.333); + if (args.length >3) + order4 = (int)((Integer.parseInt(args[3]))* 3.333); prime(); nextProbablePrime(); - arithmetic(); - divideAndRemainder(); - pow(); + arithmetic(order1); // small numbers + arithmetic(order3); // Karatsuba / Burnikel-Ziegler range + arithmetic(order4); // Toom-Cook range + + divideAndRemainder(order1); // small numbers + divideAndRemainder(order3); // Karatsuba / Burnikel-Ziegler range + divideAndRemainder(order4); // Toom-Cook range + + pow(order1); + pow(order3); + pow(order4); + + square(ORDER_MEDIUM); + square(ORDER_KARATSUBA_SQUARE); + square(ORDER_TOOM_COOK_SQUARE); bitCount(); bitLength(); - bitOps(); - bitwise(); + bitOps(order1); + bitwise(order1); - shift(); + shift(order1); - byteArrayConv(); + byteArrayConv(order1); - modInv(); - modExp(); - modExp2(); + modInv(order1); // small numbers + modInv(order3); // Karatsuba / Burnikel-Ziegler range + modInv(order4); // Toom-Cook range + + modExp(order1, order2); + modExp2(order1); stringConv(); serialize(); + multiplyLarge(); + squareLarge(); + if (failure) throw new RuntimeException("Failure in BigIntegerTest."); } @@ -747,7 +929,7 @@ */ private static BigInteger fetchNumber(int order) { boolean negative = rnd.nextBoolean(); - int numType = rnd.nextInt(6); + int numType = rnd.nextInt(7); BigInteger result = null; if (order < 2) order = 2; @@ -783,6 +965,19 @@ result = result.or(temp); } break; + case 5: // Runs of consecutive ones and zeros + result = ZERO; + int remaining = order; + int bit = rnd.nextInt(2); + while (remaining > 0) { + int runLength = Math.min(remaining, rnd.nextInt(order)); + result = result.shiftLeft(runLength); + if (bit > 0) + result = result.add(ONE.shiftLeft(runLength).subtract(ONE)); + remaining -= runLength; + bit = 1 - bit; + } + break; default: // random bits result = new BigInteger(order, rnd);