8373798: Refactor java/math tests to use JUnit

Reviewed-by: darcy
This commit is contained in:
Raffaello Giulietti 2025-12-17 09:20:48 +00:00
parent af18fbd42d
commit fc76403b01
2 changed files with 111 additions and 79 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2025, 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
@ -26,45 +26,57 @@
* @bug 4259453 8200698
* @summary Test constructors of BigDecimal
* @library ..
* @run testng Constructor
* @run junit Constructor
*/
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import org.testng.annotations.Test;
public class Constructor {
@Test(expectedExceptions=NumberFormatException.class)
@Test
public void stringConstructor() {
BigDecimal bd = new BigDecimal("1.2e");
Assertions.assertThrows(NumberFormatException.class, () -> {
BigDecimal bd = new BigDecimal("1.2e");
});
}
@Test(expectedExceptions=NumberFormatException.class)
@Test
public void charArrayConstructorNegativeOffset() {
BigDecimal bd = new BigDecimal(new char[5], -1, 4, null);
Assertions.assertThrows(NumberFormatException.class, () -> {
BigDecimal bd = new BigDecimal(new char[5], -1, 4, null);
});
}
@Test(expectedExceptions=NumberFormatException.class)
@Test
public void charArrayConstructorNegativeLength() {
BigDecimal bd = new BigDecimal(new char[5], 0, -1, null);
Assertions.assertThrows(NumberFormatException.class, () -> {
BigDecimal bd = new BigDecimal(new char[5], 0, -1, null);
});
}
@Test(expectedExceptions=NumberFormatException.class)
@Test
public void charArrayConstructorIntegerOverflow() {
try {
BigDecimal bd = new BigDecimal(new char[5], Integer.MAX_VALUE - 5,
6, null);
} catch (NumberFormatException nfe) {
if (nfe.getCause() instanceof IndexOutOfBoundsException) {
throw new RuntimeException
("NumberFormatException should not have a cause");
} else {
throw nfe;
Assertions.assertThrows(NumberFormatException.class, () -> {
try {
BigDecimal bd = new BigDecimal(new char[5], Integer.MAX_VALUE - 5,
6, null);
} catch (NumberFormatException nfe) {
if (nfe.getCause() instanceof IndexOutOfBoundsException) {
throw new RuntimeException
("NumberFormatException should not have a cause");
} else {
throw nfe;
}
}
}
});
}
@Test(expectedExceptions=NumberFormatException.class)
@Test
public void charArrayConstructorIndexOutOfBounds() {
BigDecimal bd = new BigDecimal(new char[5], 1, 5, null);
Assertions.assertThrows(NumberFormatException.class, () -> {
BigDecimal bd = new BigDecimal(new char[5], 1, 5, null);
});
}
}

View File

@ -26,13 +26,16 @@
* @bug 8200698
* @summary Tests that exceptions are thrown for ops which would overflow
* @requires (sun.arch.data.model == "64" & os.maxMemory >= 4g)
* @run testng/othervm/timeout=480 -Xmx4g LargeValueExceptions
* @run junit/othervm/timeout=480 -Xmx4g LargeValueExceptions
*/
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.math.BigInteger;
import static java.math.BigInteger.ONE;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
//
// The intent of this test is to probe the boundaries between overflow and
@ -64,47 +67,48 @@ 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.
@Test(enabled=false)
@Test
@Disabled
public void squareNoOverflow() {
BigInteger x = ONE.shiftLeft(16*MAX_INTS - 1).subtract(ONE);
BigInteger y = x.multiply(x);
}
// Smallest no overflow determined by extra calculations.
@Test(enabled=false)
@Test
@Disabled
public void squareIndefiniteOverflowSuccess() {
BigInteger x = ONE.shiftLeft(16*MAX_INTS - 1);
BigInteger y = x.multiply(x);
}
// Largest overflow detected by extra calculations.
@Test(expectedExceptions=ArithmeticException.class,enabled=false)
@Test
@Disabled
public void squareIndefiniteOverflowFailure() {
BigInteger x = ONE.shiftLeft(16*MAX_INTS).subtract(ONE);
BigInteger y = x.multiply(x);
Assertions.assertThrows(ArithmeticException.class, () -> {
BigInteger x = ONE.shiftLeft(16*MAX_INTS).subtract(ONE);
BigInteger y = x.multiply(x);
});
}
// Smallest overflow detected by examining data lengths alone.
@Test(expectedExceptions=ArithmeticException.class)
@Test
public void squareDefiniteOverflow() {
BigInteger x = ONE.shiftLeft(16*MAX_INTS);
BigInteger y = x.multiply(x);
Assertions.assertThrows(ArithmeticException.class, () -> {
BigInteger x = ONE.shiftLeft(16*MAX_INTS);
BigInteger y = x.multiply(x);
});
}
// --- multiplication ---
// Largest no overflow determined by examining data lengths alone.
@Test(enabled=false)
@Test
@Disabled
public void multiplyNoOverflow() {
final int halfMaxBits = MAX_INTS_HALF << 5;
@ -114,7 +118,8 @@ public class LargeValueExceptions {
}
// Smallest no overflow determined by extra calculations.
@Test(enabled=false)
@Test
@Disabled
public void multiplyIndefiniteOverflowSuccess() {
BigInteger x = ONE.shiftLeft((int)(MAX_BITS/2) - 1);
long m = MAX_BITS - x.bitLength();
@ -130,68 +135,83 @@ public class LargeValueExceptions {
}
// Largest overflow detected by extra calculations.
@Test(expectedExceptions=ArithmeticException.class,enabled=false)
@Test
@Disabled
public void multiplyIndefiniteOverflowFailure() {
BigInteger x = ONE.shiftLeft((int)(MAX_BITS/2)).subtract(ONE);
long m = MAX_BITS - x.bitLength();
Assertions.assertThrows(ArithmeticException.class, () -> {
BigInteger x = ONE.shiftLeft((int)(MAX_BITS/2)).subtract(ONE);
long m = MAX_BITS - x.bitLength();
BigInteger y = ONE.shiftLeft((int)(MAX_BITS/2)).subtract(ONE);
long n = MAX_BITS - y.bitLength();
BigInteger y = ONE.shiftLeft((int)(MAX_BITS/2)).subtract(ONE);
long n = MAX_BITS - y.bitLength();
if (m + n != MAX_BITS) {
throw new RuntimeException("Unexpected leading zero sum");
}
if (m + n != MAX_BITS) {
throw new RuntimeException("Unexpected leading zero sum");
}
BigInteger z = x.multiply(y);
BigInteger z = x.multiply(y);
});
}
// Smallest overflow detected by examining data lengths alone.
@Test(expectedExceptions=ArithmeticException.class)
@Test
public void multiplyDefiniteOverflow() {
// multiply by 4 as MAX_INTS_HALF refers to ints
byte[] xmag = new byte[4*MAX_INTS_HALF];
xmag[0] = (byte)0xff;
BigInteger x = new BigInteger(1, xmag);
Assertions.assertThrows(ArithmeticException.class, () -> {
// multiply by 4 as MAX_INTS_HALF refers to ints
byte[] xmag = new byte[4*MAX_INTS_HALF];
xmag[0] = (byte)0xff;
BigInteger x = new BigInteger(1, xmag);
byte[] ymag = new byte[4*MAX_INTS_HALF + 1];
ymag[0] = (byte)0xff;
BigInteger y = new BigInteger(1, ymag);
byte[] ymag = new byte[4*MAX_INTS_HALF + 1];
ymag[0] = (byte)0xff;
BigInteger y = new BigInteger(1, ymag);
BigInteger z = x.multiply(y);
BigInteger z = x.multiply(y);
});
}
// --- exponentiation ---
@Test(expectedExceptions=ArithmeticException.class)
@Test
public void powOverflow() {
BigInteger.TEN.pow(Integer.MAX_VALUE);
Assertions.assertThrows(ArithmeticException.class, () -> {
BigInteger.TEN.pow(Integer.MAX_VALUE);
});
}
@Test(expectedExceptions=ArithmeticException.class)
@Test
public void powOverflow1() {
int shift = 20;
int exponent = 1 << shift;
BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent));
BigInteger y = x.pow(exponent);
Assertions.assertThrows(ArithmeticException.class, () -> {
int shift = 20;
int exponent = 1 << shift;
BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent));
BigInteger y = x.pow(exponent);
});
}
@Test(expectedExceptions=ArithmeticException.class)
@Test
public void powOverflow2() {
int shift = 20;
int exponent = 1 << shift;
BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent)).add(ONE);
BigInteger y = x.pow(exponent);
Assertions.assertThrows(ArithmeticException.class, () -> {
int shift = 20;
int exponent = 1 << shift;
BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent)).add(ONE);
BigInteger y = x.pow(exponent);
});
}
@Test(expectedExceptions=ArithmeticException.class,enabled=false)
@Test
@Disabled
public void powOverflow3() {
int shift = 20;
int exponent = 1 << shift;
BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent)).subtract(ONE);
BigInteger y = x.pow(exponent);
Assertions.assertThrows(ArithmeticException.class, () -> {
int shift = 20;
int exponent = 1 << shift;
BigInteger x = ONE.shiftLeft((int)(MAX_BITS / exponent)).subtract(ONE);
BigInteger y = x.pow(exponent);
});
}
@Test(enabled=false)
@Test
@Disabled
public void powOverflow4() {
int shift = 20;
int exponent = 1 << shift;