/
test_assembler.rb
105 lines (85 loc) · 1.83 KB
/
test_assembler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'helper'
module RCPU
class TestAssembler < TestCase
def test_bytedata
block do
SET a, [:hello]
SET [:hello], 123
SET b, [:hello]
SET pc, :crash
data :hello, [12]
end
assert_equal 12, register(:A)
assert_equal 123, register(:B)
end
def test_zerodata
block do
SET i, 1
SET a, 1
SET a, [:hello]
SET b, 1
SET b, [i+:hello]
SET pc, :crash
data :hello, 2
end
assert_equal 0, register(:A)
assert_equal 0, register(:B)
end
def test_stringdata
block do
SET i, 1
SET a, [:hello]
SET b, [i+:hello]
SET pc, :crash
data :hello, "ab"
end
assert_equal "a".ord, register(:A)
assert_equal "b".ord, register(:B)
end
def test_unknowndata
assert_raises(AssemblerError, "unkown data type") do
block do
data :hello, :nope
end
end
end
def test_missing_label
assert_raises(AssemblerError, "no label: fail") do
block do
SET pc, :fail
end
end
end
def test_multiple_blocks
rcpu do
block :main do
JSR :_another
label :crash
SET pc, :crash
end
block :another do
SET a, 1
SET pc, pop
end
end
assert_equal 1, register(:A)
end
def test_missing_block
assert_raises(AssemblerError, "no external label: another") do
block do
JSR :_another
end
end
end
def test_data_label
block do
SUB pc, 1
data :sp, [:stack]
data :stack, 512
data :sp2, [:stack]
end
assert_equal 0x2, memory[0x1]
assert_equal 0x2, memory[0x1+512+0x1]
end
end
end