Skip to content

Instantly share code, notes, and snippets.

@ZhanruiLiang
Created January 13, 2012 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZhanruiLiang/1604721 to your computer and use it in GitHub Desktop.
Save ZhanruiLiang/1604721 to your computer and use it in GitHub Desktop.
A script for the poor men who failed to finish all the problems in algorithm exam.
#! /usr/bin/python2
# Author: Ray
# Date: Fir Jan 13, 13:00
#
from subprocess import Popen, PIPE
import sys
from time import time as tick
inFile = 'in3'
if len(sys.argv) < 5:
print """\
usage: genNtest.py PROG1 PROG2 PROG_GEN N
The genNtest will run the progrem `PROG_GEN` to generate input data, then
feed it to `PROG1` and collect the output as your output. Then feed the
same data to `PROG2` and collect the standard output. Finally compare
the outputs. The procedure will repeat `N` times.
"""
exit(0)
prog1 = sys.argv[1]
prog2 = sys.argv[2]
progGen = sys.argv[3]
n = int(sys.argv[4])
def run(s):
if not s.startswith('./'):
s = './'+s
t1 = tick()
p = Popen(s, shell=True, stdout=PIPE, stdin=open(inFile, 'r'))
p.wait()
t2 = tick()
print "%s cost time(ms): %d" %(s, (t2-t1)*1000)
return p.stdout.read()
for i in xrange(n):
print 'Test case %d' % (i,)
if not progGen.startswith('./'):
progGen = './' + progGen
pGen = Popen(progGen, shell=True, stdout=open(inFile, 'w'))
pGen.wait()
ans = ''.join(run(prog1) or [''])
ansRight = ''.join(run(prog2) or [''])
if ans != ansRight:
print 'Wrong Answer'
print 'input:\n %s\nprog1: \n%s\n prog2: \n%s\n' %(open(inFile, 'r').read(), ans, ansRight)
if ans.split() == ansRight.split():
print 'Presentation Error'
ch = raw_input('Continue? [y/N]')
if ch not in ['y','Y']:
exit(0)
else:
print 'Accepted'
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment