diff options
Diffstat (limited to 'Week7/test/polynomial/PolynomialTest.java')
-rw-r--r-- | Week7/test/polynomial/PolynomialTest.java | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/Week7/test/polynomial/PolynomialTest.java b/Week7/test/polynomial/PolynomialTest.java new file mode 100644 index 0000000..dc31900 --- /dev/null +++ b/Week7/test/polynomial/PolynomialTest.java @@ -0,0 +1,145 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package polynomial; + +import java.util.Scanner; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author camilstaps + */ +public class PolynomialTest { + + public PolynomialTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of toString method, of class Polynomial. + */ + @Test + public void testToString() { + System.out.println("toString"); + Polynomial instance = new Polynomial("3 5 -4 1 2.5 0 -3.4 -7"); // Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients + String expResult = "3.000000x^5-4.000000x+2.500000-3.400000x^-7"; + String result = instance.toString(); + assertEquals(expResult, result); + } + + /** + * Test of minus method, of class Polynomial. + */ + @Test + public void testMinus() { + System.out.println("minus"); + Polynomial b = new Polynomial("5 3 4 2 3 1 10 0"); // 5x^3 + 4x^2 + 3x + 10 + Polynomial instance = new Polynomial("7 4 -4 2 3 1 5 0"); // 7x^4 - 4x^2 + 3x + 5 + instance.minus(b); + assertEquals(new Polynomial("7 4 -5 3 -8 2 -5 0"), instance); // 7x^4 - 5x^3 - 8x^2 - 5 + } + + /** + * Test of times method, of class Polynomial. + */ + @Test + public void testTimes() { + System.out.println("times"); + Polynomial b = new Polynomial("5 3 4 2"); + Polynomial instance = new Polynomial("7 1 6 3"); + instance.times(b); + assertEquals(new Polynomial("30 6 24 5 35 4 28 3"), instance); + } + + /** + * Test of divide method, of class Polynomial. + */ + @Test + public void testDivide() { + System.out.println("divide"); + Polynomial b = null; + Polynomial instance = new Polynomial(); + instance.divide(b); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of equals method, of class Polynomial. + */ + @Test + public void testEquals() { + System.out.println("equals"); + Object other_poly = new Polynomial("3 5 4 2"); + Polynomial instance = new Polynomial("4 2 3 5"); + boolean expResult = true; + boolean result = instance.equals(other_poly); + assertEquals(expResult, result); + } + + /** + * Test of plus method, of class Polynomial. + */ + @Test + public void testPlus_Polynomial() { + System.out.println("plus"); + Polynomial b = new Polynomial("5 3 4 2 3 1 10 0"); // 5x^3 + 4x^2 + 3x + 10 + Polynomial instance = new Polynomial("7 4 -4 2 7 1 5 0"); // 7x^4 - 4x^2 + 7x + 5 + instance.plus(b); + assertEquals(new Polynomial("7 4 5 3 10 1 15 0"), instance); // 7x^4 + 5x^3 + 10x + 15 + } + + /** + * Test of plus method, of class Polynomial. + */ + @Test + public void testPlus_Term() { + System.out.println("plus"); + Term t = Term.scanTerm(new Scanner("5 3")); + + Polynomial instance = new Polynomial("4 7 2 3 1 0"); // normal addition + instance.plus(t); + assertEquals(new Polynomial("4 7 7 3 1 0"), instance); + + instance = new Polynomial("4 7 -5 3 1 0"); // addition when the term cancels out + instance.plus(t); + assertEquals(new Polynomial("4 7 1 0"), instance); + } + + /** + * Test of apply method, of class Polynomial. + */ + @Test + public void testApply() { + System.out.println("apply"); + double x = 17.0; + Polynomial instance = new Polynomial("5 4 0.5 3 9 2 8 0"); + double expResult = 422670.5; + double result = instance.apply(x); + assertEquals(expResult, result, 0.0); + } + +} |