/** * The test class ComplexTest, to test the Complex class (immutable version) * * @author Phill Conrad, and (insert YOUR NAME HERE) * @version lab04 for CS10, Spring 2009 (updated from lab02) * @see Complex */ public class ComplexTest extends junit.framework.TestCase { // 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; public void testNoArgConstructor() { // the no arg constructor should give us zero for // both imaginary and real parts Complex c = new Complex(); assertEquals(0.0,c.getReal(),TOL); assertEquals(0.0,c.getImag(),TOL); } public void testTwoArgConstructor() { // the no arg constructor should give us zero for // both imaginary and real parts Complex c = new Complex( 1.2, -3.4 ); assertEquals( 1.2, c.getReal(), TOL); assertEquals( -3.4, c.getImag(), TOL); } public void testToString1() { Complex c = new Complex(); assertEquals("0.0 + 0.0i",c.toString()); } public void testToString2() { // the no arg constructor should give us zero for // both imaginary and real parts Complex c = new Complex(1.2, -3.4); assertEquals("1.2 + -3.4i",c.toString()); } public void testToString3() { // the no arg constructor should give us zero for // both imaginary and real parts Complex c = new Complex(2.0, 3.0); assertEquals("2.0 + 3.0i",c.toString()); } public void testApproxEquals1() { // Test whether the approxEquals method works properly. Complex c1 = new Complex(2.0, 3.0); Complex c2 = new Complex(2.0, 3.0); Complex c3 = new Complex(2.0, 4.0); Complex c4 = new Complex(4.0, 3.0); assertTrue(c1.approxEquals(c2,TOL)); assertTrue(!c1.approxEquals(c3,TOL)); assertTrue(!c1.approxEquals(c4,TOL)); assertTrue(!c3.approxEquals(c4,TOL)); } public void testAdd1() { Complex c1 = new Complex(2.0, 3.0); Complex c2 = new Complex(5.0, 7.0); // (2 + 3i) + (5 + 7i) is done by adding the real part and adding the imaginary parts // giving us 7 + 10i Complex expectedAnswer = new Complex(7.0, 10.0); // It would be nice if we could write: // assertEquals(expectedAnswer, Complex.add(c1,c2)); // but that doesn't take the tolerance into account. We could try: // assertEquals(expectedAnswer, Complex.add(c1,c2), TOL); // but that doesn't compile because JUnit doesn't know about our Complex class. // So we have to write it this way, using the approxEquals method of the Complex class assertTrue( expectedAnswer.approxEquals( Complex.add(c1, c2), TOL )); } public void testAdd2 () { // @@@ ADD ANOTHER UNIT TEST HERE---one of your own choosing // that tests the add function fail("You haven't written the body of the testAdd2 function yet!"); // @@@ Remove this line when done } public void testMultiply1() { Complex c1 = new Complex(2.0, 3.0); Complex c2 = new Complex(5.0, 7.0); // (2 + 3i) * (5 + 7i) is done with FOIL ... // first, outer, inner, last: giving us: // 10 + 14i + 15i + -21, which simplifies to: -11 + 29i Complex expectedAnswer = new Complex(-11.0, 29.0); assertTrue( expectedAnswer.approxEquals(Complex.multiply(c1, c2), TOL)); } public void testMultiply2 () { // @@@ ADD ANOTHER UNIT TEST HERE---one of your own choosing // that tests the multiply function fail("You haven't written the body of the testMultiply2 function yet!"); // @@@ Remove this line when done } }