1247N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
1247N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1247N/A *
1247N/A * This code is free software; you can redistribute it and/or modify it
1247N/A * under the terms of the GNU General Public License version 2 only, as
1247N/A * published by the Free Software Foundation.
1247N/A *
1247N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1247N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1247N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1247N/A * version 2 for more details (a copy is included in the LICENSE file that
1247N/A * accompanied this code).
1247N/A *
1247N/A * You should have received a copy of the GNU General Public License version
1247N/A * 2 along with this work; if not, write to the Free Software Foundation,
1247N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1247N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1247N/A */
1247N/A
1247N/A/*
1247N/A * @test
1247N/A * @bug 6473768
1247N/A * @summary Tests of BigDecimal.compareTo
1247N/A * @author Joseph D. Darcy
1247N/A */
1247N/Aimport java.math.*;
1247N/Aimport static java.math.BigDecimal.*;
1247N/A
1247N/Apublic class CompareToTests {
1247N/A private static int compareToTests() {
1247N/A int failures = 0;
1247N/A
1247N/A final BigDecimal MINUS_ONE = BigDecimal.ONE.negate();
1247N/A
1247N/A // First operand, second operand, expected compareTo result
1247N/A BigDecimal [][] testCases = {
1247N/A // Basics
1247N/A {valueOf(0), valueOf(0), ZERO},
1247N/A {valueOf(0), valueOf(1), MINUS_ONE},
1247N/A {valueOf(1), valueOf(2), MINUS_ONE},
1247N/A {valueOf(2), valueOf(1), ONE},
1247N/A {valueOf(10), valueOf(10), ZERO},
1247N/A
1247N/A // Significands would compare differently than scaled value
1247N/A {valueOf(2,1), valueOf(2), MINUS_ONE},
1247N/A {valueOf(2,-1), valueOf(2), ONE},
1247N/A {valueOf(1,1), valueOf(2), MINUS_ONE},
1247N/A {valueOf(1,-1), valueOf(2), ONE},
1247N/A {valueOf(5,-1), valueOf(2), ONE},
1247N/A
1247N/A // Boundary and near boundary values
1247N/A {valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO},
1247N/A {valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE},
1247N/A {valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE},
1247N/A {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE},
1247N/A {valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO},
1247N/A {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), ONE},
1247N/A };
1247N/A
1247N/A for (BigDecimal[] testCase : testCases) {
1247N/A BigDecimal a = testCase[0];
1247N/A BigDecimal a_negate = a.negate();
1247N/A BigDecimal b = testCase[1];
1247N/A BigDecimal b_negate = b.negate();
1247N/A int expected = testCase[2].intValue();
1247N/A
1247N/A failures += compareToTest(a, b, expected);
1247N/A failures += compareToTest(a_negate, b, -1);
1247N/A failures += compareToTest(a, b_negate, 1);
1247N/A failures += compareToTest(a_negate, b_negate, -expected);
1247N/A }
1247N/A
1247N/A
1247N/A return failures;
1247N/A }
1247N/A
1247N/A private static int compareToTest(BigDecimal a, BigDecimal b, int expected) {
1247N/A int result = a.compareTo(b);
1247N/A int failed = (result==expected) ? 0 : 1;
1247N/A if (result == 1) {
1247N/A System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
1247N/A "\n\tExpected " + expected);
1247N/A }
1247N/A return result;
1247N/A }
1247N/A
1247N/A public static void main(String argv[]) {
1247N/A int failures = 0;
1247N/A
1247N/A failures += compareToTests();
1247N/A
1247N/A if (failures > 0) {
1247N/A throw new RuntimeException("Incurred " + failures +
1247N/A " failures while testing exact compareTo.");
1247N/A }
1247N/A }
1247N/A}