AoC19/test/intcode/assembler_test.exs

144 lines
3.2 KiB
Elixir

defmodule AssemblerTest do
use ExUnit.Case
doctest Assembler
import Assembler
test "assembles empty program" do
assert assemble("") == []
end
test "assembles instructions correctly" do
assert assemble("add 1, 2, 3") == [1101, 1, 2, 3]
assert assemble("mul 1, 2, 3") == [1102, 1, 2, 3]
assert assemble("in 7") == [3, 7]
assert assemble("out 7") == [104, 7]
assert assemble("out $7") == [4, 7]
assert assemble("jnz 1, 2") == [1105, 1, 2]
assert assemble("jnz $1, 2") == [1005, 1, 2]
assert assemble("jez 1, 2") == [1106, 1, 2]
assert assemble("jez $1, 2") == [1006, 1, 2]
assert assemble("clt 1, 2, 3") == [1107, 1, 2, 3]
assert assemble("clt $1, $2, $3") == [7, 1, 2, 3]
assert assemble("ceq 1, 2, 3") == [1108, 1, 2, 3]
assert assemble("ceq $1, $2, $3") == [8, 1, 2, 3]
assert assemble("hlt") == [99]
end
test "assembles macros" do
assert assemble("mov 1, 2") == [1101, 0, 1, 2]
assert assemble("jmp 2") == [1105, 1, 2]
assert assemble("sub 5, 3, res\nres: 0") == [1102, 3, -1, 8, 101, 5, 8, 8, 0]
assert assemble("not 4, 4") == [1102, 4, -1, 4, 101, 1, 4, 4]
assert assemble("cle 1, 2, res\nres: 0") == [1107, 1, 2, 11, 1005, 11, 11, 1108, 1, 2, 11, 0]
assert assemble("cgt 1, 2, res\nres: 0") == [
1107,
1,
2,
19,
1005,
19,
11,
1108,
1,
2,
19,
1002,
19,
-1,
19,
101,
1,
19,
19,
0
]
end
test "assembles simple program" do
program = """
add 1, 2, 10
mul 2, 3, 11
add $10, $11, 12
hlt
"""
assert assemble(program) == [
1101,
1,
2,
10,
1102,
2,
3,
11,
1,
10,
11,
12,
99
]
end
test "assembles simple program with a label" do
program = """
add 1, 2, 10
jnz $10, nonZero
nonZero:
hlt
"""
assert assemble(program) == [
1101,
1,
2,
10,
1005,
10,
7,
99
]
end
test "assembles a simple program with a data label" do
program = """
add 1, 2, var
out $var
var: 0
"""
assert assemble(program) == [
1101,
1,
2,
6,
4,
6,
0
]
end
test "assembles a program with a _self label" do
program = """
add 1, 2, _self
"""
assert assemble(program) == [1101, 1, 2, 3]
end
test "assembles parameters with expressions" do
assert assemble("add 1, _self + 1, 3") == [1101, 1, 3, 3]
assert assemble("add 1, $(_self + 1), 3") == [101, 1, 3, 3]
assert assemble("""
add 1, label + 4 - 2, 3
label: 42
""") == [1101, 1, 6, 3, 42]
assert assemble("""
add 1, $(label + 4 - 2), 3
label: 42
""") == [101, 1, 6, 3, 42]
end
end