1 /* 2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 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 package test.java.math.BigInteger; 24 25 /* 26 * @test 27 * @bug 6473768 28 * @summary Tests of BigInteger.compareTo 29 * @author Joseph D. Darcy 30 */ 31 import java.math.*; 32 33 import org.testng.Assert; 34 import org.testng.annotations.Test; 35 36 import static java.math.BigInteger.*; 37 38 // Android-changed: Replace error printing with asserts. 39 public class CompareToTests { 40 41 @Test compareToTests()42 public void compareToTests() { 43 final BigInteger MINUS_ONE = BigInteger.ONE.negate(); 44 final BigInteger TWO_POW_126 = ONE.shiftLeft(126); 45 final BigInteger TWO_POW_127 = ONE.shiftLeft(127); 46 final BigInteger TWO_POW_128 = ONE.shiftLeft(128); 47 48 // First operand, second operand, expected compareTo result 49 BigInteger [][] testCases = { 50 // Basics 51 {valueOf(0), valueOf(0), ZERO}, 52 {valueOf(0), valueOf(1), MINUS_ONE}, 53 {valueOf(1), valueOf(2), MINUS_ONE}, 54 {valueOf(2), valueOf(1), ONE}, 55 {valueOf(10), valueOf(10), ZERO}, 56 57 // Various relative lengths of internal mag array. 58 {TWO_POW_127, TWO_POW_127, ZERO}, 59 {TWO_POW_127.negate(), TWO_POW_127, MINUS_ONE}, 60 61 {TWO_POW_128.or(TWO_POW_126), TWO_POW_128, ONE}, 62 {TWO_POW_128.or(TWO_POW_126), TWO_POW_128.negate(), ONE}, 63 64 {TWO_POW_128, TWO_POW_128.or(TWO_POW_126), MINUS_ONE}, 65 {TWO_POW_128.negate(), TWO_POW_128.or(TWO_POW_126), MINUS_ONE}, 66 67 {TWO_POW_127, TWO_POW_128, MINUS_ONE}, 68 {TWO_POW_127.negate(), TWO_POW_128, MINUS_ONE}, 69 70 {TWO_POW_128, TWO_POW_127, ONE}, 71 {TWO_POW_128.negate(), TWO_POW_127, MINUS_ONE}, 72 73 // Long boundary and near boundary values 74 {valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO}, 75 {valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE}, 76 77 {valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE}, 78 {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE}, 79 80 {valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE}, 81 {valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE}, 82 83 {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE}, 84 {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO}, 85 86 {valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE}, 87 {valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE}, 88 89 {valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE}, 90 {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE}, 91 92 {valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO}, 93 {valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE}, 94 95 {valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE}, 96 {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE}, 97 }; 98 99 for (BigInteger[] testCase : testCases) { 100 BigInteger a = testCase[0]; 101 BigInteger a_negate = a.negate(); 102 BigInteger b = testCase[1]; 103 BigInteger b_negate = b.negate(); 104 int expected = testCase[2].intValue(); 105 106 compareToTest(a, b, expected); 107 compareToTest(a_negate, b_negate, -expected); 108 } 109 } 110 compareToTest(BigInteger a, BigInteger b, int expected)111 private static void compareToTest(BigInteger a, BigInteger b, int expected) { 112 int result = a.compareTo(b); 113 int failed = (result==expected) ? 0 : 1; 114 if (failed == 1) { 115 Assert.fail("(" + a + ").compareTo(" + b + ") => " + result + 116 "\n\tExpected " + expected); 117 } 118 } 119 120 } 121