-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
220 lines (195 loc) · 7 KB
/
test.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
from pathlib import Path
import argparse
import csv
import os
import re
import subprocess
import sys
import time
import traceback
CIRC_IMPORT_REGEX = re.compile(r"desc=\"file#([^\"]+?\.circ)\"")
proj_dir_path = Path(__file__).parent
tests_dir_path = proj_dir_path / "tests"
logisim_path = proj_dir_path / "tools" / "logisim-evolution.jar"
known_imports_dict = {
"cpu/cpu.circ": [
"cpu/alu.circ",
"cpu/branch-comp.circ",
"cpu/control-logic.circ",
"cpu/csr.circ",
"cpu/imm-gen.circ",
"cpu/regfile.circ",
],
"harnesses/alu-harness.circ": [
"cpu/alu.circ",
],
"harnesses/csr-harness.circ": [
"cpu/cpu.circ",
"cpu/mem.circ",
],
"harnesses/cpu-harness.circ": [
"cpu/cpu.circ",
"cpu/mem.circ",
],
"harnesses/regfile-harness.circ": [
"cpu/regfile.circ",
],
"harnesses/run.circ": [
"harnesses/cpu-harness.circ",
],
"tests/part-a/alu/*.circ": [
"cpu/alu.circ",
],
"tests/part-a/regfile/*.circ": [
"cpu/regfile.circ",
],
"tests/part-b/sanity/cpu-csr*.circ": [
"harnesses/csr-harness.circ",
],
"tests/part-b/sanity/*.circ": [
"harnesses/cpu-harness.circ",
],
}
known_imports_dict["tests/part-a/addi/*.circ"] = known_imports_dict["tests/part-b/sanity/*.circ"]
known_imports_dict["tests/part-b/custom/*.circ"] = known_imports_dict["tests/part-b/sanity/*.circ"]
class TestCase():
def __init__(self, circ_path, name=None):
self.circ_path = Path(circ_path)
self.id = str(self.circ_path)
self.name = name or self.circ_path.stem
def can_pipeline(self):
if self.circ_path.match("alu/*.circ") or self.circ_path.match("regfile/*.circ"):
return False
return True
def fix_circ(self):
fix_circ(self.circ_path)
def get_actual_table_path(self):
return self.circ_path.parent / "student-output" / f"{self.name}-student.out"
def get_expected_table_path(self, pipelined=False):
path = self.circ_path.parent / "reference-output" / f"{self.name}-ref.out"
if pipelined:
path = path.with_name(f"{self.name}-pipelined-ref.out")
return path
def run(self, pipelined=False):
self.fix_circ()
if pipelined and not self.can_pipeline():
pipelined = False
passed = False
proc = None
try:
proc = subprocess.Popen(["java", "-jar", str(logisim_path), "-tty", "table,binary,csv", str(self.circ_path)], stdout=subprocess.PIPE, encoding="utf-8", errors="ignore")
with self.get_expected_table_path(pipelined=pipelined).open("r", encoding="utf-8", errors="ignore") as expected_file:
passed = self.check_output(proc.stdout, expected_file)
kill_proc(proc)
if not passed:
return (False, "Did not match expected output")
return (True, "Matched expected output")
except KeyboardInterrupt:
kill_proc(proc)
sys.exit(1)
except:
traceback.print_exc()
kill_proc(proc)
return (False, "Errored while running test")
def check_output(self, actual_file, expected_file):
passed = True
actual_csv = csv.reader(actual_file)
expected_csv = csv.reader(expected_file)
actual_lines = []
while True:
actual_line = next(actual_csv, None)
expected_line = next(expected_csv, None)
if expected_line is None:
break
if actual_line != expected_line:
passed = False
if actual_line is None:
break
actual_lines.append(actual_line)
output_path = self.get_actual_table_path()
output_path.parent.mkdir(parents=True, exist_ok=True)
with output_path.open("w") as output_file:
output_csv = csv.writer(output_file, lineterminator="\n")
for line in actual_lines:
output_csv.writerow(line)
return passed
def fix_circ(circ_path):
circ_path = circ_path.resolve()
for glob, known_imports in known_imports_dict.items():
if circ_path.match(glob):
old_data = None
data = None
is_modified = False
with circ_path.open("r", encoding="utf-8") as test_circ:
old_data = test_circ.read()
data = old_data
for match in re.finditer(CIRC_IMPORT_REGEX, old_data):
import_path_str = match.group(1)
import_path = (circ_path.parent / Path(import_path_str)).resolve()
for known_import in known_imports:
if import_path.match(known_import):
known_import_path = (proj_dir_path / known_import).resolve()
expected_import_path = Path(os.path.relpath(known_import_path, circ_path.parent))
if import_path_str != expected_import_path.as_posix():
print(f"Fixing bad import {import_path_str} in {circ_path.as_posix()} (should be {expected_import_path.as_posix()})")
data = data.replace(import_path_str, expected_import_path.as_posix())
is_modified = True
break
else:
expected_import_path = Path(os.path.relpath(import_path, circ_path.parent))
if import_path_str != expected_import_path.as_posix():
print(f"Fixing probably bad import {import_path_str} in {circ_path.as_posix()} (should be {expected_import_path.as_posix()})")
data = data.replace(import_path_str, expected_import_path.as_posix())
is_modified = True
if is_modified:
with circ_path.open("w", encoding="utf-8") as test_circ:
test_circ.write(data)
break
def run_tests(search_paths, pipelined=False):
circ_paths = []
for search_path in search_paths:
if search_path.is_file() and search_path.suffix == ".circ":
circ_paths.append(search_path)
continue
for circ_path in search_path.rglob("*.circ"):
circ_paths.append(circ_path)
circ_paths = sorted(circ_paths)
for circ_path in proj_dir_path.rglob("cpu/*.circ"):
fix_circ(circ_path)
for circ_path in proj_dir_path.rglob("harnesses/*.circ"):
fix_circ(circ_path)
failed_tests = []
passed_tests = []
for circ_path in circ_paths:
test = TestCase(circ_path)
did_pass, reason = False, "Unknown test error"
try:
did_pass, reason = test.run(pipelined=pipelined)
except KeyboardInterrupt:
sys.exit(1)
except SystemExit:
raise
except:
traceback.print_exc()
if did_pass:
print(f"PASS: {test.id}", flush=True)
passed_tests.append(test)
else:
print(f"FAIL: {test.id} ({reason})", flush=True)
failed_tests.append(test)
print(f"Passed {len(passed_tests)}/{len(failed_tests) + len(passed_tests)} tests", flush=True)
def kill_proc(proc):
if proc.poll() is None:
proc.terminate()
for _ in range(10):
if proc.poll() is not None:
return
time.sleep(0.1)
if proc.poll() is None:
proc.kill()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run Logisim tests")
parser.add_argument("test_path", help="Path to a test circuit, or a directory containing test circuits", type=Path, nargs="+")
parser.add_argument("-p", "--pipelined", help="Check against reference output for 2-stage pipeline (when applicable)", action="store_true", default=False)
args = parser.parse_args()
run_tests(args.test_path, args.pipelined)