-
Notifications
You must be signed in to change notification settings - Fork 3
/
judge.py
executable file
·91 lines (85 loc) · 2.96 KB
/
judge.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
#!/usr/bin/python
import sys
import re
import os
import sh
import argparse
from collections import defaultdict
def load_programs(project):
r = dict();
with open(project + '/inputs.in') as f:
for line in f:
pair = re.split("[ \t]+", line.strip());
if len(pair) != 2:
continue
r[pair[0]] = project + '/' + pair[1]
return r
if __name__ == '__main__':
args_p = argparse.ArgumentParser('Testovac kompilatorov')
args_p.add_argument('project', default='c++', nargs='?',
help='Ktory projekt otestovat.')
args_p.add_argument('programs', type=str, nargs='*',
help='Ktore programy spustit.',
default=['primes', 'sort', 'graph'])
args = args_p.parse_args()
project = args.project
programs = args.programs
if len(sys.argv) >= 2:
project = sys.argv[1]
if len(sys.argv) > 2:
programs = sys.argv[2:]
files = load_programs(project)
ret = os.system(project + '/build.sh')
if ret != 0:
print("build ends with non-zero status");
prog_stat = defaultdict(lambda:defaultdict(int))
prog_bad = defaultdict(list)
for program in programs:
# compile program
binary = str(sh.tempfile()).strip()
ret = os.system(project + '/compile.sh ' + files[program] + ' ' + binary)
if ret != 0:
print("Unable to compile!")
# get testsi
path = 'test/' + program + '/in'
tests = [
f.strip()
for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f))
]
# run tests
tempfile = sh.tempfile().strip()
for test in tests:
run = './{project}/run.sh'.format(
project=project,
)
sh.Command(run)(
binary,
_in = open('./test/{program}/in/{test}'.format(
program=program,
test=test,
)),
_out = tempfile,
)
out_correct = './test/{program}/out/{test}'.format(
program=program,
test=test,
)
same = True
try:
ret = sh.diff(tempfile, out_correct, _ok_code=range(0,2))
if ret.exit_code == 1:
print('Tvoj program dal zly vysledok v ulohe {} na priklade {}:\n{}'.format(program, test, ret))
same = False
prog_bad[program].append(test)
except sh.ErrorReturnCode:
print('UGH: toto by sa nemalo stat.')
exit(1)
prog_stat[program][same] += 1
for program in programs:
print('Program {}: {}/{}, chybne vysledky na vstupoch: {}'.format(
program,
prog_stat[program][True],
prog_stat[program][True] + prog_stat[program][False],
prog_bad[program],
))