001package hw4.test; 002 003import static org.hamcrest.CoreMatchers.not; 004import static org.junit.Assert.*; 005 006import org.junit.Before; 007 008import hw4.RatNum; 009import hw4.RatTerm; 010 011import org.junit.Test; 012 013/** 014 * This class contains a set of test cases that can be used to test the 015 * implementation of the RatPoly class. 016 */ 017public final class RatTermTest { 018 019 /** 020 * checks that Java asserts are enabled, and exits if not 021 */ 022 @Before 023 public void testAssertsEnabled() { 024 SpecificationTests.checkAssertsEnabled(); 025 } 026 027 // Get a RatNum for in an integer 028 private static RatNum num(int i) { 029 return new RatNum(i); 030 } 031 032 private static RatNum num(int i, int j) { 033 return new RatNum(i, j); 034 } 035 036 private static final RatNum nanNum = (num(1)).div(num(0)); 037 private static final RatTerm nanTerm = new RatTerm(nanNum, 3); 038 private static final RatTerm zeroTerm = RatTerm.ZERO; 039 private static final RatTerm one = new RatTerm(new RatNum(1),0); 040 041 // Convenient way to make a RatTerm equals to coeff*x^expt. 042 private static RatTerm term(int coeff, int expt) { 043 return new RatTerm(num(coeff), expt); 044 } 045 046 // Convenient way to make a RatTerm equals to num/denom*x^expt. 047 private static RatTerm term(int numer, int denom, int expt) { 048 return new RatTerm(num(numer, denom), expt); 049 } 050 051 /////////////////////////////////////////////////////////////////////////////////////// 052 //// Constructor 053 /////////////////////////////////////////////////////////////////////////////////////// 054 055 // Is it good style to include these constructor tests? Explicit constructor 056 // tests are often not needed. Generally we advise you to have at 057 // least one assertion in every test unless testing that an exception IS 058 // thrown (via the "expected" test parameter). The constructor tests below 059 // violate this rule, but we chose to include them in this assignment in 060 // case they help you isolate constructor errors as you write your code. 061 @Test 062 public void testCtor() { 063 assertEquals(RatTerm.valueOf("1"), term(1, 0)); 064 assertEquals(RatTerm.valueOf("2*x^3"), term(2, 3)); 065 assertEquals(RatTerm.valueOf("4/3*x^6"), term(4, 3, 6)); 066 assertEquals(RatTerm.valueOf("-2/7*x^3"), term(-2, 7, 3)); 067 } 068 069 @Test 070 public void testCtorZeroCoeff() { 071 assertEquals(RatTerm.ZERO, term(0, 0)); 072 assertEquals(RatTerm.ZERO, term(0, 1)); 073 } 074 075 @Test 076 public void testCtorNaN() { 077 assertEquals(RatTerm.NaN, term(3, 0, 0)); 078 } 079 080 /////////////////////////////////////////////////////////////////////////////////////// 081 //// Get Coefficient 082 /////////////////////////////////////////////////////////////////////////////////////// 083 084 @Test 085 public void testGetCoeffRegular() { 086 // Simple cases 087 assertEquals(num(3), term(3, 1).getCoeff()); 088 assertEquals(num(2, 5), term(2, 5, 2).getCoeff()); 089 } 090 091 @Test 092 public void testGetCoeffZero() { 093 // Check zero 094 assertEquals(num(0), term(0, 0).getCoeff()); 095 } 096 097 @Test 098 public void testGetCoeffNegative() { 099 // Check negative coeff 100 assertEquals(num(-2, 3), term(-2, 3, 2).getCoeff()); 101 } 102 103 @Test 104 public void testGetCoeffNaN() { 105 // Check NaN 106 assertEquals(nanNum, term(3, 0, 4).getCoeff()); 107 } 108 109 /////////////////////////////////////////////////////////////////////////////////////// 110 //// Get Coefficient 111 /////////////////////////////////////////////////////////////////////////////////////// 112 113 @Test 114 public void testGetExptRegular() { 115 // Simple 116 assertEquals(4, term(2, 4).getExpt()); 117 } 118 119 @Test 120 public void testGetExptZero() { 121 // Zero always have zero expt 122 assertEquals(0, term(0, 0).getExpt()); 123 assertEquals(0, term(0, 1).getExpt()); 124 } 125 126 /////////////////////////////////////////////////////////////////////////////////////// 127 //// isNaN Test 128 /////////////////////////////////////////////////////////////////////////////////////// 129 130 @Test 131 public void testIsNaNZeroDenomRegNumer() { 132 // Check that 5/0*x^0 isNaN instead of zero 133 assertTrue(term(5, 0, 0).isNaN()); 134 } 135 136 @Test 137 public void testIsNaNZeroDenomZeroNumer() { 138 // Check that 0/0*x^4 isNaN instead of zero 139 assertTrue(term(0, 0, 4).isNaN()); 140 } 141 142 @Test 143 public void testIsNaNFalse() { 144 // Check for false positives 145 assertFalse(term(2, 3, 2).isNaN()); 146 } 147 148 /////////////////////////////////////////////////////////////////////////////////////// 149 //// isZero Test 150 /////////////////////////////////////////////////////////////////////////////////////// 151 152 @Test 153 public void testIsZeroNumAndDen() { 154 assertTrue(term(0, 0).isZero()); 155 } 156 157 @Test 158 public void testIsZeroNum() { 159 assertTrue(term(0, 1).isZero()); 160 assertTrue(term(0, 4, 3).isZero()); 161 assertTrue(term(0, -2, 2).isZero()); 162 } 163 164 @Test 165 public void testIsZeroFalsoPos() { 166 // Check for false positives 167 assertFalse(term(1, 3, 0).isZero()); 168 } 169 170 @Test 171 public void testIsNaNNotTerm() { 172 // Check that 0/0*x^4 is not zero 173 assertFalse(term(0, 0, 4).isZero()); 174 } 175 176 /////////////////////////////////////////////////////////////////////////////////////// 177 //// eval Test 178 /////////////////////////////////////////////////////////////////////////////////////// 179 180 @Test 181 public void testEvalZero() { 182 assertEquals(0.0, term(0, 0).eval(5.0), 0.0000001); 183 assertEquals(0.0, term(0, 5).eval(1.2), 0.0000001); 184 } 185 186 @Test 187 public void testEvalSmallFrac() { 188 assertEquals(.125, term(1, 2, 2).eval(.5), 0.0000001); 189 } 190 191 @Test 192 public void testEvalSmallWhole() { 193 assertEquals(2.0, term(2, 0).eval(3.1), 0.0000001); 194 assertEquals(1.0, term(1, 0).eval(100.0), 0.0000001); 195 assertEquals(1.0, term(-1, 1).eval(-1.0), 0.0000001); 196 assertEquals(2.0, term(1, 2, 2).eval(2.0), 0.0000001); 197 } 198 199 @Test 200 public void testEvalLarge() { 201 assertEquals(35.0, term(5, 1).eval(7.0), 0.0000001); 202 assertEquals(12.0, term(3, 2).eval(2.0), 0.0000001); 203 } 204 205 @Test 206 public void testEvalNegative() { 207 assertEquals(-16.0, term(-2, 3).eval(2.0), 0.0000001); 208 assertEquals(-3.0, term(3, 3).eval(-1.0), 0.0000001); 209 } 210 211 @Test 212 public void testEvalNaN() { 213 // To understand the use of "new Double(Double.NaN)" instead of 214 // "Double.NaN", see the Javadoc for Double.equals(). 215 assertEquals(new Double(Double.NaN), 216 new Double(term(3, 0, 2).eval(1.0))); 217 } 218 219 /////////////////////////////////////////////////////////////////////////////////////// 220 //// Equals Test 221 /////////////////////////////////////////////////////////////////////////////////////// 222 223 @Test 224 public void testEquals() { 225 assertEquals(term(3, 5), term(3, 5)); 226 assertEquals(term(1, 2, 4), term(1, 2, 4)); 227 assertEquals(term(-2, 4, 2), term(1, -2, 2)); 228 } 229 @Test 230 public void testNotEqualsReg() { 231 assertThat(term(4, 6), not(term(7, 8))); 232 } 233 234 @Test 235 public void testEqualsZeroCoeff() { 236 assertEquals(term(0, 0), term(0, 0)); 237 assertEquals(term(0, 1), term(0, 0)); 238 } 239 240 @Test 241 public void testEqualsNotZeroCoeff() { 242 assertThat(term(0, 0), not(term(3, 5))); 243 } 244 245 @Test 246 public void testEqualsNaNCoeff() { 247 assertEquals(nanTerm, term(19, 0, 0)); 248 assertEquals(nanTerm, term(0, 0, 0)); 249 } 250 251 @Test 252 public void testEqualsNotNaNCoeff() { 253 assertThat(nanTerm, not(term(3, 5))); 254 assertThat(term(0, 3), not(nanTerm)); 255 } 256 257 /////////////////////////////////////////////////////////////////////////////////////// 258 //// ValueOf Test 259 /////////////////////////////////////////////////////////////////////////////////////// 260 261 // All tests below depend on constructor and equals. 262 263 private void testValueOf(String actual, RatTerm target) { 264 assertEquals(target, RatTerm.valueOf(actual)); 265 } 266 267 @Test 268 public void testValueOfSimple() { 269 testValueOf("x", term(1, 1)); 270 testValueOf("-x", term(-1, 1)); 271 } 272 273 @Test 274 public void testValueOfConst() { 275 testValueOf("2", term(2, 0)); 276 testValueOf("3/4", term(3, 4, 0)); 277 testValueOf("-4", term(-4, 0)); 278 testValueOf("-7/5", term(-7, 5, 0)); 279 } 280 281 @Test 282 public void testValueOfLeadingCoeff() { 283 testValueOf("2*x", term(2, 1)); 284 testValueOf("3/7*x", term(3, 7, 1)); 285 testValueOf("-4/3*x", term(-4, 3, 1)); 286 } 287 288 @Test 289 public void testValueOfPow() { 290 testValueOf("x^3", term(1, 3)); 291 testValueOf("-x^4", term(-1, 4)); 292 } 293 294 @Test 295 public void testValueOfFull() { 296 testValueOf("4*x^2", term(4, 2)); 297 testValueOf("2/5*x^6", term(2, 5, 6)); 298 testValueOf("-3/2*x^2", term(-3, 2, 2)); 299 } 300 301 @Test 302 public void testValueOfNaN() { 303 testValueOf("NaN", term(1, 0, 0)); 304 } 305 306 @Test 307 public void testValueOfZero() { 308 testValueOf("0", term(0, 0)); 309 } 310 311 /////////////////////////////////////////////////////////////////////////////////////// 312 //// toString Test 313 /////////////////////////////////////////////////////////////////////////////////////// 314 315 private void testToString(String target, RatTerm actual) { 316 assertEquals(target, actual.toString()); 317 } 318 319 @Test 320 public void testToStringSimple() { 321 testToString("x", term(1, 1)); 322 testToString("-x", term(-1, 1)); 323 } 324 325 @Test 326 public void testToStringConst() { 327 testToString("2", term(2, 0)); 328 testToString("3/4", term(3, 4, 0)); 329 testToString("-4", term(-4, 0)); 330 testToString("-7/5", term(-7, 5, 0)); 331 } 332 333 @Test 334 public void testToStringLeadingCoeff() { 335 testToString("2*x", term(2, 1)); 336 testToString("3/7*x", term(3, 7, 1)); 337 testToString("-4/3*x", term(-4, 3, 1)); 338 } 339 340 @Test 341 public void testToStringPow() { 342 testToString("x^3", term(1, 3)); 343 testToString("-x^4", term(-1, 4)); 344 } 345 346 @Test 347 public void testToStringFull() { 348 testToString("4*x^2", term(4, 2)); 349 testToString("2/5*x^6", term(2, 5, 6)); 350 testToString("-3/2*x^2", term(-3, 2, 2)); 351 } 352 353 @Test 354 public void testToStringNaN() { 355 testToString("NaN", term(1, 0, 0)); 356 } 357 358 @Test 359 public void testToStringZero() { 360 testToString("0", term(0, 0)); 361 } 362 363 /////////////////////////////////////////////////////////////////////////////////////// 364 //// Add Test 365 /////////////////////////////////////////////////////////////////////////////////////// 366 //Relies on RatTerm equals method and RatNum add method is correct 367 368 @Test // Addition with Whole Number Coefficient 369 public void testAddWholeNumCoeff() { 370 assertEquals(term(3, 0), term(1, 0).add(term(2, 0))); //Zero Exponent Value 371 assertEquals(term(4, 2), term(3, 2).add(term(1, 2))); //Non Zero Exponent 372 } 373 374 @Test // Addition with Fractional Number Coefficient 375 public void testAddFracNumCoeff() { 376 assertEquals(term(1, 2, 3), term(1, 6, 3).add(term(1, 3, 3))); 377 assertEquals(term(1, 8, 1), term(1, 4, 1).add(term(-1, 8, 1))); 378 assertEquals(term(-1, 8, 1), term(-1, 4, 1).add(term(1, 8, 1))); 379 } 380 381 @Test // Addition Associativity 382 public void testAddAssociativity() { 383 //Whole Number Coefficient 384 assertEquals(term(6, 0), term(1, 0).add(term(2, 0)).add(term(3,0))); 385 assertEquals(term(6, 0), term(3, 0).add(term(2, 0)).add(term(1,0))); 386 387 //Fractional Number Coefficient 388 assertEquals(term(7, 8, 3), term(1, 8, 3).add(term(1, 4, 3)).add(term(1, 2, 3))); 389 assertEquals(term(7, 8, 3), term(1, 2, 3).add(term(1, 4, 3)).add(term(1, 8, 3))); 390 } 391 392 @Test // Addition Commutative 393 public void testAddCommutativity() { 394 assertEquals(term(1, 2, 3), term(1, 3, 3).add(term(1, 6, 3))); 395 assertEquals(term(3, 0), term(2, 0).add(term(1, 0))); 396 } 397 398 @Test // Zero Term + Zero Term == Zero Term 399 public void testAddZeroToZero() { 400 assertEquals(zeroTerm, zeroTerm.add(zeroTerm)); 401 } 402 403 @Test // t + Zero Term == t && Zero Term + t == t 404 public void testAddZeroToNonZero() { 405 RatTerm t = term(-2, 3); 406 assertEquals(t, zeroTerm.add(t)); 407 assertEquals(t, t.add(zeroTerm)); 408 } 409 410 @Test // NaN + NaN == NaN 411 public void testAddNaNtoNaN() { 412 assertEquals(nanTerm, nanTerm.add(nanTerm)); 413 } 414 415 @Test // t + NaN == NaN 416 public void testAddNaNtoNonNaN() { 417 assertEquals(nanTerm, nanTerm.add(term(3, 4))); 418 assertEquals(nanTerm, term(3, 4).add(nanTerm)); 419 } 420 421 /////////////////////////////////////////////////////////////////////////////////////// 422 //// Subtract Test 423 /////////////////////////////////////////////////////////////////////////////////////// 424 425 //Also Tests Addition inverse property 426 427 @Test // Subtraction with Whole Number Coefficient 428 public void testSubtractWholeNumCoeff() { 429 assertEquals(term(1, 0), term(2, 0).sub(term(1, 0))); 430 assertEquals(term(-1, 0), term(1, 0).sub(term(2, 0))); 431 assertEquals(term(2, 2), term(3, 2).sub(term(1, 2))); 432 } 433 434 @Test // Subtraction with Fractional Number Coefficient 435 public void testSubtractFractionalNumCoeff() { 436 assertEquals(term(-1, 6, 3), term(1, 6, 3).sub(term(1, 3, 3))); 437 assertEquals(term(3, 8, 1), term(1, 4, 1).sub(term(-1, 8, 1))); 438 assertEquals(term(-3, 8, 1), term(-1, 4, 1).sub(term(1, 8, 1))); 439 } 440 441 @Test // Zero Term - Zero Term == Zero Term 442 public void testSubtractZeroFromZero() { 443 assertEquals(zeroTerm, zeroTerm.sub(zeroTerm)); 444 } 445 446 //Following test method depends on correctness of negate 447 @Test // t - Zero Term == t && Zero Term - t == -t 448 public void testSubtractZeroAndNonZero() { 449 RatTerm t = term(-2, 3); 450 assertEquals(t.negate(), zeroTerm.sub(t)); 451 assertEquals(t, t.sub(zeroTerm)); 452 } 453 454 @Test // NaN - NaN == NaN 455 public void testSubtractNaNtoNaN() { 456 assertEquals(nanTerm, nanTerm.sub(nanTerm)); 457 } 458 459 @Test // t - NaN == NaN && NaN - t == NaN 460 public void testSubtractNaNtoNonNaN() { 461 assertEquals(nanTerm, nanTerm.sub(term(3, 4))); 462 assertEquals(nanTerm, term(3, 4).sub(nanTerm)); 463 } 464 465 /////////////////////////////////////////////////////////////////////////////////////// 466 //// Multiplication Test 467 /////////////////////////////////////////////////////////////////////////////////////// 468 469 @Test // Multiplication with Whole Number Coefficient 470 public void testMultiplicationWholeNumCoeff() { 471 assertEquals(term(2, 0), term(1, 0).mul(term(2, 0))); //Zero Exponent Value 472 assertEquals(term(30, 4), term(3, 2).mul(term(10, 2))); //Non Zero Exponent 473 } 474 475 @Test // Multiplication with Fractional Number Coefficient 476 public void testMultiplicationFracNumCoeff() { 477 assertEquals(term(1, 18, 6), term(1, 6, 3).mul(term(1, 3, 3))); 478 assertEquals(term(-1, 32, 2), term(1, 4, 1).mul(term(-1, 8, 1))); 479 } 480 481 @Test // Multiplication with different Exponent Values 482 public void testMultiplicationDifferentExpVal() { 483 assertEquals(term(3*2, 6*3, 9+3), term(3, 6, 9).mul(term(2, 3, 3))); 484 assertEquals(term(1*-1, 8*4, -1+1), term(1, 4, -1).mul(term(-1, 8, 1))); 485 assertEquals(term(-1*1, 4*8, 100+10), term(-1, 4, 100).mul(term(1, 8, 10))); 486 } 487 488 @Test // Multiplication Associativity 489 public void testMultiplicationAssociativity() { 490 //Whole Number Coefficient 491 assertEquals(term(12,5).mul(term(11, 4)).mul(term(10, 3)), 492 term(10, 3).mul(term(11, 4)).mul(term(12,5))); 493 494 //Fractional Number Coefficient 495 assertEquals(term(4, 9, 3).mul(term(2, 9, 2)).mul(term(1, 3, 1)), 496 term(1, 3, 1).mul(term(2, 9, 2)).mul(term(4, 9, 3))); 497 } 498 499 @Test // Multiplication Commutative 500 public void testMultiplicationCommutativity() { 501 //Whole Number Coefficient 502 assertEquals(term(7, 1).mul(term(3, 9)), term(3, 9).mul(term(7, 1))); 503 504 //Fractional Number Coefficient 505 assertEquals(term(1, 6, 3).mul(term(1, 3, 3)), term(1, 3, 3).mul(term(1, 6, 3))); 506 } 507 508 @Test // Zero Term * Zero Term == Zero Term 509 public void testMultiplicationZeroToZero() { 510 assertEquals(zeroTerm, zeroTerm.mul(zeroTerm)); 511 } 512 513 @Test // t * Zero Term == Zero Term && Zero Term * t == Zero Term 514 public void testMultiplicationZeroToNonZero() { 515 RatTerm t = term(-2, 3); 516 assertEquals(zeroTerm, zeroTerm.mul(t)); 517 assertEquals(zeroTerm, t.mul(zeroTerm)); 518 } 519 520 @Test // NaN * NaN == NaN 521 public void testMultiplicationNaNtoNaN() { 522 assertEquals(nanTerm, nanTerm.mul(nanTerm)); 523 } 524 525 @Test // t * NaN == NaN 526 public void testMultiplicationNaNtoNonNaN() { 527 assertEquals(nanTerm, nanTerm.mul(term(3, 4))); 528 assertEquals(nanTerm, term(3, 4).mul(nanTerm)); 529 } 530 531 @Test // a * 1 == a 532 public void testMultiplicationIdentity() { 533 assertEquals(term(4, 3), term(4, 3).mul(one)); 534 } 535 536 /////////////////////////////////////////////////////////////////////////////////////// 537 //// Division Test 538 /////////////////////////////////////////////////////////////////////////////////////// 539 540 @Test // Division with Whole Number Coefficient 541 public void testDivisionWholeNumCoeff() { 542 assertEquals(term(3, 0), term(3, 2).div(term(1, 2))); 543 assertEquals(term(2, 1), term(2, 1).div(term(1, 0))); 544 assertEquals(term(8, 3), term(-16, 5).div(term(-2, 2))); 545 } 546 547 @Test // Division with Fractional Number Coefficient 548 public void testDivisionFractionalNumCoeff() { 549 assertEquals(term(1, 2, 0), term(1, 0).div(term(2, 0))); 550 assertEquals(term(1, 2, 0), term(1, 6, 3).div(term(1, 3, 3))); 551 assertEquals(term(-2, 0), term(1, 4, 1).div(term(-1, 8, 1))); 552 } 553 554 @Test // Zero Term / Zero Term == NaN 555 public void testDivisionZeroFromZero() { 556 assertEquals(nanTerm, zeroTerm.div(zeroTerm)); 557 } 558 559 //Following test method depends on correctness of negate 560 @Test // t / Zero Term == NaN && Zero Term / t == 0 561 public void testDivisionZeroAndNonZero() { 562 RatTerm t = term(-2, 3); 563 assertEquals(zeroTerm, zeroTerm.div(t)); 564 assertEquals(nanTerm, t.div(zeroTerm)); 565 } 566 567 @Test // NaN / NaN == NaN 568 public void testDivisionNaNtoNaN() { 569 assertEquals(nanTerm, nanTerm.div(nanTerm)); 570 } 571 572 @Test // t / NaN == NaN && NaN / t == NaN 573 public void testDivisionNaNtoNonNaN() { 574 assertEquals(nanTerm, nanTerm.div(term(3, 4))); 575 assertEquals(nanTerm, term(3, 4).div(nanTerm)); 576 } 577 578 @Test // a / 1 == a 579 public void testDivisionByOne() { 580 assertEquals(term(4, 3), term(4, 3).div(one)); 581 } 582 583 /////////////////////////////////////////////////////////////////////////////////////// 584 //// Differentiation Test 585 /////////////////////////////////////////////////////////////////////////////////////// 586 //As stated in specification for any term b is assumed >= 0 587 //Note : Derivative of f = f' 588 589 // (NaN)' = NaN 590 @Test 591 public void testDifferentiateNaN(){ 592 assertEquals(nanTerm, nanTerm.differentiate()); 593 assertEquals(nanTerm, (new RatTerm(RatNum.NaN, 0)).differentiate()); 594 } 595 596 // (RatTerm.ZERO)' = RatTerm.ZERO 597 @Test 598 public void testDifferentiateZero(){ 599 assertEquals(term(0, 0), term(0, 0).differentiate()); 600 } 601 602 // constant a => (a)' = 0 603 @Test 604 public void testDifferentiateConstantNonZero(){ 605 assertEquals(term(0, 0), term(99, 0).differentiate()); 606 assertEquals(term(0, 0), term(1, 0).differentiate()); 607 } 608 609 // Constant Multiple Rule (af)' = af' 610 @Test 611 public void testDifferentiateMultiplicationRule(){ 612 // Whole Number Coefficient 613 assertEquals(term(5, 0).mul(term(1,3).differentiate()), 614 (term(5, 0).mul(term(1,3))).differentiate()); 615 616 // Fractional Number Coefficient 617 assertEquals(term(2,3,0).mul(term(1,4,3).differentiate()), 618 (term(2,3,0).mul(term(1,4,3))).differentiate()); 619 } 620 621 // Polynomial Power Rule (ax^b) = (a*b)*x^(b-1) 622 @Test 623 public void testDifferentiatePowerRule(){ 624 assertEquals(term(1, 0), term(1, 1).differentiate()); 625 assertEquals(term(5, 0), term(5, 1).differentiate()); 626 assertEquals(term(14, 1), term(7, 2).differentiate()); 627 assertEquals(term(-2, 3), term(-1, 2, 4).differentiate()); 628 } 629 630 // Sum rule (f + g)' = f' + g' 631 @Test 632 public void testDifferentiateSumRule(){ 633 // Whole Number Coefficient 634 assertEquals(((term(5, 2)).add(term(1,2))).differentiate(), 635 (term(5, 2).differentiate()).add(term(1,2).differentiate())); 636 637 // Fractional Number Coefficient 638 assertEquals(((term(5, 2, 7)).add(term(1,3, 7))).differentiate(), 639 (term(5, 2, 7).differentiate()).add(term(1,3, 7).differentiate())); 640 } 641 642 // Subtraction rule (f - g)' = f' - g' 643 @Test 644 public void testDifferentiateSubtractionRule(){ 645 // Whole Number Coefficient 646 assertEquals(((term(5, 2)).sub(term(1,2))).differentiate(), 647 (term(5, 2).differentiate()).sub(term(1,2).differentiate())); 648 649 // Fractional Number Coefficient 650 assertEquals(((term(5, 2, 7)).sub(term(1,3, 7))).differentiate(), 651 (term(5, 2, 7).differentiate()).sub(term(1,3, 7).differentiate())); 652 } 653 654 // Product Rule h(x) = f(x)*g(x) => h'(x) = f'(x)g(x) + f(x)g'(x) 655 @Test 656 public void testDifferentiateProductRule(){ 657 // Whole Number Coefficient 658 RatTerm init_product = term(12, 4).mul(term(5,4)); 659 RatTerm deriv_pt1 = (term(12, 4).differentiate()).mul(term(5,4)); 660 RatTerm deriv_pt2 = term(12, 4).mul(term(5,4).differentiate()); 661 662 assertEquals(init_product.differentiate() , deriv_pt1.add(deriv_pt2)); 663 664 // Fractional Number Coefficient 665 init_product = term(1,2, 4).mul(term(2,3,4)); 666 deriv_pt1 = (term(1,2, 4).differentiate()).mul(term(2,3,4)); 667 deriv_pt2 = term(1,2, 4).mul(term(2,3,4).differentiate()); 668 669 assertEquals(init_product.differentiate() , deriv_pt1.add(deriv_pt2)); 670 } 671 672 /////////////////////////////////////////////////////////////////////////////////////// 673 //// AntiDifferentiation Test 674 /////////////////////////////////////////////////////////////////////////////////////// 675 //As stated in specification for any term b is assumed >= 0 and Integration Constant is Zero 676 //Note : AntiDerivative of f(x) = F(x) , f = F 677 //Note : c = Arbitrary Constant 678 679 // Constant Rule f(x) = c => F = c*x 680 @Test 681 public void testAntiDifferentiateConstantRule() { 682 // Zero 683 assertEquals(zeroTerm, zeroTerm.antiDifferentiate()); 684 685 // Non Zero constant 686 assertEquals(term(3,1), term(3,0).antiDifferentiate()); 687 } 688 689 // Constant Multiple Rule f(x) = c*g(x) => F = c*G(x) 690 @Test 691 public void testAntiDifferentiateConstantMultipleRule() { 692 // Whole Number Coefficient 693 assertEquals(((term(5, 0)).mul(term(1,2))).antiDifferentiate(), 694 term(5, 0).mul(term(1,2).antiDifferentiate())); 695 696 // Fraction Coefficient 697 assertEquals(((term(1,500, 0)).mul(term(1,2,3))).antiDifferentiate(), 698 term(1,500, 0).mul(term(1,2,3).antiDifferentiate())); 699 } 700 701 // Power Rule f(x) = x^a => F = (x^(a+1))/(a+1) 702 @Test 703 public void testAntiDifferentiatePowerRule() { 704 assertEquals(term(1, 2), term(2, 1).antiDifferentiate()); 705 assertEquals(term(4, 3, 3), term(4, 2).antiDifferentiate()); 706 } 707 708 // Sum Rule if h(x) = f(x) + g(x) => H(x) = F(x) + G(x) 709 @Test 710 public void testAntiDifferentiateSumRule() { 711 // Whole Number Coefficient 712 assertEquals((term(1, 2).add(term(3,2))).antiDifferentiate(), 713 term(1, 2).antiDifferentiate().add(term(3,2).antiDifferentiate())); 714 715 // Fraction Coefficient 716 assertEquals((term(3,4,5).add(term(1,2,5))).antiDifferentiate(), 717 term(3,4,5).antiDifferentiate().add(term(1,2,5).antiDifferentiate())); 718 } 719 720 // Difference Rule if h(x) = f(x) - g(x) => H(x) = F(x) - G(x) 721 @Test 722 public void testAntiDifferentiateDifferenceRule() { 723 // Whole Number Coefficient 724 assertEquals((term(1, 2).sub(term(3,2))).antiDifferentiate(), 725 term(1, 2).antiDifferentiate().sub(term(3,2).antiDifferentiate())); 726 727 // Fraction Coefficient 728 assertEquals((term(3,4,5).sub(term(1,2,5))).antiDifferentiate(), 729 term(3,4,5).antiDifferentiate().sub(term(1,2,5).antiDifferentiate())); 730 } 731 732 /////////////////////////////////////////////////////////////////////////////////////// 733 //// HashCode Test 734 /////////////////////////////////////////////////////////////////////////////////////// 735 736 // Same Object same hashcode after multiple queries 737 @Test 738 public void testHashCodeSameObject() { 739 RatTerm t = term(23,45); 740 for (int i = 0; i < 25 ; i++){ //Verify Non random Hascode 741 assertEquals(t.hashCode(), t.hashCode()); 742 } 743 } 744 745 // Equals Objects same hashcode 746 @Test 747 public void testHashCodeEqualDiffObjects() { 748 RatTerm t = term(23,45); 749 RatTerm t2 = term(23,45); 750 assertEquals(t.hashCode(), t2.hashCode()); 751 } 752 753 // Non Equal Object different hashcode 754 @Test 755 public void testHashCodeNonEqualObjects() { 756 RatTerm t = term(1,2); 757 RatTerm t2 = term(3,2); 758 RatTerm t3 = term(1,3); 759 assertFalse(t.hashCode() == t2.hashCode()); 760 assertFalse(t.hashCode() == t3.hashCode()); 761 } 762}