// $Id: ExprTest.java,v 1.4 2002/09/15 23:07:20 leavens Exp leavens $
// Com S 362 homework

import junit.framework.*;

/** A class to test expressions */
public class ExprTest extends TestCase {

    /** Run the tests */
    public static void main(String args[]) {
        String[] testCaseName = {ExprTest.class.getName()};
        junit.swingui.TestRunner.main(testCaseName);
    }

    /** Return the test suite */
    public static Test suite() {
        return new TestSuite(ExprTest.class);
    }

    /** Initialize the name of this test case. */
    public ExprTest(String name) {
        super(name);
    }

    /** Useful constant for comparing double results. */
    private static final double EPSILON = 0.000001;

    /** Test constant expressions. */
    public void testExprConstant() {
        assertEquals(3.14159, new ExprConstant(3.14159).value(),
                     EPSILON);
        assertEquals(2.173, new ExprConstant(2.173).value(),
                     EPSILON);
    }

    /** Test less than expressions. */
    public void testExprLessThan() {
        for (int i = 0; i < basic_exps.length; i++) {
            for (int j = 0; j < basic_exps.length; j++) {
                assertEquals(basic_exps[i].value() < basic_exps[j].value(),
                             new ExprLessThan(basic_exps[i],
                                              basic_exps[j]).isLessThan());
            }
        }
    }

    /** Test sum expressions. */
    public void testExprSum() {
        for (int i = 0; i < basic_exps.length; i++) {
            for (int j = 0; j < basic_exps.length; j++) {
                assertEquals(basic_exps[i].value() + basic_exps[j].value(),
                             new ExprSum(basic_exps[i],
                                         basic_exps[j]).value(),
                             EPSILON);
            }
        }
    }

    /** Test product expressions. */
    public void testExprProduct() {
        for (int i = 0; i < basic_exps.length; i++) {
            for (int j = 0; j < basic_exps.length; j++) {
                assertEquals(basic_exps[i].value() * basic_exps[j].value(),
                             new ExprProduct(basic_exps[i],
                                             basic_exps[j]).value(),
                             EPSILON);
            }
        }
    }

    /** Test negation expressions. */
    public void testExprNegation() {
        for (int i = 0; i < basic_exps.length; i++) {
            assertEquals(-basic_exps[i].value(),
                         new ExprNegation(basic_exps[i]).value(),
                         EPSILON);
        }
    }

    /** Test fixture, basic expressions. */
    private ExprSum[] basic_exps;

    /** Initialize the test fixture. */
    protected void setUp() {
        basic_exps
            = new ExprSum[] {
                new ExprConstant(3.14159),
                new ExprNegation(new ExprConstant(2.713)),
                new ExprConstant(0.0),
                new ExprNegation(new ExprNegation(new ExprConstant(3.62))),
                new ExprSum(
                    new ExprProduct(
                        new ExprConstant(5.2),
                        new ExprConstant(7.4)),
                    new ExprNegation(new ExprConstant(3.6))),
                new ExprProduct(
                    new ExprSum(
                        new ExprConstant(5.2),
                        new ExprConstant(7.4)),
                    new ExprNegation(new ExprConstant(3.6))),
            };
    }
}
