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