1 /*
   2  * Copyright (c) 2013, 1025, 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  * @ignore This test has huge memory requirements
  27  * @library ..
  28  * @run main/timeout=180/othervm -Xmx8g SymmetricRangeTests
  29  * @bug 6910473 8021204 8021203 9005933 8074460
  30  * @summary Test range of BigInteger values (use -Dseed=X to set PRNG seed)
  31  * @author Dmitry Nadezhin
  32  * @key randomness
  33  */
  34 import java.io.ByteArrayInputStream;
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.IOException;
  37 import java.io.ObjectInputStream;
  38 import java.io.ObjectOutputStream;
  39 import java.util.Arrays;
  40 import java.math.BigInteger;
  41 
  42 public class SymmetricRangeTests {
  43 
  44     private static final BigInteger MAX_VALUE = makeMaxValue();
  45     private static final BigInteger MIN_VALUE = MAX_VALUE.negate();
  46 
  47     private static BigInteger makeMaxValue() {
  48         byte[] ba = new byte[1 << 28];
  49         Arrays.fill(ba, (byte) 0xFF);
  50         ba[0] = (byte) 0x7F;
  51         return new BigInteger(ba);
  52     }
  53 
  54     private static void check(String msg, BigInteger actual, BigInteger expected) {
  55         if (!actual.equals(expected)) {
  56             throw new RuntimeException(msg + ".bitLength()=" + actual.bitLength());
  57         }
  58     }
  59 
  60     private static void check(String msg, double actual, double expected) {
  61         if (actual != expected) {
  62             throw new RuntimeException(msg + "=" + actual);
  63         }
  64     }
  65 
  66     private static void check(String msg, float actual, float expected) {
  67         if (actual != expected) {
  68             throw new RuntimeException(msg + "=" + actual);
  69         }
  70     }
  71 
  72     private static void check(String msg, long actual, long expected) {
  73         if (actual != expected) {
  74             throw new RuntimeException(msg + "=" + actual);
  75         }
  76     }
  77 
  78     private static void check(String msg, int actual, int expected) {
  79         if (actual != expected) {
  80             throw new RuntimeException(msg + "=" + actual);
  81         }
  82     }
  83 
  84     private static void testOverflowInMakePositive() {
  85         System.out.println("Testing overflow in BigInteger.makePositive");
  86         byte[] ba = new byte[Integer.MAX_VALUE - 2];
  87         ba[0] = (byte) 0x80;
  88         try {
  89             BigInteger actual = new BigInteger(ba);
  90             throw new RuntimeException("new BigInteger(ba).bitLength()=" + actual.bitLength());
  91         } catch (ArithmeticException e) {
  92             // expected
  93         }
  94     }
  95 
  96     private static void testBug8021204() {
  97         System.out.println("Testing Bug 8021204");
  98         StringBuilder sb = new StringBuilder();
  99         sb.append('1');
 100         for (int i = 0; i < (1 << 30) - 1; i++) {
 101             sb.append('0');
 102         }
 103         sb.append('1');
 104         String s = sb.toString();
 105         sb = null;
 106         try {
 107             BigInteger actual = new BigInteger(s, 16);
 108             throw new RuntimeException("new BigInteger(\"1000...001\").bitLength()=" + actual.bitLength());
 109         } catch (ArithmeticException e) {
 110             // expected
 111         }
 112     }
 113 
 114     private static void testOverflowInBitSieve() {
 115         System.out.println("Testing overflow in BitSieve.sieveSingle");
 116         int bitLength = (5 << 27) - 1;
 117         try {
 118             RandomSeed rndSeed = new RandomSeed(false);
 119             System.out.println("Random number generator seed = " + rndSeed.getSeed());
 120             BigInteger actual = new BigInteger(bitLength, 0, rndSeed.getRandom());
 121             throw new RuntimeException("new BigInteger(bitLength, 0, null).bitLength()=" + actual.bitLength());
 122         } catch (ArithmeticException e) {
 123             // expected
 124         }
 125         try {
 126             BigInteger bi = BigInteger.ONE.shiftLeft(bitLength - 1).subtract(BigInteger.ONE);
 127             BigInteger actual = bi.nextProbablePrime();
 128             throw new RuntimeException("bi.nextActualPrime().bitLength()=" + actual.bitLength());
 129         } catch (ArithmeticException e) {
 130             // expected
 131         }
 132     }
 133 
 134     private static void testAdd() {
 135         System.out.println("Testing BigInteger.add");
 136         try {
 137             BigInteger actual = MAX_VALUE.add(BigInteger.ONE);
 138             throw new RuntimeException("BigInteger.MAX_VALUE.add(BigInteger.ONE).bitLength()=" + actual.bitLength());
 139         } catch (ArithmeticException e) {
 140             // expected
 141         }
 142     }
 143 
 144     private static void testSubtract() {
 145         System.out.println("Testing BigInteger.subtract");
 146         try {
 147             BigInteger actual = MIN_VALUE.subtract(BigInteger.ONE);
 148             throw new RuntimeException("BigInteger.MIN_VALUE.subtract(BigInteger.ONE).bitLength()=" + actual.bitLength());
 149         } catch (ArithmeticException e) {
 150             // expected
 151         }
 152     }
 153 
 154     private static void testMultiply() {
 155         System.out.println("Testing BigInteger.multiply");
 156         int py = 2000;
 157         int px = Integer.MAX_VALUE - py;
 158         BigInteger x = BigInteger.ONE.shiftLeft(px);
 159         BigInteger y = BigInteger.ONE.shiftLeft(py);
 160         try {
 161             BigInteger actual = x.multiply(y);
 162             throw new RuntimeException("(1 << " + px + " ) * (1 << " + py + ").bitLength()=" + actual.bitLength());
 163         } catch (ArithmeticException e) {
 164             // expected
 165         }
 166     }
 167 
 168     private static void testDivide() {
 169         System.out.println("Testing BigInteger.divide");
 170         check("BigInteger.MIN_VALUE.divide(BigInteger.valueOf(-1))",
 171                 MIN_VALUE.divide(BigInteger.valueOf(-1)), MAX_VALUE);
 172         check("BigInteger.MIN_VALUE.divide(BigInteger.ONE)",
 173                 MIN_VALUE.divide(BigInteger.ONE), MIN_VALUE);
 174     }
 175 
 176     private static void testDivideAndRemainder(String msg, BigInteger dividend, BigInteger divisor,
 177             BigInteger expectedQuotent, BigInteger expectedRemainder) {
 178         BigInteger[] qr = dividend.divideAndRemainder(divisor);
 179         check(msg + "[0]", qr[0], expectedQuotent);
 180         check(msg + "[1]", qr[1], expectedRemainder);
 181     }
 182 
 183     private static void testDivideAndRemainder() {
 184         System.out.println("Testing BigInteger.divideAndRemainder");
 185         testDivideAndRemainder("BigInteger.MIN_VALUE.divideAndRemainder(BigInteger.valueOf(-1))",
 186                 MIN_VALUE, BigInteger.valueOf(-1),
 187                 MAX_VALUE,
 188                 BigInteger.ZERO);
 189     }
 190 
 191     private static void testBug9005933() {
 192         System.out.println("Testing Bug 9005933");
 193         int dividendPow = 2147483646;
 194         int divisorPow = 1568;
 195         BigInteger dividend = BigInteger.ONE.shiftLeft(dividendPow);
 196         BigInteger divisor = BigInteger.ONE.shiftLeft(divisorPow);
 197         testDivideAndRemainder("(1 << " + dividendPow + ").divideAndRemainder(1 << " + divisorPow + ")",
 198                 dividend, divisor,
 199                 BigInteger.ONE.shiftLeft(dividendPow - divisorPow),
 200                 BigInteger.ZERO);
 201     }
 202 
 203     private static void testRemainder() {
 204         System.out.println("Testing BigInteger.remainder");
 205         check("BigInteger.MIN_VALUE.remainder(BigInteger.valueOf(-1))",
 206                 MIN_VALUE.remainder(BigInteger.valueOf(-1)), BigInteger.ZERO);
 207     }
 208 
 209     private static void testPow() {
 210         System.out.println("Testing BigInteger.pow");
 211         check("BigInteger.MIN_VALUE.pow(1)",
 212                 MIN_VALUE.pow(1), MIN_VALUE);
 213         try {
 214             BigInteger actual = BigInteger.valueOf(4).pow(Integer.MAX_VALUE);
 215             throw new RuntimeException("BigInteger.valueOf(4).pow(Integer.MAX_VALUE).bitLength()=" + actual.bitLength());
 216         } catch (ArithmeticException e) {
 217             // expected
 218         }
 219     }
 220 
 221     private static void testGcd() {
 222         System.out.println("Testing BigInteger.gcd");
 223         check("BigInteger.MIN_VALUE.gcd(BigInteger.MIN_VALUE)",
 224                 MIN_VALUE.gcd(MIN_VALUE), MAX_VALUE);
 225         check("BigInteger.MIN_VALUE.gcd(BigInteger.ZERO)",
 226                 MIN_VALUE.gcd(BigInteger.ZERO), MAX_VALUE);
 227         check("BigInteger.ZERO.gcd(MIN_VALUE)",
 228                 BigInteger.ZERO.gcd(MIN_VALUE), MAX_VALUE);
 229     }
 230 
 231     private static void testAbs() {
 232         System.out.println("Testing BigInteger.abs");
 233         check("BigInteger.MIN_VALUE.abs()",
 234                 MIN_VALUE.abs(), MAX_VALUE);
 235         check("BigInteger.MAX_VALUE.abs()",
 236                 MAX_VALUE.abs(), MAX_VALUE);
 237     }
 238 
 239     private static void testNegate() {
 240         System.out.println("Testing BigInteger.negate");
 241         check("BigInteger.MIN_VALUE.negate()",
 242                 MIN_VALUE.negate(), MAX_VALUE);
 243         check("BigInteger.MAX_VALUE.negate()",
 244                 MAX_VALUE.negate(), MIN_VALUE);
 245     }
 246 
 247     private static void testMod() {
 248         System.out.println("Testing BigInteger.mod");
 249         check("BigInteger.MIN_VALUE.mod(BigInteger.MAX_VALUE)",
 250                 MIN_VALUE.mod(MAX_VALUE), BigInteger.ZERO);
 251         check("BigInteger.MAX_VALUE.mod(BigInteger.MAX_VALUE)",
 252                 MIN_VALUE.mod(MAX_VALUE), BigInteger.ZERO);
 253     }
 254 
 255     private static void testModPow() {
 256         System.out.println("Testing BigInteger.modPow");
 257         BigInteger x = BigInteger.valueOf(3);
 258         BigInteger m = BigInteger.valueOf(-4).subtract(MIN_VALUE);
 259         check("BigInteger.valueOf(3).modPow(BigInteger.ONE, m)",
 260                 x.modPow(BigInteger.ONE, m), x);
 261     }
 262 
 263     // slow test
 264     private static void testModInverse() {
 265         System.out.println("Testing BigInteger.modInverse");
 266         check("BigInteger.MIN_VALUE.modInverse(BigInteger.MAX_VALUE)",
 267                 MIN_VALUE.modInverse(MAX_VALUE), MAX_VALUE.subtract(BigInteger.ONE));
 268     }
 269 
 270     private static void testShiftLeft() {
 271         System.out.println("Testing BigInteger.shiftLeft");
 272         try {
 273             BigInteger actual = MIN_VALUE.shiftLeft(1);
 274             throw new RuntimeException("BigInteger.MIN_VALUE.shiftLeft(1).bitLength()=" + actual.bitLength());
 275         } catch (ArithmeticException e) {
 276             // expected
 277         }
 278         try {
 279             BigInteger actual = MAX_VALUE.shiftLeft(1);
 280             throw new RuntimeException("BigInteger.MAX_VALUE.shiftLeft(1).bitLength()=" + actual.bitLength());
 281         } catch (ArithmeticException e) {
 282             // expected
 283         }
 284     }
 285 
 286     private static void testShiftRight() {
 287         System.out.println("Testing BigInteger.shiftRight");
 288         try {
 289             BigInteger actual = MIN_VALUE.shiftRight(-1);
 290             throw new RuntimeException("BigInteger.MIN_VALUE.shiftRight(-1).bitLength()=" + actual.bitLength());
 291         } catch (ArithmeticException e) {
 292             // expected
 293         }
 294         try {
 295             BigInteger actual = MAX_VALUE.shiftRight(-1);
 296             throw new RuntimeException("BigInteger.MAX_VALUE.shiftRight(-1).bitLength()=" + actual.bitLength());
 297         } catch (ArithmeticException e) {
 298             // expected
 299         }
 300     }
 301 
 302     private static void testAnd() {
 303         System.out.println("Testing BigInteger.and");
 304         check("BigInteger.MIN_VALUE.and(BigInteger.MIN_VALUE)",
 305                 MIN_VALUE.and(MIN_VALUE), MIN_VALUE);
 306         check("BigInteger.MAX_VALUE.and(BigInteger.MAX_VALUE)",
 307                 MAX_VALUE.and(MAX_VALUE), MAX_VALUE);
 308         check("BigInteger.MIN_VALUE.and(BigInteger.MAX_VALUE)",
 309                 MIN_VALUE.and(MAX_VALUE), BigInteger.ONE);
 310         try {
 311             BigInteger actual = MIN_VALUE.and(BigInteger.valueOf(-2));
 312             throw new RuntimeException("BigInteger.MIN_VALUE.and(-2)).bitLength()=" + actual.bitLength());
 313         } catch (ArithmeticException e) {
 314             // expected
 315         }
 316     }
 317 
 318     private static void testOr() {
 319         System.out.println("Testing BigInteger.or");
 320         check("BigInteger.MIN_VALUE.or(BigInteger.MIN_VALUE)",
 321                 MIN_VALUE.or(MIN_VALUE), MIN_VALUE);
 322         check("BigInteger.MAX_VALUE.or(BigInteger.MAX_VALUE)",
 323                 MAX_VALUE.or(MAX_VALUE), MAX_VALUE);
 324         check("BigInteger.MIN_VALUE.and(BigInteger.MAX_VALUE)",
 325                 MIN_VALUE.or(MAX_VALUE), BigInteger.valueOf(-1));
 326     }
 327 
 328     private static void testXor() {
 329         System.out.println("Testing BigInteger.xor");
 330         check("BigInteger.MIN_VALUE.xor(BigInteger.MIN_VALUE)",
 331                 MIN_VALUE.xor(MIN_VALUE), BigInteger.ZERO);
 332         check("BigInteger.MAX_VALUE.xor(BigInteger.MAX_VALUE)",
 333                 MAX_VALUE.xor(MAX_VALUE), BigInteger.ZERO);
 334         check("BigInteger.MIN_VALUE.xor(BigInteger.MAX_VALUE)",
 335                 MIN_VALUE.xor(MAX_VALUE), BigInteger.valueOf(-2));
 336         try {
 337             BigInteger actual = MIN_VALUE.xor(BigInteger.ONE);
 338             throw new RuntimeException("BigInteger.MIN_VALUE.xor(BigInteger.ONE)).bitLength()=" + actual.bitLength());
 339         } catch (ArithmeticException e) {
 340             // expected
 341         }
 342     }
 343 
 344     private static void testNot() {
 345         System.out.println("Testing BigInteger.not");
 346         check("BigInteger.MIN_VALUE.not()",
 347                 MIN_VALUE.not(), MAX_VALUE.subtract(BigInteger.ONE));
 348         try {
 349             BigInteger actual = MAX_VALUE.not();
 350             throw new RuntimeException("BigInteger.MAX_VALUE.not()).bitLength()=" + actual.bitLength());
 351         } catch (ArithmeticException e) {
 352             // expected
 353         }
 354     }
 355 
 356     private static void testSetBit() {
 357         System.out.println("Testing BigInteger.setBit");
 358         check("BigInteger.MIN_VALUE.setBit(" + Integer.MAX_VALUE + ")",
 359                 MIN_VALUE.setBit(Integer.MAX_VALUE), MIN_VALUE);
 360         try {
 361             BigInteger actual = MAX_VALUE.setBit(Integer.MAX_VALUE);
 362             throw new RuntimeException("BigInteger.MAX_VALUE.setBit(" + Integer.MAX_VALUE + ").bitLength()=" + actual.bitLength());
 363         } catch (ArithmeticException e) {
 364             // expected
 365         }
 366     }
 367 
 368     private static void testClearBit() {
 369         System.out.println("Testing BigInteger.clearBit");
 370         check("BigInteger.MAX_VALUE.clearBit(" + Integer.MAX_VALUE + ")",
 371                 MAX_VALUE.clearBit(Integer.MAX_VALUE), MAX_VALUE);
 372         try {
 373             BigInteger actual = MIN_VALUE.clearBit(Integer.MAX_VALUE);
 374             throw new RuntimeException("BigInteger.MIN_VALUE.clearBit(" + Integer.MAX_VALUE + ").bitLength()=" + actual.bitLength());
 375         } catch (ArithmeticException e) {
 376             // expected
 377         }
 378         try {
 379             BigInteger actual = MIN_VALUE.clearBit(0);
 380             throw new RuntimeException("BigInteger.MIN_VALUE.clearBit(0).bitLength()=" + actual.bitLength());
 381         } catch (ArithmeticException e) {
 382             // expected
 383         }
 384     }
 385 
 386     private static void testFlipBit() {
 387         System.out.println("Testing BigInteger.flipBit");
 388         try {
 389             BigInteger actual = MIN_VALUE.flipBit(Integer.MAX_VALUE);
 390             throw new RuntimeException("BigInteger.MIN_VALUE.flipBit(" + Integer.MAX_VALUE + ").bitLength()=" + actual.bitLength());
 391         } catch (ArithmeticException e) {
 392             // expected
 393         }
 394         try {
 395             BigInteger actual = MIN_VALUE.flipBit(0);
 396             throw new RuntimeException("BigInteger.MIN_VALUE.flipBit(0).bitLength()=" + actual.bitLength());
 397         } catch (ArithmeticException e) {
 398             // expected
 399         }
 400         try {
 401             BigInteger actual = MAX_VALUE.flipBit(Integer.MAX_VALUE);
 402             throw new RuntimeException("BigInteger.MAX_VALUE.flipBit(" + Integer.MAX_VALUE + ").bitLength()=" + actual.bitLength());
 403         } catch (ArithmeticException e) {
 404             // expected
 405         }
 406     }
 407 
 408     private static void testGetLowestSetBit() {
 409         System.out.println("Testing BigInteger.getLowestSetBit");
 410         check("BigInteger.MIN_VALUE.getLowestSetBit()",
 411                 MIN_VALUE.getLowestSetBit(), 0);
 412         check("BigInteger.MAX_VALUE.getLowestSetBit()",
 413                 MAX_VALUE.getLowestSetBit(), 0);
 414     }
 415 
 416     private static void testBitLength() {
 417         System.out.println("Testing BigInteger.bitLength");
 418         check("BigInteger.MIN_NEXT.bitLength()",
 419                 MIN_VALUE.bitLength(), Integer.MAX_VALUE);
 420         check("BigInteger.MAX_VALUE.bitLength()",
 421                 MAX_VALUE.bitLength(), Integer.MAX_VALUE);
 422     }
 423 
 424     private static void testBitCount() {
 425         System.out.println("Testing BigInteger.bitCount");
 426         check("BigInteger.MIN_VALUE.bitCount()",
 427                 MIN_VALUE.bitCount(), Integer.MAX_VALUE - 1);
 428         check("BigInteger.MAX_VALUE.bitCount()",
 429                 MAX_VALUE.bitCount(), Integer.MAX_VALUE);
 430     }
 431 
 432     private static void testToString(String msg, int radix, BigInteger bi, int length, String startsWith, char c) {
 433         String s = bi.toString(radix);
 434         if (s.length() != length) {
 435             throw new RuntimeException(msg + ".length=" + s.length());
 436         }
 437         if (!s.startsWith(startsWith)) {
 438             throw new RuntimeException(msg + "[0]=" + s.substring(0, startsWith.length()));
 439         }
 440         for (int i = startsWith.length(); i < s.length(); i++) {
 441             if (s.charAt(i) != c) {
 442                 throw new RuntimeException(msg + "[" + i + "]='" + s.charAt(i) + "'");
 443             }
 444         }
 445     }
 446 
 447     private static void testToString() {
 448         System.out.println("Testing BigInteger.toString");
 449         testToString("BigInteger.MIN_VALUE.toString(16)=", 16,
 450                 BigInteger.valueOf(-1).shiftLeft(Integer.MAX_VALUE - 1),
 451                 (1 << 29) + 1, "-4", '0');
 452     }
 453 
 454     private static void testToByteArrayWithConstructor(String msg, BigInteger bi, int length, byte msb, byte b, byte lsb) {
 455         byte[] ba = bi.toByteArray();
 456         if (ba.length != length) {
 457             throw new RuntimeException(msg + ".length=" + ba.length);
 458         }
 459         if (ba[0] != msb) {
 460             throw new RuntimeException(msg + "[0]=" + ba[0]);
 461         }
 462         for (int i = 1; i < ba.length - 1; i++) {
 463             if (ba[i] != b) {
 464                 throw new RuntimeException(msg + "[" + i + "]=" + ba[i]);
 465             }
 466         }
 467         if (ba[ba.length - 1] != lsb) {
 468             throw new RuntimeException(msg + "[" + (ba.length - 1) + "]=" + ba[ba.length - 1]);
 469         }
 470         BigInteger actual = new BigInteger(ba);
 471         if (!actual.equals(bi)) {
 472             throw new RuntimeException(msg + ".bitLength()=" + actual.bitLength());
 473         }
 474     }
 475 
 476     private static void testToByteArrayWithConstructor() {
 477         System.out.println("Testing BigInteger.toByteArray with constructor");
 478         testToByteArrayWithConstructor("BigInteger.MIN_VALUE.toByteArray()",
 479                 MIN_VALUE, (1 << 28), (byte) 0x80, (byte) 0x00, (byte) 0x01);
 480         testToByteArrayWithConstructor("BigInteger.MAX_VALUE.toByteArray()",
 481                 MAX_VALUE, (1 << 28), (byte) 0x7f, (byte) 0xff, (byte) 0xff);
 482 
 483         byte[] ba = new byte[1 << 28];
 484         ba[0] = (byte) 0x80;
 485         try {
 486             BigInteger actual = new BigInteger(-1, ba);
 487             throw new RuntimeException("new BigInteger(-1, ba).bitLength()=" + actual.bitLength());
 488         } catch (ArithmeticException e) {
 489             // expected
 490         }
 491         try {
 492             BigInteger actual = new BigInteger(1, ba);
 493             throw new RuntimeException("new BigInteger(1, ba).bitLength()=" + actual.bitLength());
 494         } catch (ArithmeticException e) {
 495             // expected
 496         }
 497     }
 498 
 499     private static void testIntValue() {
 500         System.out.println("Testing BigInteger.intValue");
 501         check("BigInteger.MIN_VALUE.intValue()",
 502                 MIN_VALUE.intValue(), 1);
 503         check("BigInteger.MAX_VALUE.floatValue()",
 504                 MAX_VALUE.intValue(), -1);
 505     }
 506 
 507     private static void testLongValue() {
 508         System.out.println("Testing BigInteger.longValue");
 509         check("BigInteger.MIN_VALUE.longValue()",
 510                 MIN_VALUE.longValue(), 1L);
 511         check("BigInteger.MAX_VALUE.longValue()",
 512                 MAX_VALUE.longValue(), -1L);
 513     }
 514 
 515     private static void testFloatValue() {
 516         System.out.println("Testing BigInteger.floatValue, Bug 8021203");
 517         check("BigInteger.MIN_VALUE_.floatValue()",
 518                 MIN_VALUE.floatValue(), Float.NEGATIVE_INFINITY);
 519         check("BigInteger.MAX_VALUE.floatValue()",
 520                 MAX_VALUE.floatValue(), Float.POSITIVE_INFINITY);
 521     }
 522 
 523     private static void testDoubleValue() {
 524         System.out.println("Testing BigInteger.doubleValue, Bug 8021203");
 525         check("BigInteger.MIN_VALUE.doubleValue()",
 526                 MIN_VALUE.doubleValue(), Double.NEGATIVE_INFINITY);
 527         check("BigInteger.MAX_VALUE.doubleValue()",
 528                 MAX_VALUE.doubleValue(), Double.POSITIVE_INFINITY);
 529     }
 530 
 531     private static void testSerialization(String msg, BigInteger bi) {
 532         try {
 533             ByteArrayOutputStream baOut = new ByteArrayOutputStream((1 << 28) + 1000);
 534             ObjectOutputStream out = new ObjectOutputStream(baOut);
 535             out.writeObject(bi);
 536             out.close();
 537             out = null;
 538             byte[] ba = baOut.toByteArray();
 539             baOut = null;
 540             ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(ba));
 541             BigInteger actual = (BigInteger) in.readObject();
 542             if (!actual.equals(bi)) {
 543                 throw new RuntimeException(msg + ".bitLength()=" + actual.bitLength());
 544             }
 545         } catch (IOException | ClassNotFoundException e) {
 546             throw new RuntimeException(msg + " raised exception ", e);
 547         }
 548     }
 549 
 550     private static void testSerialization() {
 551         System.out.println("Testing BigInteger serialization");
 552         testSerialization("BigInteger.MIN_VALUE.intValue()",
 553                 MIN_VALUE);
 554         testSerialization("BigInteger.MAX_VALUE.floatValue()",
 555                 MAX_VALUE);
 556     }
 557 
 558     private static void testLongValueExact() {
 559         System.out.println("Testing BigInteger.longValueExact");
 560         try {
 561             long actual = MIN_VALUE.longValueExact();
 562             throw new RuntimeException("BigInteger.MIN_VALUE.longValueExact()= " + actual);
 563         } catch (ArithmeticException e) {
 564             // excpected
 565         }
 566         try {
 567             long actual = MAX_VALUE.longValueExact();
 568             throw new RuntimeException("BigInteger.MAX_VALUE.longValueExact()= " + actual);
 569         } catch (ArithmeticException e) {
 570             // excpected
 571         }
 572     }
 573 
 574     private static void testIntValueExact() {
 575         System.out.println("Testing BigInteger.intValueExact");
 576         try {
 577             long actual = MIN_VALUE.intValueExact();
 578             throw new RuntimeException("BigInteger.MIN_VALUE.intValueExact()= " + actual);
 579         } catch (ArithmeticException e) {
 580             // excpected
 581         }
 582         try {
 583             long actual = MAX_VALUE.intValueExact();
 584             throw new RuntimeException("BigInteger.MAX_VALUE.intValueExact()= " + actual);
 585         } catch (ArithmeticException e) {
 586             // excpected
 587         }
 588     }
 589 
 590     private static void testShortValueExact() {
 591         System.out.println("Testing BigInteger.shortValueExact");
 592         try {
 593             long actual = MIN_VALUE.shortValueExact();
 594             throw new RuntimeException("BigInteger.MIN_VALUE.shortValueExact()= " + actual);
 595         } catch (ArithmeticException e) {
 596             // excpected
 597         }
 598         try {
 599             long actual = MAX_VALUE.shortValueExact();
 600             throw new RuntimeException("BigInteger.MAX_VALUE.shortValueExact()= " + actual);
 601         } catch (ArithmeticException e) {
 602             // excpected
 603         }
 604     }
 605 
 606     private static void testByteValueExact() {
 607         System.out.println("Testing BigInteger.byteValueExact");
 608         try {
 609             long actual = MIN_VALUE.byteValueExact();
 610             throw new RuntimeException("BigInteger.MIN_VALUE.byteValueExact()= " + actual);
 611         } catch (ArithmeticException e) {
 612             // excpected
 613         }
 614         try {
 615             long actual = MAX_VALUE.byteValueExact();
 616             throw new RuntimeException("BigInteger.MAX_VALUE.byteValueExact()= " + actual);
 617         } catch (ArithmeticException e) {
 618             // excpected
 619         }
 620     }
 621 
 622     public static void main(String... args) {
 623         testOverflowInMakePositive();
 624         testBug8021204();
 625         testOverflowInBitSieve();
 626         testAdd();
 627         testSubtract();
 628         testMultiply();
 629         testDivide();
 630         testDivideAndRemainder();
 631         testBug9005933();
 632         testRemainder();
 633         testPow();
 634         testGcd();
 635         testAbs();
 636         testNegate();
 637         testMod();
 638         testModPow();
 639 //        testModInverse();
 640         testShiftLeft();
 641         testShiftRight();
 642         testAnd();
 643         testOr();
 644         testXor();
 645         testNot();
 646         testSetBit();
 647         testClearBit();
 648         testFlipBit();
 649         testGetLowestSetBit();
 650         testBitLength();
 651         testBitCount();
 652         testToString();
 653         testToByteArrayWithConstructor();
 654         testIntValue();
 655         testLongValue();
 656         testFloatValue();
 657         testDoubleValue();
 658         testSerialization();
 659         testLongValueExact();
 660         testIntValueExact();
 661         testShortValueExact();
 662         testByteValueExact();
 663     }
 664 }