-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
258 lines (185 loc) · 6.5 KB
/
tests.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""
Unit tests for the micro compiler.
"""
import unittest
import os
from compiler import aux_routines
from compiler import sem_routines
from compiler.parser import Parser
# we mock our ExprRec and OpRec class here
class Record(object):
pass
class TestAuxRoutines(unittest.TestCase):
def setUp(self):
self.symbol_table = []
self.output_file = "output/op_code"
def test_generate(self):
test3, test4 = None, None
# First test with generate with 3 args
aux_routines.generate("DECLARE", "A", "Integer", s4=None, output_file=self.output_file)
with open(self.output_file, 'r') as f:
test3 = f.read()
os.remove(self.output_file)
# Now test generate with 4 args
aux_routines.generate("ADD", "314", "A", s4="Temp&1", output_file=self.output_file)
with open(self.output_file, 'r') as f:
test4 = f.read()
os.remove(self.output_file)
self.assertEqual(test3, "DECLARE A, Integer\n")
self.assertEqual(test4, "ADD 314, A, Temp&1\n")
def test_extract(self):
r1 = Record()
r2 = Record()
r1.name = "test"
r2.val = 1011
t1 = aux_routines.extract(r1)
t2 = aux_routines.extract(r2)
self.assertEqual(t1, "test")
self.assertEqual(t2, 1011)
def test_extract_op(self):
r1 = Record()
r2 = Record()
r1.op = "+"
r2.op = "-"
t1 = aux_routines.extract_op(r1)
t2 = aux_routines.extract_op(r2)
self.assertEqual(t1, "ADD")
self.assertEqual(t2, "SUB")
def test_look_up(self):
self.symbol_table.append("in_table")
t1 = aux_routines.look_up("in_table", self.symbol_table)
t2 = aux_routines.look_up("not_in_table", self.symbol_table)
self.assertEqual(t1, True)
self.assertEqual(t2, False)
def test_enter(self):
aux_routines.enter("in_table", self.symbol_table)
self.assertTrue("in_table" in self.symbol_table)
self.assertFalse("not_in_table" in self.symbol_table)
def test_check_id(self):
aux_routines.check_id("A", self.symbol_table)
t1 = None
self.assertTrue("A" in self.symbol_table)
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "DECLARE A, Integer\n")
def test_get_temp(self):
t1 = aux_routines.get_temp(self.symbol_table)
t2 = aux_routines.get_temp(self.symbol_table)
t3 = None
self.assertEqual(t1, "Temp&1")
self.assertEqual(t2, "Temp&2")
self.assertTrue(t1 in self.symbol_table)
self.assertTrue(t2 in self.symbol_table)
with open(self.output_file, 'r') as f:
t3 = f.read()
os.remove(self.output_file)
self.assertEqual(t3, "DECLARE Temp&1, Integer\nDECLARE Temp&2, Integer\n")
class TestSemRoutines(unittest.TestCase):
def setUp(self):
self.symbol_table = []
self.output_file = "output/op_code"
def test_start(self):
self.symbol_table = ["dirty", "symbol", "table"]
sem_routines.start(self.symbol_table)
self.assertFalse("dirty" in self.symbol_table)
def test_assign(self):
r1 = Record()
r2 = Record()
r3 = Record()
r1.name = "A"
r2.name = "Temp&1"
r3.val = 1011
t1 = None
sem_routines.assign(r1, r2)
sem_routines.assign(r1, r3)
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "STORE Temp&1, A\nSTORE 1011, A\n")
def test_read_id(self):
r1 = Record()
r1.name = "A"
t1 = None
sem_routines.read_id(r1)
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "READ A, Integer\n")
def test_write_expr(self):
r1 = Record()
r1.name = "A"
t1 = None
sem_routines.write_expr(r1)
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "WRITE A, Integer\n")
def test_get_infix(self):
r1 = Record()
r2 = Record()
r3 = Record()
o1 = Record()
r1.val = 314
r2.name = "A"
r3.name = "BB"
o1.op = "+"
t1 = None
sem_routines.get_infix(r1, o1, r2, self.symbol_table)
sem_routines.get_infix(r3, o1, r2, self.symbol_table)
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "DECLARE Temp&3, Integer\nADD 314, A, Temp&3\nDECLARE Temp&4, Integer\nADD BB, A, Temp&4\n")
def test_process_id(self):
r1 = Record()
t1 = None
sem_routines.process_id(r1, self.symbol_table)
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "DECLARE A, Integer\n")
self.assertEqual(r1.name, "A")
def test_proc_literal(self):
r1 = Record()
sem_routines.proc_literal(r1)
self.assertEqual(r1.val, 1011)
def test_process_op(self):
r1 = Record()
sem_routines.process_op(r1)
self.assertEqual(r1.op, "+")
def test_finish(self):
sem_routines.finish()
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
self.assertEqual(t1, "HALT")
class TestParser(unittest.TestCase):
def setUp(self):
self.input_file = "output/micro_code"
self.output_file = "output/op_code"
def test_system_goal(self):
t1 = None
with open( self.input_file, 'w') as f:
f.write('BEGIN A := BB + 314 + A; END$')
with open(self.input_file, 'r') as f:
p = Parser(f)
p.system_goal()
with open(self.output_file, 'r') as f:
t1 = f.read()
os.remove(self.output_file)
os.remove(self.input_file)
self.assertEqual(t1, "HALT")
def test_program(self):
t1 = None
with open( self.input_file, 'w') as f:
f.write('BEGIN A := BB + 314 + A; END$')
with open(self.input_file, 'r') as f:
p = Parser(f)
p.program()
os.remove(self.input_file)
#self.assertEqual() something?
def test_statement(self):
pass
if __name__ == '__main__':
unittest.main()