-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
52 lines (43 loc) · 1.29 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
import os, subprocess
# Settings
TEST_DIR = "." # Directory with our program
CODE_FILE = "main.c" # Our C code
COMPILER_TIMEOUT = 10.0 # Compiler timeout (seconds)
RUN_TIMEOUT = 10.0 # Run timeout (seconds)
# Create absolute paths
code_path = os.path.join(TEST_DIR, CODE_FILE)
app_path = os.path.join(TEST_DIR, "app")
# Compile the program
print ("Building...")
try:
ret = subprocess.run (["gcc", code_path, "-o", app_path],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
timeout = COMPILER_TIMEOUT)
except Exception as e:
print("ERROR: Compilation failed.", str(e))
exit(1)
# Parse output
output = ret.stdout.decode('utf-8')
print(output)
output = ret.stderr.decode('utf-8')
print(output)
# Check to see if the program compiled successfully
if ret.returncode !=0:
print("Compilaiton failed.")
exit(1)
# Run the compiled program
print("Running...")
try:
ret = subprocess.run([app_path],
stdout = subprocess.PIPE,
timeout = RUN_TIMEOUT)
except Exception as e:
print("ERROR: Runtime failed.", str(e))
exiit(1)
# Parse output
output = ret.stdout.decode('utf-8')
print("Output:", output)
# All tests passed! Exit gracefully
print("All tests passed!")
exit(0)