import org.junit.Test; import static org.junit.Assert.assertEquals; /** * The test class PolynomialTest, to test the Polynomial class * * @author Phill Conrad, and (insert your name here) * @version CS56, Winter 2012, lecture 01.31 * @see Polynomial */ public class PolynomialTest { // define a static final (a constant) for error tolerance // we'll pass this as the last value of every assertEquals() // call that is done on double values to allow for roundoff error public static final double TOL = 0.00001; /** test no arg constructor from PolynomialTest @see Polynomial#Polynomial() */ @Test public void testNoArgConstructor() { // default polynomial has degree 0, and value 0 Polynomial p = new Polynomial(); assertEquals(0,p.getDegree()); assertEquals(0.0,p.get(0),TOL); // use auto unboxing } /** test constructor that initializes from int array @see Polynomial#Polynomial(int [] coeffs) */ @Test public void testConstructorIntArray() { // the no arg constructor should give us zero for // both imaginary and real parts // int [] coeffs = {2,1,5}; Polynomial p = new Polynomial(new int[] {2,1,5}); assertEquals(2, p.getDegree()); assertEquals(2.0, p.get(2), TOL); assertEquals(1.0, p.get(1), TOL); assertEquals(5.0, p.get(0), TOL); } /** test constructor that inits from double array @see Polynomial#Polynomial(double [] coeffs) */ @Test public void testConstructorDoubleArray() { // the no arg constructor should give us zero for // both imaginary and real parts double [] coeffs = {2.0, 1.0, 5.0}; Polynomial p = new Polynomial(coeffs); assertEquals(2, p.getDegree()); assertEquals(2.0, p.get(2), TOL); assertEquals(1.0, p.get(1), TOL); assertEquals(5.0, p.get(0), TOL); } /** test toString @see Polynomial#toString */ @Test public void testToString1() { Polynomial p1 = new Polynomial(new int[] {0}); assertEquals("0",p1.toString()); } /** test toString @see Polynomial#toString */ @Test public void testToString2() { Polynomial p2 = new Polynomial(new int[] {1}); assertEquals("1",p2.toString()); } /** test toString @see Polynomial#toString */ @Test public void testToString3() { Polynomial p3 = new Polynomial(new int[] {2,-3}); assertEquals("2x - 3",p3.toString()); } /** test toString @see Polynomial#toString */ @Test public void testToString4() { Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); assertEquals("x^2 - 5x + 6",p4.toString()); } /** test toString @see Polynomial#toString */ @Test public void testToString5() { Polynomial p5 = new Polynomial(new int[] {7, -8, -9, -10, -11}); assertEquals("7x^4 - 8x^3 - 9x^2 - 10x - 11",p5.toString()); } /** test toString @see Polynomial#toString */ @Test public void testToString6() { Polynomial p6 = new Polynomial(new int[] {-1, 0, -2, 0, 3, 0, -4, 0}); assertEquals("-x^7 - 2x^5 + 3x^3 - 4x",p6.toString()); } /** test toString @see Polynomial#toString */ @Test public void testToString7() { Polynomial p7 = new Polynomial (new int[] {-2,0,0,0,0,0,0,0,0,0,1,0 }); assertEquals("-2x^11 + x",p7.toString()); } /** test equals method @see Polynomial#equals */ @Test public void testEquals4() { Polynomial p4a = new Polynomial(new int[] {1, -5, 6}); Polynomial p4b = new Polynomial(new int[] {1, -5, 6}); assertEquals(true,p4a.equals(p4b)); } /** test equals method @see Polynomial#equals */ @Test public void testEquals7() { Polynomial p7a = new Polynomial (new int[] {-2,0,0,0,0,0,0,0,0,0,1,0 }); Polynomial p7b = new Polynomial (new int[] {-2,0,0,0,0,0,0,0,0,0,1,0 }); assertEquals(true,p7a.equals(p7b)); } /** test equals method (should return false when null passed) @see Polynomial#equals */ @Test public void testEqualsNull() { Polynomial p7a = new Polynomial (new int[] {-2,0,0,0,0,0,0,0,0,0,1,0 }); assertEquals(false,p7a.equals(null)); } /** test equals method (should return false when wrong type passed) @see Polynomial#equals */ @Test public void testEqualsWrongType() { Polynomial p7a = new Polynomial (new int[] {-2,0,0,0,0,0,0,0,0,0,1,0 }); assertEquals(false,p7a.equals("A string")); } /** test equals method (should return false when degrees don't match) @see Polynomial#equals */ @Test public void testEqualsWrongDegree() { Polynomial p1 = new Polynomial(new int[] {1,1,1}); Polynomial p2 = new Polynomial(new int[] {1,1}); assertEquals(false,p1.equals(p2)); } /** test plus @see Polynomial#plus */ @Test public void testp1plusp2() { Polynomial p1 = new Polynomial(new int[] {0}); Polynomial p2 = new Polynomial(new int[] {1}); assertEquals(new Polynomial( new int [] {1}),p1.plus(p2)); } /** test plus @see Polynomial#plus */ @Test public void testp2plusp3() { Polynomial p2 = new Polynomial(new int[] {1}); Polynomial p3 = new Polynomial(new int[] {2,-3}); assertEquals(new Polynomial( new int [] {2,-2}),p2.plus(p3)); } /** test plus @see Polynomial#plus */ @Test public void testp3plusp4() { Polynomial p3 = new Polynomial(new int[] {2,-3}); Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); assertEquals(new Polynomial( new int [] {1,-3,3}),p3.plus(p4)); } /** test plus @see Polynomial#plus */ @Test public void testp4plusp5() { Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); Polynomial p5 = new Polynomial(new int[] {7, -8, -9, -10, -11}); assertEquals(new Polynomial( new int [] {7, -8, -8, -15, -5}),p4.plus(p5)); } /** test plus @see Polynomial#plus */ @Test public void testp5plusp6() { Polynomial p5 = new Polynomial(new int[] { 7, -8, -9, -10, -11}); Polynomial p6 = new Polynomial(new int[] {-1, 0, -2, 0, 3, 0, -4, 0}); assertEquals(new Polynomial( new int [] {-1, 0, -2, 7, -5, -9, -14, -11}),p5.plus(p6)); } /** test times @see Polynomial#times */ @Test public void testp1timesp2() { Polynomial p1 = new Polynomial(new int[] {0}); Polynomial p2 = new Polynomial(new int[] {1}); assertEquals(new Polynomial( new int [] {0}),p1.times(p2)); } /** test times @see Polynomial#times */ @Test public void testp2timesp3() { Polynomial p2 = new Polynomial(new int[] {1}); Polynomial p3 = new Polynomial(new int[] {2,-3}); assertEquals(new Polynomial( new int [] {2,-3}),p2.times(p3)); } /** test times @see Polynomial#times */ @Test public void testp3timesp4() { Polynomial p3 = new Polynomial(new int[] {2,-3}); Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); assertEquals(new Polynomial( new int [] {2,-13,27,-18}),p3.times(p4)); } /** test times @see Polynomial#times */ @Test public void testp4timesp5() { Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); Polynomial p5 = new Polynomial(new int[] {7, -8, -9, -10, -11}); assertEquals(new Polynomial( new int [] {7, -43, 73, -13, -15, -5, -66}),p4.times(p5)); } /** test times @see Polynomial#times */ @Test public void testp5timesp6() { // NOTE: Last two coeffs of expected answer updated from 0,44 to 44,0 // 02/02/2012 2:28pm Polynomial p5 = new Polynomial(new int[] { 7, -8, -9, -10, -11}); Polynomial p6 = new Polynomial(new int[] {-1, 0, -2, 0, 3, 0, -4, 0}); assertEquals(new Polynomial( new int [] {-7, 8,-5,26,50,-4,-33,2,3,40,44,0}),p5.times(p6)); } /** test minus @see Polynomial#minus */ @Test public void testp1minusp2() { Polynomial p1 = new Polynomial(new int[] {0}); Polynomial p2 = new Polynomial(new int[] {1}); assertEquals(new Polynomial( new int [] {-1}),p1.minus(p2)); } /** test minus @see Polynomial#minus */ @Test public void testp2minusp3() { Polynomial p2 = new Polynomial(new int[] {1}); Polynomial p3 = new Polynomial(new int[] {2,-3}); assertEquals(new Polynomial( new int [] {-2,4}),p2.minus(p3)); } /** test minus @see Polynomial#minus */ @Test public void testp3minusp4() { Polynomial p3 = new Polynomial(new int[] {2,-3}); Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); assertEquals(new Polynomial( new int [] {-1,7,-9}),p3.minus(p4)); } /** test minus @see Polynomial#minus */ @Test public void testp4minusp5() { // FIRST COEFF UPDATED IN ANSWER FROM -4 to -7 // 02/02/2012 2:29pm // RESULT CHANGED FROM + to , 3:15pm THU Feb 2nd Polynomial p4 = new Polynomial(new int[] {1, -5, 6}); Polynomial p5 = new Polynomial(new int[] {7, -8, -9, -10, -11}); assertEquals(new Polynomial( new int [] {-7 , 8 , 10 , 5 , 17}),p4.minus(p5)); } /** test minus @see Polynomial#minus */ @Test public void testp5minusp6() { Polynomial p5 = new Polynomial(new int[] { 7, -8, -9, -10, -11}); Polynomial p6 = new Polynomial(new int[] {-1, 0, -2, 0, 3, 0, -4, 0}); assertEquals(new Polynomial( new int [] {1,0,2,7,-11,-9,-6,-11}),p5.minus(p6)); } } // class PolynomialTest