test/java/math/BigInteger/CompareToTests.java

Print this page
rev 7751 : 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
Summary: Fail test if errors; fix test values; port BigDecimal version to BigInteger
Reviewed-by: smarks
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6473768
  27  * @summary Tests of BigDecimal.compareTo
  28  * @author Joseph D. Darcy
  29  */
  30 import java.math.*;
  31 import static java.math.BigDecimal.*;
  32 
  33 public class CompareToTests {
  34     private static int compareToTests() {
  35         int failures = 0;
  36 
  37         final BigDecimal MINUS_ONE = BigDecimal.ONE.negate();



  38 
  39         // First operand, second operand, expected compareTo result
  40         BigDecimal [][] testCases = {
  41             // Basics
  42             {valueOf(0),        valueOf(0),     ZERO},
  43             {valueOf(0),        valueOf(1),     MINUS_ONE},
  44             {valueOf(1),        valueOf(2),     MINUS_ONE},
  45             {valueOf(2),        valueOf(1),     ONE},
  46             {valueOf(10),       valueOf(10),    ZERO},
  47 
  48             // Significands would compare differently than scaled value
  49             {valueOf(2,1),      valueOf(2),     MINUS_ONE},
  50             {valueOf(2,-1),     valueOf(2),     ONE},
  51             {valueOf(1,1),      valueOf(2),     MINUS_ONE},
  52             {valueOf(1,-1),     valueOf(2),     ONE},
  53             {valueOf(5,-1),     valueOf(2),     ONE},
  54 
  55             // Boundary and near boundary values












  56             {valueOf(Long.MAX_VALUE),   valueOf(Long.MAX_VALUE),        ZERO},


  57             {valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE),        MINUS_ONE},


  58             {valueOf(Long.MIN_VALUE),   valueOf(Long.MAX_VALUE),        MINUS_ONE},


  59             {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE),        MINUS_ONE},








  60             {valueOf(Long.MIN_VALUE),   valueOf(Long.MIN_VALUE),        ZERO},
  61             {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE),        ONE},



  62         };
  63 
  64         for (BigDecimal[] testCase : testCases) {
  65             BigDecimal a = testCase[0];
  66             BigDecimal a_negate = a.negate();
  67             BigDecimal b = testCase[1];
  68             BigDecimal b_negate = b.negate();
  69             int expected = testCase[2].intValue();
  70 
  71             failures += compareToTest(a,        b,         expected);
  72             failures += compareToTest(a_negate, b,        -1);
  73             failures += compareToTest(a,        b_negate,  1);
  74             failures += compareToTest(a_negate, b_negate, -expected);
  75         }
  76 
  77 
  78         return failures;
  79     }
  80 
  81     private static int compareToTest(BigDecimal a, BigDecimal b, int expected) {
  82         int result = a.compareTo(b);
  83         int failed = (result==expected) ? 0 : 1;
  84         if (result == 1) {
  85             System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
  86                                "\n\tExpected " + expected);
  87         }
  88         return result;
  89     }
  90 
  91     public static void main(String argv[]) {
  92         int failures = 0;
  93 
  94         failures += compareToTests();
  95 
  96         if (failures > 0) {
  97             throw new RuntimeException("Incurred " + failures +
  98                                        " failures while testing exact compareTo.");
  99         }
 100     }
 101 }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6473768
  27  * @summary Tests of BigInteger.compareTo
  28  * @author Joseph D. Darcy
  29  */
  30 import java.math.*;
  31 import static java.math.BigInteger.*;
  32 
  33 public class CompareToTests {
  34     private static int compareToTests() {
  35         int failures = 0;
  36 
  37         final BigInteger MINUS_ONE = BigInteger.ONE.negate();
  38         final BigInteger TWO_POW_126 = ONE.shiftLeft(126);
  39         final BigInteger TWO_POW_127 = ONE.shiftLeft(127);
  40         final BigInteger TWO_POW_128 = ONE.shiftLeft(128);
  41 
  42         // First operand, second operand, expected compareTo result
  43         BigInteger [][] testCases = {
  44             // Basics
  45             {valueOf(0),        valueOf(0),     ZERO},
  46             {valueOf(0),        valueOf(1),     MINUS_ONE},
  47             {valueOf(1),        valueOf(2),     MINUS_ONE},
  48             {valueOf(2),        valueOf(1),     ONE},
  49             {valueOf(10),       valueOf(10),    ZERO},
  50 
  51             // Various relative lengths of internal mag array.
  52             {TWO_POW_127,                 TWO_POW_127,                 ZERO},
  53             {TWO_POW_127.negate(),        TWO_POW_127,                 MINUS_ONE},



  54 
  55             {TWO_POW_128.or(TWO_POW_126), TWO_POW_128,                 ONE},
  56             {TWO_POW_128.or(TWO_POW_126), TWO_POW_128.negate(),        ONE},
  57 
  58             {TWO_POW_128,                 TWO_POW_128.or(TWO_POW_126), MINUS_ONE},
  59             {TWO_POW_128.negate(),        TWO_POW_128.or(TWO_POW_126), MINUS_ONE},
  60 
  61             {TWO_POW_127,                 TWO_POW_128,                 MINUS_ONE},
  62             {TWO_POW_127.negate(),        TWO_POW_128,                 MINUS_ONE},
  63 
  64             {TWO_POW_128,                 TWO_POW_127,                 ONE},
  65             {TWO_POW_128.negate(),        TWO_POW_127,                 MINUS_ONE},
  66 
  67             // Long boundary and near boundary values
  68             {valueOf(Long.MAX_VALUE),            valueOf(Long.MAX_VALUE), ZERO},
  69             {valueOf(Long.MAX_VALUE).negate(),   valueOf(Long.MAX_VALUE), MINUS_ONE},
  70 
  71             {valueOf(Long.MAX_VALUE-1),          valueOf(Long.MAX_VALUE), MINUS_ONE},
  72             {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},
  73 
  74             {valueOf(Long.MIN_VALUE),            valueOf(Long.MAX_VALUE), MINUS_ONE},
  75             {valueOf(Long.MIN_VALUE).negate(),   valueOf(Long.MAX_VALUE), ONE},
  76 
  77             {valueOf(Long.MIN_VALUE+1),          valueOf(Long.MAX_VALUE), MINUS_ONE},
  78             {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO},
  79 
  80             {valueOf(Long.MAX_VALUE),            valueOf(Long.MIN_VALUE), ONE},
  81             {valueOf(Long.MAX_VALUE).negate(),   valueOf(Long.MIN_VALUE), ONE},
  82 
  83             {valueOf(Long.MAX_VALUE-1),          valueOf(Long.MIN_VALUE), ONE},
  84             {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE},
  85 
  86             {valueOf(Long.MIN_VALUE),            valueOf(Long.MIN_VALUE), ZERO},
  87             {valueOf(Long.MIN_VALUE).negate(),   valueOf(Long.MIN_VALUE), ONE},
  88 
  89             {valueOf(Long.MIN_VALUE+1),          valueOf(Long.MIN_VALUE), ONE},
  90             {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE},
  91         };
  92 
  93         for (BigInteger[] testCase : testCases) {
  94             BigInteger a = testCase[0];
  95             BigInteger a_negate = a.negate();
  96             BigInteger b = testCase[1];
  97             BigInteger b_negate = b.negate();
  98             int expected = testCase[2].intValue();
  99 
 100             failures += compareToTest(a,        b,         expected);


 101             failures += compareToTest(a_negate, b_negate, -expected);
 102         }
 103 
 104 
 105         return failures;
 106     }
 107 
 108     private static int compareToTest(BigInteger a, BigInteger b, int expected) {
 109         int result = a.compareTo(b);
 110         int failed = (result==expected) ? 0 : 1;
 111         if (failed == 1) {
 112             System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
 113                                "\n\tExpected " + expected);
 114         }
 115         return failed;
 116     }
 117 
 118     public static void main(String argv[]) {
 119         int failures = 0;
 120 
 121         failures += compareToTests();
 122 
 123         if (failures > 0) {
 124             throw new RuntimeException("Incurred " + failures +
 125                                        " failures while testing exact compareTo.");
 126         }
 127     }
 128 }