Path: blob/master/test/jdk/java/math/BigDecimal/CompareToTests.java
41149 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 BigDecimal.compareTo27* @author Joseph D. Darcy28*/29import java.math.*;30import static java.math.BigDecimal.*;3132public class CompareToTests {33private static int compareToTests() {34int failures = 0;3536final BigDecimal MINUS_ONE = BigDecimal.ONE.negate();3738// First operand, second operand, expected compareTo result39BigDecimal [][] testCases = {40// Basics41{valueOf(0), valueOf(0), ZERO},42{valueOf(0), valueOf(1), MINUS_ONE},43{valueOf(1), valueOf(2), MINUS_ONE},44{valueOf(2), valueOf(1), ONE},45{valueOf(10), valueOf(10), ZERO},4647// Significands would compare differently than scaled value48{valueOf(2,1), valueOf(2), MINUS_ONE},49{valueOf(2,-1), valueOf(2), ONE},50{valueOf(1,1), valueOf(2), MINUS_ONE},51{valueOf(1,-1), valueOf(2), ONE},52{valueOf(5,-1), valueOf(2), ONE},5354// Boundary and near boundary values55{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},56{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},5758{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},59{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},6061{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},62{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE},6364{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},65{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO},6667{valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE},68{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},6970{valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE},71{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE},7273{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},74{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE},7576{valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE},77{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE},78};7980for (BigDecimal[] testCase : testCases) {81BigDecimal a = testCase[0];82BigDecimal a_negate = a.negate();83BigDecimal b = testCase[1];84BigDecimal b_negate = b.negate();85int expected = testCase[2].intValue();8687failures += compareToTest(a, b, expected);88failures += compareToTest(a_negate, b_negate, -expected);89}909192return failures;93}9495private static int compareToTest(BigDecimal a, BigDecimal b, int expected) {96int result = a.compareTo(b);97int failed = (result==expected) ? 0 : 1;98if (failed == 1) {99System.err.println("(" + a + ").compareTo(" + b + ") => " + result +100"\n\tExpected " + expected);101}102return failed;103}104105public static void main(String argv[]) {106int failures = 0;107108failures += compareToTests();109110if (failures > 0) {111throw new RuntimeException("Incurred " + failures +112" failures while testing exact compareTo.");113}114}115}116117118