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