/** * The test class QuadraticEqnTest. * * @author Phill Conrad * @version 05/07/2009 for CS10, lab04, UCSB */ public class QuadraticEqnTest extends junit.framework.TestCase { /** declare a tolerance for testing doubles for equality */ public static final double TOL = 0.00001; /** test whether the getters for a, b, c work */ public void testConstructorAndGetters() { QuadraticEqn q = new QuadraticEqn(2.0, 3.0, 4.0); assertEquals(2.0, q.getA()); assertEquals(3.0, q.getB()); assertEquals(4.0, q.getC()); } public void testNumRealRoots() { QuadraticEqn q = new QuadraticEqn(1.0, 5.0, -14.0); assertEquals( 2, q.numRealRoots()); QuadraticEqn q2 = new QuadraticEqn(1.0, 6.0, 9.0); assertEquals( 1, q2.numRealRoots()); QuadraticEqn q3 = new QuadraticEqn(1.0, 5.0, 13.0); assertEquals( 0, q3.numRealRoots()); } public void testCalcDiscriminant() { QuadraticEqn q = new QuadraticEqn(1.0, 5.0, -14.0); assertEquals( (25-(4*(-14))), q.calcDiscriminant(), TOL); } public void testCalcDiscriminant1() { QuadraticEqn q2 = new QuadraticEqn(1.0, 6.0, 9.0); assertEquals( (36.0-(4.0*9.0)), q2.calcDiscriminant(), TOL); } public void testCalcDiscriminant2() { QuadraticEqn q3 = new QuadraticEqn(1.0, 5.0, 13.0); assertEquals( (25.0-(4.0*13.0)), q3.calcDiscriminant(), TOL); } public void testEvaluateForDouble1() { // if we evaluate ax^2 + bx + c for x=2, a=3, b=5, c=7 // we should get 3*2*2 + 5*2 + 7 = 12 + 10 + 7 = 29 QuadraticEqn q = new QuadraticEqn(3.0, 5.0 ,7.0); assertEquals( 29.0, q.evaluate(2.0), TOL); } public void testEvaluateForDouble2() { // @@@ ADD ANOTHER UNIT TEST HERE---one of your own choosing // that tests the evaluate function for doubles. (See the one above) fail("You haven't written the body of the testEvaluateForDouble2 function yet!"); // @@@ Remove this line when done } public void testEvaluateForComplex1() { // Try the same test, using only the real parts of the complex number // if we evaluate ax^2 + bx + c for x=2, a=3, b=5, c=7 // we should get 3*2*2 + 5*2 + 7 = 12 + 10 + 7 = 29 QuadraticEqn q = new QuadraticEqn(3.0, 5.0 ,7.0); Complex two = new Complex(2.0, 0.0); assertTrue( new Complex(29.0, 0.0).approxEquals(q.evaluate(two),TOL)); } public void testEvaluateForComplex2() { // if we evaluate ax^2 + bx + c for x=2 + 3i, a=5, b=7, c=11 // we should get: // 5 * (2 + 3i) * (2 * 3i) = 5 * (4 + 12i - 9) = 5 ( -5 + 12i) = -25 + 60i // plus 7 * (2 + 3i) = 14 + 21i // plus 11 = 11 // Grand total: 0.0 + 81i QuadraticEqn q = new QuadraticEqn(5.0, 7.0 ,11.0); Complex c = new Complex(2.0, 3.0); assertTrue( new Complex(0.0, 81.0).approxEquals(q.evaluate(c),TOL)); } public void testEvaluateForComplex3() { // @@@ ADD ANOTHER UNIT TEST HERE---one of your own choosing // that tests the evaluate function for Complex numbers. (See the one above) fail("You haven't written the body of the testEvaluateForComplex3 function yet!"); // @@@ Remove this line when done } public void ultimateRealRootTestRealVersion() { // the ultimate test: calculate the real roots of a quadratic equation, // the evaluate the equation at those roots, and see if they give us zero // We'll use (2x + 3)(5x - 7), which gives us 10x^2 + x -21 // The real roots should be 2x + 3 = 0 and 5x - 7 = 0 // which should be x = -3/2 = -1.5 and 7/5 = 1.4 // To figure out which root is which, we'll need to apply the quadratic formula: // ( -1 + sqrt(1 - 4(10)(-21) ) / 20 gives us: (-1 + 29 ) / 20 = 1.4 // So 1.4 should be root1, and -1.5 should be root2 QuadraticEqn q = new QuadraticEqn(10, 1, -21); assertEquals(2, q.numRealRoots()); assertEquals(1.4, q.realRoot1(), TOL); assertEquals(-1.5, q.realRoot2(), TOL); // and the ultimate test... does it evaluate to zero at its two roots? assertEquals(0.0, q.evaluate(q.realRoot1()), TOL); assertEquals(0.0, q.evaluate(q.realRoot2()), TOL); } public void ultimateRealRootTestComplexVersion1() { // the ultimate test: calculate the real roots of a quadratic equation, // the evaluate the equation at those roots, and see if they give us zero // We'll use (2x + 3)(5x - 7), which gives us 10x^2 + x -21 // The real roots should be 2x + 3 = 0 and 5x - 7 = 0 // which should be x = -3/2 = -1.5 and 7/5 = 1.4 // To figure out which root is which, we'll need to apply the quadratic formula: // ( -1 + sqrt(1 - 4(10)(-21) ) / 20 gives us: (-1 + 29 ) / 20 = 1.4 // So 1.4 should be root1, and -1.5 should be root2 QuadraticEqn q = new QuadraticEqn(10, 1, -21); assertEquals(2, q.numRealRoots()); assertTrue(new Complex(1.4,0.0).approxEquals(q.complexRoot1(), TOL)); assertTrue(new Complex(-1.5,0.0).approxEquals(q.complexRoot2(), TOL)); // and the ultimate test... does it evaluate to zero at its two roots? Complex zero = new Complex(); assertTrue(zero.approxEquals(q.complexRoot1(), TOL)); assertTrue(zero.approxEquals(q.complexRoot2(), TOL)); } }