35 lines
870 B
Python
35 lines
870 B
Python
import math, sys
|
|
sys.path.insert(0, '/root/.hermes/wizard-outputs/beam')
|
|
from expr_eval import evaluate, EvalError
|
|
|
|
cases = [
|
|
("2+3*4", 14.0),
|
|
("(2+3)*4", 20.0),
|
|
("2**3**2", 512.0), # right assoc
|
|
("10 // 3", 3.0),
|
|
("10 % 3", 1.0),
|
|
("-5 + 2", -3.0),
|
|
("2*-3", -6.0),
|
|
("sqrt(16)+abs(-2)", 6.0),
|
|
("max(1,5,2)", 5.0),
|
|
("sin(0)", 0.0),
|
|
("2**0.5", math.sqrt(2)),
|
|
("1e3 + 0.5", 1000.5),
|
|
]
|
|
for expr, exp in cases:
|
|
got = evaluate(expr)
|
|
assert got == exp, (expr, got, exp)
|
|
print("OK", expr, got)
|
|
|
|
# variables
|
|
assert evaluate("x*2 + pi", {'x': 4, 'pi': math.pi}) == 8 + math.pi
|
|
# errors
|
|
for bad in ["2 +", "foo", "1/0", "sin(1,2)", "2 **", "abc("]:
|
|
try:
|
|
evaluate(bad)
|
|
print("FAIL no error:", bad)
|
|
sys.exit(1)
|
|
except EvalError as e:
|
|
print("ERR ok:", bad, "->", e)
|
|
print("ALL PASS")
|