changeset 9505:dd1040690e31

8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect Summary: Fail test if errors; fix test values; port BigDecimal version to BigInteger Reviewed-by: smarks, alanb Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>
author bpb
date Fri, 02 Aug 2013 11:10:41 -0700
parents 883cc296ec89
children 80da091343af
files test/java/math/BigDecimal/CompareToTests.java test/java/math/BigInteger/CompareToTests.java
diffstat 2 files changed, 79 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/math/BigDecimal/CompareToTests.java	Fri Aug 02 15:30:11 2013 -0700
+++ b/test/java/math/BigDecimal/CompareToTests.java	Fri Aug 02 11:10:41 2013 -0700
@@ -53,12 +53,29 @@
             {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 @@
             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 @@
     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[]) {
--- a/test/java/math/BigInteger/CompareToTests.java	Fri Aug 02 15:30:11 2013 -0700
+++ b/test/java/math/BigInteger/CompareToTests.java	Fri Aug 02 11:10:41 2013 -0700
@@ -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 @@
             {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 @@
         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[]) {