-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
338 lines (276 loc) · 10.8 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import argparse
import collections
import itertools
import json
import os
import re
import subprocess as sp
import sys
DEFAULT_TIME_LIMIT = 2
TEST_RESULT_TYPE = {
'pass': 'OK ',
'file_not_found': " ? ",
'compile_error': 'CE ',
'wrong_answer': 'WA ',
'runtime_error': 'RE ',
'time_limit_exceed': 'TLE',
'memory_limit_exceed': 'MLE',
}
MEMORY_LIMIT = 512
COMPILE_OUT = 'a.out'
RUN_OUT = 'b.out'
ERROR_OUT = 'e.out'
CPP_COMPILE_CMD = ['g++', '-O2', '-static', '-o', COMPILE_OUT, '']
RUN_CMD = {
'cpp': ['/usr/bin/time', '--verbose', './' + COMPILE_OUT],
'py': ['/usr/bin/time', '--verbose', 'python3', '', '<', '']
}
COMPARE_CMD = ['diff', '-Z', RUN_OUT, '']
TEST_HARNESS_CMD = ['python3', '', '', RUN_OUT, '']
FAIL_SCRIPT = 'Test Failed.\n Error Code: {}\n'
class _TestEntry:
__slots__ = {'setid', 'caseid', 'fin', 'fout', 'rtype', 'time', 'memory'}
def __init__(self, setid, caseid):
self.setid = setid
self.caseid = caseid
self.fin = self.fout = self.rtype = self.time = self.memory = None
def __lt__(self, other):
if self.setid != other.setid:
return self.setid < other.setid
return self.caseid < other.caseid
def __repr__(self):
rep = {
'setid': self.setid,
'caseid': self.caseid,
'fin': self.fin,
'fout': self.fout,
'rtype': self.rtype,
'time': self.time,
'memory': self.memory
}
return json.dumps(rep, separators=(',', ':'), indent=2)
def find_test_cases(folder):
files = os.listdir(folder)
entry_dict = {}
for f in files:
name = f
if f.endswith('.in'):
out = False
f = f[:-3]
elif f.endswith('.out'):
out = True
f = f[:-4]
else:
continue
if f in entry_dict:
if not entry_dict[f].fout:
entry_dict[f].fout = os.path.join(folder, name)
else:
entry_dict[f].fin = os.path.join(folder, name)
else:
i = f.find('.')
f = f[i+1:]
if '.' in f:
i = f.find('.')
setid, caseid = f[:i], f[i+1:]
elif '-' in f:
i = f.find('-')
setid, caseid = f[:i], f[i+1:]
elif f.startswith('sample'):
setid, caseid = 'sample', f[6:]
if not caseid:
caseid = 1
elif f.startswith('samp'):
setid, caseid = 'sample', f[4:]
if not caseid:
caseid = 1
elif re.match(r'(\d+)([a-z])', f):
setid, caseid = f[:-1], ord(f[-1]) - ord('a') + 1
else:
setid, caseid = f, 1
setid = int(setid) if setid != 'sample' else 0
caseid = int(caseid) if caseid != 'sample' else 1
entry = _TestEntry(setid, caseid)
if out:
entry.fout = os.path.join(folder, name)
else:
entry.fin = os.path.join(folder, name)
entry_dict[name[:name.rfind('.')]] = entry
entries = sorted(list(entry_dict.values()))
group, setid = [], ''
for entry in entries:
if entry.setid != setid:
group.append({
'setid': entry.setid,
'cases': []
})
setid = entry.setid
group[-1]['cases'].append(entry)
return group
def compile(src):
if src.endswith('.py'):
RUN_CMD['py'][-3] = src
return True, []
CPP_COMPILE_CMD[-1] = src
proc = sp.Popen(CPP_COMPILE_CMD, stderr=sp.PIPE)
err = [line.decode('utf-8').strip() for line in proc.stderr]
success = os.path.isfile(COMPILE_OUT)
return success, err
def run(case_group, lang, limit):
total = passed = 0
cmd = RUN_CMD[lang]
for group in case_group:
setid = 'sample' if group['setid'] == 0 else group['setid']
print(f'Test Set {setid}')
success = True
for case in group['cases']:
if args.early_stop and not success:
print(f'Case #{case.caseid:02d}: -- | Runtime: --, Memory: --')
continue
stats = {}
proc = None
with open(case.fin, 'r') as fin, open(RUN_OUT, 'w') as fout, open(ERROR_OUT, 'w') as ferr:
try:
if lang == 'py':
cmd[-1] = case.fin
proc = sp.run(cmd, stdin=fin, stdout=fout, stderr=ferr, timeout=limit+1)
except sp.TimeoutExpired:
stats['time'] = limit+1
stats['memory'] = -1
with open(ERROR_OUT, 'r') as f:
for line in f:
line = line.strip()
if line.startswith('User time'):
stats['time'] = float(line[line.rfind(':')+1:])
elif line.startswith('Maximum resident set size'):
stats['memory'] = float(line[line.rfind(':')+1:]) / 1000
case.time = stats['time']
case.memory = stats['memory']
if proc is not None and proc.returncode != 0:
if proc.stderr:
print(proc.stderr.decode('utf-8'))
if proc.stdout:
print(proc.stderr.decode('utf-8'))
case.rtype = TEST_RESULT_TYPE['runtime_error']
elif stats['time'] > limit:
case.rtype = TEST_RESULT_TYPE['time_limit_exceed']
elif stats['memory'] > MEMORY_LIMIT:
case.rtype = TEST_RESULT_TYPE['memory_limit_exceed']
else:
ok = test(case)
case.rtype = TEST_RESULT_TYPE['pass'] if ok else TEST_RESULT_TYPE['wrong_answer']
ok = case.rtype == TEST_RESULT_TYPE['pass']
success &= ok
if case.rtype == TEST_RESULT_TYPE['time_limit_exceed']:
time = f'{limit:.2f}+'
memory = ' -- '
else:
time = f'{case.time:.2f}s'
memory = f'{case.memory:.3f}'
print(f'Case #{case.caseid:02d}: {case.rtype} | Runtime: {time}, Memory: {memory}MB')
print()
group['status'] = 'Pass' if success else 'Fail'
if setid != 'sample':
total += 1
if success:
passed += 1
print(f'Total {passed}/{total} test sets passed.')
def test(case):
fout = case.fout
if args.tol != -1:
return _test_num_close(RUN_OUT, fout)
elif args.harness:
return _test_harness(args.harness, case.fin, RUN_OUT, fout)
else:
return _test_diff(fout)
def _test_diff(sol):
COMPARE_CMD[-1] = sol
proc = sp.run(COMPARE_CMD, stdout=sp.PIPE, stderr=sp.PIPE)
if proc.stdout:
print(proc.stdout.decode('utf-8'))
if proc.stderr:
print(proc.stderr.decode('utf-8'))
return not proc.stderr and not proc.stdout
def _test_num_close(ans, sol):
with open(ans, 'r') as fans, open(sol, 'r') as fsol:
for la, ls in itertools.zip_longest(iter(fans), iter(fsol), fillvalue=''):
if not la or not ls:
return False
try:
la, ls = float(la), float(ls)
if abs(la - ls) > args.tol:
return False
except Exception:
return False
return True
def _test_harness(test_file, input_file, ans, sol):
TEST_HARNESS_CMD[1] = test_file
TEST_HARNESS_CMD[2] = input_file
TEST_HARNESS_CMD[-1] = sol
proc = sp.run(TEST_HARNESS_CMD, stdout=sp.PIPE, stderr=sp.PIPE)
if proc.stdout:
print(proc.stdout.decode('utf-8'))
if proc.stderr:
print(proc.stderr.decode('utf-8'))
return not proc.stderr and not proc.stdout
def print_result(case_group):
# not used
total = ok = 0
for group in case_group:
setid = 'Sample' if group['setid'] == 0 else group['setid']
print(f'Test Set {setid}: {group["status"]}')
if setid != 'Sample':
total += 1
if (group['status'] == 'Pass'):
ok += 1
for case in group['cases']:
print(f'Case #{case.caseid}: {case.rtype} | Runtime: {case.time:.2f}s, Memory: {case.memory:.3f}MB')
print()
print(f'Total {ok}/{total} test sets passed.')
def cleanup(compile_file=True):
if compile_file and os.path.exists(COMPILE_OUT):
os.remove(COMPILE_OUT)
if os.path.exists(RUN_OUT):
os.remove(RUN_OUT)
if os.path.exists(ERROR_OUT):
os.remove(ERROR_OUT)
def check(src, test_folder, lang, limit):
# cleanup old files
cleanup()
# file src file
if not os.path.isfile(src):
print(f"File {os.path.abspath(src)} does not exist.")
# list test cases
if not os.path.isdir(test_folder):
print(f"Path {os.path.abspath(test_folder)} does not exist or is not a folder.")
case_group = find_test_cases(test_folder)
# compile src if needed
success, error = compile(src)
if error:
for line in error:
print(line)
if not success:
print(FAIL_SCRIPT.format(TEST_RESULT_TYPE['compile_error']))
sys.exit(1)
# run src on test cases
run(case_group, lang, limit)
# cleanup, keep compile files in case manual debugging is needed
cleanup(args.cleanup)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--src', '-s', type=str, help='source program file', required=True)
parser.add_argument('--data', '-d', type=str, help='test folder', required=True)
parser.add_argument('--lang', '-l', type=str, help='language used. Now support cpp and py', required=True)
parser.add_argument('--tol', '-t', type=float, help='Threshold to numeric answers.', required=False, default=-1)
parser.add_argument('--harness', '-H', type=str, help='program to test correctness', required=False)
parser.add_argument('--timeout', '-T', type=float, help='timeout limit on each test case.', required=False, default=2)
parser.add_argument('--cleanup', '-c', type=str, help='delete compiled file after finishing the testing', required=False, default='')
parser.add_argument('--early-stop', '-e', type=str, help='stop current test set when one case if failed', default='')
args = parser.parse_args()
if args.lang not in {'cpp', 'py'}:
print('Language only support cpp or py now.')
sys.exit(1)
if args.tol != -1 and args.harness:
print('tol and harness cannot be set together.')
sys.exit(1)
check(args.src, args.data, args.lang, args.timeout)