49 lines
1.1 KiB
Elixir
49 lines
1.1 KiB
Elixir
|
defmodule ExpressionEvaluatorTest do
|
||
|
use ExUnit.Case
|
||
|
doctest ExpressionEvaluator
|
||
|
import ExpressionEvaluator
|
||
|
|
||
|
test "evaluates individual numbers" do
|
||
|
assert eval("1") == 1
|
||
|
end
|
||
|
|
||
|
test "evaluates negated numbers" do
|
||
|
assert eval("-1") == -1
|
||
|
end
|
||
|
|
||
|
test "evaluates variables" do
|
||
|
assert eval("x", %{"x" => 3}) == 3
|
||
|
end
|
||
|
|
||
|
test "evaluates negated variables" do
|
||
|
assert eval("-x", %{"x" => 3}) == -3
|
||
|
assert eval("-x", %{"x" => -3}) == 3
|
||
|
end
|
||
|
|
||
|
test "evaluates binary operations" do
|
||
|
assert eval("1 + 2") == 3
|
||
|
assert eval("3 - 2") == 1
|
||
|
assert eval("3 * 2") == 6
|
||
|
assert eval("10 / 2") == 5
|
||
|
end
|
||
|
|
||
|
test "floors division" do
|
||
|
assert eval("3 / 2") == 1
|
||
|
end
|
||
|
|
||
|
test "obeys operator precedence" do
|
||
|
assert eval("1 + 2 * 3") == 7
|
||
|
assert eval("(1 + 2) * 3") == 9
|
||
|
assert eval("12 - 6 / 2") == 9
|
||
|
assert eval("(12 - 6) / 2") == 3
|
||
|
end
|
||
|
|
||
|
test "evaluates complex expressions" do
|
||
|
assert eval("1 + 2 * (3 - ((4 / 2 + 5) * 6))") == -77
|
||
|
end
|
||
|
|
||
|
test "evaluates complex expressions with variables" do
|
||
|
assert eval("x * ((4 - y) / 6 + z))", %{"x" => 2, "y" => -8, "z" => 5}) == 14
|
||
|
end
|
||
|
end
|