jdk/test/java/math/BigInteger/PrimitiveConversionTests.java
author malenkov
Fri, 16 May 2014 15:51:57 +0400
changeset 25088 8d4b058368f0
parent 18538 642cc17315fd
permissions -rw-r--r--
8043152: KSS: javax.swing.plaf.synth.SynthContext Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18538
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     1
/*
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     4
 *
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     8
 *
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    13
 * accompanied this code).
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    14
 *
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    18
 *
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    21
 * questions.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    22
 */
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    23
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    24
import static java.math.BigInteger.ONE;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    25
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    26
import java.math.BigInteger;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    27
import java.util.ArrayList;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    28
import java.util.Arrays;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    29
import java.util.Collections;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    30
import java.util.List;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    31
import java.util.Random;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    32
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    33
/**
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    34
 * @test
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    35
 * @bug 7131192
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    36
 * @summary This test ensures that BigInteger.floatValue() and
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    37
 *          BigInteger.doubleValue() behave correctly.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    38
 * @author Louis Wasserman
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    39
 */
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    40
public class PrimitiveConversionTests {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    41
    static final List<BigInteger> ALL_BIGINTEGER_CANDIDATES;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    42
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    43
    static {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    44
        List<BigInteger> samples = new ArrayList<>();
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    45
        // Now add values near 2^N for lots of values of N.
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    46
        for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33,
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    47
                34, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511,
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    48
                512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT,
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    49
                Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    50
            BigInteger x = ONE.shiftLeft(exponent);
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    51
            for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    52
                samples.add(y);
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    53
                samples.add(y.negate());
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    54
            }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    55
        }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    56
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    57
        Random rng = new Random(1234567);
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    58
        for (int i = 0; i < 2000; i++) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    59
            samples.add(new BigInteger(rng.nextInt(2000), rng));
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    60
        }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    61
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    62
        ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples);
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    63
    }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    64
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    65
    public static int testDoubleValue() {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    66
        int failures = 0;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    67
        for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    68
            double expected = Double.parseDouble(big.toString());
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    69
            double actual = big.doubleValue();
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    70
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    71
            // should be bitwise identical
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    72
            if (Double.doubleToRawLongBits(expected) != Double
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    73
                    .doubleToRawLongBits(actual)) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    74
                System.out.println(big);
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    75
                failures++;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    76
            }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    77
        }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    78
        return failures;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    79
    }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    80
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    81
    public static int testFloatValue() {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    82
        int failures = 0;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    83
        for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    84
            float expected = Float.parseFloat(big.toString());
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    85
            float actual = big.floatValue();
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    86
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    87
            // should be bitwise identical
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    88
            if (Float.floatToRawIntBits(expected) != Float
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    89
                    .floatToRawIntBits(actual)) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    90
                System.out.println(big + " " + expected + " " + actual);
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    91
                failures++;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    92
            }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    93
        }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    94
        return failures;
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    95
    }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    96
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    97
    public static void main(String[] args) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    98
        int failures = testDoubleValue();
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
    99
        failures += testFloatValue();
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
   100
        if (failures > 0) {
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
   101
            throw new RuntimeException("Incurred " + failures
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
   102
                    + " failures while testing primitive conversions.");
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
   103
        }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
   104
    }
642cc17315fd 7131192: BigInteger.doubleValue() is depressingly slow
bpb
parents:
diff changeset
   105
}