mirror of
https://github.com/openjdk/jdk.git
synced 2026-07-31 13:15:26 +00:00
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
Fail test if errors; fix test values; port BigDecimal version to BigInteger Reviewed-by: smarks, alanb
This commit is contained in:
parent
b661fb4e1a
commit
aafacb6fa6
@ -53,12 +53,29 @@ public class CompareToTests {
|
||||
{valueOf(5,-1), valueOf(2), ONE},
|
||||
|
||||
// Boundary and near boundary values
|
||||
{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},
|
||||
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), ONE},
|
||||
{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},
|
||||
{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
|
||||
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO},
|
||||
|
||||
{valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE},
|
||||
{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE},
|
||||
{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},
|
||||
{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE},
|
||||
{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
};
|
||||
|
||||
for (BigDecimal[] testCase : testCases) {
|
||||
@ -69,8 +86,6 @@ public class CompareToTests {
|
||||
int expected = testCase[2].intValue();
|
||||
|
||||
failures += compareToTest(a, b, expected);
|
||||
failures += compareToTest(a_negate, b, -1);
|
||||
failures += compareToTest(a, b_negate, 1);
|
||||
failures += compareToTest(a_negate, b_negate, -expected);
|
||||
}
|
||||
|
||||
@ -81,11 +96,11 @@ public class CompareToTests {
|
||||
private static int compareToTest(BigDecimal a, BigDecimal b, int expected) {
|
||||
int result = a.compareTo(b);
|
||||
int failed = (result==expected) ? 0 : 1;
|
||||
if (result == 1) {
|
||||
if (failed == 1) {
|
||||
System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
|
||||
"\n\tExpected " + expected);
|
||||
}
|
||||
return result;
|
||||
return failed;
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
@ -24,20 +24,23 @@
|
||||
/*
|
||||
* @test
|
||||
* @bug 6473768
|
||||
* @summary Tests of BigDecimal.compareTo
|
||||
* @summary Tests of BigInteger.compareTo
|
||||
* @author Joseph D. Darcy
|
||||
*/
|
||||
import java.math.*;
|
||||
import static java.math.BigDecimal.*;
|
||||
import static java.math.BigInteger.*;
|
||||
|
||||
public class CompareToTests {
|
||||
private static int compareToTests() {
|
||||
int failures = 0;
|
||||
|
||||
final BigDecimal MINUS_ONE = BigDecimal.ONE.negate();
|
||||
final BigInteger MINUS_ONE = BigInteger.ONE.negate();
|
||||
final BigInteger TWO_POW_126 = ONE.shiftLeft(126);
|
||||
final BigInteger TWO_POW_127 = ONE.shiftLeft(127);
|
||||
final BigInteger TWO_POW_128 = ONE.shiftLeft(128);
|
||||
|
||||
// First operand, second operand, expected compareTo result
|
||||
BigDecimal [][] testCases = {
|
||||
BigInteger [][] testCases = {
|
||||
// Basics
|
||||
{valueOf(0), valueOf(0), ZERO},
|
||||
{valueOf(0), valueOf(1), MINUS_ONE},
|
||||
@ -45,32 +48,56 @@ public class CompareToTests {
|
||||
{valueOf(2), valueOf(1), ONE},
|
||||
{valueOf(10), valueOf(10), ZERO},
|
||||
|
||||
// Significands would compare differently than scaled value
|
||||
{valueOf(2,1), valueOf(2), MINUS_ONE},
|
||||
{valueOf(2,-1), valueOf(2), ONE},
|
||||
{valueOf(1,1), valueOf(2), MINUS_ONE},
|
||||
{valueOf(1,-1), valueOf(2), ONE},
|
||||
{valueOf(5,-1), valueOf(2), ONE},
|
||||
// Various relative lengths of internal mag array.
|
||||
{TWO_POW_127, TWO_POW_127, ZERO},
|
||||
{TWO_POW_127.negate(), TWO_POW_127, MINUS_ONE},
|
||||
|
||||
// Boundary and near boundary values
|
||||
{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},
|
||||
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), ONE},
|
||||
{TWO_POW_128.or(TWO_POW_126), TWO_POW_128, ONE},
|
||||
{TWO_POW_128.or(TWO_POW_126), TWO_POW_128.negate(), ONE},
|
||||
|
||||
{TWO_POW_128, TWO_POW_128.or(TWO_POW_126), MINUS_ONE},
|
||||
{TWO_POW_128.negate(), TWO_POW_128.or(TWO_POW_126), MINUS_ONE},
|
||||
|
||||
{TWO_POW_127, TWO_POW_128, MINUS_ONE},
|
||||
{TWO_POW_127.negate(), TWO_POW_128, MINUS_ONE},
|
||||
|
||||
{TWO_POW_128, TWO_POW_127, ONE},
|
||||
{TWO_POW_128.negate(), TWO_POW_127, MINUS_ONE},
|
||||
|
||||
// Long boundary and near boundary values
|
||||
{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},
|
||||
{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
|
||||
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},
|
||||
{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO},
|
||||
|
||||
{valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE},
|
||||
{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE},
|
||||
{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},
|
||||
{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
|
||||
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE},
|
||||
{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE},
|
||||
};
|
||||
|
||||
for (BigDecimal[] testCase : testCases) {
|
||||
BigDecimal a = testCase[0];
|
||||
BigDecimal a_negate = a.negate();
|
||||
BigDecimal b = testCase[1];
|
||||
BigDecimal b_negate = b.negate();
|
||||
for (BigInteger[] testCase : testCases) {
|
||||
BigInteger a = testCase[0];
|
||||
BigInteger a_negate = a.negate();
|
||||
BigInteger b = testCase[1];
|
||||
BigInteger b_negate = b.negate();
|
||||
int expected = testCase[2].intValue();
|
||||
|
||||
failures += compareToTest(a, b, expected);
|
||||
failures += compareToTest(a_negate, b, -1);
|
||||
failures += compareToTest(a, b_negate, 1);
|
||||
failures += compareToTest(a_negate, b_negate, -expected);
|
||||
}
|
||||
|
||||
@ -78,14 +105,14 @@ public class CompareToTests {
|
||||
return failures;
|
||||
}
|
||||
|
||||
private static int compareToTest(BigDecimal a, BigDecimal b, int expected) {
|
||||
private static int compareToTest(BigInteger a, BigInteger b, int expected) {
|
||||
int result = a.compareTo(b);
|
||||
int failed = (result==expected) ? 0 : 1;
|
||||
if (result == 1) {
|
||||
if (failed == 1) {
|
||||
System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
|
||||
"\n\tExpected " + expected);
|
||||
}
|
||||
return result;
|
||||
return failed;
|
||||
}
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user