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