Repository navigation
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·139 lines (101 loc) · 4.04 KB
/
Copy pathrun_tests.py
File metadata and controls
executable file
·139 lines (101 loc) · 4.04 KB
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
#!/usr/bin/python3
import argparse
import pathlib
import sys
import subprocess
from typing import Tuple
PROGRAM_EXTENSION = '.lat'
IN_EXTENSION = '.input'
OUTPUT_EXTENSION = '.output'
LLVM_EXTENSION = '.ll'
BC_EXTENSION = '.bc'
ERROR = 'ERROR'
OK = 'OK'
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description='Runs tests for Latte Programming Language compiler. Folder \'good\' should contain programs that run without failure. Folder \'bad\' should contain programs with syntax, typecheck or runtime errors.')
parser.add_argument('--compiler', type=pathlib.Path,
default='latc_llvm', help='Compiler executable')
parser.add_argument('--good', type=pathlib.Path, nargs='*',
help='Folders with good exmaples, should contain `lat` and `output` files')
parser.add_argument('--bad', type=pathlib.Path, nargs='*',
help='Folders with bad exmaples')
return parser.parse_args()
def get_file_content(file_path: pathlib.Path) -> str:
with open(file_path, 'r') as f:
text = f.read()
return text
def compile_file(in_file: pathlib.Path, compiler: pathlib.Path) -> str:
print(f'Running {str(in_file):<80}', end='', flush=True)
compiler_process = subprocess.run(
[str(compiler.resolve()), str(in_file.resolve())], capture_output=True)
return compiler_process.stderr.decode()
def remove_compiled(in_file: pathlib.Path) -> None:
extensions_to_remove = [LLVM_EXTENSION, BC_EXTENSION, '']
for ext in extensions_to_remove:
ext_file = in_file.with_suffix(ext)
if ext_file.is_file():
ext_file.unlink()
def run_bad(folder: pathlib.Path, compiler: pathlib.Path) -> Tuple[int, int]:
correct = 0
total = 0
for file in folder.iterdir():
if file.suffix == PROGRAM_EXTENSION:
total += 1
compilation_result = compile_file(file, compiler)
if compilation_result.splitlines()[0] == ERROR:
correct += 1
print(f'Success {file}')
else:
print(f'Wrong {file}')
remove_compiled(file)
return correct, total
def run_file(file: pathlib.Path) -> bool:
input_file = file.with_suffix(IN_EXTENSION)
output_file = file.with_suffix(OUTPUT_EXTENSION)
bytecode_file = file.with_suffix(BC_EXTENSION)
executable = file.with_suffix('')
subprocess.run(['clang', '-o', executable, bytecode_file])
if input_file.is_file():
run_process = subprocess.run([executable], input=get_file_content(
input_file).encode(), capture_output=True)
else:
run_process = subprocess.run([executable], capture_output=True)
out = run_process.stdout.decode()
target_out = get_file_content(output_file)
return out == target_out
def run_good(folder: pathlib.Path, compiler: pathlib.Path) -> Tuple[int, int]:
correct = 0
total = 0
for file in folder.iterdir():
if file.suffix == PROGRAM_EXTENSION:
total += 1
compilation_result = compile_file(file, compiler)
if OK in compilation_result and run_file(file):
print(f'Success {file}')
correct += 1
else:
print(f'Wrong {file}')
remove_compiled(file)
return correct, total
def main() -> None:
args = parse_args()
compiler = args.compiler
correct, total = 0, 0
if args.bad is not None:
for folder in args.bad:
bad_correct, bad_total = run_bad(folder, compiler)
correct += bad_correct
total += bad_total
if args.good is not None:
for folder in args.good:
good_correct, good_total = run_good(folder, compiler)
correct += good_correct
total += good_total
print(f'Correct {correct} / {total}')
if __name__ == "__main__":
try:
main()
except FileNotFoundError as not_found:
print(
f'Directory/file \'{not_found.filename}\' does not exist', file=sys.stderr)