Path: blob/master/test/jdk/java/math/BigInteger/CompareToTests.java
41152 views
/*1* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 647376826* @summary Tests of BigInteger.compareTo27* @author Joseph D. Darcy28*/29import java.math.*;30import static java.math.BigInteger.*;3132public class CompareToTests {33private static int compareToTests() {34int failures = 0;3536final BigInteger MINUS_ONE = BigInteger.ONE.negate();37final BigInteger TWO_POW_126 = ONE.shiftLeft(126);38final BigInteger TWO_POW_127 = ONE.shiftLeft(127);39final BigInteger TWO_POW_128 = ONE.shiftLeft(128);4041// First operand, second operand, expected compareTo result42BigInteger [][] testCases = {43// Basics44{valueOf(0), valueOf(0), ZERO},45{valueOf(0), valueOf(1), MINUS_ONE},46{valueOf(1), valueOf(2), MINUS_ONE},47{valueOf(2), valueOf(1), ONE},48{valueOf(10), valueOf(10), ZERO},4950// Various relative lengths of internal mag array.51{TWO_POW_127, TWO_POW_127, ZERO},52{TWO_POW_127.negate(), TWO_POW_127, MINUS_ONE},5354{TWO_POW_128.or(TWO_POW_126), TWO_POW_128, ONE},55{TWO_POW_128.or(TWO_POW_126), TWO_POW_128.negate(), ONE},5657{TWO_POW_128, TWO_POW_128.or(TWO_POW_126), MINUS_ONE},58{TWO_POW_128.negate(), TWO_POW_128.or(TWO_POW_126), MINUS_ONE},5960{TWO_POW_127, TWO_POW_128, MINUS_ONE},61{TWO_POW_127.negate(), TWO_POW_128, MINUS_ONE},6263{TWO_POW_128, TWO_POW_127, ONE},64{TWO_POW_128.negate(), TWO_POW_127, MINUS_ONE},6566// Long boundary and near boundary values67{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},68{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},6970{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},71{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},7273{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},74{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE},7576{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},77{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO},7879{valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE},80{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},8182{valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE},83{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE},8485{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},86{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},8788{valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE},89{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE},90};9192for (BigInteger[] testCase : testCases) {93BigInteger a = testCase[0];94BigInteger a_negate = a.negate();95BigInteger b = testCase[1];96BigInteger b_negate = b.negate();97int expected = testCase[2].intValue();9899failures += compareToTest(a, b, expected);100failures += compareToTest(a_negate, b_negate, -expected);101}102103104return failures;105}106107private static int compareToTest(BigInteger a, BigInteger b, int expected) {108int result = a.compareTo(b);109int failed = (result==expected) ? 0 : 1;110if (failed == 1) {111System.err.println("(" + a + ").compareTo(" + b + ") => " + result +112"\n\tExpected " + expected);113}114return failed;115}116117public static void main(String argv[]) {118int failures = 0;119120failures += compareToTests();121122if (failures > 0) {123throw new RuntimeException("Incurred " + failures +124" failures while testing exact compareTo.");125}126}127}128129130