forked from OI-wiki/OI-wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
61 lines (54 loc) · 1.66 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
import os
import sys
import json
annotations = []
def generate_annotations_and_exit(file, message):
print(f"::error file={file},line={1},col={1}::{message}")
sys.exit(1)
def test(cppname):
name = cppname[:cppname.rfind('.')]
num = name.rfind('/')
content = name[:num]
filename = name[num:]
# 文件名
cpp = name+'.cpp'
indata = name+'.in'
ansdata = name+'.ans'
outdata = name+'.out'
skiptest = name+'.skip_test'
indata = indata.replace('code', 'examples')
outdata = outdata.replace('code', 'examples')
ansdata = ansdata.replace('code', 'examples')
# 判断测试是否要执行
if os.path.exists(skiptest):
print(cpp + ' test skipped')
return
cmd = 'g++ -std=c++17 '+cpp+' -o '+name
# 判断CE
if os.system(cmd) == 0:
print(cpp+' Successfully compiled')
else:
print(cpp+' Compiled Error')
generate_annotations_and_exit(cpp, 'Compiled Error')
# 运行程序并重定向输出
cmd = content+'/.'+filename+' <'+indata+'> '+outdata
os.system(cmd)
# 判断RE
if os.system(cmd) == 0:
print(cpp+' Run successfully')
else:
print(cpp+' Runtime Error')
generate_annotations_and_exit(cpp, 'Runtime Error')
# 判断答案
cmd = 'diff -b -B '+outdata+' '+ansdata
if os.system(cmd) == 0:
print(cpp+' Successfully passed the test')
else:
print(cpp + ' Wrong Answer')
generate_annotations_and_exit(cpp, 'Wrong Answer')
filename = "res.txt"
with open(filename) as file_object:
lines = file_object.readlines()
for line in lines:
for filename in line.split(' '):
test(filename)