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