Skip to content

Instantly share code, notes, and snippets.

@Hikari9
Last active March 11, 2017 03:48
Show Gist options
  • Save Hikari9/359876f0e8755a632a606c75179c77ba to your computer and use it in GitHub Desktop.
Save Hikari9/359876f0e8755a632a606c75179c77ba to your computer and use it in GitHub Desktop.
Judge tester
import sys
import os
import os.path
import time
JUDGE_INPUT_FORMATS = ['judge%04d.in', 'judge%d.in']
JUDGE_OUTPUT_FORMATS = ['judge%04d.ans', 'judge%d.ans']
TEST_OUTPUT_FORMAT = 'judge%04d.test.out'
def compare_files(a, b):
with open(a, 'r') as fa:
with open(b, 'r') as fb:
f1 = fa.readlines()
f2 = fb.readlines();
if len(f1) == len(f2):
for x, y in zip(f1, f2):
if x != y:
return False
else:
return False
return True
def main(args):
if len(args) != 2:
print("Usage: python -m py_compile tester.py <*.(java|cpp)> <io directory>")
return
program, iodir = args
test = 1
executor = ''
if program[-3:] == 'cpp':
print('Compiling...')
os.system('g++ -lm %s' % program)
executor = ('a < %s > %s')
if program[-4:] == 'java':
print('Compiling...')
os.system('javac %s' % program)
executor = ('java %s ' % program[:-5]) + '< %s > %s'
else:
executor = ('python %s ' % program) + '< %s > %s'
failed = set()
max_time_elapsed = 0
passed = 0
while True:
judgein = ''
judgeout = ''
testout = '%s/%s' % (iodir, TEST_OUTPUT_FORMAT) % test
for input_format in JUDGE_INPUT_FORMATS:
judgein = '%s/%s' % (iodir, input_format) % test
if os.path.isfile(judgein):
break
for output_format in JUDGE_OUTPUT_FORMATS:
judgeout = '%s/%s' % (iodir, output_format) % test
if os.path.isfile(judgeout):
break
if judgein == '' or judgeout == '':
break
exe = executor % (judgein, testout)
time_before = time.clock()
os.system(exe)
time_elapsed = time.clock() - time_before
max_time_elapsed = max(time_elapsed, max_time_elapsed)
if compare_files(judgeout, testout):
print('passed %d (%.3f seconds)' % (test, time_elapsed))
passed += 1
else:
print('FAILED %d (%.3f seconds)' % (test, time_elapsed))
failed.add(test)
test += 1
if test == 1:
print('No test cases!')
else:
print("Passed cases: %d/%d" % (passed, test-1))
print("Failed cases: %s" % list(failed))
print("Max time elapsed: %.3lf" % max_time_elapsed)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment