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