Skip to content

Commit

Permalink
8294137: Review running times of java.math tests
Browse files Browse the repository at this point in the history
Backport-of: 51035a75e493f64b26f78e7fc87f6f6e536e4f56
  • Loading branch information
Amos Shi committed Apr 28, 2024
1 parent d8b297f commit 1e1688a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 79 deletions.
86 changes: 51 additions & 35 deletions test/jdk/java/math/BigInteger/BigIntegerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023, 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
Expand Down Expand Up @@ -1212,6 +1212,17 @@ public static void serialize() throws Exception {
*
*/
public static void main(String[] args) throws Exception {
// subset zero indicates to run all subsets
int subset = Integer.valueOf(System.getProperty("subset",
String.valueOf(1 + random.nextInt(3))));
if (subset < 0 || subset > 3) {
throw new RuntimeException("Unknown subset " + subset);
}
if (subset == 0)
System.out.println("Testing all subsets");
else
System.out.println("Testing subset " + subset);

// Some variables for sizing test numbers in bits
int order1 = ORDER_MEDIUM;
int order2 = ORDER_SMALL;
Expand All @@ -1227,52 +1238,57 @@ public static void main(String[] args) throws Exception {
if (args.length >3)
order4 = (int)((Integer.parseInt(args[3]))* 3.333);

constructor();

prime();
nextProbablePrime();
if (subset == 0 || subset == 1) {
constructor();

arithmetic(order1); // small numbers
arithmetic(order3); // Karatsuba range
arithmetic(order4); // Toom-Cook / Burnikel-Ziegler range
prime();
nextProbablePrime();

divideAndRemainder(order1); // small numbers
divideAndRemainder(order3); // Karatsuba range
divideAndRemainder(order4); // Toom-Cook / Burnikel-Ziegler range
arithmetic(order1); // small numbers
arithmetic(order3); // Karatsuba range
arithmetic(order4); // Toom-Cook / Burnikel-Ziegler range

pow(order1);
pow(order3);
pow(order4);
divideAndRemainder(order1); // small numbers
divideAndRemainder(order3); // Karatsuba range
divideAndRemainder(order4); // Toom-Cook / Burnikel-Ziegler range

square(ORDER_MEDIUM);
square(ORDER_KARATSUBA_SQUARE);
square(ORDER_TOOM_COOK_SQUARE);
pow(order1);
pow(order3);
pow(order4);

squareRoot();
squareRootAndRemainder();
square(ORDER_MEDIUM);
square(ORDER_KARATSUBA_SQUARE);
square(ORDER_TOOM_COOK_SQUARE);

bitCount();
bitLength();
bitOps(order1);
bitwise(order1);
squareRoot();
squareRootAndRemainder();

shift(order1);
bitCount();
bitLength();
bitOps(order1);
bitwise(order1);

byteArrayConv(order1);
shift(order1);

modInv(order1); // small numbers
modInv(order3); // Karatsuba range
modInv(order4); // Toom-Cook / Burnikel-Ziegler range
byteArrayConv(order1);

modExp(order1, order2);
modExp2(order1);
modInv(order1); // small numbers
modInv(order3); // Karatsuba range
}
if (subset == 0 || subset == 2) {
modInv(order4); // Toom-Cook / Burnikel-Ziegler range

stringConv();
serialize();
modExp(order1, order2);
modExp2(order1);
}
if (subset == 0 || subset == 3) {
stringConv();
serialize();

multiplyLarge();
squareLarge();
divideLarge();
multiplyLarge();
squareLarge();
divideLarge();
}

if (failure)
throw new RuntimeException("Failure in BigIntegerTest.");
Expand Down
11 changes: 10 additions & 1 deletion test/jdk/java/math/BigInteger/LargeValueExceptions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2023, 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
Expand Down Expand Up @@ -30,6 +30,8 @@
*/
import java.math.BigInteger;
import static java.math.BigInteger.ONE;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

//
Expand Down Expand Up @@ -62,6 +64,13 @@ public class LargeValueExceptions {
// Half BigInteger.MAX_MAG_LENGTH
private static final int MAX_INTS_HALF = MAX_INTS / 2;

// Print the run time of each sub-test in milliseconds
@AfterMethod
public void getRunTime(ITestResult tr) {
long time = tr.getEndMillis() - tr.getStartMillis();
System.out.printf("Run time: %d ms%n", time);
}

// --- squaring ---

// Largest no overflow determined by examining data lengths alone.
Expand Down
106 changes: 63 additions & 43 deletions test/jdk/java/math/BigInteger/largeMemory/SymmetricRangeTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, 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
Expand Down Expand Up @@ -46,6 +46,8 @@ public class SymmetricRangeTests {
private static final BigInteger MAX_VALUE = makeMaxValue();
private static final BigInteger MIN_VALUE = MAX_VALUE.negate();

private static final Random RANDOM = RandomFactory.getRandom();

private static BigInteger makeMaxValue() {
byte[] ba = new byte[1 << 28];
Arrays.fill(ba, (byte) 0xFF);
Expand Down Expand Up @@ -117,8 +119,7 @@ private static void testOverflowInBitSieve() {
System.out.println("Testing overflow in BitSieve.sieveSingle");
int bitLength = (5 << 27) - 1;
try {
Random random = RandomFactory.getRandom();
BigInteger actual = new BigInteger(bitLength, 0, random);
BigInteger actual = new BigInteger(bitLength, 0, RANDOM);
throw new RuntimeException("new BigInteger(bitLength, 0, null).bitLength()=" + actual.bitLength());
} catch (ArithmeticException e) {
// expected
Expand Down Expand Up @@ -621,45 +622,64 @@ private static void testByteValueExact() {
}

public static void main(String... args) {
testOverflowInMakePositive();
testBug8021204();
testOverflowInBitSieve();
testAdd();
testSubtract();
testMultiply();
testDivide();
testDivideAndRemainder();
testBug9005933();
testRemainder();
testPow();
testGcd();
testAbs();
testNegate();
testMod();
testModPow();
// testModInverse();
testShiftLeft();
testShiftRight();
testAnd();
testOr();
testXor();
testNot();
testSetBit();
testClearBit();
testFlipBit();
testGetLowestSetBit();
testBitLength();
testBitCount();
testToString();
testToByteArrayWithConstructor();
testIntValue();
testLongValue();
testFloatValue();
testDoubleValue();
testSerialization();
testLongValueExact();
testIntValueExact();
testShortValueExact();
testByteValueExact();
// subset zero indicates to run all subsets
int subset = Integer.valueOf(System.getProperty("subset",
String.valueOf(1 + RANDOM.nextInt(4))));
if (subset < 0 || subset > 4) {
throw new RuntimeException("Unknown subset " + subset);
}
if (subset == 0)
System.out.println("Testing all subsets");
else
System.out.println("Testing subset " + subset);

if (subset == 0 || subset == 1) {
testOverflowInMakePositive();
testBug8021204();
testOverflowInBitSieve();
testAdd();
testSubtract();
}
if (subset == 0 || subset == 2) {
testMultiply();
testDivide();
testDivideAndRemainder();
testBug9005933();
}
if (subset == 0 || subset == 3) {
testRemainder();
testPow();
testGcd();
testAbs();
testNegate();
testMod();
testModPow();
// testModInverse();
testShiftLeft();
testShiftRight();
testAnd();
testOr();
testXor();
testNot();
testSetBit();
testClearBit();
testFlipBit();
testGetLowestSetBit();
testBitLength();
testBitCount();
}
if (subset == 0 || subset == 4) {
testToString();
testToByteArrayWithConstructor();
testIntValue();
testLongValue();
testFloatValue();
testDoubleValue();
testSerialization();
testLongValueExact();
testIntValueExact();
testShortValueExact();
testByteValueExact();
}
}
}

1 comment on commit 1e1688a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.