1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.math; 19 20import java.math.BigInteger; 21import java.util.Random; 22 23public class BigIntegerTest extends junit.framework.TestCase { 24 25 BigInteger minusTwo = new BigInteger("-2", 10); 26 27 BigInteger minusOne = new BigInteger("-1", 10); 28 29 BigInteger zero = new BigInteger("0", 10); 30 31 BigInteger one = new BigInteger("1", 10); 32 33 BigInteger two = new BigInteger("2", 10); 34 35 BigInteger ten = new BigInteger("10", 10); 36 37 BigInteger sixteen = new BigInteger("16", 10); 38 39 BigInteger oneThousand = new BigInteger("1000", 10); 40 41 BigInteger aZillion = new BigInteger( 42 "100000000000000000000000000000000000000000000000000", 10); 43 44 BigInteger twoToTheTen = new BigInteger("1024", 10); 45 46 BigInteger twoToTheSeventy = two.pow(70); 47 48 Random rand = new Random(); 49 50 BigInteger bi; 51 52 BigInteger bi1; 53 54 BigInteger bi2; 55 56 BigInteger bi3; 57 58 BigInteger bi11; 59 60 BigInteger bi22; 61 62 BigInteger bi33; 63 64 BigInteger bi12; 65 66 BigInteger bi23; 67 68 BigInteger bi13; 69 70 BigInteger largePos; 71 72 BigInteger smallPos; 73 74 BigInteger largeNeg; 75 76 BigInteger smallNeg; 77 78 BigInteger[][] booleanPairs; 79 80 /** 81 * @tests java.math.BigInteger#BigInteger(int, java.util.Random) 82 */ 83 public void test_ConstructorILjava_util_Random() { 84 // regression test for HARMONY-1047 85 try { 86 new BigInteger(Integer.MAX_VALUE, (Random)null); 87 fail("NegativeArraySizeException expected"); 88 } catch (NegativeArraySizeException e) { 89 // PASSED 90 } 91 92 bi = new BigInteger(70, rand); 93 bi2 = new BigInteger(70, rand); 94 assertTrue("Random number is negative", bi.compareTo(zero) >= 0); 95 assertTrue("Random number is too big", 96 bi.compareTo(twoToTheSeventy) < 0); 97 assertTrue( 98 "Two random numbers in a row are the same (might not be a bug but it very likely is)", 99 !bi.equals(bi2)); 100 assertTrue("Not zero", new BigInteger(0, rand).equals(BigInteger.ZERO)); 101 } 102 103 /** 104 * @tests java.math.BigInteger#BigInteger(int, int, java.util.Random) 105 */ 106 public void test_ConstructorIILjava_util_Random() { 107 bi = new BigInteger(10, 5, rand); 108 bi2 = new BigInteger(10, 5, rand); 109 assertTrue("Random number one is negative", bi.compareTo(zero) >= 0); 110 assertTrue("Random number one is too big", 111 bi.compareTo(twoToTheTen) < 0); 112 assertTrue("Random number two is negative", bi2.compareTo(zero) >= 0); 113 assertTrue("Random number two is too big", 114 bi2.compareTo(twoToTheTen) < 0); 115 116 Random rand = new Random(); 117 BigInteger bi; 118 int certainty[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 119 Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -2, -1 }; 120 for (int i = 2; i <= 20; i++) { 121 for (int c = 0; c < certainty.length; c++) { 122 bi = new BigInteger(i, c, rand); // Create BigInteger 123 assertTrue("Bit length incorrect", bi.bitLength() == i); 124 } 125 } 126 } 127 128 /** 129 * @tests java.math.BigInteger#BigInteger(byte[]) 130 */ 131 public void test_Constructor$B() { 132 byte[] myByteArray; 133 myByteArray = new byte[] { (byte) 0x00, (byte) 0xFF, (byte) 0xFE }; 134 bi = new BigInteger(myByteArray); 135 assertTrue("Incorrect value for pos number", bi.equals(BigInteger.ZERO 136 .setBit(16).subtract(two))); 137 myByteArray = new byte[] { (byte) 0xFF, (byte) 0xFE }; 138 bi = new BigInteger(myByteArray); 139 assertTrue("Incorrect value for neg number", bi.equals(minusTwo)); 140 } 141 142 /** 143 * @tests java.math.BigInteger#BigInteger(int, byte[]) 144 */ 145 public void test_ConstructorI$B() { 146 byte[] myByteArray; 147 myByteArray = new byte[] { (byte) 0xFF, (byte) 0xFE }; 148 bi = new BigInteger(1, myByteArray); 149 assertTrue("Incorrect value for pos number", bi.equals(BigInteger.ZERO 150 .setBit(16).subtract(two))); 151 bi = new BigInteger(-1, myByteArray); 152 assertTrue("Incorrect value for neg number", bi.equals(BigInteger.ZERO 153 .setBit(16).subtract(two).negate())); 154 myByteArray = new byte[] { (byte) 0, (byte) 0 }; 155 bi = new BigInteger(0, myByteArray); 156 assertTrue("Incorrect value for zero", bi.equals(zero)); 157 myByteArray = new byte[] { (byte) 1 }; 158 try { 159 new BigInteger(0, myByteArray); 160 fail("Failed to throw NumberFormatException"); 161 } catch (NumberFormatException e) { 162 // correct 163 } 164 } 165 166 /** 167 * @tests java.math.BigInteger#BigInteger(java.lang.String) 168 */ 169 public void test_constructor_String_empty() { 170 try { 171 new BigInteger(""); 172 fail("Expected NumberFormatException for new BigInteger(\"\")"); 173 } catch (NumberFormatException e) { 174 } 175 } 176 177 /** 178 * @tests java.math.BigInteger#toByteArray() 179 */ 180 public void test_toByteArray() { 181 byte[] myByteArray, anotherByteArray; 182 myByteArray = new byte[] { 97, 33, 120, 124, 50, 2, 0, 0, 0, 12, 124, 183 42 }; 184 anotherByteArray = new BigInteger(myByteArray).toByteArray(); 185 assertTrue("Incorrect byte array returned", 186 myByteArray.length == anotherByteArray.length); 187 for (int counter = myByteArray.length - 1; counter >= 0; counter--) { 188 assertTrue("Incorrect values in returned byte array", 189 myByteArray[counter] == anotherByteArray[counter]); 190 } 191 } 192 193 /** 194 * @tests java.math.BigInteger#isProbablePrime(int) 195 */ 196 public void test_isProbablePrimeI() { 197 int fails = 0; 198 bi = new BigInteger(20, 20, rand); 199 if (!bi.isProbablePrime(17)) { 200 fails++; 201 } 202 bi = new BigInteger("4", 10); 203 if (bi.isProbablePrime(17)) { 204 fail("isProbablePrime failed for: " + bi); 205 } 206 bi = BigInteger.valueOf(17L * 13L); 207 if (bi.isProbablePrime(17)) { 208 fail("isProbablePrime failed for: " + bi); 209 } 210 for (long a = 2; a < 1000; a++) { 211 if (isPrime(a)) { 212 assertTrue("false negative on prime number <1000", BigInteger 213 .valueOf(a).isProbablePrime(5)); 214 } else if (BigInteger.valueOf(a).isProbablePrime(17)) { 215 System.out.println("isProbablePrime failed for: " + a); 216 fails++; 217 } 218 } 219 for (int a = 0; a < 1000; a++) { 220 bi = BigInteger.valueOf(rand.nextInt(1000000)).multiply( 221 BigInteger.valueOf(rand.nextInt(1000000))); 222 if (bi.isProbablePrime(17)) { 223 System.out.println("isProbablePrime failed for: " + bi); 224 fails++; 225 } 226 } 227 for (int a = 0; a < 200; a++) { 228 bi = new BigInteger(70, rand).multiply(new BigInteger(70, rand)); 229 if (bi.isProbablePrime(17)) { 230 System.out.println("isProbablePrime failed for: " + bi); 231 fails++; 232 } 233 } 234 assertTrue("Too many false positives - may indicate a problem", 235 fails <= 1); 236 } 237 238 /** 239 * @tests java.math.BigInteger#equals(java.lang.Object) 240 */ 241 public void test_equalsLjava_lang_Object() { 242 assertTrue("0=0", zero.equals(BigInteger.valueOf(0))); 243 assertTrue("-123=-123", BigInteger.valueOf(-123).equals( 244 BigInteger.valueOf(-123))); 245 assertTrue("0=1", !zero.equals(one)); 246 assertTrue("0=-1", !zero.equals(minusOne)); 247 assertTrue("1=-1", !one.equals(minusOne)); 248 assertTrue("bi3=bi3", bi3.equals(bi3)); 249 assertTrue("bi3=copy of bi3", bi3.equals(bi3.negate().negate())); 250 assertTrue("bi3=bi2", !bi3.equals(bi2)); 251 } 252 253 /** 254 * @tests java.math.BigInteger#compareTo(java.math.BigInteger) 255 */ 256 public void test_compareToLjava_math_BigInteger() { 257 assertTrue("Smaller number returned >= 0", one.compareTo(two) < 0); 258 assertTrue("Larger number returned >= 0", two.compareTo(one) > 0); 259 assertTrue("Equal numbers did not return 0", one.compareTo(one) == 0); 260 assertTrue("Neg number messed things up", 261 two.negate().compareTo(one) < 0); 262 } 263 264 /** 265 * @tests java.math.BigInteger#intValue() 266 */ 267 public void test_intValue() { 268 assertTrue("Incorrect intValue for 2**70", 269 twoToTheSeventy.intValue() == 0); 270 assertTrue("Incorrect intValue for 2", two.intValue() == 2); 271 } 272 273 /** 274 * @tests java.math.BigInteger#longValue() 275 */ 276 public void test_longValue() { 277 assertTrue("Incorrect longValue for 2**70", 278 twoToTheSeventy.longValue() == 0); 279 assertTrue("Incorrect longValue for 2", two.longValue() == 2); 280 } 281 282 /** 283 * @tests java.math.BigInteger#valueOf(long) 284 */ 285 public void test_valueOfJ() { 286 assertTrue("Incurred number returned for 2", BigInteger.valueOf(2L) 287 .equals(two)); 288 assertTrue("Incurred number returned for 200", BigInteger.valueOf(200L) 289 .equals(BigInteger.valueOf(139).add(BigInteger.valueOf(61)))); 290 } 291 292 /** 293 * @tests java.math.BigInteger#add(java.math.BigInteger) 294 */ 295 public void test_addLjava_math_BigInteger() { 296 assertTrue("Incorrect sum--wanted a zillion", aZillion.add(aZillion) 297 .add(aZillion.negate()).equals(aZillion)); 298 assertTrue("0+0", zero.add(zero).equals(zero)); 299 assertTrue("0+1", zero.add(one).equals(one)); 300 assertTrue("1+0", one.add(zero).equals(one)); 301 assertTrue("1+1", one.add(one).equals(two)); 302 assertTrue("0+(-1)", zero.add(minusOne).equals(minusOne)); 303 assertTrue("(-1)+0", minusOne.add(zero).equals(minusOne)); 304 assertTrue("(-1)+(-1)", minusOne.add(minusOne).equals(minusTwo)); 305 assertTrue("1+(-1)", one.add(minusOne).equals(zero)); 306 assertTrue("(-1)+1", minusOne.add(one).equals(zero)); 307 308 for (int i = 0; i < 200; i++) { 309 BigInteger midbit = zero.setBit(i); 310 assertTrue("add fails to carry on bit " + i, midbit.add(midbit) 311 .equals(zero.setBit(i + 1))); 312 } 313 BigInteger bi2p3 = bi2.add(bi3); 314 BigInteger bi3p2 = bi3.add(bi2); 315 assertTrue("bi2p3=bi3p2", bi2p3.equals(bi3p2)); 316 317 // add large positive + small positive 318 319 // add large positive + small negative 320 321 // add large negative + small positive 322 323 // add large negative + small negative 324 } 325 326 /** 327 * @tests java.math.BigInteger#negate() 328 */ 329 public void test_negate() { 330 assertTrue("Single negation of zero did not result in zero", zero 331 .negate().equals(zero)); 332 assertTrue("Single negation resulted in original nonzero number", 333 !aZillion.negate().equals(aZillion)); 334 assertTrue("Double negation did not result in original number", 335 aZillion.negate().negate().equals(aZillion)); 336 337 assertTrue("0.neg", zero.negate().equals(zero)); 338 assertTrue("1.neg", one.negate().equals(minusOne)); 339 assertTrue("2.neg", two.negate().equals(minusTwo)); 340 assertTrue("-1.neg", minusOne.negate().equals(one)); 341 assertTrue("-2.neg", minusTwo.negate().equals(two)); 342 assertTrue("0x62EB40FEF85AA9EBL*2.neg", BigInteger.valueOf( 343 0x62EB40FEF85AA9EBL * 2).negate().equals( 344 BigInteger.valueOf(-0x62EB40FEF85AA9EBL * 2))); 345 for (int i = 0; i < 200; i++) { 346 BigInteger midbit = zero.setBit(i); 347 BigInteger negate = midbit.negate(); 348 assertTrue("negate negate", negate.negate().equals(midbit)); 349 assertTrue("neg fails on bit " + i, midbit.negate().add(midbit) 350 .equals(zero)); 351 } 352 } 353 354 /** 355 * @tests java.math.BigInteger#signum() 356 */ 357 public void test_signum() { 358 assertTrue("Wrong positive signum", two.signum() == 1); 359 assertTrue("Wrong zero signum", zero.signum() == 0); 360 assertTrue("Wrong neg zero signum", zero.negate().signum() == 0); 361 assertTrue("Wrong neg signum", two.negate().signum() == -1); 362 } 363 364 /** 365 * @tests java.math.BigInteger#abs() 366 */ 367 public void test_abs() { 368 assertTrue("Invalid number returned for zillion", aZillion.negate() 369 .abs().equals(aZillion.abs())); 370 assertTrue("Invalid number returned for zero neg", zero.negate().abs() 371 .equals(zero)); 372 assertTrue("Invalid number returned for zero", zero.abs().equals(zero)); 373 assertTrue("Invalid number returned for two", two.negate().abs() 374 .equals(two)); 375 } 376 377 /** 378 * @tests java.math.BigInteger#pow(int) 379 */ 380 public void test_powI() { 381 assertTrue("Incorrect exponent returned for 2**10", two.pow(10).equals( 382 twoToTheTen)); 383 assertTrue("Incorrect exponent returned for 2**70", two.pow(30) 384 .multiply(two.pow(40)).equals(twoToTheSeventy)); 385 assertTrue("Incorrect exponent returned for 10**50", ten.pow(50) 386 .equals(aZillion)); 387 } 388 389 /** 390 * @tests java.math.BigInteger#modInverse(java.math.BigInteger) 391 */ 392 public void test_modInverseLjava_math_BigInteger() { 393 BigInteger a = zero, mod, inv; 394 for (int j = 3; j < 50; j++) { 395 mod = BigInteger.valueOf(j); 396 for (int i = -j + 1; i < j; i++) { 397 try { 398 a = BigInteger.valueOf(i); 399 inv = a.modInverse(mod); 400 assertTrue("bad inverse: " + a + " inv mod " + mod 401 + " equals " + inv, one.equals(a.multiply(inv).mod( 402 mod))); 403 assertTrue("inverse greater than modulo: " + a 404 + " inv mod " + mod + " equals " + inv, inv 405 .compareTo(mod) < 0); 406 assertTrue("inverse less than zero: " + a + " inv mod " 407 + mod + " equals " + inv, inv 408 .compareTo(BigInteger.ZERO) >= 0); 409 } catch (ArithmeticException e) { 410 assertTrue("should have found inverse for " + a + " mod " 411 + mod, !one.equals(a.gcd(mod))); 412 } 413 } 414 } 415 for (int j = 1; j < 10; j++) { 416 mod = bi2.add(BigInteger.valueOf(j)); 417 for (int i = 0; i < 20; i++) { 418 try { 419 a = bi3.add(BigInteger.valueOf(i)); 420 inv = a.modInverse(mod); 421 assertTrue("bad inverse: " + a + " inv mod " + mod 422 + " equals " + inv, one.equals(a.multiply(inv).mod( 423 mod))); 424 assertTrue("inverse greater than modulo: " + a 425 + " inv mod " + mod + " equals " + inv, inv 426 .compareTo(mod) < 0); 427 assertTrue("inverse less than zero: " + a + " inv mod " 428 + mod + " equals " + inv, inv 429 .compareTo(BigInteger.ZERO) >= 0); 430 } catch (ArithmeticException e) { 431 assertTrue("should have found inverse for " + a + " mod " 432 + mod, !one.equals(a.gcd(mod))); 433 } 434 } 435 } 436 } 437 438 /** 439 * @tests java.math.BigInteger#shiftRight(int) 440 */ 441 public void test_shiftRightI() { 442 assertTrue("1 >> 0", BigInteger.valueOf(1).shiftRight(0).equals( 443 BigInteger.ONE)); 444 assertTrue("1 >> 1", BigInteger.valueOf(1).shiftRight(1).equals( 445 BigInteger.ZERO)); 446 assertTrue("1 >> 63", BigInteger.valueOf(1).shiftRight(63).equals( 447 BigInteger.ZERO)); 448 assertTrue("1 >> 64", BigInteger.valueOf(1).shiftRight(64).equals( 449 BigInteger.ZERO)); 450 assertTrue("1 >> 65", BigInteger.valueOf(1).shiftRight(65).equals( 451 BigInteger.ZERO)); 452 assertTrue("1 >> 1000", BigInteger.valueOf(1).shiftRight(1000).equals( 453 BigInteger.ZERO)); 454 assertTrue("-1 >> 0", BigInteger.valueOf(-1).shiftRight(0).equals( 455 minusOne)); 456 assertTrue("-1 >> 1", BigInteger.valueOf(-1).shiftRight(1).equals( 457 minusOne)); 458 assertTrue("-1 >> 63", BigInteger.valueOf(-1).shiftRight(63).equals( 459 minusOne)); 460 assertTrue("-1 >> 64", BigInteger.valueOf(-1).shiftRight(64).equals( 461 minusOne)); 462 assertTrue("-1 >> 65", BigInteger.valueOf(-1).shiftRight(65).equals( 463 minusOne)); 464 assertTrue("-1 >> 1000", BigInteger.valueOf(-1).shiftRight(1000) 465 .equals(minusOne)); 466 467 BigInteger a = BigInteger.ONE; 468 BigInteger c = bi3; 469 BigInteger E = bi3.negate(); 470 BigInteger e = E; 471 for (int i = 0; i < 200; i++) { 472 BigInteger b = BigInteger.ZERO.setBit(i); 473 assertTrue("a==b", a.equals(b)); 474 a = a.shiftLeft(1); 475 assertTrue("a non-neg", a.signum() >= 0); 476 477 BigInteger d = bi3.shiftRight(i); 478 assertTrue("c==d", c.equals(d)); 479 c = c.shiftRight(1); 480 assertTrue(">>1 == /2", d.divide(two).equals(c)); 481 assertTrue("c non-neg", c.signum() >= 0); 482 483 BigInteger f = E.shiftRight(i); 484 assertTrue("e==f", e.equals(f)); 485 e = e.shiftRight(1); 486 assertTrue(">>1 == /2", f.subtract(one).divide(two).equals(e)); 487 assertTrue("e negative", e.signum() == -1); 488 489 assertTrue("b >> i", b.shiftRight(i).equals(one)); 490 assertTrue("b >> i+1", b.shiftRight(i + 1).equals(zero)); 491 assertTrue("b >> i-1", b.shiftRight(i - 1).equals(two)); 492 } 493 } 494 495 /** 496 * @tests java.math.BigInteger#shiftLeft(int) 497 */ 498 public void test_shiftLeftI() { 499 assertTrue("1 << 0", one.shiftLeft(0).equals(one)); 500 assertTrue("1 << 1", one.shiftLeft(1).equals(two)); 501 assertTrue("1 << 63", one.shiftLeft(63).equals( 502 new BigInteger("8000000000000000", 16))); 503 assertTrue("1 << 64", one.shiftLeft(64).equals( 504 new BigInteger("10000000000000000", 16))); 505 assertTrue("1 << 65", one.shiftLeft(65).equals( 506 new BigInteger("20000000000000000", 16))); 507 assertTrue("-1 << 0", minusOne.shiftLeft(0).equals(minusOne)); 508 assertTrue("-1 << 1", minusOne.shiftLeft(1).equals(minusTwo)); 509 assertTrue("-1 << 63", minusOne.shiftLeft(63).equals( 510 new BigInteger("-9223372036854775808"))); 511 assertTrue("-1 << 64", minusOne.shiftLeft(64).equals( 512 new BigInteger("-18446744073709551616"))); 513 assertTrue("-1 << 65", minusOne.shiftLeft(65).equals( 514 new BigInteger("-36893488147419103232"))); 515 516 BigInteger a = bi3; 517 BigInteger c = minusOne; 518 for (int i = 0; i < 200; i++) { 519 BigInteger b = bi3.shiftLeft(i); 520 assertTrue("a==b", a.equals(b)); 521 assertTrue("a >> i == bi3", a.shiftRight(i).equals(bi3)); 522 a = a.shiftLeft(1); 523 assertTrue("<<1 == *2", b.multiply(two).equals(a)); 524 assertTrue("a non-neg", a.signum() >= 0); 525 assertTrue("a.bitCount==b.bitCount", a.bitCount() == b.bitCount()); 526 527 BigInteger d = minusOne.shiftLeft(i); 528 assertTrue("c==d", c.equals(d)); 529 c = c.shiftLeft(1); 530 assertTrue("<<1 == *2 negative", d.multiply(two).equals(c)); 531 assertTrue("c negative", c.signum() == -1); 532 assertTrue("d >> i == minusOne", d.shiftRight(i).equals(minusOne)); 533 } 534 } 535 536 /** 537 * @tests java.math.BigInteger#multiply(java.math.BigInteger) 538 */ 539 public void test_multiplyLjava_math_BigInteger() { 540 assertTrue("Incorrect sum--wanted three zillion", aZillion 541 .add(aZillion).add(aZillion).equals( 542 aZillion.multiply(new BigInteger("3", 10)))); 543 544 assertTrue("0*0", zero.multiply(zero).equals(zero)); 545 assertTrue("0*1", zero.multiply(one).equals(zero)); 546 assertTrue("1*0", one.multiply(zero).equals(zero)); 547 assertTrue("1*1", one.multiply(one).equals(one)); 548 assertTrue("0*(-1)", zero.multiply(minusOne).equals(zero)); 549 assertTrue("(-1)*0", minusOne.multiply(zero).equals(zero)); 550 assertTrue("(-1)*(-1)", minusOne.multiply(minusOne).equals(one)); 551 assertTrue("1*(-1)", one.multiply(minusOne).equals(minusOne)); 552 assertTrue("(-1)*1", minusOne.multiply(one).equals(minusOne)); 553 554 testAllMults(bi1, bi1, bi11); 555 testAllMults(bi2, bi2, bi22); 556 testAllMults(bi3, bi3, bi33); 557 testAllMults(bi1, bi2, bi12); 558 testAllMults(bi1, bi3, bi13); 559 testAllMults(bi2, bi3, bi23); 560 } 561 562 /** 563 * @tests java.math.BigInteger#divide(java.math.BigInteger) 564 */ 565 public void test_divideLjava_math_BigInteger() { 566 testAllDivs(bi33, bi3); 567 testAllDivs(bi22, bi2); 568 testAllDivs(bi11, bi1); 569 testAllDivs(bi13, bi1); 570 testAllDivs(bi13, bi3); 571 testAllDivs(bi12, bi1); 572 testAllDivs(bi12, bi2); 573 testAllDivs(bi23, bi2); 574 testAllDivs(bi23, bi3); 575 testAllDivs(largePos, bi1); 576 testAllDivs(largePos, bi2); 577 testAllDivs(largePos, bi3); 578 testAllDivs(largeNeg, bi1); 579 testAllDivs(largeNeg, bi2); 580 testAllDivs(largeNeg, bi3); 581 testAllDivs(largeNeg, largePos); 582 testAllDivs(largePos, largeNeg); 583 testAllDivs(bi3, bi3); 584 testAllDivs(bi2, bi2); 585 testAllDivs(bi1, bi1); 586 testDivRanges(bi1); 587 testDivRanges(bi2); 588 testDivRanges(bi3); 589 testDivRanges(smallPos); 590 testDivRanges(largePos); 591 testDivRanges(new BigInteger("62EB40FEF85AA9EB", 16)); 592 testAllDivs(BigInteger.valueOf(0xCC0225953CL), BigInteger 593 .valueOf(0x1B937B765L)); 594 595 try { 596 largePos.divide(zero); 597 fail("ArithmeticException expected"); 598 } catch (ArithmeticException e) { 599 } 600 601 try { 602 bi1.divide(zero); 603 fail("ArithmeticException expected"); 604 } catch (ArithmeticException e) { 605 } 606 607 try { 608 bi3.negate().divide(zero); 609 fail("ArithmeticException expected"); 610 } catch (ArithmeticException e) { 611 } 612 613 try { 614 zero.divide(zero); 615 fail("ArithmeticException expected"); 616 } catch (ArithmeticException e) { 617 } 618 } 619 620 /** 621 * @tests java.math.BigInteger#remainder(java.math.BigInteger) 622 */ 623 public void test_remainderLjava_math_BigInteger() { 624 try { 625 largePos.remainder(zero); 626 fail("ArithmeticException expected"); 627 } catch (ArithmeticException e) { 628 } 629 630 try { 631 bi1.remainder(zero); 632 fail("ArithmeticException expected"); 633 } catch (ArithmeticException e) { 634 } 635 636 try { 637 bi3.negate().remainder(zero); 638 fail("ArithmeticException expected"); 639 } catch (ArithmeticException e) { 640 } 641 642 try { 643 zero.remainder(zero); 644 fail("ArithmeticException expected"); 645 } catch (ArithmeticException e) { 646 } 647 } 648 649 /** 650 * @tests java.math.BigInteger#mod(java.math.BigInteger) 651 */ 652 public void test_modLjava_math_BigInteger() { 653 try { 654 largePos.mod(zero); 655 fail("ArithmeticException expected"); 656 } catch (ArithmeticException e) { 657 } 658 659 try { 660 bi1.mod(zero); 661 fail("ArithmeticException expected"); 662 } catch (ArithmeticException e) { 663 } 664 665 try { 666 bi3.negate().mod(zero); 667 fail("ArithmeticException expected"); 668 } catch (ArithmeticException e) { 669 } 670 671 try { 672 zero.mod(zero); 673 fail("ArithmeticException expected"); 674 } catch (ArithmeticException e) { 675 } 676 } 677 678 /** 679 * @tests java.math.BigInteger#divideAndRemainder(java.math.BigInteger) 680 */ 681 public void test_divideAndRemainderLjava_math_BigInteger() { 682 try { 683 largePos.divideAndRemainder(zero); 684 fail("ArithmeticException expected"); 685 } catch (ArithmeticException e) { 686 } 687 688 try { 689 bi1.divideAndRemainder(zero); 690 fail("ArithmeticException expected"); 691 } catch (ArithmeticException e) { 692 } 693 694 try { 695 bi3.negate().divideAndRemainder(zero); 696 fail("ArithmeticException expected"); 697 } catch (ArithmeticException e) { 698 } 699 700 try { 701 zero.divideAndRemainder(zero); 702 fail("ArithmeticException expected"); 703 } catch (ArithmeticException e) { 704 } 705 } 706 707 /** 708 * @tests java.math.BigInteger#BigInteger(java.lang.String) 709 */ 710 public void test_ConstructorLjava_lang_String() { 711 assertTrue("new(0)", new BigInteger("0").equals(BigInteger.valueOf(0))); 712 assertTrue("new(1)", new BigInteger("1").equals(BigInteger.valueOf(1))); 713 assertTrue("new(12345678901234)", new BigInteger("12345678901234") 714 .equals(BigInteger.valueOf(12345678901234L))); 715 assertTrue("new(-1)", new BigInteger("-1").equals(BigInteger 716 .valueOf(-1))); 717 assertTrue("new(-12345678901234)", new BigInteger("-12345678901234") 718 .equals(BigInteger.valueOf(-12345678901234L))); 719 } 720 721 /** 722 * @tests java.math.BigInteger#BigInteger(java.lang.String, int) 723 */ 724 public void test_ConstructorLjava_lang_StringI() { 725 assertTrue("new(0,16)", new BigInteger("0", 16).equals(BigInteger 726 .valueOf(0))); 727 assertTrue("new(1,16)", new BigInteger("1", 16).equals(BigInteger 728 .valueOf(1))); 729 assertTrue("new(ABF345678901234,16)", new BigInteger("ABF345678901234", 730 16).equals(BigInteger.valueOf(0xABF345678901234L))); 731 assertTrue("new(abf345678901234,16)", new BigInteger("abf345678901234", 732 16).equals(BigInteger.valueOf(0xABF345678901234L))); 733 assertTrue("new(-1,16)", new BigInteger("-1", 16).equals(BigInteger 734 .valueOf(-1))); 735 assertTrue("new(-ABF345678901234,16)", new BigInteger( 736 "-ABF345678901234", 16).equals(BigInteger 737 .valueOf(-0xABF345678901234L))); 738 assertTrue("new(-abf345678901234,16)", new BigInteger( 739 "-abf345678901234", 16).equals(BigInteger 740 .valueOf(-0xABF345678901234L))); 741 assertTrue("new(-101010101,2)", new BigInteger("-101010101", 2) 742 .equals(BigInteger.valueOf(-341))); 743 } 744 745 /** 746 * @tests java.math.BigInteger#toString() 747 */ 748 public void test_toString() { 749 assertTrue("0.toString", "0".equals(BigInteger.valueOf(0).toString())); 750 assertTrue("1.toString", "1".equals(BigInteger.valueOf(1).toString())); 751 assertTrue("12345678901234.toString", "12345678901234" 752 .equals(BigInteger.valueOf(12345678901234L).toString())); 753 assertTrue("-1.toString", "-1" 754 .equals(BigInteger.valueOf(-1).toString())); 755 assertTrue("-12345678901234.toString", "-12345678901234" 756 .equals(BigInteger.valueOf(-12345678901234L).toString())); 757 } 758 759 /** 760 * @tests java.math.BigInteger#toString(int) 761 */ 762 public void test_toStringI() { 763 assertTrue("0.toString(16)", "0".equals(BigInteger.valueOf(0).toString( 764 16))); 765 assertTrue("1.toString(16)", "1".equals(BigInteger.valueOf(1).toString( 766 16))); 767 assertTrue("ABF345678901234.toString(16)", "abf345678901234" 768 .equals(BigInteger.valueOf(0xABF345678901234L).toString(16))); 769 assertTrue("-1.toString(16)", "-1".equals(BigInteger.valueOf(-1) 770 .toString(16))); 771 assertTrue("-ABF345678901234.toString(16)", "-abf345678901234" 772 .equals(BigInteger.valueOf(-0xABF345678901234L).toString(16))); 773 assertTrue("-101010101.toString(2)", "-101010101".equals(BigInteger 774 .valueOf(-341).toString(2))); 775 } 776 777 /** 778 * @tests java.math.BigInteger#and(java.math.BigInteger) 779 */ 780 public void test_andLjava_math_BigInteger() { 781 for (BigInteger[] element : booleanPairs) { 782 BigInteger i1 = element[0], i2 = element[1]; 783 BigInteger res = i1.and(i2); 784 assertTrue("symmetry of and", res.equals(i2.and(i1))); 785 int len = Math.max(i1.bitLength(), i2.bitLength()) + 66; 786 for (int i = 0; i < len; i++) { 787 assertTrue("and", (i1.testBit(i) && i2.testBit(i)) == res 788 .testBit(i)); 789 } 790 } 791 } 792 793 /** 794 * @tests java.math.BigInteger#or(java.math.BigInteger) 795 */ 796 public void test_orLjava_math_BigInteger() { 797 for (BigInteger[] element : booleanPairs) { 798 BigInteger i1 = element[0], i2 = element[1]; 799 BigInteger res = i1.or(i2); 800 assertTrue("symmetry of or", res.equals(i2.or(i1))); 801 int len = Math.max(i1.bitLength(), i2.bitLength()) + 66; 802 for (int i = 0; i < len; i++) { 803 assertTrue("or", (i1.testBit(i) || i2.testBit(i)) == res 804 .testBit(i)); 805 } 806 } 807 } 808 809 /** 810 * @tests java.math.BigInteger#xor(java.math.BigInteger) 811 */ 812 public void test_xorLjava_math_BigInteger() { 813 for (BigInteger[] element : booleanPairs) { 814 BigInteger i1 = element[0], i2 = element[1]; 815 BigInteger res = i1.xor(i2); 816 assertTrue("symmetry of xor", res.equals(i2.xor(i1))); 817 int len = Math.max(i1.bitLength(), i2.bitLength()) + 66; 818 for (int i = 0; i < len; i++) { 819 assertTrue("xor", (i1.testBit(i) ^ i2.testBit(i)) == res 820 .testBit(i)); 821 } 822 } 823 } 824 825 /** 826 * @tests java.math.BigInteger#not() 827 */ 828 public void test_not() { 829 for (BigInteger[] element : booleanPairs) { 830 BigInteger i1 = element[0]; 831 BigInteger res = i1.not(); 832 int len = i1.bitLength() + 66; 833 for (int i = 0; i < len; i++) { 834 assertTrue("not", !i1.testBit(i) == res.testBit(i)); 835 } 836 } 837 } 838 839 /** 840 * @tests java.math.BigInteger#andNot(java.math.BigInteger) 841 */ 842 public void test_andNotLjava_math_BigInteger() { 843 for (BigInteger[] element : booleanPairs) { 844 BigInteger i1 = element[0], i2 = element[1]; 845 BigInteger res = i1.andNot(i2); 846 int len = Math.max(i1.bitLength(), i2.bitLength()) + 66; 847 for (int i = 0; i < len; i++) { 848 assertTrue("andNot", (i1.testBit(i) && !i2.testBit(i)) == res 849 .testBit(i)); 850 } 851 // asymmetrical 852 i1 = element[1]; 853 i2 = element[0]; 854 res = i1.andNot(i2); 855 for (int i = 0; i < len; i++) { 856 assertTrue("andNot reversed", 857 (i1.testBit(i) && !i2.testBit(i)) == res.testBit(i)); 858 } 859 } 860 //regression for HARMONY-4653 861 try{ 862 BigInteger.ZERO.andNot(null); 863 fail("should throw NPE"); 864 }catch(Exception e){ 865 //expected 866 } 867 BigInteger bi = new BigInteger(0, new byte[]{}); 868 assertEquals(BigInteger.ZERO, bi.andNot(BigInteger.ZERO)); 869 } 870 871 872 public void testClone() { 873 // Regression test for HARMONY-1770 874 MyBigInteger myBigInteger = new MyBigInteger("12345"); 875 myBigInteger = (MyBigInteger) myBigInteger.clone(); 876 } 877 878 static class MyBigInteger extends BigInteger implements Cloneable { 879 public MyBigInteger(String val) { 880 super(val); 881 } 882 public Object clone() { 883 try { 884 return super.clone(); 885 } catch (CloneNotSupportedException e) { 886 return null; 887 } 888 } 889 } 890 891 @Override 892 protected void setUp() { 893 bi1 = new BigInteger("2436798324768978", 16); 894 bi2 = new BigInteger("4576829475724387584378543764555", 16); 895 bi3 = new BigInteger("43987298363278574365732645872643587624387563245", 896 16); 897 898 bi33 = new BigInteger( 899 "10730846694701319120609898625733976090865327544790136667944805934175543888691400559249041094474885347922769807001", 900 10); 901 bi22 = new BigInteger( 902 "33301606932171509517158059487795669025817912852219962782230629632224456249", 903 10); 904 bi11 = new BigInteger("6809003003832961306048761258711296064", 10); 905 bi23 = new BigInteger( 906 "597791300268191573513888045771594235932809890963138840086083595706565695943160293610527214057", 907 10); 908 bi13 = new BigInteger( 909 "270307912162948508387666703213038600031041043966215279482940731158968434008", 910 10); 911 bi12 = new BigInteger( 912 "15058244971895641717453176477697767050482947161656458456", 10); 913 914 largePos = new BigInteger( 915 "834759814379857314986743298675687569845986736578576375675678998612743867438632986243982098437620983476924376", 916 16); 917 smallPos = new BigInteger("48753269875973284765874598630960986276", 16); 918 largeNeg = new BigInteger( 919 "-878824397432651481891353247987891423768534321387864361143548364457698487264387568743568743265873246576467643756437657436587436", 920 16); 921 smallNeg = new BigInteger("-567863254343798609857456273458769843", 16); 922 booleanPairs = new BigInteger[][] { { largePos, smallPos }, 923 { largePos, smallNeg }, { largeNeg, smallPos }, 924 { largeNeg, smallNeg } }; 925 } 926 927 private void testDiv(BigInteger i1, BigInteger i2) { 928 BigInteger q = i1.divide(i2); 929 BigInteger r = i1.remainder(i2); 930 BigInteger[] temp = i1.divideAndRemainder(i2); 931 932 assertTrue("divide and divideAndRemainder do not agree", q 933 .equals(temp[0])); 934 assertTrue("remainder and divideAndRemainder do not agree", r 935 .equals(temp[1])); 936 assertTrue("signum and equals(zero) do not agree on quotient", q 937 .signum() != 0 938 || q.equals(zero)); 939 assertTrue("signum and equals(zero) do not agree on remainder", r 940 .signum() != 0 941 || r.equals(zero)); 942 assertTrue("wrong sign on quotient", q.signum() == 0 943 || q.signum() == i1.signum() * i2.signum()); 944 assertTrue("wrong sign on remainder", r.signum() == 0 945 || r.signum() == i1.signum()); 946 assertTrue("remainder out of range", r.abs().compareTo(i2.abs()) < 0); 947 assertTrue("quotient too small", q.abs().add(one).multiply(i2.abs()) 948 .compareTo(i1.abs()) > 0); 949 assertTrue("quotient too large", q.abs().multiply(i2.abs()).compareTo( 950 i1.abs()) <= 0); 951 BigInteger p = q.multiply(i2); 952 BigInteger a = p.add(r); 953 assertTrue("(a/b)*b+(a%b) != a", a.equals(i1)); 954 try { 955 BigInteger mod = i1.mod(i2); 956 assertTrue("mod is negative", mod.signum() >= 0); 957 assertTrue("mod out of range", mod.abs().compareTo(i2.abs()) < 0); 958 assertTrue("positive remainder == mod", r.signum() < 0 959 || r.equals(mod)); 960 assertTrue("negative remainder == mod - divisor", r.signum() >= 0 961 || r.equals(mod.subtract(i2))); 962 } catch (ArithmeticException e) { 963 assertTrue("mod fails on negative divisor only", i2.signum() <= 0); 964 } 965 } 966 967 private void testDivRanges(BigInteger i) { 968 BigInteger bound = i.multiply(two); 969 for (BigInteger j = bound.negate(); j.compareTo(bound) <= 0; j = j 970 .add(i)) { 971 BigInteger innerbound = j.add(two); 972 BigInteger k = j.subtract(two); 973 for (; k.compareTo(innerbound) <= 0; k = k.add(one)) { 974 testDiv(k, i); 975 } 976 } 977 } 978 979 private boolean isPrime(long b) { 980 if (b == 2) { 981 return true; 982 } 983 // check for div by 2 984 if ((b & 1L) == 0) { 985 return false; 986 } 987 long maxlen = ((long) Math.sqrt(b)) + 2; 988 for (long x = 3; x < maxlen; x += 2) { 989 if (b % x == 0) { 990 return false; 991 } 992 } 993 return true; 994 } 995 996 private void testAllMults(BigInteger i1, BigInteger i2, BigInteger ans) { 997 assertTrue("i1*i2=ans", i1.multiply(i2).equals(ans)); 998 assertTrue("i2*i1=ans", i2.multiply(i1).equals(ans)); 999 assertTrue("-i1*i2=-ans", i1.negate().multiply(i2).equals(ans.negate())); 1000 assertTrue("-i2*i1=-ans", i2.negate().multiply(i1).equals(ans.negate())); 1001 assertTrue("i1*-i2=-ans", i1.multiply(i2.negate()).equals(ans.negate())); 1002 assertTrue("i2*-i1=-ans", i2.multiply(i1.negate()).equals(ans.negate())); 1003 assertTrue("-i1*-i2=ans", i1.negate().multiply(i2.negate()).equals(ans)); 1004 assertTrue("-i2*-i1=ans", i2.negate().multiply(i1.negate()).equals(ans)); 1005 } 1006 1007 private void testAllDivs(BigInteger i1, BigInteger i2) { 1008 testDiv(i1, i2); 1009 testDiv(i1.negate(), i2); 1010 testDiv(i1, i2.negate()); 1011 testDiv(i1.negate(), i2.negate()); 1012 } 1013} 1014