test/jdk/java/math/BigInteger/DivisionOverflow.java
branchihse-cflags-rewrite-branch
changeset 58734 c4be316c3df4
parent 58733 fd8dc801ef32
parent 58732 d2da05214592
child 58736 e878a0b7cff0
equal deleted inserted replaced
58733:fd8dc801ef32 58734:c4be316c3df4
     1 /*
       
     2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8022780
       
    27  * @summary Test division of large values
       
    28  * @run main/othervm -Xshare:off DivisionOverflow
       
    29  * @author Dmitry Nadezhin
       
    30  */
       
    31 import java.math.BigInteger;
       
    32 
       
    33 public class DivisionOverflow {
       
    34 
       
    35     public static void main(String[] args) {
       
    36         try {
       
    37             BigInteger a = BigInteger.ONE.shiftLeft(2147483646);
       
    38             BigInteger b = BigInteger.ONE.shiftLeft(1568);
       
    39             BigInteger[] qr = a.divideAndRemainder(b);
       
    40             BigInteger q = qr[0];
       
    41             BigInteger r = qr[1];
       
    42             if (!r.equals(BigInteger.ZERO)) {
       
    43                 throw new RuntimeException("Incorrect signum() of remainder " + r.signum());
       
    44             }
       
    45             if (q.bitLength() != 2147482079) {
       
    46                 throw new RuntimeException("Incorrect bitLength() of quotient " + q.bitLength());
       
    47             }
       
    48             System.out.println("Division of large values passed without overflow.");
       
    49         } catch (OutOfMemoryError e) {
       
    50             // possible
       
    51             System.err.println("DivisionOverflow skipped: OutOfMemoryError");
       
    52             System.err.println("Run jtreg with -javaoption:-Xmx8g");
       
    53         }
       
    54     }
       
    55 }