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 * @author Elena Semukhina 19 */ 20 21 package org.apache.harmony.tests.java.math; 22 23 import junit.framework.TestCase; 24 import java.math.BigInteger; 25 26 /** 27 * Class: java.math.BigInteger 28 * Methods: bitLength, shiftLeft, shiftRight, 29 * clearBit, flipBit, setBit, testBit 30 */ 31 public class BigIntegerOperateBitsTest extends TestCase { 32 /** 33 * bitCount() of zero. 34 */ 35 public void testBitCountZero() { 36 BigInteger aNumber = new BigInteger("0"); 37 assertEquals(0, aNumber.bitCount()); 38 } 39 40 /** 41 * bitCount() of a negative number. 42 */ 43 public void testBitCountNeg() { 44 BigInteger aNumber = new BigInteger("-12378634756382937873487638746283767238657872368748726875"); 45 assertEquals(87, aNumber.bitCount()); 46 } 47 48 /** 49 * bitCount() of a negative number. 50 */ 51 public void testBitCountPos() { 52 BigInteger aNumber = new BigInteger("12378634756343564757582937873487638746283767238657872368748726875"); 53 assertEquals(107, aNumber.bitCount()); 54 } 55 56 /** 57 * bitLength() of zero. 58 */ 59 public void testBitLengthZero() { 60 BigInteger aNumber = new BigInteger("0"); 61 assertEquals(0, aNumber.bitLength()); 62 } 63 64 /** 65 * bitLength() of a positive number. 66 */ 67 public void testBitLengthPositive1() { 68 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 69 int aSign = 1; 70 BigInteger aNumber = new BigInteger(aSign, aBytes); 71 assertEquals(108, aNumber.bitLength()); 72 } 73 74 /** 75 * bitLength() of a positive number with the leftmost bit set 76 */ 77 public void testBitLengthPositive2() { 78 byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 79 int aSign = 1; 80 BigInteger aNumber = new BigInteger(aSign, aBytes); 81 assertEquals(96, aNumber.bitLength()); 82 } 83 84 /** 85 * bitLength() of a positive number which is a power of 2 86 */ 87 public void testBitLengthPositive3() { 88 byte aBytes[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 89 int aSign = 1; 90 BigInteger aNumber = new BigInteger(aSign, aBytes); 91 assertEquals(81, aNumber.bitLength()); 92 } 93 94 /** 95 * bitLength() of a negative number. 96 */ 97 public void testBitLengthNegative1() { 98 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 99 int aSign = -1; 100 BigInteger aNumber = new BigInteger(aSign, aBytes); 101 assertEquals(108, aNumber.bitLength()); 102 } 103 104 /** 105 * bitLength() of a negative number with the leftmost bit set 106 */ 107 public void testBitLengthNegative2() { 108 byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 109 int aSign = -1; 110 BigInteger aNumber = new BigInteger(aSign, aBytes); 111 assertEquals(96, aNumber.bitLength()); 112 } 113 114 /** 115 * bitLength() of a negative number which is a power of 2 116 */ 117 public void testBitLengthNegative3() { 118 byte aBytes[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 119 int aSign = -1; 120 BigInteger aNumber = new BigInteger(aSign, aBytes); 121 assertEquals(80, aNumber.bitLength()); 122 } 123 124 /** 125 * clearBit(int n) of a negative n 126 */ 127 public void testClearBitException() { 128 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 129 int aSign = 1; 130 int number = -7; 131 BigInteger aNumber = new BigInteger(aSign, aBytes); 132 try { 133 aNumber.clearBit(number); 134 fail("ArithmeticException has not been caught"); 135 } catch (ArithmeticException e) { 136 } 137 } 138 139 /** 140 * clearBit(int n) outside zero 141 */ 142 public void testClearBitZero() { 143 byte aBytes[] = {0}; 144 int aSign = 0; 145 int number = 0; 146 byte rBytes[] = {0}; 147 BigInteger aNumber = new BigInteger(aSign, aBytes); 148 BigInteger result = aNumber.clearBit(number); 149 byte resBytes[] = new byte[rBytes.length]; 150 resBytes = result.toByteArray(); 151 for(int i = 0; i < resBytes.length; i++) { 152 assertTrue(resBytes[i] == rBytes[i]); 153 } 154 assertEquals("incorrect sign", 0, result.signum()); 155 } 156 157 /** 158 * clearBit(int n) outside zero 159 */ 160 public void testClearBitZeroOutside1() { 161 byte aBytes[] = {0}; 162 int aSign = 0; 163 int number = 95; 164 byte rBytes[] = {0}; 165 BigInteger aNumber = new BigInteger(aSign, aBytes); 166 BigInteger result = aNumber.clearBit(number); 167 byte resBytes[] = new byte[rBytes.length]; 168 resBytes = result.toByteArray(); 169 for(int i = 0; i < resBytes.length; i++) { 170 assertTrue(resBytes[i] == rBytes[i]); 171 } 172 assertEquals("incorrect sign", 0, result.signum()); 173 } 174 175 /** 176 * clearBit(int n) inside a negative number 177 */ 178 public void testClearBitNegativeInside1() { 179 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 180 int aSign = -1; 181 int number = 15; 182 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, 92, -26}; 183 BigInteger aNumber = new BigInteger(aSign, aBytes); 184 BigInteger result = aNumber.clearBit(number); 185 byte resBytes[] = new byte[rBytes.length]; 186 resBytes = result.toByteArray(); 187 for(int i = 0; i < resBytes.length; i++) { 188 assertTrue(resBytes[i] == rBytes[i]); 189 } 190 assertEquals("incorrect sign", -1, result.signum()); 191 } 192 193 /** 194 * clearBit(int n) inside a negative number 195 */ 196 public void testClearBitNegativeInside2() { 197 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 198 int aSign = -1; 199 int number = 44; 200 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -62, -92, -4, 14, -36, -26}; 201 BigInteger aNumber = new BigInteger(aSign, aBytes); 202 BigInteger result = aNumber.clearBit(number); 203 byte resBytes[] = new byte[rBytes.length]; 204 resBytes = result.toByteArray(); 205 for(int i = 0; i < resBytes.length; i++) { 206 assertTrue(resBytes[i] == rBytes[i]); 207 } 208 assertEquals("incorrect sign", -1, result.signum()); 209 } 210 211 /** 212 * clearBit(2) in the negative number with all ones in bit representation 213 */ 214 public void testClearBitNegativeInside3() { 215 String as = "-18446744073709551615"; 216 int number = 2; 217 BigInteger aNumber = new BigInteger(as); 218 BigInteger result = aNumber.clearBit(number); 219 assertEquals(as, result.toString()); 220 } 221 222 /** 223 * clearBit(0) in the negative number of length 1 224 * with all ones in bit representation. 225 * the resulting number's length is 2. 226 */ 227 public void testClearBitNegativeInside4() { 228 String as = "-4294967295"; 229 String res = "-4294967296"; 230 int number = 0; 231 BigInteger aNumber = new BigInteger(as); 232 BigInteger result = aNumber.clearBit(number); 233 assertEquals(res, result.toString()); 234 } 235 236 /** 237 * clearBit(0) in the negative number of length 2 238 * with all ones in bit representation. 239 * the resulting number's length is 3. 240 */ 241 public void testClearBitNegativeInside5() { 242 String as = "-18446744073709551615"; 243 String res = "-18446744073709551616"; 244 int number = 0; 245 BigInteger aNumber = new BigInteger(as); 246 BigInteger result = aNumber.clearBit(number); 247 assertEquals(res, result.toString()); 248 } 249 250 /** 251 * clearBit(int n) outside a negative number 252 */ 253 public void testClearBitNegativeOutside1() { 254 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 255 int aSign = -1; 256 int number = 150; 257 byte rBytes[] = {-65, -1, -1, -1, -1, -1, -2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 258 BigInteger aNumber = new BigInteger(aSign, aBytes); 259 BigInteger result = aNumber.clearBit(number); 260 byte resBytes[] = new byte[rBytes.length]; 261 resBytes = result.toByteArray(); 262 for(int i = 0; i < resBytes.length; i++) { 263 assertTrue(resBytes[i] == rBytes[i]); 264 } 265 assertEquals("incorrect sign", -1, result.signum()); 266 } 267 268 /** 269 * clearBit(int n) outside a negative number 270 */ 271 public void testClearBitNegativeOutside2() { 272 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 273 int aSign = -1; 274 int number = 165; 275 byte rBytes[] = {-33, -1, -1, -1, -1, -1, -1, -1, -2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 276 BigInteger aNumber = new BigInteger(aSign, aBytes); 277 BigInteger result = aNumber.clearBit(number); 278 byte resBytes[] = new byte[rBytes.length]; 279 resBytes = result.toByteArray(); 280 for(int i = 0; i < resBytes.length; i++) { 281 assertTrue(resBytes[i] == rBytes[i]); 282 } 283 assertEquals("incorrect sign", -1, result.signum()); 284 } 285 286 /** 287 * clearBit(int n) inside a positive number 288 */ 289 public void testClearBitPositiveInside1() { 290 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 291 int aSign = 1; 292 int number = 20; 293 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -31, 35, 26}; 294 BigInteger aNumber = new BigInteger(aSign, aBytes); 295 BigInteger result = aNumber.clearBit(number); 296 byte resBytes[] = new byte[rBytes.length]; 297 resBytes = result.toByteArray(); 298 for(int i = 0; i < resBytes.length; i++) { 299 assertTrue(resBytes[i] == rBytes[i]); 300 } 301 assertEquals("incorrect sign", 1, result.signum()); 302 } 303 304 /** 305 * clearBit(int n) inside a positive number 306 */ 307 public void testClearBitPositiveInside2() { 308 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 309 int aSign = 1; 310 int number = 17; 311 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 312 BigInteger aNumber = new BigInteger(aSign, aBytes); 313 BigInteger result = aNumber.clearBit(number); 314 byte resBytes[] = new byte[rBytes.length]; 315 resBytes = result.toByteArray(); 316 for(int i = 0; i < resBytes.length; i++) { 317 assertTrue(resBytes[i] == rBytes[i]); 318 } 319 assertEquals("incorrect sign", 1, result.signum()); 320 } 321 322 /** 323 * clearBit(int n) inside a positive number 324 */ 325 public void testClearBitPositiveInside3() { 326 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 327 int aSign = 1; 328 int number = 45; 329 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 13, 91, 3, -15, 35, 26}; 330 BigInteger aNumber = new BigInteger(aSign, aBytes); 331 BigInteger result = aNumber.clearBit(number); 332 byte resBytes[] = new byte[rBytes.length]; 333 resBytes = result.toByteArray(); 334 for(int i = 0; i < resBytes.length; i++) { 335 assertTrue(resBytes[i] == rBytes[i]); 336 } 337 assertEquals("incorrect sign", 1, result.signum()); 338 } 339 340 /** 341 * clearBit(int n) inside a positive number 342 */ 343 public void testClearBitPositiveInside4 () { 344 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 345 int aSign = 1; 346 int number = 50; 347 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 348 BigInteger aNumber = new BigInteger(aSign, aBytes); 349 BigInteger result = aNumber.clearBit(number); 350 byte resBytes[] = new byte[rBytes.length]; 351 resBytes = result.toByteArray(); 352 for(int i = 0; i < resBytes.length; i++) { 353 assertTrue(resBytes[i] == rBytes[i]); 354 } 355 assertEquals("incorrect sign", 1, result.signum()); 356 } 357 358 /** 359 * clearBit(int n) inside a positive number 360 */ 361 public void testClearBitPositiveInside5 () { 362 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 363 int aSign = 1; 364 int number = 63; 365 byte rBytes[] = {1, -128, 56, 100, -2, 52, 89, 45, 91, 3, -15, 35, 26}; 366 BigInteger aNumber = new BigInteger(aSign, aBytes); 367 BigInteger result = aNumber.clearBit(number); 368 byte resBytes[] = new byte[rBytes.length]; 369 resBytes = result.toByteArray(); 370 for(int i = 0; i < resBytes.length; i++) { 371 assertTrue(resBytes[i] == rBytes[i]); 372 } 373 assertEquals("incorrect sign", 1, result.signum()); 374 } 375 376 /** 377 * clearBit(int n) outside a positive number 378 */ 379 public void testClearBitPositiveOutside1() { 380 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 381 int aSign = 1; 382 int number = 150; 383 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 384 BigInteger aNumber = new BigInteger(aSign, aBytes); 385 BigInteger result = aNumber.clearBit(number); 386 byte resBytes[] = new byte[rBytes.length]; 387 resBytes = result.toByteArray(); 388 for(int i = 0; i < resBytes.length; i++) { 389 assertTrue(resBytes[i] == rBytes[i]); 390 } 391 assertEquals("incorrect sign", 1, result.signum()); 392 } 393 394 /** 395 * clearBit(int n) outside a positive number 396 */ 397 public void testClearBitPositiveOutside2() { 398 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 399 int aSign = 1; 400 int number = 191; 401 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 402 BigInteger aNumber = new BigInteger(aSign, aBytes); 403 BigInteger result = aNumber.clearBit(number); 404 byte resBytes[] = new byte[rBytes.length]; 405 resBytes = result.toByteArray(); 406 for(int i = 0; i < resBytes.length; i++) { 407 assertTrue(resBytes[i] == rBytes[i]); 408 } 409 assertEquals("incorrect sign", 1, result.signum()); 410 } 411 412 /** 413 * clearBit(int n) the leftmost bit in a negative number 414 */ 415 public void testClearBitTopNegative() { 416 byte aBytes[] = {1, -128, 56, 100, -15, 35, 26}; 417 int aSign = -1; 418 int number = 63; 419 byte rBytes[] = {-1, 127, -2, 127, -57, -101, 14, -36, -26}; 420 BigInteger aNumber = new BigInteger(aSign, aBytes); 421 BigInteger result = aNumber.clearBit(number); 422 byte resBytes[] = new byte[rBytes.length]; 423 resBytes = result.toByteArray(); 424 for(int i = 0; i < resBytes.length; i++) { 425 assertTrue(resBytes[i] == rBytes[i]); 426 } 427 assertEquals("incorrect sign", -1, result.signum()); 428 } 429 430 /** 431 * flipBit(int n) of a negative n 432 */ 433 public void testFlipBitException() { 434 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 435 int aSign = 1; 436 int number = -7; 437 BigInteger aNumber = new BigInteger(aSign, aBytes); 438 try { 439 aNumber.flipBit(number); 440 fail("ArithmeticException has not been caught"); 441 } catch (ArithmeticException e) { 442 } 443 } 444 445 /** 446 * flipBit(int n) zero 447 */ 448 public void testFlipBitZero() { 449 byte aBytes[] = {0}; 450 int aSign = 0; 451 int number = 0; 452 byte rBytes[] = {1}; 453 BigInteger aNumber = new BigInteger(aSign, aBytes); 454 BigInteger result = aNumber.flipBit(number); 455 byte resBytes[] = new byte[rBytes.length]; 456 resBytes = result.toByteArray(); 457 for(int i = 0; i < resBytes.length; i++) { 458 assertTrue(resBytes[i] == rBytes[i]); 459 } 460 assertEquals("incorrect sign", 1, result.signum()); 461 } 462 463 /** 464 * flipBit(int n) outside zero 465 */ 466 public void testFlipBitZeroOutside1() { 467 byte aBytes[] = {0}; 468 int aSign = 0; 469 int number = 62; 470 byte rBytes[] = {64, 0, 0, 0, 0, 0, 0, 0}; 471 BigInteger aNumber = new BigInteger(aSign, aBytes); 472 BigInteger result = aNumber.flipBit(number); 473 byte resBytes[] = new byte[rBytes.length]; 474 resBytes = result.toByteArray(); 475 for(int i = 0; i < resBytes.length; i++) { 476 assertTrue("incorrect value", resBytes[i] == rBytes[i]); 477 } 478 assertEquals("incorrect sign", 1, result.signum()); 479 } 480 481 /** 482 * flipBit(int n) outside zero 483 */ 484 public void testFlipBitZeroOutside2() { 485 byte aBytes[] = {0}; 486 int aSign = 0; 487 int number = 63; 488 byte rBytes[] = {0, -128, 0, 0, 0, 0, 0, 0, 0}; 489 BigInteger aNumber = new BigInteger(aSign, aBytes); 490 BigInteger result = aNumber.flipBit(number); 491 byte resBytes[] = new byte[rBytes.length]; 492 resBytes = result.toByteArray(); 493 for(int i = 0; i < resBytes.length; i++) { 494 assertTrue("incorrect value", resBytes[i] == rBytes[i]); 495 } 496 assertEquals("incorrect sign", 1, result.signum()); 497 } 498 499 /** 500 * flipBit(int n) the leftmost bit in a negative number 501 */ 502 public void testFlipBitLeftmostNegative() { 503 byte aBytes[] = {1, -128, 56, 100, -15, 35, 26}; 504 int aSign = -1; 505 int number = 48; 506 byte rBytes[] = {-1, 127, -57, -101, 14, -36, -26, 49}; 507 BigInteger aNumber = new BigInteger(aSign, aBytes); 508 BigInteger result = aNumber.flipBit(number); 509 byte resBytes[] = new byte[rBytes.length]; 510 resBytes = result.toByteArray(); 511 for(int i = 0; i < resBytes.length; i++) { 512 assertTrue(resBytes[i] == rBytes[i]); 513 } 514 assertEquals("incorrect sign", -1, result.signum()); 515 } 516 517 /** 518 * flipBit(int n) the leftmost bit in a positive number 519 */ 520 public void testFlipBitLeftmostPositive() { 521 byte aBytes[] = {1, -128, 56, 100, -15, 35, 26}; 522 int aSign = 1; 523 int number = 48; 524 byte rBytes[] = {0, -128, 56, 100, -15, 35, 26}; 525 BigInteger aNumber = new BigInteger(aSign, aBytes); 526 BigInteger result = aNumber.flipBit(number); 527 byte resBytes[] = new byte[rBytes.length]; 528 resBytes = result.toByteArray(); 529 for(int i = 0; i < resBytes.length; i++) { 530 assertTrue(resBytes[i] == rBytes[i]); 531 } 532 assertEquals("incorrect sign", 1, result.signum()); 533 } 534 535 /** 536 * flipBit(int n) inside a negative number 537 */ 538 public void testFlipBitNegativeInside1() { 539 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 540 int aSign = -1; 541 int number = 15; 542 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, 92, -26}; 543 BigInteger aNumber = new BigInteger(aSign, aBytes); 544 BigInteger result = aNumber.flipBit(number); 545 byte resBytes[] = new byte[rBytes.length]; 546 resBytes = result.toByteArray(); 547 for(int i = 0; i < resBytes.length; i++) { 548 assertTrue(resBytes[i] == rBytes[i]); 549 } 550 assertEquals("incorrect sign", -1, result.signum()); 551 } 552 553 /** 554 * flipBit(int n) inside a negative number 555 */ 556 public void testFlipBitNegativeInside2() { 557 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 558 int aSign = -1; 559 int number = 45; 560 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -14, -92, -4, 14, -36, -26}; 561 BigInteger aNumber = new BigInteger(aSign, aBytes); 562 BigInteger result = aNumber.flipBit(number); 563 byte resBytes[] = new byte[rBytes.length]; 564 resBytes = result.toByteArray(); 565 for(int i = 0; i < resBytes.length; i++) { 566 assertTrue(resBytes[i] == rBytes[i]); 567 } 568 assertEquals("incorrect sign", -1, result.signum()); 569 } 570 571 /** 572 * flipBit(int n) inside a negative number with all ones in bit representation 573 */ 574 public void testFlipBitNegativeInside3() { 575 String as = "-18446744073709551615"; 576 String res = "-18446744073709551611"; 577 int number = 2; 578 BigInteger aNumber = new BigInteger(as); 579 BigInteger result = aNumber.flipBit(number); 580 assertEquals(res, result.toString()); 581 } 582 583 /** 584 * flipBit(0) in the negative number of length 1 585 * with all ones in bit representation. 586 * the resulting number's length is 2. 587 */ 588 public void testFlipBitNegativeInside4() { 589 String as = "-4294967295"; 590 String res = "-4294967296"; 591 int number = 0; 592 BigInteger aNumber = new BigInteger(as); 593 BigInteger result = aNumber.flipBit(number); 594 assertEquals(res, result.toString()); 595 } 596 597 /** 598 * flipBit(0) in the negative number of length 2 599 * with all ones in bit representation. 600 * the resulting number's length is 3. 601 */ 602 public void testFlipBitNegativeInside5() { 603 String as = "-18446744073709551615"; 604 String res = "-18446744073709551616"; 605 int number = 0; 606 BigInteger aNumber = new BigInteger(as); 607 BigInteger result = aNumber.flipBit(number); 608 assertEquals(res, result.toString()); 609 } 610 611 /** 612 * flipBit(int n) outside a negative number 613 */ 614 public void testFlipBitNegativeOutside1() { 615 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 616 int aSign = -1; 617 int number = 150; 618 byte rBytes[] = {-65, -1, -1, -1, -1, -1, -2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 619 BigInteger aNumber = new BigInteger(aSign, aBytes); 620 BigInteger result = aNumber.flipBit(number); 621 byte resBytes[] = new byte[rBytes.length]; 622 resBytes = result.toByteArray(); 623 for(int i = 0; i < resBytes.length; i++) { 624 assertTrue(resBytes[i] == rBytes[i]); 625 } 626 assertEquals("incorrect sign", -1, result.signum()); 627 } 628 629 /** 630 * flipBit(int n) outside a negative number 631 */ 632 public void testFlipBitNegativeOutside2() { 633 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 634 int aSign = -1; 635 int number = 191; 636 byte rBytes[] = {-1, 127, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 637 BigInteger aNumber = new BigInteger(aSign, aBytes); 638 BigInteger result = aNumber.flipBit(number); 639 byte resBytes[] = new byte[rBytes.length]; 640 resBytes = result.toByteArray(); 641 for(int i = 0; i < resBytes.length; i++) { 642 assertTrue(resBytes[i] == rBytes[i]); 643 } 644 assertEquals("incorrect sign", -1, result.signum()); 645 } 646 647 /** 648 * flipBit(int n) inside a positive number 649 */ 650 public void testFlipBitPositiveInside1() { 651 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 652 int aSign = 1; 653 int number = 15; 654 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, -93, 26}; 655 BigInteger aNumber = new BigInteger(aSign, aBytes); 656 BigInteger result = aNumber.flipBit(number); 657 byte resBytes[] = new byte[rBytes.length]; 658 resBytes = result.toByteArray(); 659 for(int i = 0; i < resBytes.length; i++) { 660 assertTrue(resBytes[i] == rBytes[i]); 661 } 662 assertEquals("incorrect sign", 1, result.signum()); 663 } 664 665 /** 666 * flipBit(int n) inside a positive number 667 */ 668 public void testFlipBitPositiveInside2() { 669 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 670 int aSign = 1; 671 int number = 45; 672 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 13, 91, 3, -15, 35, 26}; 673 BigInteger aNumber = new BigInteger(aSign, aBytes); 674 BigInteger result = aNumber.flipBit(number); 675 byte resBytes[] = new byte[rBytes.length]; 676 resBytes = result.toByteArray(); 677 for(int i = 0; i < resBytes.length; i++) { 678 assertTrue(resBytes[i] == rBytes[i]); 679 } 680 assertEquals("incorrect sign", 1, result.signum()); 681 } 682 683 /** 684 * flipBit(int n) outside a positive number 685 */ 686 public void testFlipBitPositiveOutside1() { 687 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 688 int aSign = 1; 689 int number = 150; 690 byte rBytes[] = {64, 0, 0, 0, 0, 0, 1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 691 BigInteger aNumber = new BigInteger(aSign, aBytes); 692 BigInteger result = aNumber.flipBit(number); 693 byte resBytes[] = new byte[rBytes.length]; 694 resBytes = result.toByteArray(); 695 for(int i = 0; i < resBytes.length; i++) { 696 assertTrue(resBytes[i] == rBytes[i]); 697 } 698 assertEquals("incorrect sign", 1, result.signum()); 699 } 700 701 /** 702 * flipBit(int n) outside a positive number 703 */ 704 public void testFlipBitPositiveOutside2() { 705 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 706 int aSign = 1; 707 int number = 191; 708 byte rBytes[] = {0, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 709 BigInteger aNumber = new BigInteger(aSign, aBytes); 710 BigInteger result = aNumber.flipBit(number); 711 byte resBytes[] = new byte[rBytes.length]; 712 resBytes = result.toByteArray(); 713 for(int i = 0; i < resBytes.length; i++) { 714 assertTrue(resBytes[i] == rBytes[i]); 715 } 716 assertEquals("incorrect sign", 1, result.signum()); 717 } 718 719 /** 720 * setBit(int n) of a negative n 721 */ 722 public void testSetBitException() { 723 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 724 int aSign = 1; 725 int number = -7; 726 BigInteger aNumber = new BigInteger(aSign, aBytes); 727 try { 728 aNumber.setBit(number); 729 fail("ArithmeticException has not been caught"); 730 } catch (ArithmeticException e) { 731 } 732 } 733 734 /** 735 * setBit(int n) outside zero 736 */ 737 public void testSetBitZero() { 738 byte aBytes[] = {0}; 739 int aSign = 0; 740 int number = 0; 741 byte rBytes[] = {1}; 742 BigInteger aNumber = new BigInteger(aSign, aBytes); 743 BigInteger result = aNumber.setBit(number); 744 byte resBytes[] = new byte[rBytes.length]; 745 resBytes = result.toByteArray(); 746 for(int i = 0; i < resBytes.length; i++) { 747 assertTrue(resBytes[i] == rBytes[i]); 748 } 749 assertEquals("incorrect sign", 1, result.signum()); 750 } 751 752 /** 753 * setBit(int n) outside zero 754 */ 755 public void testSetBitZeroOutside1() { 756 byte aBytes[] = {0}; 757 int aSign = 0; 758 int number = 95; 759 byte rBytes[] = {0, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 760 BigInteger aNumber = new BigInteger(aSign, aBytes); 761 BigInteger result = aNumber.setBit(number); 762 byte resBytes[] = new byte[rBytes.length]; 763 resBytes = result.toByteArray(); 764 for(int i = 0; i < resBytes.length; i++) { 765 assertTrue(resBytes[i] == rBytes[i]); 766 } 767 assertEquals("incorrect sign", 1, result.signum()); 768 } 769 770 /** 771 * setBit(int n) inside a positive number 772 */ 773 public void testSetBitPositiveInside1() { 774 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 775 int aSign = 1; 776 int number = 20; 777 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 778 BigInteger aNumber = new BigInteger(aSign, aBytes); 779 BigInteger result = aNumber.setBit(number); 780 byte resBytes[] = new byte[rBytes.length]; 781 resBytes = result.toByteArray(); 782 for(int i = 0; i < resBytes.length; i++) { 783 assertTrue(resBytes[i] == rBytes[i]); 784 } 785 assertEquals("incorrect sign", 1, result.signum()); 786 } 787 788 /** 789 * setBit(int n) inside a positive number 790 */ 791 public void testSetBitPositiveInside2() { 792 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 793 int aSign = 1; 794 int number = 17; 795 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -13, 35, 26}; 796 BigInteger aNumber = new BigInteger(aSign, aBytes); 797 BigInteger result = aNumber.setBit(number); 798 byte resBytes[] = new byte[rBytes.length]; 799 resBytes = result.toByteArray(); 800 for(int i = 0; i < resBytes.length; i++) { 801 assertTrue(resBytes[i] == rBytes[i]); 802 } 803 assertEquals("incorrect sign", 1, result.signum()); 804 } 805 806 /** 807 * setBit(int n) inside a positive number 808 */ 809 public void testSetBitPositiveInside3() { 810 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 811 int aSign = 1; 812 int number = 45; 813 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 814 BigInteger aNumber = new BigInteger(aSign, aBytes); 815 BigInteger result = aNumber.setBit(number); 816 byte resBytes[] = new byte[rBytes.length]; 817 resBytes = result.toByteArray(); 818 for(int i = 0; i < resBytes.length; i++) { 819 assertTrue(resBytes[i] == rBytes[i]); 820 } 821 assertEquals("incorrect sign", 1, result.signum()); 822 } 823 824 /** 825 * setBit(int n) inside a positive number 826 */ 827 public void testSetBitPositiveInside4 () { 828 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 829 int aSign = 1; 830 int number = 50; 831 byte rBytes[] = {1, -128, 56, 100, -2, -76, 93, 45, 91, 3, -15, 35, 26}; 832 BigInteger aNumber = new BigInteger(aSign, aBytes); 833 BigInteger result = aNumber.setBit(number); 834 byte resBytes[] = new byte[rBytes.length]; 835 resBytes = result.toByteArray(); 836 for(int i = 0; i < resBytes.length; i++) { 837 assertTrue(resBytes[i] == rBytes[i]); 838 } 839 assertEquals("incorrect sign", 1, result.signum()); 840 } 841 842 /** 843 * setBit(int n) outside a positive number 844 */ 845 public void testSetBitPositiveOutside1() { 846 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 847 int aSign = 1; 848 int number = 150; 849 byte rBytes[] = {64, 0, 0, 0, 0, 0, 1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 850 BigInteger aNumber = new BigInteger(aSign, aBytes); 851 BigInteger result = aNumber.setBit(number); 852 byte resBytes[] = new byte[rBytes.length]; 853 resBytes = result.toByteArray(); 854 for(int i = 0; i < resBytes.length; i++) { 855 assertTrue(resBytes[i] == rBytes[i]); 856 } 857 assertEquals("incorrect sign", 1, result.signum()); 858 } 859 860 /** 861 * setBit(int n) outside a positive number 862 */ 863 public void testSetBitPositiveOutside2() { 864 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 865 int aSign = 1; 866 int number = 223; 867 byte rBytes[] = {0, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 868 BigInteger aNumber = new BigInteger(aSign, aBytes); 869 BigInteger result = aNumber.setBit(number); 870 byte resBytes[] = new byte[rBytes.length]; 871 resBytes = result.toByteArray(); 872 for(int i = 0; i < resBytes.length; i++) { 873 assertTrue(resBytes[i] == rBytes[i]); 874 } 875 assertEquals("incorrect sign", 1, result.signum()); 876 } 877 878 /** 879 * setBit(int n) the leftmost bit in a positive number 880 */ 881 public void testSetBitTopPositive() { 882 byte aBytes[] = {1, -128, 56, 100, -15, 35, 26}; 883 int aSign = 1; 884 int number = 63; 885 byte rBytes[] = {0, -128, 1, -128, 56, 100, -15, 35, 26}; 886 BigInteger aNumber = new BigInteger(aSign, aBytes); 887 BigInteger result = aNumber.setBit(number); 888 byte resBytes[] = new byte[rBytes.length]; 889 resBytes = result.toByteArray(); 890 for(int i = 0; i < resBytes.length; i++) { 891 assertTrue(resBytes[i] == rBytes[i]); 892 } 893 assertEquals("incorrect sign", 1, result.signum()); 894 } 895 896 /** 897 * setBit(int n) the leftmost bit in a negative number 898 */ 899 public void testSetBitLeftmostNegative() { 900 byte aBytes[] = {1, -128, 56, 100, -15, 35, 26}; 901 int aSign = -1; 902 int number = 48; 903 byte rBytes[] = {-1, 127, -57, -101, 14, -36, -26, 49}; 904 BigInteger aNumber = new BigInteger(aSign, aBytes); 905 BigInteger result = aNumber.setBit(number); 906 byte resBytes[] = new byte[rBytes.length]; 907 resBytes = result.toByteArray(); 908 for(int i = 0; i < resBytes.length; i++) { 909 assertTrue(resBytes[i] == rBytes[i]); 910 } 911 assertEquals("incorrect sign", -1, result.signum()); 912 } 913 914 /** 915 * setBit(int n) inside a negative number 916 */ 917 public void testSetBitNegativeInside1() { 918 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 919 int aSign = -1; 920 int number = 15; 921 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 922 BigInteger aNumber = new BigInteger(aSign, aBytes); 923 BigInteger result = aNumber.setBit(number); 924 byte resBytes[] = new byte[rBytes.length]; 925 resBytes = result.toByteArray(); 926 for(int i = 0; i < resBytes.length; i++) { 927 assertTrue(resBytes[i] == rBytes[i]); 928 } 929 assertEquals("incorrect sign", -1, result.signum()); 930 } 931 932 /** 933 * setBit(int n) inside a negative number 934 */ 935 public void testSetBitNegativeInside2() { 936 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 937 int aSign = -1; 938 int number = 44; 939 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 940 BigInteger aNumber = new BigInteger(aSign, aBytes); 941 BigInteger result = aNumber.setBit(number); 942 byte resBytes[] = new byte[rBytes.length]; 943 resBytes = result.toByteArray(); 944 for(int i = 0; i < resBytes.length; i++) { 945 assertTrue(resBytes[i] == rBytes[i]); 946 } 947 assertEquals("incorrect sign", -1, result.signum()); 948 } 949 950 /** 951 * setBit(int n) inside a negative number with all ones in bit representation 952 */ 953 public void testSetBitNegativeInside3() { 954 String as = "-18446744073709551615"; 955 String res = "-18446744073709551611"; 956 int number = 2; 957 BigInteger aNumber = new BigInteger(as); 958 BigInteger result = aNumber.setBit(number); 959 assertEquals(res, result.toString()); 960 } 961 962 /** 963 * setBit(0) in the negative number of length 1 964 * with all ones in bit representation. 965 * the resulting number's length is 2. 966 */ 967 public void testSetBitNegativeInside4() { 968 String as = "-4294967295"; 969 int number = 0; 970 BigInteger aNumber = new BigInteger(as); 971 BigInteger result = aNumber.setBit(number); 972 assertEquals(as, result.toString()); 973 } 974 975 /** 976 * setBit(0) in the negative number of length 2 977 * with all ones in bit representation. 978 * the resulting number's length is 3. 979 */ 980 public void testSetBitNegativeInside5() { 981 String as = "-18446744073709551615"; 982 int number = 0; 983 BigInteger aNumber = new BigInteger(as); 984 BigInteger result = aNumber.setBit(number); 985 assertEquals(as, result.toString()); 986 } 987 988 /** 989 * setBit(int n) outside a negative number 990 */ 991 public void testSetBitNegativeOutside1() { 992 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 993 int aSign = -1; 994 int number = 150; 995 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 996 BigInteger aNumber = new BigInteger(aSign, aBytes); 997 BigInteger result = aNumber.setBit(number); 998 byte resBytes[] = new byte[rBytes.length]; 999 resBytes = result.toByteArray(); 1000 for(int i = 0; i < resBytes.length; i++) { 1001 assertTrue(resBytes[i] == rBytes[i]); 1002 } 1003 assertEquals("incorrect sign", -1, result.signum()); 1004 } 1005 1006 /** 1007 * setBit(int n) outside a negative number 1008 */ 1009 public void testSetBitNegativeOutside2() { 1010 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1011 int aSign = -1; 1012 int number = 191; 1013 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -26}; 1014 BigInteger aNumber = new BigInteger(aSign, aBytes); 1015 BigInteger result = aNumber.setBit(number); 1016 byte resBytes[] = new byte[rBytes.length]; 1017 resBytes = result.toByteArray(); 1018 for(int i = 0; i < resBytes.length; i++) { 1019 assertTrue(resBytes[i] == rBytes[i]); 1020 } 1021 assertEquals("incorrect sign", -1, result.signum()); 1022 } 1023 1024 /** 1025 * setBit: check the case when the number of bit to be set can be 1026 * represented as n * 32 + 31, where n is an arbitrary integer. 1027 * Here 191 = 5 * 32 + 31 1028 */ 1029 public void testSetBitBug1331() { 1030 BigInteger result = BigInteger.valueOf(0L).setBit(191); 1031 assertEquals("incorrect value", "3138550867693340381917894711603833208051177722232017256448", result.toString()); 1032 assertEquals("incorrect sign", 1, result.signum()); 1033 } 1034 1035 /** 1036 * shiftLeft(int n), n = 0 1037 */ 1038 public void testShiftLeft1() { 1039 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1040 int aSign = 1; 1041 int number = 0; 1042 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1043 BigInteger aNumber = new BigInteger(aSign, aBytes); 1044 BigInteger result = aNumber.shiftLeft(number); 1045 byte resBytes[] = new byte[rBytes.length]; 1046 resBytes = result.toByteArray(); 1047 for(int i = 0; i < resBytes.length; i++) { 1048 assertTrue(resBytes[i] == rBytes[i]); 1049 } 1050 assertEquals("incorrect sign", 1, result.signum()); 1051 } 1052 1053 /** 1054 * shiftLeft(int n), n < 0 1055 */ 1056 public void testShiftLeft2() { 1057 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1058 int aSign = 1; 1059 int number = -27; 1060 byte rBytes[] = {48, 7, 12, -97, -42, -117, 37, -85, 96}; 1061 BigInteger aNumber = new BigInteger(aSign, aBytes); 1062 BigInteger result = aNumber.shiftLeft(number); 1063 byte resBytes[] = new byte[rBytes.length]; 1064 resBytes = result.toByteArray(); 1065 for(int i = 0; i < resBytes.length; i++) { 1066 assertTrue(resBytes[i] == rBytes[i]); 1067 } 1068 assertEquals("incorrect sign", 1, result.signum()); 1069 } 1070 1071 /** 1072 * shiftLeft(int n) a positive number, n > 0 1073 */ 1074 public void testShiftLeft3() { 1075 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1076 int aSign = 1; 1077 int number = 27; 1078 byte rBytes[] = {12, 1, -61, 39, -11, -94, -55, 106, -40, 31, -119, 24, -48, 0, 0, 0}; 1079 BigInteger aNumber = new BigInteger(aSign, aBytes); 1080 BigInteger result = aNumber.shiftLeft(number); 1081 byte resBytes[] = new byte[rBytes.length]; 1082 resBytes = result.toByteArray(); 1083 for(int i = 0; i < resBytes.length; i++) { 1084 assertTrue(resBytes[i] == rBytes[i]); 1085 } 1086 assertEquals("incorrect sign", 1, result.signum()); 1087 } 1088 1089 /** 1090 * shiftLeft(int n) a positive number, n > 0 1091 */ 1092 public void testShiftLeft4() { 1093 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1094 int aSign = 1; 1095 int number = 45; 1096 byte rBytes[] = {48, 7, 12, -97, -42, -117, 37, -85, 96, 126, 36, 99, 64, 0, 0, 0, 0, 0}; 1097 BigInteger aNumber = new BigInteger(aSign, aBytes); 1098 BigInteger result = aNumber.shiftLeft(number); 1099 byte resBytes[] = new byte[rBytes.length]; 1100 resBytes = result.toByteArray(); 1101 for(int i = 0; i < resBytes.length; i++) { 1102 assertTrue(resBytes[i] == rBytes[i]); 1103 } 1104 assertEquals("incorrect sign", 1, result.signum()); 1105 } 1106 1107 /** 1108 * shiftLeft(int n) a negative number, n > 0 1109 */ 1110 public void testShiftLeft5() { 1111 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1112 int aSign = -1; 1113 int number = 45; 1114 byte rBytes[] = {-49, -8, -13, 96, 41, 116, -38, 84, -97, -127, -37, -100, -64, 0, 0, 0, 0, 0}; 1115 BigInteger aNumber = new BigInteger(aSign, aBytes); 1116 BigInteger result = aNumber.shiftLeft(number); 1117 byte resBytes[] = new byte[rBytes.length]; 1118 resBytes = result.toByteArray(); 1119 for(int i = 0; i < resBytes.length; i++) { 1120 assertTrue(resBytes[i] == rBytes[i]); 1121 } 1122 assertEquals("incorrect sign", -1, result.signum()); 1123 } 1124 1125 /** 1126 * shiftRight(int n), n = 0 1127 */ 1128 public void testShiftRight1() { 1129 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1130 int aSign = 1; 1131 int number = 0; 1132 byte rBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1133 BigInteger aNumber = new BigInteger(aSign, aBytes); 1134 BigInteger result = aNumber.shiftRight(number); 1135 byte resBytes[] = new byte[rBytes.length]; 1136 resBytes = result.toByteArray(); 1137 for(int i = 0; i < resBytes.length; i++) { 1138 assertTrue(resBytes[i] == rBytes[i]); 1139 } 1140 assertEquals("incorrect sign", 1, result.signum()); 1141 } 1142 1143 /** 1144 * shiftRight(int n), n < 0 1145 */ 1146 public void testShiftRight2() { 1147 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1148 int aSign = 1; 1149 int number = -27; 1150 byte rBytes[] = {12, 1, -61, 39, -11, -94, -55, 106, -40, 31, -119, 24, -48, 0, 0, 0}; 1151 BigInteger aNumber = new BigInteger(aSign, aBytes); 1152 BigInteger result = aNumber.shiftRight(number); 1153 byte resBytes[] = new byte[rBytes.length]; 1154 resBytes = result.toByteArray(); 1155 for(int i = 0; i < resBytes.length; i++) { 1156 assertTrue(resBytes[i] == rBytes[i]); 1157 } 1158 assertEquals("incorrect sign", 1, result.signum()); 1159 } 1160 1161 /** 1162 * shiftRight(int n), 0 < n < 32 1163 */ 1164 public void testShiftRight3() { 1165 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1166 int aSign = 1; 1167 int number = 27; 1168 byte rBytes[] = {48, 7, 12, -97, -42, -117, 37, -85, 96}; 1169 BigInteger aNumber = new BigInteger(aSign, aBytes); 1170 BigInteger result = aNumber.shiftRight(number); 1171 byte resBytes[] = new byte[rBytes.length]; 1172 resBytes = result.toByteArray(); 1173 for(int i = 0; i < resBytes.length; i++) { 1174 assertTrue(resBytes[i] == rBytes[i]); 1175 } 1176 assertEquals("incorrect sign", 1, result.signum()); 1177 } 1178 1179 /** 1180 * shiftRight(int n), n > 32 1181 */ 1182 public void testShiftRight4() { 1183 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1184 int aSign = 1; 1185 int number = 45; 1186 byte rBytes[] = {12, 1, -61, 39, -11, -94, -55}; 1187 BigInteger aNumber = new BigInteger(aSign, aBytes); 1188 BigInteger result = aNumber.shiftRight(number); 1189 byte resBytes[] = new byte[rBytes.length]; 1190 resBytes = result.toByteArray(); 1191 for(int i = 0; i < resBytes.length; i++) { 1192 assertTrue(resBytes[i] == rBytes[i]); 1193 } 1194 assertEquals("incorrect sign", 1, result.signum()); 1195 } 1196 1197 /** 1198 * shiftRight(int n), n is greater than bitLength() 1199 */ 1200 public void testShiftRight5() { 1201 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1202 int aSign = 1; 1203 int number = 300; 1204 byte rBytes[] = {0}; 1205 BigInteger aNumber = new BigInteger(aSign, aBytes); 1206 BigInteger result = aNumber.shiftRight(number); 1207 byte resBytes[] = new byte[rBytes.length]; 1208 resBytes = result.toByteArray(); 1209 for(int i = 0; i < resBytes.length; i++) { 1210 assertTrue(resBytes[i] == rBytes[i]); 1211 } 1212 assertEquals("incorrect sign", 0, result.signum()); 1213 } 1214 1215 /** 1216 * shiftRight a negative number; 1217 * shift distance is multiple of 32; 1218 * shifted bits are NOT zeroes. 1219 */ 1220 public void testShiftRightNegNonZeroesMul32() { 1221 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 1, 0, 0, 0, 0, 0, 0, 0}; 1222 int aSign = -1; 1223 int number = 64; 1224 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -92}; 1225 BigInteger aNumber = new BigInteger(aSign, aBytes); 1226 BigInteger result = aNumber.shiftRight(number); 1227 byte resBytes[] = new byte[rBytes.length]; 1228 resBytes = result.toByteArray(); 1229 for(int i = 0; i < resBytes.length; i++) { 1230 assertTrue(resBytes[i] == rBytes[i]); 1231 } 1232 assertEquals("incorrect sign", -1, result.signum()); 1233 } 1234 1235 /** 1236 * shiftRight a negative number; 1237 * shift distance is NOT multiple of 32; 1238 * shifted bits are NOT zeroes. 1239 */ 1240 public void testShiftRightNegNonZeroes() { 1241 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 0, 0, 0, 0, 0, 0, 0, 0}; 1242 int aSign = -1; 1243 int number = 68; 1244 byte rBytes[] = {-25, -4, 121, -80, 20, -70, 109, 42}; 1245 BigInteger aNumber = new BigInteger(aSign, aBytes); 1246 BigInteger result = aNumber.shiftRight(number); 1247 byte resBytes[] = new byte[rBytes.length]; 1248 resBytes = result.toByteArray(); 1249 for(int i = 0; i < resBytes.length; i++) { 1250 assertTrue(resBytes[i] == rBytes[i]); 1251 } 1252 assertEquals("incorrect sign", -1, result.signum()); 1253 } 1254 1255 /** 1256 * shiftRight a negative number; 1257 * shift distance is NOT multiple of 32; 1258 * shifted bits are zeroes. 1259 */ 1260 public void testShiftRightNegZeroes() { 1261 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 1262 int aSign = -1; 1263 int number = 68; 1264 byte rBytes[] = {-25, -4, 121, -80, 20, -70, 109, 48}; 1265 BigInteger aNumber = new BigInteger(aSign, aBytes); 1266 BigInteger result = aNumber.shiftRight(number); 1267 byte resBytes[] = new byte[rBytes.length]; 1268 resBytes = result.toByteArray(); 1269 for(int i = 0; i < resBytes.length; i++) { 1270 assertTrue(resBytes[i] == rBytes[i]); 1271 } 1272 assertEquals("incorrect sign", -1, result.signum()); 1273 } 1274 1275 /** 1276 * shiftRight a negative number; 1277 * shift distance is multiple of 32; 1278 * shifted bits are zeroes. 1279 */ 1280 public void testShiftRightNegZeroesMul32() { 1281 byte aBytes[] = {1, -128, 56, 100, -2, -76, 89, 45, 91, 0, 0, 0, 0, 0, 0, 0, 0}; 1282 int aSign = -1; 1283 int number = 64; 1284 byte rBytes[] = {-2, 127, -57, -101, 1, 75, -90, -46, -91}; 1285 BigInteger aNumber = new BigInteger(aSign, aBytes); 1286 BigInteger result = aNumber.shiftRight(number); 1287 byte resBytes[] = new byte[rBytes.length]; 1288 resBytes = result.toByteArray(); 1289 for(int i = 0; i < resBytes.length; i++) { 1290 assertTrue(resBytes[i] == rBytes[i]); 1291 } 1292 assertEquals("incorrect sign", -1, result.signum()); 1293 } 1294 1295 /** 1296 * testBit(int n) of a negative n 1297 */ 1298 public void testTestBitException() { 1299 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1300 int aSign = 1; 1301 int number = -7; 1302 BigInteger aNumber = new BigInteger(aSign, aBytes); 1303 try { 1304 aNumber.testBit(number); 1305 fail("ArithmeticException has not been caught"); 1306 } catch (ArithmeticException e) { 1307 } 1308 } 1309 1310 /** 1311 * testBit(int n) of a positive number 1312 */ 1313 public void testTestBitPositive1() { 1314 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1315 int aSign = 1; 1316 int number = 7; 1317 BigInteger aNumber = new BigInteger(aSign, aBytes); 1318 assertTrue(!aNumber.testBit(number)); 1319 } 1320 1321 /** 1322 * testBit(int n) of a positive number 1323 */ 1324 public void testTestBitPositive2() { 1325 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1326 int aSign = 1; 1327 int number = 45; 1328 BigInteger aNumber = new BigInteger(aSign, aBytes); 1329 assertTrue(aNumber.testBit(number)); 1330 } 1331 1332 /** 1333 * testBit(int n) of a positive number, n > bitLength() 1334 */ 1335 public void testTestBitPositive3() { 1336 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1337 int aSign = 1; 1338 int number = 300; 1339 BigInteger aNumber = new BigInteger(aSign, aBytes); 1340 assertTrue(!aNumber.testBit(number)); 1341 } 1342 1343 /** 1344 * testBit(int n) of a negative number 1345 */ 1346 public void testTestBitNegative1() { 1347 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1348 int aSign = -1; 1349 int number = 7; 1350 BigInteger aNumber = new BigInteger(aSign, aBytes); 1351 assertTrue(aNumber.testBit(number)); 1352 } 1353 1354 /** 1355 * testBit(int n) of a positive n 1356 */ 1357 public void testTestBitNegative2() { 1358 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1359 int aSign = -1; 1360 int number = 45; 1361 BigInteger aNumber = new BigInteger(aSign, aBytes); 1362 assertTrue(!aNumber.testBit(number)); 1363 } 1364 1365 /** 1366 * testBit(int n) of a positive n, n > bitLength() 1367 */ 1368 public void testTestBitNegative3() { 1369 byte aBytes[] = {-1, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26}; 1370 int aSign = -1; 1371 int number = 300; 1372 BigInteger aNumber = new BigInteger(aSign, aBytes); 1373 assertTrue(aNumber.testBit(number)); 1374 } 1375 } 1376