jdk/test/java/math/BigDecimal/CompareToTests.java
author darcy
Mon, 26 Jan 2009 19:49:26 -0800
changeset 1826 39d505a353e8
child 5506 202f599c92aa
permissions -rw-r--r--
6601457: Move wrapper class tests from closed to open 6601458: Move java.math tests from closed to open 6740185: Move java/lang/annotations tests to open 6759433: Move Math and StrictMath regression tests from closed to open Summary: Move some more regression tests to the open Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1826
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     1
/*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     4
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     8
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    13
 * accompanied this code).
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    14
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    18
 *
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    21
 * have any questions.
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    22
 */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    23
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    24
/*
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    25
 * @test
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    26
 * @bug 6473768
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    27
 * @summary Tests of BigDecimal.compareTo
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    28
 * @author Joseph D. Darcy
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    29
 */
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    30
import java.math.*;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    31
import static java.math.BigDecimal.*;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    32
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    33
public class CompareToTests {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    34
    private static int compareToTests() {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    35
        int failures = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    36
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    37
        final BigDecimal MINUS_ONE = BigDecimal.ONE.negate();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    38
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    39
        // First operand, second operand, expected compareTo result
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    40
        BigDecimal [][] testCases = {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    41
            // Basics
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    42
            {valueOf(0),        valueOf(0),     ZERO},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    43
            {valueOf(0),        valueOf(1),     MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    44
            {valueOf(1),        valueOf(2),     MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    45
            {valueOf(2),        valueOf(1),     ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    46
            {valueOf(10),       valueOf(10),    ZERO},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    47
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    48
            // Significands would compare differently than scaled value
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    49
            {valueOf(2,1),      valueOf(2),     MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    50
            {valueOf(2,-1),     valueOf(2),     ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    51
            {valueOf(1,1),      valueOf(2),     MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    52
            {valueOf(1,-1),     valueOf(2),     ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    53
            {valueOf(5,-1),     valueOf(2),     ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    54
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    55
            // Boundary and near boundary values
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    56
            {valueOf(Long.MAX_VALUE),   valueOf(Long.MAX_VALUE),        ZERO},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    57
            {valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE),        MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    58
            {valueOf(Long.MIN_VALUE),   valueOf(Long.MAX_VALUE),        MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    59
            {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE),        MINUS_ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    60
            {valueOf(Long.MIN_VALUE),   valueOf(Long.MIN_VALUE),        ZERO},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    61
            {valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE),        ONE},
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    62
        };
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    63
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    64
        for (BigDecimal[] testCase : testCases) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    65
            BigDecimal a = testCase[0];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    66
            BigDecimal a_negate = a.negate();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    67
            BigDecimal b = testCase[1];
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    68
            BigDecimal b_negate = b.negate();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    69
            int expected = testCase[2].intValue();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    70
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    71
            failures += compareToTest(a,        b,         expected);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    72
            failures += compareToTest(a_negate, b,        -1);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    73
            failures += compareToTest(a,        b_negate,  1);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    74
            failures += compareToTest(a_negate, b_negate, -expected);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    75
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    76
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    77
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    78
        return failures;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    79
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    80
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    81
    private static int compareToTest(BigDecimal a, BigDecimal b, int expected) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    82
        int result = a.compareTo(b);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    83
        int failed = (result==expected) ? 0 : 1;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    84
        if (result == 1) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    85
            System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    86
                               "\n\tExpected " + expected);
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    87
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    88
        return result;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    89
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    90
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    91
    public static void main(String argv[]) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    92
        int failures = 0;
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    93
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    94
        failures += compareToTests();
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    95
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    96
        if (failures > 0) {
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    97
            throw new RuntimeException("Incurred " + failures +
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    98
                                       " failures while testing exact compareTo.");
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
    99
        }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   100
    }
39d505a353e8 6601457: Move wrapper class tests from closed to open
darcy
parents:
diff changeset
   101
}