-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_compiler.py
187 lines (126 loc) · 4.83 KB
/
test_compiler.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
import unittest
from compile import Compiler
from pathlib import Path
import os, sys
from io import StringIO
import importlib
stdin_inputs = []
# Copied from: https://stackoverflow.com/a/45669280
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, "w")
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
# Copied from: https://stackoverflow.com/a/16571630
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
class TestProgram(unittest.TestCase):
def run_file(self, filename, inputs=[]):
global stdin_inputs
# Import the simulator
import utils.hmmm as hmmm_simulator
# It needs to be reloaded each time its used
importlib.reload(hmmm_simulator)
# Monkey patch the read function to use our custom function (instead of reading from terminal it reads from stdin_inputs)
def custom_op_read(args):
global stdin_inputs
if len(stdin_inputs) == 0:
raise Exception("No more inputs to read")
hmmm_simulator.registers[args[0]] = stdin_inputs.pop(0)
hmmm_simulator.implementations["read"] = custom_op_read
compiler = Compiler()
program = compiler.compile(str(Path("sample_files") / filename))
with HiddenPrints():
machine_code = hmmm_simulator.programToMachineCode(
hmmm_simulator.hmmmAssembler(program.to_array())
)
hmmm_simulator.convertMachineCode(machine_code)
if len(inputs) > 0:
stdin_inputs = []
for i in inputs:
stdin_inputs.append(i)
with Capturing() as output:
hmmm_simulator.runHmmm()
output = [int(x) for x in output]
return output
class TestMath(TestProgram):
FILENAME = "math.c"
def testA(self):
assert self.run_file(self.FILENAME) == [18]
class TestMaxOfThree(TestProgram):
FILENAME = "max_3.c"
def testA(self):
assert self.run_file(self.FILENAME, [0, 1, 2]) == [2]
def testB(self):
assert self.run_file(self.FILENAME, [1, 2470, 121]) == [2470]
class TestPosNegZero(TestProgram):
FILENAME = "pos_neg_zero.c"
def testA(self):
assert self.run_file(self.FILENAME, [300]) == [1]
def testB(self):
assert self.run_file(self.FILENAME, [0]) == [0]
def testC(self):
assert self.run_file(self.FILENAME, [-152]) == [-1]
class TestSumUpTo(TestProgram):
FILENAME = "sum_up_to.c"
def testA(self):
assert self.run_file(self.FILENAME, [3]) == [6]
def testB(self):
assert self.run_file(self.FILENAME, [10]) == [55]
class TestFibonacci(TestProgram):
FILENAME = "fibonacci.c"
def testA(self):
assert self.run_file(self.FILENAME, [0]) == []
def testB(self):
assert self.run_file(self.FILENAME, [10]) == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
class TestPrimesBetween(TestProgram):
FILENAME = "primes_between.c"
def testA(self):
assert self.run_file(self.FILENAME, [1, 9]) == [2, 3, 5, 7]
def testB(self):
assert self.run_file(self.FILENAME, [82, 98]) == [83, 89, 97]
class TestDoubleFunc(TestProgram):
FILENAME = "double_func.c"
def testA(self):
assert self.run_file(self.FILENAME, [24]) == [48, 6]
def testB(self):
assert self.run_file(self.FILENAME, [17]) == [34, 6]
class TestQuadrupleFunc(TestProgram):
FILENAME = "quadruple_func.c"
def testA(self):
assert self.run_file(self.FILENAME, [-3]) == [-12,-12]
def testB(self):
assert self.run_file(self.FILENAME, [4]) == [16,16]
class TestDivisibleByFunc(TestProgram):
FILENAME = "divisible_by.c"
def testA(self):
assert self.run_file(self.FILENAME, [10,5]) == [1,1]
def testB(self):
assert self.run_file(self.FILENAME, [10,7]) == [0,0]
class TestRecursiveFactorial(TestProgram):
FILENAME = "recursive_factorial.c"
def testA(self):
assert self.run_file(self.FILENAME, [2]) == [2,6]
def testB(self):
assert self.run_file(self.FILENAME, [5]) == [120,6]
class TestAckermann(TestProgram):
FILENAME = "ackermann.c"
def testA(self):
assert self.run_file(self.FILENAME, [1,1]) == [3]
def testB(self):
assert self.run_file(self.FILENAME, [1,2]) == [4]
def testC(self):
assert self.run_file(self.FILENAME, [2,1]) == [5]
def testD(self):
assert self.run_file(self.FILENAME, [2,2]) == [7]
if __name__ == "__main__":
unittest.main()