diff --git a/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala b/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala index 83cc1a5..b5d8ef0 100644 --- a/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala +++ b/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala @@ -35,6 +35,13 @@ class ExprEvaluatorSuite extends AnyFunSuite { assert(Math.abs(49.0 - actual) < 1e-9) } + test("eval same-operator chains use left-associativity") { + assert(1.0 === ExprEvaluator.eval(parser.parse("8/4/2"), 0.0)) + assert(5.0 === ExprEvaluator.eval(parser.parse("10-3-2"), 0.0)) + val pow = ExprEvaluator.eval(parser.parse("2^3^2"), 0.0) + assert(Math.abs(64.0 - pow) < 1e-9) + } + test("eval division by zero yields Infinity") { val expr = parser.parse("1/x") assert(ExprEvaluator.eval(expr, 0.0).isInfinity) diff --git a/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserExtendedSuite.scala b/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserExtendedSuite.scala index ba9eff9..d9eedef 100644 --- a/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserExtendedSuite.scala +++ b/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserExtendedSuite.scala @@ -3,7 +3,7 @@ */ package com.barrybecker4.expression.mathexpression -import com.barrybecker4.expression.{ExprEvaluator, ParseError, TreeSerializer} +import com.barrybecker4.expression.{BinOp, Const, ExprEvaluator, ParseError, TreeSerializer} import com.barrybecker4.testsupport.strip import org.scalatest.Assertions import org.scalatest.funsuite.AnyFunSuite @@ -25,38 +25,72 @@ class MathExpressionParserExtendedSuite extends AnyFunSuite { } } - // --- Precedence and associativity (locks tree shape) --- + private def evalAt(exp: String, x: Double = 0.0): Double = + ExprEvaluator.eval(parser.parse(exp), x) + + // --- Precedence and associativity (value and tree shape; serialization alone is insufficient) --- test("division chains are left-associative") { verify("8/4/2", "8.0 / 4.0 / 2.0") + // right-associative reduction would evaluate to 4 + assert(1.0 === evalAt("8/4/2")) + parser.parse("8/4/2") match { + case BinOp(DIVIDE, BinOp(DIVIDE, Const(a), Const(b)), Const(c)) => + assert(a === 8.0 && b === 4.0 && c === 2.0) + case other => fail(other.toString) + } } test("multiplication chains are left-associative") { verify("2*3*4", "2.0 * 3.0 * 4.0") + assert(24.0 === evalAt("2*3*4")) + parser.parse("2*3*4") match { + case BinOp(TIMES, BinOp(TIMES, Const(a), Const(b)), Const(c)) => + assert(a === 2.0 && b === 3.0 && c === 4.0) + case other => fail(other.toString) + } } test("subtraction chains are left-associative") { verify("10-3-2", "10.0 - 3.0 - 2.0") + // right-associative reduction would evaluate to 9 + assert(5.0 === evalAt("10-3-2")) } test("addition chains are left-associative") { verify("1+2+3", "1.0 + 2.0 + 3.0") + assert(6.0 === evalAt("1+2+3")) + parser.parse("1+2+3") match { + case BinOp(PLUS, BinOp(PLUS, Const(a), Const(b)), Const(c)) => + assert(a === 1.0 && b === 2.0 && c === 3.0) + case other => fail(other.toString) + } } test("exponent towers use left-associativity in reducer") { verify("2^3^2", "2.0 ^ 3.0 ^ 2.0") + // right-associative would be 512; left-associative is the documented choice + val pow = evalAt("2^3^2") + assert(math.abs(64.0 - pow) < 1e-9) } test("division binds tighter than subtraction") { verify("10-8/2", "10.0 - 8.0 / 2.0") + // (10-8)/2 would be 1 + assert(6.0 === evalAt("10-8/2")) } test("multiplication binds tighter than subtraction") { verify("10-2*3", "10.0 - 2.0 * 3.0") + // (10-2)*3 would be 24 + assert(4.0 === evalAt("10-2*3")) } test("exponent binds tighter than multiplication") { verify("x^2*3", "x ^ 2.0 * 3.0") + // (x^(2*3)) at x=2 would be 64 + val e = evalAt("x^2*3", 2.0) + assert(math.abs(12.0 - e) < 1e-9) } test("parentheses force addition before multiplication") { diff --git a/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala b/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala index c225f26..8201b5f 100644 --- a/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala +++ b/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala @@ -133,6 +133,8 @@ class MathExpressionParserSuite extends AnyFunSuite { test("multiplication binds tighter than addition") { verifyParse("2 + 3 * 4", "2.0 + 3.0 * 4.0") + // (2+3)*4 would be 20; serialization is identical for both trees + assert(14.0 === ExprEvaluator.eval(parser.parse("2 + 3 * 4"), 0.0)) } test("parse rejects null expression") {