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