Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Loading