-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_runner.py
161 lines (131 loc) · 4.26 KB
/
test_runner.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import armsim
#run instruction tests
import instruction_tests
import sys
from io import StringIO,BytesIO
'''
Full program tests in /tests directory
exit code in x0 for all test cases should be 7
'''
with open('tests/arithmetic_test.s','r') as f:
armsim.parse(f.readlines())
armsim.run()
assert armsim.reg['x0'] == 7, "arithmetic_test returned incorrect value of {}".format(armsim.reg['x0'])
armsim.reset()
with open('tests/branch_test.s','r') as f:
armsim.parse(f.readlines())
armsim.run()
assert armsim.reg['x0'] == 7, "arithmetic_test returned incorrect value of {}".format(armsim.reg['x0'])
armsim.reset()
with open('tests/ldp_stp_test.s','r') as f:
armsim.parse(f.readlines())
armsim.run()
assert armsim.reg['x0'] == 7, "ldp_stp_test returned incorrect value of {}".format(armsim.reg['x0'])
armsim.reset()
with open('tests/ldr_str_test.s','r') as f:
armsim.parse(f.readlines())
armsim.run()
assert armsim.reg['x0'] == 7, "ldr_str_test returned incorrect value of {}".format(armsim.reg['x0'])
armsim.reset()
with open('tests/brk_test.s','r') as f:
armsim.parse(f.readlines())
armsim.run()
assert armsim.reg['x0'] == 7, "brk_test returned incorrect value of {}".format(armsim.reg['x0'])
armsim.reset()
'''
Test the sort program
'''
with open('examples/sort.s','r') as f:
armsim.parse(f.readlines())
original = armsim.getdata('array')
armsim.run()
assert armsim.getdata('array') == sorted(original), "incorrect result produced after running sort.s"
assert armsim.getdata('sorted') == sorted(original), "incorrect result produced after running sort.s"
assert armsim.getdata('reverse') == sorted(original), "incorrect result produced after running sort.s"
assert armsim.getdata('nearly_sorted') == sorted(original), "incorrect result produced after running sort.s"
armsim.reset()
'''
collatz.s is currently the most complex program, so it's
worth having an automated test to make sure it's working
'''
#use StringIO to drive collatz program
stdin = sys.stdin
stdout = sys.stdout
#supress stdout
sys.stdout = StringIO()
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
sys.stdin = StringIO('37')
armsim.run()
assert armsim.reg['x0'] == 22, "collatz of 37 should not be {}".format(armsim.reg['x0'])
armsim.reset()
'''
Tests for check_static_rules()
'''
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
#test forbid_loops flag
armsim.forbid_loops = True
try:
armsim.check_static_rules()
assert False, "check_static_rules should raise error with collaz.s when loops forbidden"
except ValueError:
#expected
armsim.reset()
#test forbid_recursion flag
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
armsim.forbid_recursion = True
try:
sys.stdin = StringIO('37')
armsim.run()
assert False, "should raise error with collaz.s when recursion forbidden"
except ValueError:
#expected
armsim.reset()
#test require_recursion flag
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
armsim.require_recursion = True
try:
sys.stdin = StringIO('37')
armsim.run()
#expected
armsim.reset()
except ValueError:
assert False, "should NOT raise error with collaz.s when recursion required"
#forbidden instructions
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
armsim.forbidden_instructions = {'mov'}
try:
armsim.check_static_rules()
assert False, "check_static_rules should raise error with collaz.s when mov instr forbidden"
except ValueError:
#expected
armsim.reset()
#duplicate labels
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
armsim.asm.append('duplicate_label:')
armsim.asm.append('duplicate_label:')
try:
armsim.check_static_rules()
assert False, "check_static_rules should raise error when duplicate labels added"
except ValueError:
#expected
armsim.reset()
#branch to label that doesn't exist
with open('examples/collatz.s','r') as f:
armsim.parse(f.readlines())
armsim.asm.append('b not_a_label_in_program')
try:
armsim.check_static_rules()
assert False, "check_static_rules should raise error nonexistent label in branch"
except ValueError:
#expected
armsim.reset()
#restore to std io
sys.stdin = stdin
sys.stdout = stdout
print("All tests passed")