-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_test.py
96 lines (78 loc) · 2.75 KB
/
auto_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
import os
import math
from subprocess import PIPE, Popen
RELATIVE_TOLERANCE = 1e-6
ZERO_ISH = 1e-50
def get_sample_output(N=3, R=1, dt=0.001, i=10):
return open(f"auto_test_outputs/{N}_{R}_{dt}_{i}.cor").read()
def run_code(version, N=3, R=1, dt=0.001, i=10):
if version == "Python":
return os.popen(f"python3 md.py {N} {R} {dt} {i}").read()
return os.popen(f"./md_{version} {N} {R} {dt} {i}").read()
def compare_outputs(output, correct, n):
out_lines = output.split("\n")
cor_lines = correct.split("\n")
for i, (out, cor) in enumerate(list(zip(out_lines, cor_lines))[6 : 6 + n]):
diff = any(
math.isnan(float(o))
or (
abs(float(o)) > ZERO_ISH
if abs(float(c)) < ZERO_ISH
else abs((float(o) - float(c)) / float(c)) > RELATIVE_TOLERANCE
)
for o, c in zip(out.split(), cor.split())
)
if diff:
return (
f"First difference on line {i}\n"
+ f"Output: {out.strip()}\n"
+ f"Correct: {cor.strip()}\n"
+ (
f"Next out: {out_lines[7+i].strip()}\nNext cor: {cor_lines[7+i].strip()}"
if i < n - 1
else ""
)
)
return None
def main():
print("=== Compiling ===")
os.popen("make clean").read()
make_out, make_err = Popen(
"make", shell=True, stdout=PIPE, stderr=PIPE
).communicate()
if "error" in str(make_err):
print("Error while running 'make':")
print("STDOUT:")
print(make_out.decode())
print("\nSTDERR:")
print(make_err.decode())
exit()
warnings = (str(make_out) + str(make_err)).count("warning")
if warnings:
print(f"Make ran with ~{warnings} warnings")
tests = [
{"N": 3, "R": 1, "dt": 0.001, "i": 10},
{"N": 5, "R": 3, "dt": 0.001, "i": 100},
]
for imp in ["Python", "AoS", "SoA", "Opt"]:
print(f"\n=== Testing {imp} implementation ===")
for test in tests:
correct_output = get_sample_output(**test)
output = run_code(imp, **test)
if not output:
print(f"No {imp} implementation found.")
break
with open(
f"auto_test_outputs/{imp}_"
+ "_".join(str(test[x]) for x in ["N", "R", "dt", "i"])
+ ".out",
"w",
) as out_file:
out_file.write(output)
if compare_outputs(output, correct_output, n=test["i"]):
print(f"Difference on test {test}")
break
else:
print(f"{imp} looks good to me!")
if __name__ == "__main__":
main()