| 1 16 package org.apache.commons.lang.math; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 22 29 public class FractionTest extends TestCase { 30 31 private static final int SKIP = 500; 33 public FractionTest(String name) { 34 super(name); 35 } 36 37 public static Test suite() { 38 TestSuite suite = new TestSuite(FractionTest.class); 39 suite.setName("Fraction Tests"); 40 return suite; 41 } 42 43 public void setUp() { 44 } 45 46 48 public void testConstants() { 49 assertEquals(0, Fraction.ZERO.getNumerator()); 50 assertEquals(1, Fraction.ZERO.getDenominator()); 51 52 assertEquals(1, Fraction.ONE.getNumerator()); 53 assertEquals(1, Fraction.ONE.getDenominator()); 54 55 assertEquals(1, Fraction.ONE_HALF.getNumerator()); 56 assertEquals(2, Fraction.ONE_HALF.getDenominator()); 57 58 assertEquals(1, Fraction.ONE_THIRD.getNumerator()); 59 assertEquals(3, Fraction.ONE_THIRD.getDenominator()); 60 61 assertEquals(2, Fraction.TWO_THIRDS.getNumerator()); 62 assertEquals(3, Fraction.TWO_THIRDS.getDenominator()); 63 64 assertEquals(1, Fraction.ONE_QUARTER.getNumerator()); 65 assertEquals(4, Fraction.ONE_QUARTER.getDenominator()); 66 67 assertEquals(2, Fraction.TWO_QUARTERS.getNumerator()); 68 assertEquals(4, Fraction.TWO_QUARTERS.getDenominator()); 69 70 assertEquals(3, Fraction.THREE_QUARTERS.getNumerator()); 71 assertEquals(4, Fraction.THREE_QUARTERS.getDenominator()); 72 73 assertEquals(1, Fraction.ONE_FIFTH.getNumerator()); 74 assertEquals(5, Fraction.ONE_FIFTH.getDenominator()); 75 76 assertEquals(2, Fraction.TWO_FIFTHS.getNumerator()); 77 assertEquals(5, Fraction.TWO_FIFTHS.getDenominator()); 78 79 assertEquals(3, Fraction.THREE_FIFTHS.getNumerator()); 80 assertEquals(5, Fraction.THREE_FIFTHS.getDenominator()); 81 82 assertEquals(4, Fraction.FOUR_FIFTHS.getNumerator()); 83 assertEquals(5, Fraction.FOUR_FIFTHS.getDenominator()); 84 } 85 86 public void testFactory_int_int() { 87 Fraction f = null; 88 89 f = Fraction.getFraction(0, 1); 91 assertEquals(0, f.getNumerator()); 92 assertEquals(1, f.getDenominator()); 93 94 f = Fraction.getFraction(0, 2); 95 assertEquals(0, f.getNumerator()); 96 assertEquals(2, f.getDenominator()); 97 98 f = Fraction.getFraction(1, 1); 100 assertEquals(1, f.getNumerator()); 101 assertEquals(1, f.getDenominator()); 102 103 f = Fraction.getFraction(2, 1); 104 assertEquals(2, f.getNumerator()); 105 assertEquals(1, f.getDenominator()); 106 107 f = Fraction.getFraction(23, 345); 108 assertEquals(23, f.getNumerator()); 109 assertEquals(345, f.getDenominator()); 110 111 f = Fraction.getFraction(22, 7); 113 assertEquals(22, f.getNumerator()); 114 assertEquals(7, f.getDenominator()); 115 116 f = Fraction.getFraction(-6, 10); 118 assertEquals(-6, f.getNumerator()); 119 assertEquals(10, f.getDenominator()); 120 121 f = Fraction.getFraction(6, -10); 122 assertEquals(-6, f.getNumerator()); 123 assertEquals(10, f.getDenominator()); 124 125 f = Fraction.getFraction(-6, -10); 126 assertEquals(6, f.getNumerator()); 127 assertEquals(10, f.getDenominator()); 128 129 try { 131 f = Fraction.getFraction(1, 0); 132 fail("expecting ArithmeticException"); 133 } catch (ArithmeticException ex) {} 134 135 try { 136 f = Fraction.getFraction(2, 0); 137 fail("expecting ArithmeticException"); 138 } catch (ArithmeticException ex) {} 139 140 try { 141 f = Fraction.getFraction(-3, 0); 142 fail("expecting ArithmeticException"); 143 } catch (ArithmeticException ex) {} 144 145 try { 147 f = Fraction.getFraction(4, Integer.MIN_VALUE); 148 fail("expecting ArithmeticException"); 149 } catch (ArithmeticException ex) {} 150 try { 151 f = Fraction.getFraction(1, Integer.MIN_VALUE); 152 fail("expecting ArithmeticException"); 153 } catch (ArithmeticException ex) {} 154 } 155 156 public void testFactory_int_int_int() { 157 Fraction f = null; 158 159 f = Fraction.getFraction(0, 0, 2); 161 assertEquals(0, f.getNumerator()); 162 assertEquals(2, f.getDenominator()); 163 164 f = Fraction.getFraction(2, 0, 2); 165 assertEquals(4, f.getNumerator()); 166 assertEquals(2, f.getDenominator()); 167 168 f = Fraction.getFraction(0, 1, 2); 169 assertEquals(1, f.getNumerator()); 170 assertEquals(2, f.getDenominator()); 171 172 f = Fraction.getFraction(1, 1, 2); 174 assertEquals(3, f.getNumerator()); 175 assertEquals(2, f.getDenominator()); 176 177 try { 179 f = Fraction.getFraction(1, -6, -10); 180 fail("expecting ArithmeticException"); 181 } catch (ArithmeticException ex) {} 182 183 try { 184 f = Fraction.getFraction(1, -6, -10); 185 fail("expecting ArithmeticException"); 186 } catch (ArithmeticException ex) {} 187 188 try { 189 f = Fraction.getFraction(1, -6, -10); 190 fail("expecting ArithmeticException"); 191 } catch (ArithmeticException ex) {} 192 193 f = Fraction.getFraction(-1, 6, 10); 195 assertEquals(-16, f.getNumerator()); 196 assertEquals(10, f.getDenominator()); 197 198 try { 199 f = Fraction.getFraction(-1, -6, 10); 200 fail("expecting ArithmeticException"); 201 } catch (ArithmeticException ex) {} 202 203 try { 204 f = Fraction.getFraction(-1, 6, -10); 205 fail("expecting ArithmeticException"); 206 } catch (ArithmeticException ex) {} 207 208 try { 209 f = Fraction.getFraction(-1, -6, -10); 210 fail("expecting ArithmeticException"); 211 } catch (ArithmeticException ex) {} 212 213 try { 215 f = Fraction.getFraction(0, 1, 0); 216 fail("expecting ArithmeticException"); 217 } catch (ArithmeticException ex) {} 218 219 try { 220 f = Fraction.getFraction(1, 2, 0); 221 fail("expecting ArithmeticException"); 222 } catch (ArithmeticException ex) {} 223 224 try { 225 f = Fraction.getFraction(-1, -3, 0); 226 fail("expecting ArithmeticException"); 227 } catch (ArithmeticException ex) {} 228 229 try { 230 f = Fraction.getFraction(Integer.MAX_VALUE, 1, 2); 231 fail("expecting ArithmeticException"); 232 } catch (ArithmeticException ex) {} 233 234 try { 235 f = Fraction.getFraction(-Integer.MAX_VALUE, 1, 2); 236 fail("expecting ArithmeticException"); 237 } catch (ArithmeticException ex) {} 238 239 f = Fraction.getFraction(-1, 0, Integer.MAX_VALUE); 241 assertEquals(-Integer.MAX_VALUE, f.getNumerator()); 242 assertEquals(Integer.MAX_VALUE, f.getDenominator()); 243 244 try { 245 f = Fraction.getFraction(0, 4, Integer.MIN_VALUE); 247 fail("expecting ArithmeticException"); 248 } catch (ArithmeticException ex) {} 249 try { 250 f = Fraction.getFraction(1, 1, Integer.MAX_VALUE); 251 fail("expecting ArithmeticException"); 252 } catch (ArithmeticException ex) {} 253 try { 254 f = Fraction.getFraction(-1, 2, Integer.MAX_VALUE); 255 fail("expecting ArithmeticException"); 256 } catch (ArithmeticException ex) {} 257 } 258 public void testReducedFactory_int_int() { 259 Fraction f = null; 260 261 f = Fraction.getReducedFraction(0, 1); 263 assertEquals(0, f.getNumerator()); 264 assertEquals(1, f.getDenominator()); 265 266 f = Fraction.getReducedFraction(1, 1); 268 assertEquals(1, f.getNumerator()); 269 assertEquals(1, f.getDenominator()); 270 271 f = Fraction.getReducedFraction(2, 1); 272 assertEquals(2, f.getNumerator()); 273 assertEquals(1, f.getDenominator()); 274 275 f = Fraction.getReducedFraction(22, 7); 277 assertEquals(22, f.getNumerator()); 278 assertEquals(7, f.getDenominator()); 279 280 f = Fraction.getReducedFraction(-6, 10); 282 assertEquals(-3, f.getNumerator()); 283 assertEquals(5, f.getDenominator()); 284 285 f = Fraction.getReducedFraction(6, -10); 286 assertEquals(-3, f.getNumerator()); 287 assertEquals(5, f.getDenominator()); 288 289 f = Fraction.getReducedFraction(-6, -10); 290 assertEquals(3, f.getNumerator()); 291 assertEquals(5, f.getDenominator()); 292 293 try { 295 f = Fraction.getReducedFraction(1, 0); 296 fail("expecting ArithmeticException"); 297 } catch (ArithmeticException ex) {} 298 299 try { 300 f = Fraction.getReducedFraction(2, 0); 301 fail("expecting ArithmeticException"); 302 } catch (ArithmeticException ex) {} 303 304 try { 305 f = Fraction.getReducedFraction(-3, 0); 306 fail("expecting ArithmeticException"); 307 } catch (ArithmeticException ex) {} 308 309 f = Fraction.getReducedFraction(0, 2); 311 assertEquals(0, f.getNumerator()); 312 assertEquals(1, f.getDenominator()); 313 314 f = Fraction.getReducedFraction(2, 2); 315 assertEquals(1, f.getNumerator()); 316 assertEquals(1, f.getDenominator()); 317 318 f = Fraction.getReducedFraction(2, 4); 319 assertEquals(1, f.getNumerator()); 320 assertEquals(2, f.getDenominator()); 321 322 f = Fraction.getReducedFraction(15, 10); 323 assertEquals(3, f.getNumerator()); 324 assertEquals(2, f.getDenominator()); 325 326 f = Fraction.getReducedFraction(121, 22); 327 assertEquals(11, f.getNumerator()); 328 assertEquals(2, f.getDenominator()); 329 330 f = Fraction.getReducedFraction(-2, Integer.MIN_VALUE); 333 assertEquals(1, f.getNumerator()); 334 assertEquals(-(Integer.MIN_VALUE / 2), f.getDenominator()); 335 336 try { 338 f = Fraction.getReducedFraction(-7, Integer.MIN_VALUE); 339 fail("Expecting ArithmeticException"); 340 } catch (ArithmeticException ex) {} 341 } 342 343 public void testFactory_double() { 344 Fraction f = null; 345 346 try { 347 f = Fraction.getFraction(Double.NaN); 348 fail("expecting ArithmeticException"); 349 } catch (ArithmeticException ex) {} 350 351 try { 352 f = Fraction.getFraction(Double.POSITIVE_INFINITY); 353 fail("expecting ArithmeticException"); 354 } catch (ArithmeticException ex) {} 355 356 try { 357 f = Fraction.getFraction(Double.NEGATIVE_INFINITY); 358 fail("expecting ArithmeticException"); 359 } catch (ArithmeticException ex) {} 360 361 try { 362 f = Fraction.getFraction((double) Integer.MAX_VALUE + 1); 363 fail("expecting ArithmeticException"); 364 } catch (ArithmeticException ex) {} 365 366 f = Fraction.getFraction(0.0d); 368 assertEquals(0, f.getNumerator()); 369 assertEquals(1, f.getDenominator()); 370 371 f = Fraction.getFraction(1.0d); 373 assertEquals(1, f.getNumerator()); 374 assertEquals(1, f.getDenominator()); 375 376 f = Fraction.getFraction(0.5d); 378 assertEquals(1, f.getNumerator()); 379 assertEquals(2, f.getDenominator()); 380 381 f = Fraction.getFraction(-0.875d); 383 assertEquals(-7, f.getNumerator()); 384 assertEquals(8, f.getDenominator()); 385 386 f = Fraction.getFraction(1.25d); 388 assertEquals(5, f.getNumerator()); 389 assertEquals(4, f.getDenominator()); 390 391 f = Fraction.getFraction(0.66666d); 393 assertEquals(2, f.getNumerator()); 394 assertEquals(3, f.getDenominator()); 395 396 f = Fraction.getFraction(1.0d/10001d); 398 assertEquals(0, f.getNumerator()); 399 assertEquals(1, f.getDenominator()); 400 401 Fraction f2 = null; 403 int remainder, number1, number2 = 0; 404 for (int i = 1; i <= 100; i++) { for (int j = 1; j <= i; j++) { try { 407 f = Fraction.getFraction((double) j / (double) i); 408 } catch (ArithmeticException ex) { 409 System.err.println(j + " " + i); 410 throw ex; 411 } 412 f2 = Fraction.getReducedFraction(j, i); 413 assertEquals(f2.getNumerator(), f.getNumerator()); 414 assertEquals(f2.getDenominator(), f.getDenominator()); 415 } 416 } 417 for (int i = 1001; i <= 10000; i+=SKIP) { for (int j = 1; j <= i; j++) { try { 421 f = Fraction.getFraction((double) j / (double) i); 422 } catch (ArithmeticException ex) { 423 System.err.println(j + " " + i); 424 throw ex; 425 } 426 f2 = Fraction.getReducedFraction(j, i); 427 assertEquals(f2.getNumerator(), f.getNumerator()); 428 assertEquals(f2.getDenominator(), f.getDenominator()); 429 } 430 } 431 } 432 433 public void testFactory_String() { 434 try { 435 Fraction.getFraction(null); 436 fail("expecting IllegalArgumentException"); 437 } catch (IllegalArgumentException ex) {} 438 } 439 440 441 public void testFactory_String_double() { 442 Fraction f = null; 443 444 f = Fraction.getFraction("0.0"); 445 assertEquals(0, f.getNumerator()); 446 assertEquals(1, f.getDenominator()); 447 448 f = Fraction.getFraction("0.2"); 449 assertEquals(1, f.getNumerator()); 450 assertEquals(5, f.getDenominator()); 451 452 f = Fraction.getFraction("0.5"); 453 assertEquals(1, f.getNumerator()); 454 assertEquals(2, f.getDenominator()); 455 456 f = Fraction.getFraction("0.66666"); 457 assertEquals(2, f.getNumerator()); 458 assertEquals(3, f.getDenominator()); 459 460 try { 461 f = Fraction.getFraction("2.3R"); 462 fail("Expecting NumberFormatException"); 463 } catch (NumberFormatException ex) {} 464 465 try { 466 f = Fraction.getFraction("2147483648"); fail("Expecting NumberFormatException"); 468 } catch (NumberFormatException ex) {} 469 470 try { 471 f = Fraction.getFraction("."); 472 fail("Expecting NumberFormatException"); 473 } catch (NumberFormatException ex) {} 474 } 475 476 public void testFactory_String_proper() { 477 Fraction f = null; 478 479 f = Fraction.getFraction("0 0/1"); 480 assertEquals(0, f.getNumerator()); 481 assertEquals(1, f.getDenominator()); 482 483 f = Fraction.getFraction("1 1/5"); 484 assertEquals(6, f.getNumerator()); 485 assertEquals(5, f.getDenominator()); 486 487 f = Fraction.getFraction("7 1/2"); 488 assertEquals(15, f.getNumerator()); 489 assertEquals(2, f.getDenominator()); 490 491 f = Fraction.getFraction("1 2/4"); 492 assertEquals(6, f.getNumerator()); 493 assertEquals(4, f.getDenominator()); 494 495 f = Fraction.getFraction("-7 1/2"); 496 assertEquals(-15, f.getNumerator()); 497 assertEquals(2, f.getDenominator()); 498 499 f = Fraction.getFraction("-1 2/4"); 500 assertEquals(-6, f.getNumerator()); 501 assertEquals(4, f.getDenominator()); 502 503 try { 504 f = Fraction.getFraction("2 3"); 505 fail("expecting NumberFormatException"); 506 } catch (NumberFormatException ex) {} 507 508 try { 509 f = Fraction.getFraction("a 3"); 510 fail("expecting NumberFormatException"); 511 } catch (NumberFormatException ex) {} 512 513 try { 514 f = Fraction.getFraction("2 b/4"); 515 fail("expecting NumberFormatException"); 516 } catch (NumberFormatException ex) {} 517 518 try { 519 f = Fraction.getFraction("2 "); 520 fail("expecting NumberFormatException"); 521 } catch (NumberFormatException ex) {} 522 523 try { 524 f = Fraction.getFraction(" 3"); 525 fail("expecting NumberFormatException"); 526 } catch (NumberFormatException ex) {} 527 528 try { 529 f = Fraction.getFraction(" "); 530 fail("expecting NumberFormatException"); 531 } catch (NumberFormatException ex) {} 532 } 533 534 public void testFactory_String_improper() { 535 Fraction f = null; 536 537 f = Fraction.getFraction("0/1"); 538 assertEquals(0, f.getNumerator()); 539 assertEquals(1, f.getDenominator()); 540 541 f = Fraction.getFraction("1/5"); 542 assertEquals(1, f.getNumerator()); 543 assertEquals(5, f.getDenominator()); 544 545 f = Fraction.getFraction("1/2"); 546 assertEquals(1, f.getNumerator()); 547 assertEquals(2, f.getDenominator()); 548 549 f = Fraction.getFraction("2/3"); 550 assertEquals(2, f.getNumerator()); 551 assertEquals(3, f.getDenominator()); 552 553 f = Fraction.getFraction("7/3"); 554 assertEquals(7, f.getNumerator()); 555 assertEquals(3, f.getDenominator()); 556 557 f = Fraction.getFraction("2/4"); 558 assertEquals(2, f.getNumerator()); 559 assertEquals(4, f.getDenominator()); 560 561 try { 562 f = Fraction.getFraction("2/d"); 563 fail("expecting NumberFormatException"); 564 } catch (NumberFormatException ex) {} 565 566 try { 567 f = Fraction.getFraction("2e/3"); 568 fail("expecting NumberFormatException"); 569 } catch (NumberFormatException ex) {} 570 571 try { 572 f = Fraction.getFraction("2/"); 573 fail("expecting NumberFormatException"); 574 } catch (NumberFormatException ex) {} 575 576 try { 577 f = Fraction.getFraction("/"); 578 fail("expecting NumberFormatException"); 579 } catch (NumberFormatException ex) {} 580 } 581 582 public void testGets() { 583 Fraction f = null; 584 585 f = Fraction.getFraction(3, 5, 6); 586 assertEquals(23, f.getNumerator()); 587 assertEquals(3, f.getProperWhole()); 588 assertEquals(5, f.getProperNumerator()); 589 assertEquals(6, f.getDenominator()); 590 591 f = Fraction.getFraction(-3, 5, 6); 592 assertEquals(-23, f.getNumerator()); 593 assertEquals(-3, f.getProperWhole()); 594 assertEquals(5, f.getProperNumerator()); 595 assertEquals(6, f.getDenominator()); 596 597 f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1); 598 assertEquals(Integer.MIN_VALUE, f.getNumerator()); 599 assertEquals(Integer.MIN_VALUE, f.getProperWhole()); 600 assertEquals(0, f.getProperNumerator()); 601 assertEquals(1, f.getDenominator()); 602 } 603 604 public void testConversions() { 605 Fraction f = null; 606 607 f = Fraction.getFraction(3, 7, 8); 608 assertEquals(3, f.intValue()); 609 assertEquals(3L, f.longValue()); 610 assertEquals(3.875f, f.floatValue(), 0.00001f); 611 assertEquals(3.875d, f.doubleValue(), 0.00001d); 612 } 613 614 public void testReduce() { 615 Fraction f = null; 616 617 f = Fraction.getFraction(50, 75); 618 f = f.reduce(); 619 assertEquals(2, f.getNumerator()); 620 assertEquals(3, f.getDenominator()); 621 622 f = Fraction.getFraction(2, 3); 623 f = f.reduce(); 624 assertEquals(2, f.getNumerator()); 625 assertEquals(3, f.getDenominator()); 626 } 627 628 public void testInvert() { 629 Fraction f = null; 630 631 f = Fraction.getFraction(50, 75); 632 f = f.invert(); 633 assertEquals(75, f.getNumerator()); 634 assertEquals(50, f.getDenominator()); 635 636 f = Fraction.getFraction(4, 3); 637 f = f.invert(); 638 assertEquals(3, f.getNumerator()); 639 assertEquals(4, f.getDenominator()); 640 641 f = Fraction.getFraction(-15, 47); 642 f = f.invert(); 643 assertEquals(-47, f.getNumerator()); 644 assertEquals(15, f.getDenominator()); 645 646 f = Fraction.getFraction(0, 3); 647 try { 648 f = f.invert(); 649 fail("expecting ArithmeticException"); 650 } catch (ArithmeticException ex) {} 651 652 f = Fraction.getFraction(Integer.MIN_VALUE, 1); 654 try { 655 f = f.invert(); 656 fail("expecting ArithmeticException"); 657 } catch (ArithmeticException ex) {} 658 659 f = Fraction.getFraction(Integer.MAX_VALUE, 1); 660 f = f.invert(); 661 assertEquals(1, f.getNumerator()); 662 assertEquals(Integer.MAX_VALUE, f.getDenominator()); 663 } 664 665 public void testNegate() { 666 Fraction f = null; 667 668 f = Fraction.getFraction(50, 75); 669 f = f.negate(); 670 assertEquals(-50, f.getNumerator()); 671 assertEquals(75, f.getDenominator()); 672 673 f = Fraction.getFraction(-50, 75); 674 f = f.negate(); 675 assertEquals(50, f.getNumerator()); 676 assertEquals(75, f.getDenominator()); 677 678 f = Fraction.getFraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE); 680 f = f.negate(); 681 assertEquals(Integer.MIN_VALUE+2, f.getNumerator()); 682 assertEquals(Integer.MAX_VALUE, f.getDenominator()); 683 684 f = Fraction.getFraction(Integer.MIN_VALUE, 1); 685 try { 686 f = f.negate(); 687 fail("expecting ArithmeticException"); 688 } catch (ArithmeticException ex) {} 689 } 690 691 public void testAbs() { 692 Fraction f = null; 693 694 f = Fraction.getFraction(50, 75); 695 f = f.abs(); 696 assertEquals(50, f.getNumerator()); 697 assertEquals(75, f.getDenominator()); 698 699 f = Fraction.getFraction(-50, 75); 700 f = f.abs(); 701 assertEquals(50, f.getNumerator()); 702 assertEquals(75, f.getDenominator()); 703 704 f = Fraction.getFraction(Integer.MAX_VALUE, 1); 705 f = f.abs(); 706 assertEquals(Integer.MAX_VALUE, f.getNumerator()); 707 assertEquals(1, f.getDenominator()); 708 709 f = Fraction.getFraction(Integer.MAX_VALUE, -1); 710 f = f.abs(); 711 assertEquals(Integer.MAX_VALUE, f.getNumerator()); 712 assertEquals(1, f.getDenominator()); 713 714 f = Fraction.getFraction(Integer.MIN_VALUE, 1); 715 try { 716 f = f.abs(); 717 fail("expecting ArithmeticException"); 718 } catch (ArithmeticException ex) {} 719 } 720 721 public void testPow() { 722 Fraction f = null; 723 724 f = Fraction.getFraction(3, 5); 725 assertEquals(Fraction.ONE, f.pow(0)); 726 727 f = Fraction.getFraction(3, 5); 728 assertSame(f, f.pow(1)); 729 assertEquals(f, f.pow(1)); 730 731 f = Fraction.getFraction(3, 5); 732 f = f.pow(2); 733 assertEquals(9, f.getNumerator()); 734 assertEquals(25, f.getDenominator()); 735 736 f = Fraction.getFraction(3, 5); 737 f = f.pow(3); 738 assertEquals(27, f.getNumerator()); 739 assertEquals(125, f.getDenominator()); 740 741 f = Fraction.getFraction(3, 5); 742 f = f.pow(-1); 743 assertEquals(5, f.getNumerator()); 744 assertEquals(3, f.getDenominator()); 745 746 f = Fraction.getFraction(3, 5); 747 f = f.pow(-2); 748 assertEquals(25, f.getNumerator()); 749 assertEquals(9, f.getDenominator()); 750 751 f = Fraction.getFraction(6, 10); 753 assertEquals(Fraction.ONE, f.pow(0)); 754 755 f = Fraction.getFraction(6, 10); 756 assertEquals(f, f.pow(1)); 757 assertFalse(f.pow(1).equals(Fraction.getFraction(3,5))); 758 759 f = Fraction.getFraction(6, 10); 760 f = f.pow(2); 761 assertEquals(9, f.getNumerator()); 762 assertEquals(25, f.getDenominator()); 763 764 f = Fraction.getFraction(6, 10); 765 f = f.pow(3); 766 assertEquals(27, f.getNumerator()); 767 assertEquals(125, f.getDenominator()); 768 769 f = Fraction.getFraction(6, 10); 770 f = f.pow(-1); 771 assertEquals(10, f.getNumerator()); 772 assertEquals(6, f.getDenominator()); 773 774 f = Fraction.getFraction(6, 10); 775 f = f.pow(-2); 776 assertEquals(25, f.getNumerator()); 777 assertEquals(9, f.getDenominator()); 778 779 f = Fraction.getFraction(0, 1231); 781 f = f.pow(1); 782 assertTrue(0==f.compareTo(Fraction.ZERO)); 783 assertEquals(0, f.getNumerator()); 784 assertEquals(1231, f.getDenominator()); 785 f = f.pow(2); 786 assertTrue(0==f.compareTo(Fraction.ZERO)); 787 assertEquals(0, f.getNumerator()); 788 assertEquals(1, f.getDenominator()); 789 790 try { 792 f = f.pow(-1); 793 fail("expecting ArithmeticException"); 794 } catch (ArithmeticException ex) {} 795 try { 796 f = f.pow(Integer.MIN_VALUE); 797 fail("expecting ArithmeticException"); 798 } catch (ArithmeticException ex) {} 799 800 f = Fraction.getFraction(1, 1); 802 f = f.pow(0); 803 assertEquals(f, Fraction.ONE); 804 f = f.pow(1); 805 assertEquals(f, Fraction.ONE); 806 f = f.pow(-1); 807 assertEquals(f, Fraction.ONE); 808 f = f.pow(Integer.MAX_VALUE); 809 assertEquals(f, Fraction.ONE); 810 f = f.pow(Integer.MIN_VALUE); 811 assertEquals(
|