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: divide, remainder, mod, and divideAndRemainder 29 */ 30 public class BigIntegerDivideTest extends TestCase { 31 /** 32 * Divide by zero 33 */ 34 public void testCase1() { 35 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 36 byte bBytes[] = {0}; 37 int aSign = 1; 38 int bSign = 0; 39 BigInteger aNumber = new BigInteger(aSign, aBytes); 40 BigInteger bNumber = new BigInteger(bSign, bBytes); 41 try { 42 aNumber.divide(bNumber); 43 fail("ArithmeticException has not been caught"); 44 } catch (ArithmeticException e) { 45 } 46 } 47 48 /** 49 * Divide by ZERO 50 */ 51 public void testCase2() { 52 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 53 int aSign = 1; 54 BigInteger aNumber = new BigInteger(aSign, aBytes); 55 BigInteger bNumber = BigInteger.ZERO; 56 try { 57 aNumber.divide(bNumber); 58 fail("ArithmeticException has not been caught"); 59 } catch (ArithmeticException e) { 60 } 61 } 62 63 /** 64 * Divide two equal positive numbers 65 */ 66 public void testCase3() { 67 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 68 byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 69 int aSign = 1; 70 int bSign = 1; 71 byte rBytes[] = {1}; 72 BigInteger aNumber = new BigInteger(aSign, aBytes); 73 BigInteger bNumber = new BigInteger(bSign, bBytes); 74 BigInteger result = aNumber.divide(bNumber); 75 byte resBytes[] = new byte[rBytes.length]; 76 resBytes = result.toByteArray(); 77 for(int i = 0; i < resBytes.length; i++) { 78 assertTrue(resBytes[i] == rBytes[i]); 79 } 80 assertEquals("incorrect sign", 1, result.signum()); 81 } 82 83 /** 84 * Divide two equal in absolute value numbers of different signs. 85 */ 86 public void testCase4() { 87 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 88 byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 89 int aSign = -1; 90 int bSign = 1; 91 byte rBytes[] = {-1}; 92 BigInteger aNumber = new BigInteger(aSign, aBytes); 93 BigInteger bNumber = new BigInteger(bSign, bBytes); 94 BigInteger result = aNumber.divide(bNumber); 95 byte resBytes[] = new byte[rBytes.length]; 96 resBytes = result.toByteArray(); 97 for(int i = 0; i < resBytes.length; i++) { 98 assertTrue(resBytes[i] == rBytes[i]); 99 } 100 assertEquals("incorrect sign", -1, result.signum()); 101 } 102 103 /** 104 * Divide two numbers of different length and different signs. 105 * The second is longer. 106 */ 107 public void testCase5() { 108 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 109 byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 1, 2, 3, 4, 5}; 110 int aSign = -1; 111 int bSign = 1; 112 byte rBytes[] = {0}; 113 BigInteger aNumber = new BigInteger(aSign, aBytes); 114 BigInteger bNumber = new BigInteger(bSign, bBytes); 115 BigInteger result = aNumber.divide(bNumber); 116 byte resBytes[] = new byte[rBytes.length]; 117 resBytes = result.toByteArray(); 118 for(int i = 0; i < resBytes.length; i++) { 119 assertTrue(resBytes[i] == rBytes[i]); 120 } 121 assertEquals("incorrect sign", 0, result.signum()); 122 } 123 124 /** 125 * Divide two positive numbers of the same length. 126 * The second is greater. 127 */ 128 public void testCase6() { 129 byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127}; 130 byte bBytes[] = {15, 100, 56, 7, 98, -1, 39, -128, 127}; 131 int aSign = 1; 132 int bSign = 1; 133 byte rBytes[] = {0}; 134 BigInteger aNumber = new BigInteger(aSign, aBytes); 135 BigInteger bNumber = new BigInteger(bSign, bBytes); 136 BigInteger result = aNumber.divide(bNumber); 137 byte resBytes[] = new byte[rBytes.length]; 138 resBytes = result.toByteArray(); 139 for(int i = 0; i < resBytes.length; i++) { 140 assertTrue(resBytes[i] == rBytes[i]); 141 } 142 assertEquals("incorrect sign", 0, result.signum()); 143 } 144 145 /** 146 * Divide two positive numbers. 147 */ 148 public void testCase7() { 149 byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 150 byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 151 int aSign = 1; 152 int bSign = 1; 153 byte rBytes[] = {23, 115, 11, 78, 35, -11}; 154 BigInteger aNumber = new BigInteger(aSign, aBytes); 155 BigInteger bNumber = new BigInteger(bSign, bBytes); 156 BigInteger result = aNumber.divide(bNumber); 157 byte resBytes[] = new byte[rBytes.length]; 158 resBytes = result.toByteArray(); 159 for(int i = 0; i < resBytes.length; i++) { 160 assertTrue(resBytes[i] == rBytes[i]); 161 } 162 assertEquals("incorrect sign", 1, result.signum()); 163 } 164 165 /** 166 * Divide a positive number by a negative one. 167 */ 168 public void testCase8() { 169 byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 170 byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 171 int aSign = 1; 172 int bSign = -1; 173 byte rBytes[] = {-24, -116, -12, -79, -36, 11}; 174 BigInteger aNumber = new BigInteger(aSign, aBytes); 175 BigInteger bNumber = new BigInteger(bSign, bBytes); 176 BigInteger result = aNumber.divide(bNumber); 177 byte resBytes[] = new byte[rBytes.length]; 178 resBytes = result.toByteArray(); 179 for(int i = 0; i < resBytes.length; i++) { 180 assertTrue(resBytes[i] == rBytes[i]); 181 } 182 assertEquals("incorrect sign", -1, result.signum()); 183 } 184 185 /** 186 * Divide a negative number by a positive one. 187 */ 188 public void testCase9() { 189 byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 190 byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 191 int aSign = -1; 192 int bSign = 1; 193 byte rBytes[] = {-24, -116, -12, -79, -36, 11}; 194 BigInteger aNumber = new BigInteger(aSign, aBytes); 195 BigInteger bNumber = new BigInteger(bSign, bBytes); 196 BigInteger result = aNumber.divide(bNumber); 197 byte resBytes[] = new byte[rBytes.length]; 198 resBytes = result.toByteArray(); 199 for(int i = 0; i < resBytes.length; i++) { 200 assertTrue(resBytes[i] == rBytes[i]); 201 } 202 assertEquals("incorrect sign", -1, result.signum()); 203 } 204 205 /** 206 * Divide two negative numbers. 207 */ 208 public void testCase10() { 209 byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 210 byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 211 int aSign = -1; 212 int bSign = -1; 213 byte rBytes[] = {23, 115, 11, 78, 35, -11}; 214 BigInteger aNumber = new BigInteger(aSign, aBytes); 215 BigInteger bNumber = new BigInteger(bSign, bBytes); 216 BigInteger result = aNumber.divide(bNumber); 217 byte resBytes[] = new byte[rBytes.length]; 218 resBytes = result.toByteArray(); 219 for(int i = 0; i < resBytes.length; i++) { 220 assertTrue(resBytes[i] == rBytes[i]); 221 } 222 assertEquals("incorrect sign", 1, result.signum()); 223 } 224 225 /** 226 * Divide zero by a negative number. 227 */ 228 public void testCase11() { 229 byte aBytes[] = {0}; 230 byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 231 int aSign = 0; 232 int bSign = -1; 233 byte rBytes[] = {0}; 234 BigInteger aNumber = new BigInteger(aSign, aBytes); 235 BigInteger bNumber = new BigInteger(bSign, bBytes); 236 BigInteger result = aNumber.divide(bNumber); 237 byte resBytes[] = new byte[rBytes.length]; 238 resBytes = result.toByteArray(); 239 for(int i = 0; i < resBytes.length; i++) { 240 assertTrue(resBytes[i] == rBytes[i]); 241 } 242 assertEquals("incorrect sign", 0, result.signum()); 243 } 244 245 /** 246 * Divide ZERO by a negative number. 247 */ 248 public void testCase12() { 249 byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 250 int bSign = -1; 251 byte rBytes[] = {0}; 252 BigInteger aNumber = BigInteger.ZERO; 253 BigInteger bNumber = new BigInteger(bSign, bBytes); 254 BigInteger result = aNumber.divide(bNumber); 255 byte resBytes[] = new byte[rBytes.length]; 256 resBytes = result.toByteArray(); 257 for(int i = 0; i < resBytes.length; i++) { 258 assertTrue(resBytes[i] == rBytes[i]); 259 } 260 assertEquals("incorrect sign", 0, result.signum()); 261 } 262 263 /** 264 * Divide a positive number by ONE. 265 */ 266 public void testCase13() { 267 byte aBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 268 int aSign = 1; 269 byte rBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 270 BigInteger aNumber = new BigInteger(aSign, aBytes); 271 BigInteger bNumber = BigInteger.ONE; 272 BigInteger result = aNumber.divide(bNumber); 273 byte resBytes[] = new byte[rBytes.length]; 274 resBytes = result.toByteArray(); 275 for(int i = 0; i < resBytes.length; i++) { 276 assertTrue(resBytes[i] == rBytes[i]); 277 } 278 assertEquals("incorrect sign", 1, result.signum()); 279 } 280 281 /** 282 * Divide ONE by ONE. 283 */ 284 public void testCase14() { 285 byte rBytes[] = {1}; 286 BigInteger aNumber = BigInteger.ONE; 287 BigInteger bNumber = BigInteger.ONE; 288 BigInteger result = aNumber.divide(bNumber); 289 byte resBytes[] = new byte[rBytes.length]; 290 resBytes = result.toByteArray(); 291 for(int i = 0; i < resBytes.length; i++) { 292 assertTrue(resBytes[i] == rBytes[i]); 293 } 294 assertEquals("incorrect sign", 1, result.signum()); 295 } 296 297 /** 298 * Verifies the case when borrow != 0 in the private divide method. 299 */ 300 public void testDivisionKnuth1() { 301 byte aBytes[] = {-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; 302 byte bBytes[] = {-3, -3, -3, -3}; 303 int aSign = 1; 304 int bSign = 1; 305 byte rBytes[] = {0, -5, -12, -33, -96, -36, -105, -56, 92, 15, 48, -109}; 306 BigInteger aNumber = new BigInteger(aSign, aBytes); 307 BigInteger bNumber = new BigInteger(bSign, bBytes); 308 BigInteger result = aNumber.divide(bNumber); 309 byte resBytes[] = new byte[rBytes.length]; 310 resBytes = result.toByteArray(); 311 for(int i = 0; i < resBytes.length; i++) { 312 assertTrue(resBytes[i] == rBytes[i]); 313 } 314 assertEquals("incorrect sign", 1, result.signum()); 315 } 316 317 /** 318 * Verifies the case when the divisor is already normalized. 319 */ 320 public void testDivisionKnuthIsNormalized() { 321 byte aBytes[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; 322 byte bBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1}; 323 int aSign = -1; 324 int bSign = -1; 325 byte rBytes[] = {0, -9, -8, -7, -6, -5, -4, -3}; 326 BigInteger aNumber = new BigInteger(aSign, aBytes); 327 BigInteger bNumber = new BigInteger(bSign, bBytes); 328 BigInteger result = aNumber.divide(bNumber); 329 byte resBytes[] = new byte[rBytes.length]; 330 resBytes = result.toByteArray(); 331 for(int i = 0; i < resBytes.length; i++) { 332 assertTrue(resBytes[i] == rBytes[i]); 333 } 334 assertEquals("incorrect sign", 1, result.signum()); 335 } 336 337 /** 338 * Verifies the case when the first digits of the dividend 339 * and divisor equal. 340 */ 341 public void testDivisionKnuthFirstDigitsEqual() { 342 byte aBytes[] = {2, -3, -4, -5, -1, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; 343 byte bBytes[] = {2, -3, -4, -5, -1, -1, -1, -1}; 344 int aSign = -1; 345 int bSign = -1; 346 byte rBytes[] = {0, -1, -1, -1, -1, -2, -88, -60, 41}; 347 BigInteger aNumber = new BigInteger(aSign, aBytes); 348 BigInteger bNumber = new BigInteger(bSign, bBytes); 349 BigInteger result = aNumber.divide(bNumber); 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 * Divide the number of one digit by the number of one digit 360 */ 361 public void testDivisionKnuthOneDigitByOneDigit() { 362 byte aBytes[] = {113, -83, 123, -5}; 363 byte bBytes[] = {2, -3, -4, -5}; 364 int aSign = 1; 365 int bSign = -1; 366 byte rBytes[] = {-37}; 367 BigInteger aNumber = new BigInteger(aSign, aBytes); 368 BigInteger bNumber = new BigInteger(bSign, bBytes); 369 BigInteger result = aNumber.divide(bNumber); 370 byte resBytes[] = new byte[rBytes.length]; 371 resBytes = result.toByteArray(); 372 for(int i = 0; i < resBytes.length; i++) { 373 assertTrue(resBytes[i] == rBytes[i]); 374 } 375 assertEquals("incorrect sign", -1, result.signum()); 376 } 377 378 /** 379 * Divide the number of multi digits by the number of one digit 380 */ 381 public void testDivisionKnuthMultiDigitsByOneDigit() { 382 byte aBytes[] = {113, -83, 123, -5, 18, -34, 67, 39, -29}; 383 byte bBytes[] = {2, -3, -4, -5}; 384 int aSign = 1; 385 int bSign = -1; 386 byte rBytes[] = {-38, 2, 7, 30, 109, -43}; 387 BigInteger aNumber = new BigInteger(aSign, aBytes); 388 BigInteger bNumber = new BigInteger(bSign, bBytes); 389 BigInteger result = aNumber.divide(bNumber); 390 byte resBytes[] = new byte[rBytes.length]; 391 resBytes = result.toByteArray(); 392 for(int i = 0; i < resBytes.length; i++) { 393 assertTrue(resBytes[i] == rBytes[i]); 394 } 395 assertEquals("incorrect sign", -1, result.signum()); 396 } 397 398 /** 399 * Remainder of division by zero 400 */ 401 public void testCase15() { 402 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 403 byte bBytes[] = {0}; 404 int aSign = 1; 405 int bSign = 0; 406 BigInteger aNumber = new BigInteger(aSign, aBytes); 407 BigInteger bNumber = new BigInteger(bSign, bBytes); 408 try { 409 aNumber.remainder(bNumber); 410 fail("ArithmeticException has not been caught"); 411 } catch (ArithmeticException e) { 412 } 413 } 414 415 /** 416 * Remainder of division of equal numbers 417 */ 418 public void testCase16() { 419 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 420 byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 421 int aSign = 1; 422 int bSign = 1; 423 byte rBytes[] = {0}; 424 BigInteger aNumber = new BigInteger(aSign, aBytes); 425 BigInteger bNumber = new BigInteger(bSign, bBytes); 426 BigInteger result = aNumber.remainder(bNumber); 427 byte resBytes[] = new byte[rBytes.length]; 428 resBytes = result.toByteArray(); 429 for(int i = 0; i < resBytes.length; i++) { 430 assertTrue(resBytes[i] == rBytes[i]); 431 } 432 assertEquals("incorrect sign", 0, result.signum()); 433 } 434 435 /** 436 * Remainder of division of two positive numbers 437 */ 438 public void testCase17() { 439 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 440 byte bBytes[] = {27, -15, 65, 39, 100}; 441 int aSign = 1; 442 int bSign = 1; 443 byte rBytes[] = {12, -21, 73, 56, 27}; 444 BigInteger aNumber = new BigInteger(aSign, aBytes); 445 BigInteger bNumber = new BigInteger(bSign, bBytes); 446 BigInteger result = aNumber.remainder(bNumber); 447 byte resBytes[] = new byte[rBytes.length]; 448 resBytes = result.toByteArray(); 449 for(int i = 0; i < resBytes.length; i++) { 450 assertTrue(resBytes[i] == rBytes[i]); 451 } 452 assertEquals("incorrect sign", 1, result.signum()); 453 } 454 455 /** 456 * Remainder of division of two negative numbers 457 */ 458 public void testCase18() { 459 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 460 byte bBytes[] = {27, -15, 65, 39, 100}; 461 int aSign = -1; 462 int bSign = -1; 463 byte rBytes[] = {-13, 20, -74, -57, -27}; 464 BigInteger aNumber = new BigInteger(aSign, aBytes); 465 BigInteger bNumber = new BigInteger(bSign, bBytes); 466 BigInteger result = aNumber.remainder(bNumber); 467 byte resBytes[] = new byte[rBytes.length]; 468 resBytes = result.toByteArray(); 469 for(int i = 0; i < resBytes.length; i++) { 470 assertTrue(resBytes[i] == rBytes[i]); 471 } 472 assertEquals("incorrect sign", -1, result.signum()); 473 } 474 475 /** 476 * Remainder of division of two numbers of different signs. 477 * The first is positive. 478 */ 479 public void testCase19() { 480 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 481 byte bBytes[] = {27, -15, 65, 39, 100}; 482 int aSign = 1; 483 int bSign = -1; 484 byte rBytes[] = {12, -21, 73, 56, 27}; 485 BigInteger aNumber = new BigInteger(aSign, aBytes); 486 BigInteger bNumber = new BigInteger(bSign, bBytes); 487 BigInteger result = aNumber.remainder(bNumber); 488 byte resBytes[] = new byte[rBytes.length]; 489 resBytes = result.toByteArray(); 490 for(int i = 0; i < resBytes.length; i++) { 491 assertTrue(resBytes[i] == rBytes[i]); 492 } 493 assertEquals("incorrect sign", 1, result.signum()); 494 } 495 496 /** 497 * Remainder of division of two numbers of different signs. 498 * The first is negative. 499 */ 500 public void testCase20() { 501 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 502 byte bBytes[] = {27, -15, 65, 39, 100}; 503 int aSign = -1; 504 int bSign = 1; 505 byte rBytes[] = {-13, 20, -74, -57, -27}; 506 BigInteger aNumber = new BigInteger(aSign, aBytes); 507 BigInteger bNumber = new BigInteger(bSign, bBytes); 508 BigInteger result = aNumber.remainder(bNumber); 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 * Tests the step D6 from the Knuth algorithm 519 */ 520 public void testRemainderKnuth1() { 521 byte aBytes[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1}; 522 byte bBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 523 int aSign = 1; 524 int bSign = 1; 525 byte rBytes[] = {1, 2, 3, 4, 5, 6, 7, 7, 18, -89}; 526 BigInteger aNumber = new BigInteger(aSign, aBytes); 527 BigInteger bNumber = new BigInteger(bSign, bBytes); 528 BigInteger result = aNumber.remainder(bNumber); 529 byte resBytes[] = new byte[rBytes.length]; 530 resBytes = result.toByteArray(); 531 for(int i = 0; i < resBytes.length; i++) { 532 assertTrue(resBytes[i] == rBytes[i]); 533 } 534 assertEquals("incorrect sign", 1, result.signum()); 535 } 536 537 /** 538 * Divide the number of one digit by the number of one digit 539 */ 540 public void testRemainderKnuthOneDigitByOneDigit() { 541 byte aBytes[] = {113, -83, 123, -5}; 542 byte bBytes[] = {2, -3, -4, -50}; 543 int aSign = 1; 544 int bSign = -1; 545 byte rBytes[] = {2, -9, -14, 53}; 546 BigInteger aNumber = new BigInteger(aSign, aBytes); 547 BigInteger bNumber = new BigInteger(bSign, bBytes); 548 BigInteger result = aNumber.remainder(bNumber); 549 byte resBytes[] = new byte[rBytes.length]; 550 resBytes = result.toByteArray(); 551 for(int i = 0; i < resBytes.length; i++) { 552 assertTrue(resBytes[i] == rBytes[i]); 553 } 554 assertEquals("incorrect sign", 1, result.signum()); 555 } 556 557 /** 558 * Divide the number of multi digits by the number of one digit 559 */ 560 public void testRemainderKnuthMultiDigitsByOneDigit() { 561 byte aBytes[] = {113, -83, 123, -5, 18, -34, 67, 39, -29}; 562 byte bBytes[] = {2, -3, -4, -50}; 563 int aSign = 1; 564 int bSign = -1; 565 byte rBytes[] = {2, -37, -60, 59}; 566 BigInteger aNumber = new BigInteger(aSign, aBytes); 567 BigInteger bNumber = new BigInteger(bSign, bBytes); 568 BigInteger result = aNumber.remainder(bNumber); 569 byte resBytes[] = new byte[rBytes.length]; 570 resBytes = result.toByteArray(); 571 for(int i = 0; i < resBytes.length; i++) { 572 assertTrue(resBytes[i] == rBytes[i]); 573 } 574 assertEquals("incorrect sign", 1, result.signum()); 575 } 576 577 /** 578 * divideAndRemainder of two numbers of different signs. 579 * The first is negative. 580 */ 581 public void testCase21() { 582 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 583 byte bBytes[] = {27, -15, 65, 39, 100}; 584 int aSign = -1; 585 int bSign = 1; 586 byte rBytes[][] = { 587 {-5, 94, -115, -74, -85, 84}, 588 {-13, 20, -74, -57, -27} 589 }; 590 BigInteger aNumber = new BigInteger(aSign, aBytes); 591 BigInteger bNumber = new BigInteger(bSign, bBytes); 592 BigInteger result[] = aNumber.divideAndRemainder(bNumber); 593 byte resBytes[] = new byte[rBytes.length]; 594 resBytes = result[0].toByteArray(); 595 for(int i = 0; i < resBytes.length; i++) { 596 if (resBytes[i] != rBytes[0][i]) { 597 fail("Incorrect quotation"); 598 } 599 } 600 assertEquals(-1, result[0].signum()); 601 resBytes = result[1].toByteArray(); 602 for(int i = 0; i < resBytes.length; i++) { 603 if (resBytes[i] != rBytes[1][i]) { 604 fail("Incorrect remainder"); 605 } 606 assertEquals(-1, result[1].signum()); 607 } 608 } 609 610 /** 611 * mod when modulus is negative 612 */ 613 public void testCase22() { 614 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 615 byte bBytes[] = {1, 30, 40, 56, -1, 45}; 616 int aSign = 1; 617 int bSign = -1; 618 BigInteger aNumber = new BigInteger(aSign, aBytes); 619 BigInteger bNumber = new BigInteger(bSign, bBytes); 620 try { 621 aNumber.mod(bNumber); 622 fail("ArithmeticException has not been caught"); 623 } catch (ArithmeticException e) { 624 } 625 } 626 627 /** 628 * mod when a divisor is positive 629 */ 630 public void testCase23() { 631 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 632 byte bBytes[] = {27, -15, 65, 39, 100}; 633 int aSign = 1; 634 int bSign = 1; 635 byte rBytes[] = {12, -21, 73, 56, 27}; 636 BigInteger aNumber = new BigInteger(aSign, aBytes); 637 BigInteger bNumber = new BigInteger(bSign, bBytes); 638 BigInteger result = aNumber.mod(bNumber); 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 * mod when a divisor is negative 649 */ 650 public void testCase24() { 651 byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 652 byte bBytes[] = {27, -15, 65, 39, 100}; 653 int aSign = -1; 654 int bSign = 1; 655 byte rBytes[] = {15, 5, -9, -17, 73}; 656 BigInteger aNumber = new BigInteger(aSign, aBytes); 657 BigInteger bNumber = new BigInteger(bSign, bBytes); 658 BigInteger result = aNumber.mod(bNumber); 659 byte resBytes[] = new byte[rBytes.length]; 660 resBytes = result.toByteArray(); 661 for(int i = 0; i < resBytes.length; i++) { 662 assertTrue(resBytes[i] == rBytes[i]); 663 } 664 assertEquals("incorrect sign", 1, result.signum()); 665 } 666 } 667