-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.py
More file actions
120 lines (101 loc) · 3.97 KB
/
test.py
File metadata and controls
120 lines (101 loc) · 3.97 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
import os
import subprocess
import pytest
tests = os.listdir("tests")
try:
os.mkdir("tests/results")
except FileExistsError:
tests.remove("results")
try:
os.mkdir("tests/temp")
except FileExistsError:
tests.remove("temp")
if subprocess.getstatusoutput("fasm -v")[0] == 0: # Check if fasm is installed
print("Please install Flat Assembler (Fasm)")
exit(1)
@pytest.mark.parametrize("test_name", tests)
def test(test_name):
with open(f"tests/{test_name}", "r") as f:
test = f.read()
parts = test.split("\n:\n")
with open(f"tests/temp/code_{test_name}.cn", "w") as f:
f.write(parts[0])
with open(f"tests/temp/stdin_{test_name}", "w") as f:
if len(parts) > 2:
f.write(parts[2])
exp_stdout = parts[1]
if len(parts) > 3:
exp_stderr = parts[3].format(source_file=f"tests/temp/code_{test_name}")
else:
exp_stderr = ""
subprocess.run(
[
"python", "cont.py", f"tests/temp/code_{test_name}.cn",
"-t", "fasm_x86_64_linux",
"-i", f"tests/temp/stdin_{test_name}",
"-e", f"tests/results/{test_name}_stderr",
"--stdout", f"tests/results/{test_name}_stdout",
"-r",
]
)
os.remove(f"tests/temp/code_{test_name}.cn")
os.remove(f"tests/temp/stdin_{test_name}")
try:
os.remove(f"tests/temp/code_{test_name}.asm")
os.remove(f"tests/temp/code_{test_name}")
except FileNotFoundError:
pass
with open(f"tests/results/{test_name}_stdout", "r") as f:
stdout = f.read()
with open(f"tests/results/{test_name}_stderr", "r") as f:
stderr = f.read()
assert stdout == exp_stdout
assert stderr == exp_stderr
if subprocess.getstatusoutput("node -v")[0] != 0:
print("[OPTIONAL] Cannot run tests for wasm, please install node.js.")
else:
@pytest.mark.parametrize("test_name", tests)
def test_node_wat64(test_name):
with open(f"tests/{test_name}", "r") as f:
test = f.read()
parts = test.split("\n:\n")
with open(f"tests/temp/code_{test_name}_wasm.cn", "w") as f:
f.write(parts[0])
with open(f"tests/temp/stdin_{test_name}_wasm", "w") as f:
if len(parts) > 2:
f.write(parts[2])
exp_stdout = parts[1]
if len(parts) > 3:
exp_stderr = parts[3].format(source_file=f"tests/temp/code_{test_name}_wasm")
else:
exp_stderr = ""
subprocess.run(
[
"python", "cont.py", f"tests/temp/code_{test_name}_wasm.cn",
"-t", "wat64",
"-o", f"tests/temp/code_{test_name}",
"-e", f"tests/results/{test_name}_stderr_wasm",
"--stdout", f"tests/results/{test_name}_stdout_wasm",
]
)
if os.path.isfile(f"tests/temp/code_{test_name}.wasm"):
with open(f"tests/results/{test_name}_stdout_wasm", "a") as stdout:
with open(f"tests/results/{test_name}_stderr_wasm", "a") as stderr:
with open(f"tests/temp/stdin_{test_name}_wasm", "r") as stdin:
subprocess.run(
["node", "test.js", test_name],
stdout=stdout, stderr=stderr, stdin=stdin
)
os.remove(f"tests/temp/code_{test_name}_wasm.cn")
os.remove(f"tests/temp/stdin_{test_name}_wasm")
try:
os.remove(f"tests/temp/code_{test_name}.wat")
os.remove(f"tests/temp/code_{test_name}.wasm")
except FileNotFoundError:
pass
with open(f"tests/results/{test_name}_stdout_wasm", "r") as f:
stdout = f.read()
with open(f"tests/results/{test_name}_stderr_wasm", "r") as f:
stderr = f.read()
assert stdout == exp_stdout
assert stderr == exp_stderr