/** The test case class for the ArithExpr composite hierarchy. */ class ArithExprTest extends TestCase { static Double EPSILON = 1.1e-16; static Const zero = new Const(0.); static Const negSeven = new Const(-7.); static Const five = new Const(5.); static Const negTen = new Const(-10.); static Const ten = new Const(10.); static Const twentyFive = new Const(25.); static Var x = new Var("x"); static Var y = new Var("y"); static Var z = new Var("z"); /** Tests the eval() method in the ArithExpr composite. */ public void testExprPrint() { assertEquals("const 0.", "0.0", zero.exprString()); assertEquals("const -7.", "-7.0", negSeven.exprString()); assertEquals("const -10.", "-10.0", negTen.exprString()); assertEquals("5 + 5", "(5.0 + 5.0)", new Sum(five, five).exprString()); assertEquals("5 * 10", "(5.0 * 10.0)", new Prod(five, ten).exprString()); assertEquals("10/5", "(10.0 / 5.0)", new Quot(ten, five).exprString()); assertEquals("10 - 5", "(10.0 - 5.0)", new Diff(ten, five).exprString()); assertEquals("x * x", "(x * x)", new Prod(x, x).exprString()); assertEquals("y/z", "(y / z)", new Quot(y, z).exprString()); } static EmptyEnv e = EmptyEnv.ONLY; static Env env1 = e.cons("x", -10.).cons("y", 25.).cons("z", -5.); /** Tests the eval() method in the ArithExpr composite. */ public void testEval() { assertEquals("const 0.", 0., zero.eval(e), EPSILON); assertEquals("const 7.", -7., negSeven.eval(e), EPSILON); assertEquals("const -10.", -10., negTen.eval(e), EPSILON); assertEquals("25 + 5", 30., new Sum(twentyFive, five).eval(e), EPSILON); assertEquals("-7 * 10", -70., new Prod(negSeven, ten).eval(e), EPSILON); assertEquals("25/5", 5., new Quot(twentyFive, five).eval(e), EPSILON); assertEquals("25 - 5", 20., new Diff(twentyFive, five).eval(e), EPSILON); assertEquals("x * z", 50., new Prod(negTen, z).eval(env1), EPSILON); assertEquals("y/z", -5., new Quot(y, z).eval(env1), EPSILON); } }