/**
 * CIS20  Asg5
 * ComplexTest.java
 * Purpose: Represent a complex number
 *
 * @author Ed Parrish
 * @version 1.0 3/20/03
 */
public class ComplexTest {
    /**
     * The main program for the ComplexTest class
     *
     * @param args -- not used
     */
    public static void main(String args[]) {
        testComplex();
        testComplex1();
        testComplex2();
        testAdd();
        testSubtract();
        testMultiply();
        testDivide();
        testToString();
        System.out.println("*** All tests passed ***");
    }

    /**
     * Convenience method to test for assertions.
     *
     * @param condition The test condition that must be true to pass.
     * @param message The reason for the failure.
     */
    public static void assertTrue(boolean condition, String message) {
        if (!condition) {
            throw new RuntimeException(message);
        }
    }

    /**
     * Test method: public Complex() and get methods
     */
    public static void testComplex() {
        System.out.println("Testing Complex()");
        Complex a;
        a = new Complex();
        assertTrue((a.getReal() - 0.0) < 0.000001,
            "Real value wrong: " + a.getReal());
        assertTrue((a.getImaginary() - 0.0) < 0.000001,
            "Imaginary value wrong: " + a.getImaginary());
    }

    /**
     * Test method: public Complex(double)
     */
    public static void testComplex1() {
        System.out.println("Testing Complex(double)");
        Complex a;
        a = new Complex(1.0);
        assertTrue((a.getReal() - 1.0)  < 0.000001,
            "Real value wrong: " + a.getReal());
        assertTrue((a.getImaginary() - 0.0) < 0.000001,
            "Imaginary value wrong: " + a.getImaginary());
    }

    /**
     * Test method: public Complex(double, double)
     */
    public static void testComplex2() {
        System.out.println("Testing Complex(double, double)");
        Complex a;
        a = new Complex(1.0F, 2.0F);
        assertTrue((a.getReal() - 1.0) < 0.000001,
            "Real value wrong: " + a.getReal());
        assertTrue((a.getImaginary() - 2.0) < 0.000001,
            "Imaginary value wrong: " + a.getImaginary());
    }

    /**
     * Test method: public Complex add(Complex)
     */
    public static void testAdd() {
        System.out.println("Testing add(Complex)");
        Complex a, b, c;
        a = new Complex(9.9F, 7.7F);
        b = new Complex(1.2F, 3.1F);
        c = a.add(b);
        assertTrue((c.getReal() - 11.1) < 0.000001,
            "Real value wrong: " + c.getReal());
        assertTrue((c.getImaginary() - 10.8) < 0.000001,
            "Imaginary value wrong: " + c.getImaginary());
    }

    /**
     * Test method: public Complex sub(Complex)
     */
    public static void testSubtract() {
        System.out.println("Testing subtract(Complex)");
        Complex a, b, c;
        a = new Complex(9.9F, 7.7F);
        b = new Complex(1.2F, 3.1F);
        c = a.subtract(b);
        assertTrue(Math.abs(c.getReal() - 8.7) < 0.000001,
            "Real value wrong: " + c.getReal());
        assertTrue(Math.abs(c.getImaginary() - 4.6) < 0.000001,
            "Imaginary value wrong: " + c.getImaginary());
    }

    /**
     * Test method: public Complex multiply(Complex)
     */
    public static void testMultiply() {
        System.out.println("Testing multiply(Complex)");
        Complex a, b, c;
        a = new Complex(9.9F, 7.7F);
        b = new Complex(1.2F, 3.1F);
        c = a.multiply(b);
        assertTrue(Math.abs(c.getReal() + 11.99) < 0.0001,
            "Real value wrong: " + c.getReal());
        assertTrue(Math.abs(c.getImaginary() - 39.93) < 0.0001,
            "Imaginary value wrong: " + c.getImaginary());
    }

    /**
     * Test method: public Complex divide(Complex)
     */
    public static void testDivide() {
        System.out.println("Testing divide(Complex)");
        Complex a, b, c;
        a = new Complex(3F, -7F);
        b = new Complex(2F, 6F);
        c = a.divide(b);
        assertTrue(Math.abs(c.getReal() + 0.9) < 0.0001,
            "Real value wrong: " + c.getReal());
        assertTrue(Math.abs(c.getImaginary() + 0.8) < 0.0001,
            "Imaginary value wrong: " + c.getImaginary());
        try {
            c = a.divide(new Complex());
            if (c != null)
                assertTrue(false, "Did not catch division by zero");
        } catch (Exception e) {
            // do nothing -- allow students to use any exception
        }
    }

    /**
     * Test method: public String toString()
     */
    public static void testToString() {
        System.out.println("Testing toString()");
        Complex a;
        a = new Complex(1.125F, 2.25F);
        assertTrue(a.toString().equals("(1.125, 2.25)"),
            "\nError in toString: " + a.toString());
    }
}

