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