Skip to content

Commit 79db90f

Browse files
committed
add units test for the .set directive
1 parent bb7f3a7 commit 79db90f

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

tests/assemble.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from esp32_ulp.nocomment import remove_comments
44

55
src = """\
6+
.set const, 123
67
78
start: wait 42
89
ld r0, r1, 0
@@ -14,14 +15,14 @@
1415

1516
def test_parse_line():
1617
a = Assembler()
17-
lines = src.splitlines()
18-
# note: line number = index + 1
19-
assert a.parse_line(lines[0]) == None
20-
assert a.parse_line(lines[1]) == ('start', 'wait', ('42', ))
21-
assert a.parse_line(lines[2]) == (None, 'ld', ('r0', 'r1', '0', ))
22-
assert a.parse_line(lines[3]) == (None, 'st', ('r0', 'r1', '0', ))
23-
assert a.parse_line(lines[4]) == (None, 'halt', ())
24-
assert a.parse_line(lines[5]) == ('end', None, ())
18+
lines = iter(src.splitlines())
19+
assert a.parse_line(next(lines)) == (None, '.set', ('const', '123', ))
20+
assert a.parse_line(next(lines)) == None
21+
assert a.parse_line(next(lines)) == ('start', 'wait', ('42', ))
22+
assert a.parse_line(next(lines)) == (None, 'ld', ('r0', 'r1', '0', ))
23+
assert a.parse_line(next(lines)) == (None, 'st', ('r0', 'r1', '0', ))
24+
assert a.parse_line(next(lines)) == (None, 'halt', ())
25+
assert a.parse_line(next(lines)) == ('end', None, ())
2526

2627

2728
def test_parse():
@@ -34,8 +35,10 @@ def test_parse():
3435
def test_assemble():
3536
a = Assembler()
3637
a.assemble(src)
38+
assert a.symbols.has_sym('const')
3739
assert a.symbols.has_sym('start')
3840
assert a.symbols.has_sym('end')
41+
assert a.symbols.get_sym('const') == (ABS, None, 123)
3942
assert a.symbols.get_sym('start') == (REL, TEXT, 0)
4043
assert a.symbols.get_sym('end') == (REL, TEXT, 4)
4144
assert len(b''.join(a.sections[TEXT])) == 16 # 4 instructions * 4B
@@ -50,6 +53,7 @@ def test_symbols():
5053
('abs_t4', ABS, TEXT, 4),
5154
('rel_d4', REL, DATA, 4),
5255
('abs_d4', ABS, DATA, 4),
56+
('const', ABS, None, 123),
5357
]:
5458
st.set_sym(*entry)
5559
# PASS 1 ========================================================
@@ -62,11 +66,13 @@ def test_symbols():
6266
assert st.resolve_absolute('abs_d4') == 4
6367
assert st.resolve_absolute('rel_t4') == 4
6468
assert st.resolve_absolute('rel_d4') == 4
69+
assert st.resolve_absolute('const') == 123
6570
st.set_from(TEXT, 8)
6671
assert st.resolve_relative('abs_t4') == -4
6772
assert st.resolve_relative('abs_d4') == -4
6873
assert st.resolve_relative('rel_t4') == -4
6974
assert st.resolve_relative('rel_d4') == -4
75+
assert st.resolve_absolute('const') == 123
7076
# PASS 2 ========================================================
7177
st.set_bases({TEXT: 100, DATA: 200})
7278
st.set_pass(2)
@@ -84,11 +90,13 @@ def test_symbols():
8490
assert st.resolve_absolute('abs_d4') == 4
8591
assert st.resolve_absolute('rel_t4') == 100 + 4
8692
assert st.resolve_absolute('rel_d4') == 200 + 4
93+
assert st.resolve_absolute('const') == 123
8794
st.set_from(TEXT, 8)
8895
assert st.resolve_relative('abs_t4') == 4 - 108
8996
assert st.resolve_relative('abs_d4') == 4 - 108
9097
assert st.resolve_relative('rel_t4') == 104 - 108
9198
assert st.resolve_relative('rel_d4') == 204 - 108
99+
assert st.resolve_absolute('const') == 123
92100

93101

94102
test_parse_line()

0 commit comments

Comments
 (0)