001package hw4.test; 002 003import static org.junit.Assert.*; 004import hw4.*; 005 006import org.junit.Before; 007import org.junit.Test; 008 009/** 010 * This class contains a set of test cases that can be used to test the 011 * implementation of the RatPoly class. 012 * <p> 013 */ 014public final class RatPolyTest { 015 private final double JUNIT_DOUBLE_DELTA = 0.00001; 016 017 /** 018 * checks that Java asserts are enabled, and exits if not 019 */ 020 @Before 021 public void testAssertsEnabled() { 022 SpecificationTests.checkAssertsEnabled(); 023 } 024 025 // get a RatNum for an integer 026 private static RatNum num(int i) { 027 return new RatNum(i); 028 } 029 030 // convenient way to make a RatPoly 031 private RatPoly poly(int coef, int expt) { 032 return new RatPoly(coef, expt); 033 } 034 035 // Convenient way to make a quadratic polynomial, arguments 036 // are just the coefficients, highest degree term to lowest 037 private RatPoly quadPoly(int x2, int x1, int x0) { 038 RatPoly ratPoly = new RatPoly(x2, 2); 039 return ratPoly.add(poly(x1, 1)).add(poly(x0, 0)); 040 } 041 042 // convenience for valueOf 043 private RatPoly valueOf(String s) { 044 return RatPoly.valueOf(s); 045 } 046 047 // convenience for zero RatPoly 048 private RatPoly zero() { 049 return new RatPoly(); 050 } 051 052 // only toString is tested here 053 private void eq(RatPoly p, String target) { 054 String t = p.toString(); 055 assertEquals(target, t); 056 } 057 058 private void eq(RatPoly p, String target, String message) { 059 String t = p.toString(); 060 assertEquals(message, target, t); 061 } 062 063 // parses s into p, and then checks that it is as anticipated 064 // forall i, valueOf(s).coeff(anticipDegree - i) = anticipCoeffForExpts(i) 065 // (anticipDegree - i) means that we expect coeffs to be expressed 066 // corresponding to decreasing expts 067 private void eqP(String s, int anticipDegree, RatNum[] anticipCoeffs) { 068 RatPoly p = valueOf(s); 069 assertEquals(anticipDegree, p.degree()); 070 for (int i = 0; i <= anticipDegree; i++) { 071 assertTrue("wrong coeff; \n" + "anticipated: " + anticipCoeffs[i] 072 + "; received: " + p.getTerm(anticipDegree - i).getCoeff() 073 + "\n" + " received: " + p + " anticipated:" + s, p 074 .getTerm(anticipDegree - i).getCoeff().equals( 075 anticipCoeffs[i])); 076 } 077 } 078 079 // added convenience: express coeffs as ints 080 private void eqP(String s, int anticipDegree, int[] intCoeffs) { 081 RatNum[] coeffs = new RatNum[intCoeffs.length]; 082 for (int i = 0; i < coeffs.length; i++) { 083 coeffs[i] = num(intCoeffs[i]); 084 } 085 eqP(s, anticipDegree, coeffs); 086 } 087 088 // make sure that unparsing a parsed string yields the string itself 089 private void assertToStringWorks(String s) { 090 assertEquals(s, valueOf(s).toString()); 091 } 092 093 RatPoly poly1, neg_poly1, poly2, neg_poly2, poly3, neg_poly3; 094 095 //SetUp Method depends on RatPoly add and Negate 096 //Tests that are intended to verify add or negate should variables declared in this setUp method 097 @Before 098 public void setUp(){ 099 //poly1 = 1*x^1 + 2*x^2 + 3*x^3 + 4*x^4 + 5*x^5 100 poly1 = RatPoly.valueOf("1*x^1+2*x^2+3*x^3+4*x^4+5*x^5"); 101 102 //neg_poly1 = -1*x^1 + -2*x^2 + -3*x^3 + -4*x^4 + -5*x^5 103 neg_poly1 = poly1.negate(); 104 105 //poly2 = 6*x^2 + 7*x^3 + 8*x^4 106 poly2 = RatPoly.valueOf("6*x^2+7*x^3+8*x^4"); 107 108 //neg_poly2 = -6*x^2 + -7*x^3 + -8*x^4 109 neg_poly2 = poly2.negate(); 110 111 // poly3 = 9*x^3 + 10*x^4 112 poly3 = RatPoly.valueOf("9*x^3+10*x^4"); 113 114 // neg_poly3 = -9*x^3 + -10*x^4 115 neg_poly3 = poly3.negate(); 116 } 117 118 /////////////////////////////////////////////////////////////////////////////////////// 119 //// Constructor 120 /////////////////////////////////////////////////////////////////////////////////////// 121 122 @Test 123 public void testNoArgCtor() { 124 eq(new RatPoly(), "0"); 125 } 126 127 @Test 128 public void testTwoArgCtorWithZeroExp() { 129 eq(poly(0, 0), "0"); 130 eq(poly(0, 1), "0"); 131 eq(poly(1, 0), "1"); 132 eq(poly(-1, 0), "-1"); 133 } 134 135 @Test 136 public void testTwoArgCtorWithOneExp() { 137 eq(poly(1, 1), "x"); 138 eq(poly(-1, 1), "-x"); 139 } 140 141 @Test 142 public void testTwoArgCtorWithLargeExp() { 143 eq(poly(1, 2), "x^2"); 144 eq(poly(2, 2), "2*x^2"); 145 eq(poly(2, 3), "2*x^3"); 146 eq(poly(-2, 3), "-2*x^3"); 147 eq(poly(-1, 3), "-x^3"); 148 } 149 150 /////////////////////////////////////////////////////////////////////////////////////// 151 //// isNaN Test 152 /////////////////////////////////////////////////////////////////////////////////////// 153 154 @Test 155 public void testIsNaN() { 156 assertTrue(RatPoly.valueOf("NaN").isNaN()); 157 } 158 159 @Test 160 public void testIsNotNaN() { 161 assertFalse(RatPoly.valueOf("1").isNaN()); 162 assertFalse(RatPoly.valueOf("1/2").isNaN()); 163 assertFalse(RatPoly.valueOf("x+1").isNaN()); 164 assertFalse(RatPoly.valueOf("x^2+x+1").isNaN()); 165 } 166 167 @Test 168 public void testIsNaNEmptyPolynomial() { 169 RatPoly empty = new RatPoly(); 170 assertTrue(empty.div(empty).isNaN()); 171 } 172 173 /////////////////////////////////////////////////////////////////////////////////////// 174 //// Value Of Test 175 /////////////////////////////////////////////////////////////////////////////////////// 176 177 @Test 178 public void testValueOfSimple() { 179 eqP("0", 0, new int[] { 0 }); 180 eqP("x", 1, new int[] { 1, 0 }); 181 eqP("x^2", 2, new int[] { 1, 0, 0 }); 182 } 183 184 @Test 185 public void testValueOfMultTerms() { 186 eqP("x^3+x^2", 3, new int[] { 1, 1, 0, 0 }); 187 eqP("x^3-x^2", 3, new int[] { 1, -1, 0, 0 }); 188 eqP("x^10+x^2", 10, new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 }); 189 } 190 191 @Test 192 public void testValueOfLeadingNeg() { 193 eqP("-x^2", 2, new int[] { -1, 0, 0 }); 194 eqP("-x^2+1", 2, new int[] { -1, 0, 1 }); 195 eqP("-x^2+x", 2, new int[] { -1, 1, 0 }); 196 } 197 198 @Test 199 public void testValueOfLeadingConstants() { 200 eqP("10*x", 1, new int[] { 10, 0 }); 201 eqP("10*x^4+x^2", 4, new int[] { 10, 0, 1, 0, 0 }); 202 eqP("10*x^4+100*x^2", 4, new int[] { 10, 0, 100, 0, 0 }); 203 eqP("-10*x^4+100*x^2", 4, new int[] { -10, 0, 100, 0, 0 }); 204 } 205 206 @Test 207 public void testValueOfRationalsSingleTerms() { 208 eqP("1/2", 0, new RatNum[] { num(1).div(num(2)) }); 209 eqP("1/2*x", 1, new RatNum[] { num(1).div(num(2)), num(0) }); 210 eqP("1/1000", 0, new RatNum[] { num(1).div(num(1000)) }); 211 eqP("1/1000*x", 1, new RatNum[] { num(1).div(num(1000)), num(0) }); 212 } 213 214 @Test 215 public void testValueOfRationalsMultipleTerms() { 216 eqP("x+1/3", 1, new RatNum[] { num(1), num(1).div(num(3)) }); 217 eqP("1/2*x+1/3", 1, new RatNum[] { num(1).div(num(2)), 218 num(1).div(num(3)) }); 219 eqP("1/2*x+3/2", 1, new RatNum[] { num(1).div(num(2)), 220 num(3).div(num(2)) }); 221 eqP("1/2*x^3+3/2", 3, new RatNum[] { num(1).div(num(2)), num(0), 222 num(0), num(3).div(num(2)) }); 223 eqP("1/2*x^3+3/2*x^2+1", 3, new RatNum[] { num(1).div(num(2)), 224 num(3).div(num(2)), num(0), num(1) }); 225 } 226 227 @Test 228 public void testValueOfNaN() { 229 assertTrue(valueOf("NaN").isNaN()); 230 } 231 232 /////////////////////////////////////////////////////////////////////////////////////// 233 //// To String Test 234 /////////////////////////////////////////////////////////////////////////////////////// 235 236 @Test 237 public void testToStringSimple() { 238 assertToStringWorks("0"); 239 assertToStringWorks("x"); 240 assertToStringWorks("x^2"); 241 } 242 243 @Test 244 public void testToStringMultTerms() { 245 assertToStringWorks("x^3+x^2"); 246 assertToStringWorks("x^3-x^2"); 247 assertToStringWorks("x^100+x^2"); 248 } 249 250 @Test 251 public void testToStringLeadingNeg() { 252 assertToStringWorks("-x^2"); 253 assertToStringWorks("-x^2+1"); 254 assertToStringWorks("-x^2+x"); 255 } 256 257 @Test 258 public void testToStringLeadingConstants() { 259 assertToStringWorks("10*x"); 260 assertToStringWorks("10*x^100+x^2"); 261 assertToStringWorks("10*x^100+100*x^2"); 262 assertToStringWorks("-10*x^100+100*x^2"); 263 } 264 265 @Test 266 public void testToStringRationalsSingleElems() { 267 assertToStringWorks("1/2"); 268 assertToStringWorks("1/2*x"); 269 } 270 271 @Test 272 public void testToStringRationalsMultiplElems() { 273 assertToStringWorks("x+1/3"); 274 assertToStringWorks("1/2*x+1/3"); 275 assertToStringWorks("1/2*x+3/2"); 276 assertToStringWorks("1/2*x^10+3/2"); 277 assertToStringWorks("1/2*x^10+3/2*x^2+1"); 278 } 279 280 @Test 281 public void testToStringNaN() { 282 assertToStringWorks("NaN"); 283 } 284 285 /////////////////////////////////////////////////////////////////////////////////////// 286 //// Degree Test 287 /////////////////////////////////////////////////////////////////////////////////////// 288 289 @Test // test degree is zero when it should be 290 public void testDegreeZero() { 291 assertEquals("x^0 degree 0", 0, poly(1, 0).degree()); 292 assertEquals("0*x^100 degree 0", 0, poly(0, 100).degree()); 293 assertEquals("0*x^0 degree 0", 0, poly(0, 0).degree()); 294 } 295 296 @Test 297 public void testDegreeNonZero() { 298 assertEquals("x^1 degree 1", 1, poly(1, 1).degree()); 299 assertEquals("x^100 degree 100", 100, poly(1, 100).degree()); 300 } 301 302 @Test // test degree for multi termed polynomial 303 public void testDegreeNonZeroMultiTerm() { 304 assertEquals(poly1.toString() + " has Correct Degree", 5, poly1.degree()); 305 assertEquals(poly2.toString() + " has Correct Degree", 4, poly2.degree()); 306 } 307 308 /////////////////////////////////////////////////////////////////////////////////////// 309 //// Negate Tests 310 /////////////////////////////////////////////////////////////////////////////////////// 311 312 @Test // test degree is zero when it should be 313 public void testNegateZero() { 314 assertEquals(RatPoly.ZERO, RatPoly.ZERO.negate()); 315 } 316 317 @Test // test degree is zero when it should be 318 public void testNegateNaN() { 319 assertEquals(RatPoly.NaN, RatPoly.NaN.negate()); 320 } 321 322 @Test // test degree is zero when it should be 323 public void testNegatePosToNeg() { 324 assertEquals(RatPoly.valueOf("-x-2*x^2-3*x^3-4*x^4-5*x^5"), poly1.negate()); 325 assertEquals(RatPoly.valueOf("-6*x^2-7*x^3-8*x^4"), poly2.negate()); 326 assertEquals(RatPoly.valueOf("-9*x^3-10*x^4"), poly3.negate()); 327 } 328 329 @Test // test degree is zero when it should be 330 public void testNegatNegToPos() { 331 assertEquals(poly1, RatPoly.valueOf("-x-2*x^2-3*x^3-4*x^4-5*x^5").negate()); 332 assertEquals(poly2, RatPoly.valueOf("-6*x^2-7*x^3-8*x^4").negate()); 333 assertEquals(poly3, RatPoly.valueOf("-9*x^3-10*x^4").negate()); 334 } 335 336 /////////////////////////////////////////////////////////////////////////////////////// 337 //// Addition Test 338 /////////////////////////////////////////////////////////////////////////////////////// 339 340 @Test 341 public void testAddSingleTerm() { 342 eq(poly(1, 0).add(poly(1, 0)), "2"); 343 eq(poly(1, 0).add(poly(5, 0)), "6"); 344 eq(poly(1, 0).add(poly(-1, 0)), "0"); 345 eq(poly(1, 1).add(poly(1, 1)), "2*x"); 346 eq(poly(1, 2).add(poly(1, 2)), "2*x^2"); 347 } 348 349 @Test 350 public void testAddMultipleTerm() { 351 RatPoly _XSqPlus2X = poly(1, 2).add(poly(1, 1)).add(poly(1, 1)); 352 RatPoly _2XSqPlusX = poly(1, 2).add(poly(1, 2)).add(poly(1, 1)); 353 354 eq(_XSqPlus2X, "x^2+2*x"); 355 eq(_2XSqPlusX, "2*x^2+x"); 356 357 eq(poly(1, 2).add(poly(1, 1)), "x^2+x"); 358 eq(poly(1, 3).add(poly(1, 1)), "x^3+x"); 359 } 360 361 // Note Polynomial is annotated as p 362 @Test // p1 + p2 = p3 , p1 != 0 && p2 != 0, p1.degree == p2.degree 363 public void testAddSameDegree(){ 364 RatPoly temp = RatPoly.valueOf("3*x^1+4*x^2+5*x^3+6*x^4+7*x^5"); 365 assertEquals(RatPoly.valueOf("4*x^1+6*x^2+8*x^3+10*x^4+12*x^5"), poly1.add(temp)); 366 367 RatPoly temp2 = RatPoly.valueOf("-1*x^2-2*x^3-3*x^4"); 368 assertEquals(RatPoly.valueOf("-7*x^2-9*x^3-11*x^4"), neg_poly2.add(temp2)); 369 } 370 371 @Test // p1 + p2 = p3 , p1 != 0 && p2 != 0, p1.degree != p2.degree 372 public void testAddDifferentDegree(){ 373 assertEquals(RatPoly.valueOf("1*x^1+8*x^2+10*x^3+12*x^4+5*x^5"), poly1.add(poly2)); 374 assertEquals(RatPoly.valueOf("-6*x^2-16*x^3-18*x^4"), neg_poly2.add(neg_poly3)); 375 } 376 377 @Test // p + p = 2p 378 public void testAddWithItSelf() { 379 assertEquals(RatPoly.valueOf("2*x^1+4*x^2+6*x^3+8*x^4+10*x^5"), poly1.add(poly1)); 380 assertEquals(RatPoly.valueOf("-12*x^2-14*x^3-16*x^4"), neg_poly2.add(neg_poly2)); 381 } 382 383 @Test // Addition Associativity (p1 + p2) + p3 = p1 + (p2 + p3) 384 public void testAddAssociativity() { 385 RatPoly operation1 = (poly1.add(poly2)).add(poly3); 386 RatPoly operation2 = (poly3.add(poly2)).add(poly1); 387 assertEquals(operation1, operation2); 388 389 operation1 = (poly1.add(neg_poly2)).add(neg_poly3); 390 operation2 = (neg_poly3.add(neg_poly2)).add(poly1); 391 assertEquals(operation1, operation2); 392 } 393 394 @Test // Addition Commutative Rule p1 + p2 = p2 + p1 395 public void testAddCommutativity() { 396 assertEquals(poly1.add(neg_poly2), neg_poly2.add(poly1)); 397 assertEquals(neg_poly3.add(poly2), poly2.add(neg_poly3)); 398 } 399 400 @Test // Zero Polynomial + Zero Polynomial == Zero Polynomial 401 public void testAddZeroToZero() { 402 assertEquals(RatPoly.ZERO, RatPoly.ZERO.add(RatPoly.ZERO)); 403 } 404 405 @Test // Additive Identity p + Zero Polynomial == p && Zero Polynomial + p == p 406 public void testAddZeroToNonZero() { 407 assertEquals(poly1, RatPoly.ZERO.add(poly1)); 408 assertEquals(poly1, poly1.add(RatPoly.ZERO)); 409 } 410 411 @Test // Additive Inverse p + (-p) = 0 412 public void testAddInverse() { 413 assertEquals(RatPoly.ZERO, poly1.add(neg_poly1)); 414 assertEquals(RatPoly.ZERO, poly2.add(neg_poly2)); 415 assertEquals(RatPoly.ZERO, poly3.add(neg_poly3)); 416 } 417 418 @Test // NaN + NaN == NaN 419 public void testAddNaNtoNaN() { 420 assertEquals(RatPoly.NaN, RatPoly.NaN.add(RatPoly.NaN)); 421 } 422 423 @Test // t + NaN == NaN 424 public void testAddNaNtoNonNaN() { 425 assertEquals(RatPoly.NaN, RatPoly.NaN.add(poly1)); 426 assertEquals(RatPoly.NaN, poly1.add(RatPoly.NaN)); 427 } 428 429 /////////////////////////////////////////////////////////////////////////////////////// 430 //// Subtraction Test 431 /////////////////////////////////////////////////////////////////////////////////////// 432 433 //Also Tests Addition inverse property 434 435 @Test // p1 - p2 = p3 , p1 != 0 && p2 != 0, p1.degree == p2.degree 436 public void testSubtractSameDegree() { 437 RatPoly temp = RatPoly.valueOf("3*x^1+4*x^2+5*x^3+6*x^4+7*x^5"); 438 assertEquals(RatPoly.valueOf("2*x^1+2*x^2+2*x^3+2*x^4+2*x^5"), temp.sub(poly1)); 439 440 RatPoly temp2 = RatPoly.valueOf("-1*x^2-2*x^3-3*x^4"); 441 assertEquals(RatPoly.valueOf("7*x^2+9*x^3+11*x^4"), poly2.sub(temp2)); 442 } 443 444 @Test // p1 - p2 = p3 , p1 != 0 && p2 != 0, p1.degree != p2.degree 445 public void testSubtractDiffDegree() { 446 assertEquals(RatPoly.valueOf("1*x^1-4*x^2-4*x^3-4*x^4+5*x^5"), poly1.sub(poly2)); 447 assertEquals(RatPoly.valueOf("-6*x^2-16*x^3-18*x^4"), neg_poly2.sub(poly3)); 448 } 449 450 @Test // Zero Polynomial - Zero Polynomial == Zero Polynomial 451 public void testSubtractZeroFromZero() { 452 assertEquals(RatPoly.ZERO, RatPoly.ZERO.sub(RatPoly.ZERO)); 453 } 454 455 //Following test method depends on correctness of negate 456 @Test // p - ZeroPolynomial == t && ZeroPolynomial - p == -p 457 public void testSubtractZeroAndNonZero() { 458 assertEquals(neg_poly1, RatPoly.ZERO.sub(poly1)); 459 assertEquals(poly1, poly1.sub(RatPoly.ZERO)); 460 } 461 462 @Test // NaN - NaN == NaN 463 public void testSubtractNaNtoNaN() { 464 assertEquals(RatPoly.NaN, RatPoly.NaN.sub(RatPoly.NaN)); 465 } 466 467 @Test // p - NaN == NaN && NaN - p == NaN 468 public void testSubtractNaNtoNonNaN() { 469 assertEquals(RatPoly.NaN, RatPoly.NaN.sub(poly1)); 470 assertEquals(RatPoly.NaN, poly1.sub(RatPoly.NaN)); 471 } 472 473 /////////////////////////////////////////////////////////////////////////////////////// 474 //// Remove zero when appropriate test 475 /////////////////////////////////////////////////////////////////////////////////////// 476 477 @Test 478 public void testZeroElim() { 479 // make sure zeros are removed from poly 480 eqP("1+0", 0, new int[] { 1 }); 481 // test zero-elimination from intermediate result of sub 482 eq(quadPoly(1, 1, 1).sub(poly(1, 1)), "x^2+1"); 483 // test internal cancellation of terms in mul. (x+1)*(x-1)=x^2-1 484 eq(poly(1, 1).add(poly(1, 0)).mul(poly(1, 1).sub(poly(1, 0))), "x^2-1"); 485 } 486 487 /////////////////////////////////////////////////////////////////////////////////////// 488 //// Small Value Test 489 /////////////////////////////////////////////////////////////////////////////////////// 490 491 @Test 492 public void testSmallCoeff() { 493 // try to flush out errors when small coefficients are in use. 494 eq(quadPoly(1, 1, 1).sub(poly(999, 1).div(poly(1000, 0))), 495 "x^2+1/1000*x+1"); 496 } 497 498 /////////////////////////////////////////////////////////////////////////////////////// 499 //// Multiplication Test 500 /////////////////////////////////////////////////////////////////////////////////////// 501 502 @Test // p1 + p2 = p3 , p1 != 0 && p2 != 0, p1.degree == p2.degree 503 public void testMultiplicationSameDegree() { 504 eq(poly(0, 0).mul(poly(0, 0)), "0"); 505 eq(poly(1, 0).mul(poly(1, 0)), "1"); 506 eq(poly(1, 0).mul(poly(2, 0)), "2"); 507 eq(poly(2, 0).mul(poly(2, 0)), "4"); 508 RatPoly temp = RatPoly.valueOf("3*x^4+2"); 509 assertEquals(RatPoly.valueOf("30*x^8+27*x^7+20*x^4+18*x^3"), temp.mul(poly3)); 510 } 511 512 @Test // p1 + p2 = p3 , p1 != 0 && p2 != 0, p1.degree != p2.degree 513 public void testMultiplicationDiffDegree() { 514 RatPoly temp = RatPoly.valueOf("3*x^2"); 515 assertEquals(RatPoly.valueOf("18*x^4+21*x^5+24*x^6"), temp.mul(poly2)); 516 assertEquals(RatPoly.valueOf("27*x^5+30*x^6"), temp.mul(poly3)); 517 } 518 519 @Test // Multiplication Associativity 520 public void testMultiplicationAssociativity() { 521 assertEquals(poly1.mul(poly2).mul(poly3), 522 poly3.mul(poly2).mul(poly1)); 523 assertEquals(poly1.mul(neg_poly2).mul(neg_poly3), 524 neg_poly3.mul(neg_poly2).mul(poly1)); 525 } 526 527 @Test // Multiplication Commutative 528 public void testMultiplicationCommutativity() { 529 assertEquals(poly1.mul(poly2), poly2.mul(poly1)); 530 assertEquals(neg_poly3.mul(poly2), poly2.mul(neg_poly3)); 531 } 532 533 @Test // ZeroPolynomial * ZeroPolynomial == ZeroPolynomial 534 public void testMultiplicationZeroToZero() { 535 assertEquals(RatPoly.ZERO, RatPoly.ZERO.mul(RatPoly.ZERO)); 536 } 537 538 @Test // p * ZeroPolynomial == ZeroPolynomial && ZeroPolynomial * p == ZeroPolynomial 539 public void testMultiplicationZeroToNonZero() { 540 assertEquals(RatPoly.ZERO, RatPoly.ZERO.mul(poly2)); 541 assertEquals(RatPoly.ZERO, poly2.mul(RatPoly.ZERO)); 542 } 543 544 @Test // NaN * NaN == NaN 545 public void testMultiplicationNaNtoNaN() { 546 assertEquals(RatPoly.NaN, RatPoly.NaN.mul(RatPoly.NaN)); 547 } 548 549 @Test // p * NaN == NaN 550 public void testMultiplicationNaNtoNonNaN() { 551 assertEquals(RatPoly.NaN, RatPoly.NaN.mul(poly1)); 552 assertEquals(RatPoly.NaN, poly1.mul(RatPoly.NaN)); 553 } 554 555 @Test // p * 1 == p 556 public void testMultiplicationIdentity() { 557 assertEquals(poly2, poly2.mul(RatPoly.valueOf("1"))); 558 assertEquals(neg_poly3, neg_poly3.mul(RatPoly.valueOf("1"))); 559 } 560 561 @Test 562 public void testMulMultiplElem() { 563 eq(poly(1, 1).sub(poly(1, 0)).mul(poly(1, 1).add(poly(1, 0))), "x^2-1"); 564 } 565 566 /////////////////////////////////////////////////////////////////////////////////////// 567 //// Division Test 568 /////////////////////////////////////////////////////////////////////////////////////// 569 570 @Test 571 public void testDivEvaltoSingleCoeff() { 572 // 0/x = 0 573 eq(poly(0, 1).div(poly(1, 1)), "0"); 574 575 // 2/1 = 2 576 eq(poly(2, 0).div(poly(1, 0)), "2"); 577 578 // x/x = 1 579 eq(poly(1, 1).div(poly(1, 1)), "1"); 580 581 // -x/x = -1 582 eq(poly(-1, 1).div(poly(1, 1)), "-1"); 583 584 // x/-x = -1 585 eq(poly(1, 1).div(poly(-1, 1)), "-1"); 586 587 // -x/-x = 1 588 eq(poly(-1, 1).div(poly(-1, 1)), "1"); 589 590 // x^100/x^1000 = 0 591 eq(poly(1, 100).div(poly(1, 1000)), "0"); 592 } 593 594 @Test 595 public void testDivtoSingleTerm() { 596 597 // 5x/5 = x 598 eq(poly(5, 1).div(poly(5, 0)), "x"); 599 600 // -x^2/x = -x 601 eq(poly(-1, 2).div(poly(1, 1)), "-x"); 602 603 // x^100/x = x^99 604 eq(poly(1, 100).div(poly(1, 1)), "x^99"); 605 606 // x^99/x^98 = x 607 eq(poly(1, 99).div(poly(1, 98)), "x"); 608 609 // x^10 / x = x^9 (r: 0) 610 eq(poly(1, 10).div(poly(1, 1)), "x^9"); 611 } 612 613 @Test 614 public void testDivtoMultipleTerms() { 615 // x^10 / x^3+x^2 = x^7-x^6+x^5-x^4+x^3-x^2+x-1 (r: -x^2) 616 eq(poly(1, 10).div(poly(1, 3).add(poly(1, 2))), 617 "x^7-x^6+x^5-x^4+x^3-x^2+x-1"); 618 619 // x^10 / x^3+x^2+x = x^7-x^6+x^4-x^3+x-1 (r: -x) 620 eq(poly(1, 10).div(poly(1, 3).add(poly(1, 2).add(poly(1, 1)))), 621 "x^7-x^6+x^4-x^3+x-1"); 622 623 // 5x^2+5x/5 = x^2+x 624 eq(poly(5, 2).add(poly(5, 1)).div(poly(5, 0)), "x^2+x"); 625 626 // x^10+x^5 / x = x^9+x^4 (r: 0) 627 eq(poly(1, 10).add(poly(1, 5)).div(poly(1, 1)), "x^9+x^4"); 628 629 // x^10+x^5 / x^3 = x^7+x^2 (r: 0) 630 eq(poly(1, 10).add(poly(1, 5)).div(poly(1, 3)), "x^7+x^2"); 631 632 // x^10+x^5 / x^3+x+3 = x^7-x^5-3*x^4+x^3+7*x^2+8*x-10 633 // (with remainder: 29*x^2+14*x-30) 634 eq(poly(1, 10).add(poly(1, 5)).div( 635 poly(1, 3).add(poly(1, 1)).add(poly(3, 0))), 636 "x^7-x^5-3*x^4+x^3+7*x^2+8*x-10"); 637 } 638 639 @Test 640 public void testDivComplexI() { 641 // (x+1)*(x+1) = x^2+2*x+1 642 eq(poly(1, 2).add(poly(2, 1)).add(poly(1, 0)).div( 643 poly(1, 1).add(poly(1, 0))), "x+1"); 644 645 // (x-1)*(x+1) = x^2-1 646 eq(poly(1, 2).add(poly(-1, 0)).div(poly(1, 1).add(poly(1, 0))), "x-1"); 647 } 648 649 @Test 650 public void testDivComplexII() { 651 // x^8+2*x^6+8*x^5+2*x^4+17*x^3+11*x^2+8*x+3 = 652 // (x^3+2*x+1) * (x^5+7*x^2+2*x+3) 653 RatPoly large = poly(1, 8).add(poly(2, 6)).add(poly(8, 5)).add( 654 poly(2, 4)).add(poly(17, 3)).add(poly(11, 2)).add(poly(8, 1)) 655 .add(poly(3, 0)); 656 657 // x^3+2*x+1 658 RatPoly sub1 = poly(1, 3).add(poly(2, 1)).add(poly(1, 0)); 659 // x^5+7*x^2+2*x+3 660 RatPoly sub2 = poly(1, 5).add(poly(7, 2)).add(poly(2, 1)).add( 661 poly(3, 0)); 662 663 // just a last minute typo check... 664 eq(sub1.mul(sub2), large.toString()); 665 eq(sub2.mul(sub1), large.toString()); 666 667 eq(large.div(sub2), "x^3+2*x+1"); 668 eq(large.div(sub1), "x^5+7*x^2+2*x+3"); 669 } 670 671 @Test 672 public void testDivExamplesFromSpec() { 673 // seperated this test case out because it has a dependency on 674 // both "valueOf" and "div" functioning properly 675 676 // example 1 from spec 677 eq(valueOf("x^3-2*x+3").div(valueOf("3*x^2")), "1/3*x"); 678 // example 2 from spec 679 eq(valueOf("x^2+2*x+15").div(valueOf("2*x^3")), "0"); 680 } 681 682 @Test 683 public void testDivExampleFromPset() { 684 eq(valueOf("x^8+x^6+10*x^4+10*x^3+8*x^2+2*x+8").div( 685 valueOf("3*x^6+5*x^4+9*x^2+4*x+8")), "1/3*x^2-2/9"); 686 } 687 688 @Test 689 public void testBigDiv() { 690 // don't "fix" the "infinite loop" in div by simply stopping after 691 // 50 terms! 692 eq( 693 valueOf("x^102").div(valueOf("x+1")), 694 "x^101-x^100+x^99-x^98+x^97-x^96+x^95-x^94+x^93-x^92+x^91-x^90+" 695 + "x^89-x^88+x^87-x^86+x^85-x^84+x^83-x^82+x^81-x^80+x^79-x^78+" 696 + "x^77-x^76+x^75-x^74+x^73-x^72+x^71-x^70+x^69-x^68+x^67-x^66+" 697 + "x^65-x^64+x^63-x^62+x^61-x^60+x^59-x^58+x^57-x^56+x^55-x^54+" 698 + "x^53-x^52+x^51-x^50+x^49-x^48+x^47-x^46+x^45-x^44+x^43-x^42+" 699 + "x^41-x^40+x^39-x^38+x^37-x^36+x^35-x^34+x^33-x^32+x^31-x^30+" 700 + "x^29-x^28+x^27-x^26+x^25-x^24+x^23-x^22+x^21-x^20+x^19-x^18+" 701 + "x^17-x^16+x^15-x^14+x^13-x^12+x^11-x^10+x^9-x^8+x^7-x^6+x^5-" 702 + "x^4+x^3-x^2+x-1"); 703 } 704 705 @Test // p / 0 = NaN 706 public void testDivByZero() { 707 assertEquals(RatPoly.NaN, poly2.div(RatPoly.ZERO)); 708 assertEquals(RatPoly.NaN, neg_poly1.div(RatPoly.ZERO)); 709 assertEquals(RatPoly.NaN, poly1.div(RatPoly.ZERO)); 710 } 711 712 @Test // Zero Polynomial / Zero Polynomial == NaN 713 public void testDivisionZeroFromZero() { 714 assertEquals(RatPoly.NaN, RatPoly.ZERO.div(RatPoly.ZERO)); 715 } 716 717 //Following test method depends on correctness of negate 718 @Test // p / Zero Polynomial == NaN && Zero Polynomial / p == 0 719 public void testDivisionZeroAndNonZero() { 720 assertEquals(RatPoly.ZERO, RatPoly.ZERO.div(poly1)); 721 } 722 723 @Test // NaN / NaN == NaN 724 public void testDivisionNaNtoNaN() { 725 assertEquals(RatPoly.NaN, RatPoly.NaN.div(RatPoly.NaN)); 726 } 727 728 @Test // p / NaN == NaN && NaN / p == NaN 729 public void testDivisionNaNtoNonNaN() { 730 assertEquals(RatPoly.NaN, RatPoly.NaN.div(poly1)); 731 assertEquals(RatPoly.NaN, poly1.div(RatPoly.NaN)); 732 } 733 734 @Test // p / 1 == p 735 public void testDivisionByOne() { 736 assertEquals(poly2, poly2.div(RatPoly.valueOf("1"))); 737 } 738 739 /////////////////////////////////////////////////////////////////////////////////////// 740 //// Immutable Test 741 /////////////////////////////////////////////////////////////////////////////////////// 742 743 @Test 744 public void testImmutabilityOfOperations() { 745 // not the most thorough test possible, but hopefully will 746 // catch the easy cases early on... 747 RatPoly one = poly(1, 0); 748 RatPoly two = poly(2, 0); 749 RatPoly empty = new RatPoly(); 750 751 one.degree(); 752 two.degree(); 753 eq(one, "1", "Degree mutates receiver!"); 754 eq(two, "2", "Degree mutates receiver!"); 755 756 one.getTerm(0); 757 two.getTerm(0); 758 eq(one, "1", "Coeff mutates receiver!"); 759 eq(two, "2", "Coeff mutates receiver!"); 760 761 one.isNaN(); 762 two.isNaN(); 763 eq(one, "1", "isNaN mutates receiver!"); 764 eq(two, "2", "isNaN mutates receiver!"); 765 766 one.eval(0.0); 767 two.eval(0.0); 768 eq(one, "1", "eval mutates receiver!"); 769 eq(two, "2", "eval mutates receiver!"); 770 771 one.negate(); 772 two.negate(); 773 eq(one, "1", "Negate mutates receiver!"); 774 eq(two, "2", "Negate mutates receiver!"); 775 776 one.add(two); 777 eq(one, "1", "Add mutates receiver!"); 778 eq(two, "2", "Add mutates argument!"); 779 780 one.sub(two); 781 eq(one, "1", "Sub mutates receiver!"); 782 eq(two, "2", "Sub mutates argument!"); 783 784 one.mul(two); 785 eq(one, "1", "Mul mutates receiver!"); 786 eq(two, "2", "Mul mutates argument!"); 787 788 one.div(two); 789 eq(one, "1", "Div mutates receiver!"); 790 eq(two, "2", "Div mutates argument!"); 791 792 empty.div(new RatPoly()); 793 assertFalse("Div Mutates reciever", empty.isNaN()); 794 } 795 796 /////////////////////////////////////////////////////////////////////////////////////// 797 //// Eval Test 798 /////////////////////////////////////////////////////////////////////////////////////// 799 800 @Test 801 public void testEvalZero() { 802 RatPoly zero = new RatPoly(); 803 assertEquals(" 0 at 0 ", 0.0, zero.eval(0.0), JUNIT_DOUBLE_DELTA); 804 assertEquals(" 0 at 1 ", 0.0, zero.eval(1.0), JUNIT_DOUBLE_DELTA); 805 assertEquals(" 0 at 2 ", 0.0, zero.eval(2.0), JUNIT_DOUBLE_DELTA); 806 } 807 808 @Test 809 public void testEvalOne() { 810 RatPoly one = new RatPoly(1, 0); 811 812 assertEquals(" 1 at 0 ", 1.0, one.eval(0.0), JUNIT_DOUBLE_DELTA); 813 assertEquals(" 1 at 1 ", 1.0, one.eval(1.0), JUNIT_DOUBLE_DELTA); 814 assertEquals(" 1 at 1 ", 1.0, one.eval(2.0), JUNIT_DOUBLE_DELTA); 815 } 816 817 @Test 818 public void testEvalX() { 819 RatPoly _X = new RatPoly(1, 1); 820 821 assertEquals(" x at 0 ", 0.0, _X.eval(0.0), JUNIT_DOUBLE_DELTA); 822 assertEquals(" x at 1 ", 1.0, _X.eval(1.0), JUNIT_DOUBLE_DELTA); 823 assertEquals(" x at 2 ", 2.0, _X.eval(2.0), JUNIT_DOUBLE_DELTA); 824 } 825 826 @Test 827 public void testEval2X() { 828 RatPoly _2X = new RatPoly(2, 1); 829 830 assertEquals(" 2*x at 0 ", 0.0, _2X.eval(0.0), JUNIT_DOUBLE_DELTA); 831 assertEquals(" 2*x at 1 ", 2.0, _2X.eval(1.0), JUNIT_DOUBLE_DELTA); 832 assertEquals(" 2*x at 2 ", 4.0, _2X.eval(2.0), JUNIT_DOUBLE_DELTA); 833 } 834 835 @Test 836 public void testEvalXsq() { 837 RatPoly _XSq = new RatPoly(1, 2); 838 assertEquals(" x^2 at 0 ", 0.0, _XSq.eval(0.0), JUNIT_DOUBLE_DELTA); 839 assertEquals(" x^2 at 1 ", 1.0, _XSq.eval(1.0), JUNIT_DOUBLE_DELTA); 840 assertEquals(" x^2 at 2 ", 4.0, _XSq.eval(2.0), JUNIT_DOUBLE_DELTA); 841 } 842 843 @Test 844 public void testEvalXSq_minus_2X() { 845 RatPoly _2X = new RatPoly(2, 1); 846 RatPoly _XSq = new RatPoly(1, 2); 847 RatPoly _XSq_minus_2X = _XSq.sub(_2X); 848 849 assertEquals(" x^2-2*x at 0 ", 0.0, _XSq_minus_2X.eval(0.0), JUNIT_DOUBLE_DELTA); 850 assertEquals(" x^2-2*x at 1 ", -1.0, _XSq_minus_2X.eval(1.0), JUNIT_DOUBLE_DELTA); 851 assertEquals(" x^2-2*x at 2 ", 0.0, _XSq_minus_2X.eval(2.0), JUNIT_DOUBLE_DELTA); 852 assertEquals(" x^2-2*x at 3 ", 3.0, _XSq_minus_2X.eval(3.0), JUNIT_DOUBLE_DELTA); 853 } 854 855 /////////////////////////////////////////////////////////////////////////////////////// 856 //// Get Term Test 857 /////////////////////////////////////////////////////////////////////////////////////// 858 859 @Test 860 public void testGetTerm() { 861 // getTerm already gets some grunt testing in eqP; checking an 862 // interesting 863 // input here... 864 RatPoly _XSqPlus2X = poly(1, 2).add(poly(1, 1)).add(poly(1, 1)); 865 RatPoly _2XSqPlusX = poly(1, 2).add(poly(1, 2)).add(poly(1, 1)); 866 867 assertEquals(RatTerm.ZERO, _XSqPlus2X.getTerm(-1)); 868 assertEquals(RatTerm.ZERO, _XSqPlus2X.getTerm(-10)); 869 assertEquals(RatTerm.ZERO, _2XSqPlusX.getTerm(-1)); 870 assertEquals(RatTerm.ZERO, _2XSqPlusX.getTerm(-10)); 871 assertEquals(RatTerm.ZERO, zero().getTerm(-10)); 872 assertEquals(RatTerm.ZERO, zero().getTerm(-1)); 873 } 874 875 876 private void assertIsNaNanswer(RatPoly nanAnswer) { 877 eq(nanAnswer, "NaN"); 878 } 879 880 /////////////////////////////////////////////////////////////////////////////////////// 881 //// Differentiate Test 882 /////////////////////////////////////////////////////////////////////////////////////// 883 884 // (NaN)' = NaN 885 @Test 886 public void testDifferentiateNaN(){ 887 assertEquals(RatPoly.NaN, RatPoly.NaN.differentiate()); 888 } 889 890 // (RatPoly.ZERO)' = RatPoly.ZERO 891 @Test 892 public void testDifferentiateZero(){ 893 assertEquals(RatPoly.ZERO, RatPoly.ZERO.differentiate()); 894 } 895 896 // constant a => (a)' = 0 897 @Test 898 public void testDifferentiateConstantNonZero(){ 899 assertEquals(RatPoly.ZERO, RatPoly.valueOf("1").differentiate()); 900 assertEquals(RatPoly.ZERO, RatPoly.valueOf("999").differentiate()); 901 } 902 903 @Test //f(x) = x => f' = 1 904 public void testDifferentiatetoOne() { 905 eq(RatPoly.valueOf("x").differentiate(), "1"); 906 } 907 908 // Constant Multiple Rule (af)' = af' 909 @Test 910 public void testDifferentiateMultiplicationRule(){ 911 RatPoly a_constant = RatPoly.valueOf("2"); 912 assertEquals(a_constant.mul(poly1.differentiate()), 913 (a_constant.mul(poly1)).differentiate()); 914 assertEquals(a_constant.mul(neg_poly2.differentiate()), 915 (a_constant.mul(neg_poly2)).differentiate()); 916 } 917 918 // Polynomial Power Rule (ax^b) = (a*b)*x^(b-1) 919 @Test 920 public void testDifferentiatePowerRule(){ 921 assertEquals(RatPoly.valueOf("1+4*x+9*x^2+16*x^3+25*x^4"), poly1.differentiate()); 922 assertEquals(RatPoly.valueOf("12*x+21*x^2+32*x^3"), poly2.differentiate()); 923 } 924 925 // Sum rule (f + g)' = f' + g' 926 @Test 927 public void testDifferentiateSumRule(){ 928 assertEquals(((poly2).add(neg_poly3)).differentiate(), 929 (poly2.differentiate()).add(neg_poly3.differentiate())); 930 assertEquals(((poly1).add(poly3)).differentiate(), 931 (poly1.differentiate()).add(poly3.differentiate())); 932 } 933 934 // Subtraction rule (f - g)' = f' - g' 935 @Test 936 public void testDifferentiateSubtractionRule(){ 937 assertEquals(((poly2).sub(neg_poly3)).differentiate(), 938 (poly2.differentiate()).sub(neg_poly3.differentiate())); 939 assertEquals(((poly1).sub(poly3)).differentiate(), 940 (poly1.differentiate()).sub(poly3.differentiate())); 941 } 942 943 // Product Rule h(x) = f(x)*g(x) => h'(x) = f'(x)g(x) + f(x)g'(x) 944 @Test 945 public void testDifferentiateProductRule(){ 946 // Whole Number Coefficient 947 RatPoly init_product = poly1.mul(poly2); 948 RatPoly deriv_pt1 = (poly1.differentiate()).mul(poly2); 949 RatPoly deriv_pt2 = poly1.mul(poly2.differentiate()); 950 951 assertEquals(init_product.differentiate() , deriv_pt1.add(deriv_pt2)); 952 953 // Fractional Number Coefficient 954 init_product = neg_poly2.mul(poly3); 955 deriv_pt1 = (neg_poly2.differentiate()).mul(poly3); 956 deriv_pt2 = neg_poly2.mul(poly3.differentiate()); 957 958 assertEquals(init_product.differentiate() , deriv_pt1.add(deriv_pt2)); 959 } 960 961 @Test 962 public void testDifferentiatetoMultipleTerms() { 963 eq(quadPoly(7, 5, 99).differentiate(), "14*x+5"); 964 eq(quadPoly(3, 2, 1).differentiate(), "6*x+2"); 965 eq(quadPoly(1, 0, 1).differentiate(), "2*x"); 966 } 967 968 /////////////////////////////////////////////////////////////////////////////////////// 969 //// Anti Differentiate Test 970 /////////////////////////////////////////////////////////////////////////////////////// 971 //As stated in specification for any term b is assumed >= 0 and Integration Constant is Zero 972 //Note : AntiDerivative of f(x) = F(x) + c , f = F 973 //Note : c = Integration Constant 974 975 @Test //AntiDifferentiate Basic functionality 976 public void testAntiDifferentiate() { 977 eq(poly(1, 0).antiDifferentiate(new RatNum(1)), "x+1"); 978 eq(poly(2, 1).antiDifferentiate(new RatNum(1)), "x^2+1"); 979 } 980 981 @Test 982 public void testAntiDifferentiateWithQuadPoly() { 983 eq(quadPoly(0, 6, 2).antiDifferentiate(new RatNum(1)), "3*x^2+2*x+1"); 984 eq(quadPoly(4, 6, 2).antiDifferentiate(new RatNum(0)), 985 "4/3*x^3+3*x^2+2*x"); 986 } 987 988 @Test // Constant Rule with zero f(x) = 0 => F = c 989 public void testAntiDifferentiateFromZero() { 990 // Zero 991 assertEquals(RatPoly.ZERO, RatPoly.ZERO.antiDifferentiate(RatNum.ZERO)); 992 // Zero with integration constant 5 993 assertEquals(RatPoly.valueOf("5"), RatPoly.ZERO.antiDifferentiate(RatNum.valueOf("5"))); 994 } 995 996 // Constant Rule f(x) = c => F = c*x 997 @Test 998 public void testAntiDifferentiateConstantRule() { 999 // Zero Integration Constant 1000 assertEquals(RatPoly.valueOf("5*x"), RatPoly.valueOf("5").antiDifferentiate(RatNum.ZERO)); 1001 1002 // Non Zero Integration Constant 1003 assertEquals(RatPoly.valueOf("5*x+10"), RatPoly.valueOf("5").antiDifferentiate(RatNum.valueOf("10"))); 1004 } 1005 1006 // Constant Multiple Rule f(x) = c*g(x) => F = c*G(x) 1007 @Test 1008 public void testAntiDifferentiateConstantMultipleRule() { 1009 RatPoly a_constant = RatPoly.valueOf("7"); 1010 RatPoly b_constant = RatPoly.valueOf("13"); 1011 RatNum i_constant = RatNum.valueOf("11"); 1012 1013 assertEquals(((a_constant).mul(poly1)).antiDifferentiate(i_constant), 1014 a_constant.mul(poly1.antiDifferentiate(RatNum.ZERO)).add(new RatPoly(new RatTerm(i_constant , 0)))); 1015 1016 assertEquals(((b_constant).mul(poly3)).antiDifferentiate(RatNum.ZERO), 1017 b_constant.mul(poly3.antiDifferentiate(RatNum.ZERO))); 1018 } 1019 1020 // Power Rule f(x) = x^a => F = (x^(a+1))/(a+1) 1021 @Test 1022 public void testAntiDifferentiatePowerRule() { 1023 assertEquals(RatPoly.valueOf("9/4*x^4+2*x^5"), poly3.antiDifferentiate(RatNum.ZERO)); 1024 assertEquals(RatPoly.valueOf("2*x^3+7/4*x^4+8/5*x^5+1"), poly2.antiDifferentiate(RatNum.valueOf("1"))); 1025 } 1026 1027 // Sum Rule if h(x) = f(x) + g(x) => H(x) = F(x) + G(x) 1028 @Test 1029 public void testAntiDifferentiateSumRule() { 1030 assertEquals((poly1.add(poly2)).antiDifferentiate(RatNum.ZERO), 1031 poly1.antiDifferentiate(RatNum.ZERO).add(poly2.antiDifferentiate(RatNum.ZERO))); 1032 1033 assertEquals((neg_poly3.add(neg_poly1)).antiDifferentiate(RatNum.valueOf("3")), 1034 neg_poly3.antiDifferentiate(RatNum.ZERO).add(neg_poly1.antiDifferentiate(RatNum.ZERO)) 1035 .add(new RatPoly(new RatTerm(RatNum.valueOf("3") , 0)))); 1036 } 1037 1038 // Difference Rule if h(x) = f(x) - g(x) => H(x) = F(x) - G(x) 1039 @Test 1040 public void testAntiDifferentiateDifferenceRule() { 1041 assertEquals((poly1.sub(poly2)).antiDifferentiate(RatNum.ZERO), 1042 poly1.antiDifferentiate(RatNum.ZERO).sub(poly2.antiDifferentiate(RatNum.ZERO))); 1043 1044 assertEquals((neg_poly3.sub(neg_poly1)).antiDifferentiate(RatNum.valueOf("3")), 1045 neg_poly3.antiDifferentiate(RatNum.ZERO).sub(neg_poly1.antiDifferentiate(RatNum.ZERO)) 1046 .add(new RatPoly(new RatTerm(RatNum.valueOf("3") , 0)))); 1047 } 1048 1049 1050 1051 @Test 1052 public void testAntiDifferentiateWithNaN() { 1053 assertIsNaNanswer(RatPoly.valueOf("NaN").antiDifferentiate( 1054 new RatNum(1))); 1055 assertIsNaNanswer(poly(1, 0).antiDifferentiate(new RatNum(1, 0))); 1056 } 1057 1058 /////////////////////////////////////////////////////////////////////////////////////// 1059 //// Integrate Test 1060 /////////////////////////////////////////////////////////////////////////////////////// 1061 1062 @Test 1063 public void testIntegrateEqualBounds() { 1064 assertEquals( 0.0 , poly3.integrate(1, 1), JUNIT_DOUBLE_DELTA); 1065 assertEquals( 0.0 , poly1.integrate(0,0), JUNIT_DOUBLE_DELTA); 1066 } 1067 1068 @Test 1069 public void testIntegrateBoundsDiffBy1() { 1070 assertEquals( 17.0 / 4.0 , poly3.integrate(0, 1), JUNIT_DOUBLE_DELTA); 1071 assertEquals( 71.0 / 20.0 , poly1.integrate(0,1), JUNIT_DOUBLE_DELTA); 1072 } 1073 1074 @Test 1075 public void testIntegrateLowBoundGreaterThanHigh() { 1076 assertEquals( -19375.0 / 4.0 , poly3.integrate(0, -5), JUNIT_DOUBLE_DELTA); 1077 assertEquals( -5683.0 / 60.0 , poly1.integrate(2,1), JUNIT_DOUBLE_DELTA); 1078 } 1079 1080 @Test 1081 public void testIntegrateLargeBoundDiff() { 1082 assertEquals( 20225000000.0, poly3.integrate(0, 100), JUNIT_DOUBLE_DELTA); 1083 assertEquals( 841409005000.0 , poly1.integrate(0,100), JUNIT_DOUBLE_DELTA); 1084 } 1085 1086 @Test 1087 public void testIntegrateZero() { 1088 assertEquals("Integrate f(x) = 0 from 0 to 10", 0.0, RatPoly.ZERO.integrate(0, 10), JUNIT_DOUBLE_DELTA); 1089 } 1090 1091 @Test 1092 public void testIntegrateOne() { 1093 assertEquals("Integrate f(x) = 1 from 0 to 10", 10.0, RatPoly.valueOf("1").integrate(0, 10), JUNIT_DOUBLE_DELTA); 1094 } 1095 1096 @Test 1097 public void testIntegrateNaN() { 1098 assertEquals("NaN", RatPoly.valueOf("NaN").integrate(0, 1), Double.NaN, JUNIT_DOUBLE_DELTA); 1099 } 1100}