jdk/test/java/math/BigInteger/CompareToTests.java
author bpb
Fri, 06 Mar 2015 16:00:58 -0800
changeset 29371 6f7f029a6b63
parent 23010 6dadb192ad81
permissions -rw-r--r--
8074460: Always print seeds used in [Splittable]Random instances in java.math tests Summary: Create a utility class which creates a pseudorandom number generator (PRNG) and retains the seed. Use this class in java.math tests which use a PRNG. Always print the seed value before the PRNG is used. Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 19198
diff changeset
     2
 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     4
 *
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     8
 *
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    13
 * accompanied this code).
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    14
 *
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2923
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2923
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2923
diff changeset
    21
 * questions.
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    22
 */
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    23
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    24
/*
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    25
 * @test
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    26
 * @bug 6473768
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    27
 * @summary Tests of BigInteger.compareTo
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    28
 * @author Joseph D. Darcy
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    29
 */
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    30
import java.math.*;
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    31
import static java.math.BigInteger.*;
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    32
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    33
public class CompareToTests {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    34
    private static int compareToTests() {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    35
        int failures = 0;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    36
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    37
        final BigInteger MINUS_ONE = BigInteger.ONE.negate();
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    38
        final BigInteger TWO_POW_126 = ONE.shiftLeft(126);
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    39
        final BigInteger TWO_POW_127 = ONE.shiftLeft(127);
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    40
        final BigInteger TWO_POW_128 = ONE.shiftLeft(128);
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    41
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    42
        // First operand, second operand, expected compareTo result
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    43
        BigInteger [][] testCases = {
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    44
            // Basics
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    45
            {valueOf(0),        valueOf(0),     ZERO},
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    46
            {valueOf(0),        valueOf(1),     MINUS_ONE},
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    47
            {valueOf(1),        valueOf(2),     MINUS_ONE},
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    48
            {valueOf(2),        valueOf(1),     ONE},
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    49
            {valueOf(10),       valueOf(10),    ZERO},
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    50
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    51
            // Various relative lengths of internal mag array.
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    52
            {TWO_POW_127,                 TWO_POW_127,                 ZERO},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    53
            {TWO_POW_127.negate(),        TWO_POW_127,                 MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    54
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    55
            {TWO_POW_128.or(TWO_POW_126), TWO_POW_128,                 ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    56
            {TWO_POW_128.or(TWO_POW_126), TWO_POW_128.negate(),        ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    57
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    58
            {TWO_POW_128,                 TWO_POW_128.or(TWO_POW_126), MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    59
            {TWO_POW_128.negate(),        TWO_POW_128.or(TWO_POW_126), MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    60
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    61
            {TWO_POW_127,                 TWO_POW_128,                 MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    62
            {TWO_POW_127.negate(),        TWO_POW_128,                 MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    63
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    64
            {TWO_POW_128,                 TWO_POW_127,                 ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    65
            {TWO_POW_128.negate(),        TWO_POW_127,                 MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    66
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    67
            // Long boundary and near boundary values
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    68
            {valueOf(Long.MAX_VALUE),            valueOf(Long.MAX_VALUE), ZERO},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    69
            {valueOf(Long.MAX_VALUE).negate(),   valueOf(Long.MAX_VALUE), MINUS_ONE},
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    70
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    71
            {valueOf(Long.MAX_VALUE-1),          valueOf(Long.MAX_VALUE), MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    72
            {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    73
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    74
            {valueOf(Long.MIN_VALUE),            valueOf(Long.MAX_VALUE), MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    75
            {valueOf(Long.MIN_VALUE).negate(),   valueOf(Long.MAX_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    76
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    77
            {valueOf(Long.MIN_VALUE+1),          valueOf(Long.MAX_VALUE), MINUS_ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    78
            {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    79
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    80
            {valueOf(Long.MAX_VALUE),            valueOf(Long.MIN_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    81
            {valueOf(Long.MAX_VALUE).negate(),   valueOf(Long.MIN_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    82
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    83
            {valueOf(Long.MAX_VALUE-1),          valueOf(Long.MIN_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    84
            {valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    85
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    86
            {valueOf(Long.MIN_VALUE),            valueOf(Long.MIN_VALUE), ZERO},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    87
            {valueOf(Long.MIN_VALUE).negate(),   valueOf(Long.MIN_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    88
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    89
            {valueOf(Long.MIN_VALUE+1),          valueOf(Long.MIN_VALUE), ONE},
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    90
            {valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE},
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    91
        };
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    92
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    93
        for (BigInteger[] testCase : testCases) {
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    94
            BigInteger a = testCase[0];
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    95
            BigInteger a_negate = a.negate();
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    96
            BigInteger b = testCase[1];
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
    97
            BigInteger b_negate = b.negate();
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    98
            int expected = testCase[2].intValue();
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
    99
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   100
            failures += compareToTest(a,        b,         expected);
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   101
            failures += compareToTest(a_negate, b_negate, -expected);
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   102
        }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   103
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   104
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   105
        return failures;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   106
    }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   107
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
   108
    private static int compareToTest(BigInteger a, BigInteger b, int expected) {
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   109
        int result = a.compareTo(b);
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   110
        int failed = (result==expected) ? 0 : 1;
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
   111
        if (failed == 1) {
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   112
            System.err.println("(" + a + ").compareTo(" + b + ") => " + result +
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   113
                               "\n\tExpected " + expected);
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   114
        }
19198
513b533ed31a 8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents: 5506
diff changeset
   115
        return failed;
2923
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   116
    }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   117
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   118
    public static void main(String argv[]) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   119
        int failures = 0;
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   120
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   121
        failures += compareToTests();
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   122
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   123
        if (failures > 0) {
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   124
            throw new RuntimeException("Incurred " + failures +
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   125
                                       " failures while testing exact compareTo.");
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   126
        }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   127
    }
eca68ab5d025 6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff changeset
   128
}