jdk/test/java/math/BigInteger/BitLengthOverflow.java
author bpb
Wed, 30 Oct 2013 17:45:12 -0700
changeset 21420 a56f40ab71ce
child 24030 1b54fabeee2e
permissions -rw-r--r--
6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers 8021203: BigInteger.doubleValue/floatValue returns 0.0 instead of Infinity 8021204: Constructor BigInteger(String val, int radix) doesn't detect overflow 8022780: Incorrect BigInteger division because of MutableBigInteger.bitLength() overflow Summary: Prevent construction of overflowed BigIntegers. Reviewed-by: bpb, darcy, psandoz Contributed-by: Dmitry Nadezhin <dmitry.nadezhin@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21420
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     1
/*
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     4
 *
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     8
 *
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    13
 * accompanied this code).
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    14
 *
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    18
 *
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    21
 * questions.
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    22
 */
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    23
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    24
/*
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    25
 * @ test
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    26
 * @bug 6910473
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    27
 * @summary Test that bitLength() is not negative
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    28
 * @author Dmitry Nadezhin
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    29
 */
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    30
import java.math.BigInteger;
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    31
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    32
public class BitLengthOverflow {
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    33
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    34
    public static void main(String[] args) {
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    35
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    36
        try {
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    37
            BigInteger x = BigInteger.ONE.shiftLeft(Integer.MAX_VALUE); // x = pow(2,Integer.MAX_VALUE)
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    38
            if (x.bitLength() != (1L << 31))
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    39
                throw new RuntimeException("Incorrect bitLength() " + x.bitLength());
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    40
            System.out.println("Surprisingly passed with correct bitLength() " + x.bitLength());
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    41
        } catch (ArithmeticException e) {
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    42
            // expected
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    43
            System.out.println("Overflow is reported by ArithmeticException, as expected");
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    44
        } catch (OutOfMemoryError e) {
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    45
            // possible
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    46
            System.out.println("OutOfMemoryError");
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    47
        }
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    48
    }
a56f40ab71ce 6910473: java.math.BigInteger.bitLength() may return negative "int" on large numbers
bpb
parents:
diff changeset
    49
}