diff options
Diffstat (limited to 'Week7')
-rw-r--r-- | Week7/test/polynomial/PolynomialTest.java | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/Week7/test/polynomial/PolynomialTest.java b/Week7/test/polynomial/PolynomialTest.java index 13f7405..c0f9c4b 100644 --- a/Week7/test/polynomial/PolynomialTest.java +++ b/Week7/test/polynomial/PolynomialTest.java @@ -43,8 +43,10 @@ public class PolynomialTest { @Test public void testToString() { System.out.println("toString"); - // Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients + // Test exp=0, exp=1, exp>1, positive & negative integer & floating point coefficients all at once assertEquals("3.000000x^5-4.000000x+2.500000-3.400000x^-7", new Polynomial("3 5 -4 1 2.5 0 -3.4 -7").toString()); + // Test zero polynomial + assertEquals("", new Polynomial().toString()); } /** @@ -53,6 +55,7 @@ public class PolynomialTest { @Test public void testMinus() { System.out.println("minus"); + // Test cancelling out and exponents that don't exist in either of the two polynomials all at once Polynomial instance = new Polynomial("7 4 -4 2 3 1 5 0"); // 7x^4 - 4x^2 + 3x + 5 instance.minus(new Polynomial("5 3 4 2 3 1 10 0")); // 5x^3 + 4x^2 + 3x + 10 assertEquals(new Polynomial("7 4 -5 3 -8 2 -5 0"), instance); // 7x^4 - 5x^3 - 8x^2 - 5 @@ -64,9 +67,21 @@ public class PolynomialTest { @Test public void testTimes() { System.out.println("times"); + + // Simple case Polynomial instance = new Polynomial("7 1 6 3"); instance.times(new Polynomial("5 3 4 2")); assertEquals(new Polynomial("30 6 24 5 35 4 28 3"), instance); + + // Simple case with zero polynomial + instance = new Polynomial("5 3"); + instance.times(new Polynomial()); + assertEquals(new Polynomial(), instance); + + // Advanced case with coefficient 0 in the result + instance = new Polynomial("4 4 2 3"); // 4x^4 + 2x^3 + instance.times(new Polynomial("2 2 -4 3")); // 2x^2 - 4x^3 + assertEquals(new Polynomial("4 5 -16 7"), instance); // 4x^5 - 16x^7 (exponent 6 cancels out) } /** @@ -88,7 +103,18 @@ public class PolynomialTest { @Test public void testEquals() { System.out.println("equals"); + // Order of terms irrelevant assertEquals(true, new Polynomial("4 2 3 5").equals(new Polynomial("3 5 4 2"))); + // Terms with same exponent are added + assertEquals(true, new Polynomial("5 5 10 5").equals(new Polynomial("15 5"))); + // Simple coefficient 0 removed + assertEquals(true, new Polynomial("0 7").equals(new Polynomial())); + // Advanced coefficient 0 removed + assertEquals(true, new Polynomial("5 10 -5 10").equals(new Polynomial())); + // Difference in exponent matters + assertEquals(false, new Polynomial("10 0").equals(new Polynomial("10 1"))); + // Difference in coefficient matters + assertEquals(false, new Polynomial("10 0").equals(new Polynomial("5 0"))); } /** @@ -97,6 +123,7 @@ public class PolynomialTest { @Test public void testPlus_Polynomial() { System.out.println("plus"); + // Test cancelling out and exponents that don't exist in either of the two polynomials all at once Polynomial instance = new Polynomial("7 4 -4 2 7 1 5 0"); // 7x^4 - 4x^2 + 7x + 5 instance.plus(new Polynomial("5 3 4 2 3 1 10 0")); // 5x^3 + 4x^2 + 3x + 10 assertEquals(new Polynomial("7 4 5 3 10 1 15 0"), instance); // 7x^4 + 5x^3 + 10x + 15 @@ -108,8 +135,9 @@ public class PolynomialTest { @Test public void testApply() { System.out.println("apply"); - Polynomial instance = new Polynomial("5 4 0.5 3 9 2 8 0"); - assertEquals(422670.5, instance.apply(17.0), 0.0); + // Test zero coefficient, floating point coefficient and negative coefficient all at once + Polynomial instance = new Polynomial("0 7 5 4 0.5 3 9 2 -8 0"); + assertEquals(422654.5, instance.apply(17.0), 0.0); } } |