package tests;

import calc.*;

import junit.framework.TestCase;

/** Tests of the calculator.
 * @author Gary T. Leavens
 */
public class TestCalc extends TestCase {

    /**
     * Constructor for TestCalc.
     */
    public TestCalc(String name) {
        super(name);
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestCalc.class);
    }

    /**
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }
    
    /**
     * Test simple summations in the calculator.
     */
    public void testSummation1() {
        FormulaType f1 = new Sum(0, 1);
        FormulaType f2 = new Sum(1, 2);
        Registers.put(0, 4.0);
        Registers.put(1, 8.0);
        Registers.put(2, -1.0);
        FormulaMap.put("add01", f1);
        FormulaMap.put("add12", f2);
        assertEquals(12.0, FormulaMap.get("add01").evaluate(), 0.01);
        assertEquals(7.0, FormulaMap.get("add12").evaluate(), 0.01);
    }

    /**
     * Test simple differences in the calculator.
     */
    public void testDiff1() {
        FormulaType f1 = new Diff(0, 1);
        FormulaType f2 = new Diff(1, 2);
        Registers.put(0, 4.0);
        Registers.put(1, 8.0);
        Registers.put(2, -1.0);
        FormulaMap.put("sub01", f1);
        FormulaMap.put("sub12", f2);
        assertEquals(-4.0, FormulaMap.get("sub01").evaluate(), 0.01);
        assertEquals(9.0, FormulaMap.get("sub12").evaluate(), 0.01);
    }

    /**
     * Test simple products in the calculator.
     */
    public void testMult1() {
        FormulaType f1 = new Mult(0, 1);
        FormulaType f2 = new Mult(1, 2);
        Registers.put(0, 4.0);
        Registers.put(1, 8.0);
        Registers.put(2, -1.0);
        FormulaMap.put("prod01", f1);
        FormulaMap.put("prod12", f2);
        assertEquals(32.0, FormulaMap.get("prod01").evaluate(), 0.01);
        assertEquals(-8.0, FormulaMap.get("prod12").evaluate(), 0.01);
    }

    /**
     * Test simple negations in the calculator.
     */
    public void testNegate1() {
        FormulaType f1 = new Negate(0);
        FormulaType f2 = new Negate(1);
        Registers.put(0, 4.0);
        Registers.put(1, 8.0);
        FormulaMap.put("neg0", f1);
        FormulaMap.put("neg1", f2);
        assertEquals(-4.0, FormulaMap.get("neg0").evaluate(), 0.01);
        assertEquals(-8.0, FormulaMap.get("neg1").evaluate(), 0.01);
    }
    /**
     * Test simple negations in the calculator.
     */
    public void testSum3() {
        FormulaType f1 = new Sum3(0, 1, 2);
        Registers.put(0, 4.0);
        Registers.put(1, 8.0);
        Registers.put(2, -2.0);
        FormulaMap.put("sum3", f1);
        assertEquals(10.0, FormulaMap.get("sum3").evaluate(), 0.01);
    }
    /**
     * Test simple negations in the calculator.
     */
    public void testIsPositive() {
        FormulaType f1 = new Sum3(0, 1, 2);
        Registers.put(0, 4.0);
        Registers.put(1, 8.0);
        Registers.put(2, -2.0);
        FormulaMap.put("sum3", f1);
        assertTrue(FormulaMap.get("sum3").isPositive());
    }
}
