Skip to content

Instantly share code, notes, and snippets.

@Kyly
Forked from Antrikshy/tester-pa3.py
Last active August 29, 2015 14:09
Show Gist options
  • Save Kyly/ea8c600e97650ddd18db to your computer and use it in GitHub Desktop.
Save Kyly/ea8c600e97650ddd18db to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os, sys, subprocess, time
import filecmp as fc
# Get the total number of args passed to the demo.py
argc = len(sys.argv)
usage = " Usage: ./tester-pa3.py <testsDirectoryName>"
# Check if correct number of args where given
if argc != 2:
print "Must enter directory name containing test files."
print usage
sys.exit()
# Get the arguments list
cmdargs = str(sys.argv[1])
testDir = "./" + cmdargs
testFile = cmdargs
# Opening files
netplanOut = open('tester-user-out', 'w')
refnetplanOut = open('tester-ref-out', 'w')
# Clearing and opening log file
open('pa3-tester-log', 'w').close()
testLog = open('pa3-tester-log', 'a')
# Used in the end to print the ugly 'failed' message
didFail = False
print " ------------------------------------------"
print " | Welcome to Antriksh and Kyly's tester! |"
print " ------------------------------------------"
print ""
try:
subprocess.check_call(['make'])
except subprocess.CalledProcessError:
print "Could not 'make' for some reason. Exiting..."
sys.exit()
print ""
# Error checking for missing directory etc.
try:
if os.listdir(testDir) == []:
print testDir + " is empty. Add some sample input files to it.\n"
sys.exit()
except OSError:
print "Do you have a directory called "+ testDir + "?"
print "If not, make one and put sample input files in it.\n"
sys.exit()
failList = []
# Loops through all testcases
for testFileName in os.listdir( testDir ):
testFilePath = os.path.join( testFile, testFileName)
# Tests user's program
print "Testing " + testFileName + " on ./netplan..."
start = time.time()
uOut = subprocess.check_output(['./netplan', testFilePath])
userTime = time.time() - start
print "Running time: %.3f sec" % userTime
# Tests reference program
print "Testing " + testFileName + " on ./refnetplan..."
start = time.time()
rOut = subprocess.check_output(['./refnetplan', testFilePath])
refTime = time.time() - start
print "Running time: %.3f sec" % refTime
# Prints time difference
print "Time difference (your time - ref time): %.3f" % (userTime - refTime)
testLog.write('---------------------------------\n\n')
# Conclusion for current test (to stdout and log)
if uOut == rOut:
testLog.write('PASS: ' + testFileName + '\nTime difference: ' + str(userTime - refTime) + '\n')
print "Test passed.\n"
else:
testLog.write('FAIL: ' + testFileName + '\n')
didFail = True
failList.append( testFileName )
print "Test failed.\n"
# Write outputs to log
testLog.write('\nUser output:\n')
testLog.write(uOut)
testLog.write('\nRef output:\n')
testLog.write(rOut)
testLog.write('---------------------------------\n\n')
# Prints fail/success messages
if didFail:
print "%d test cases failed. Test log saved in 'pa3-tester-log'." % len(failList)
print ""
print "Test case(s) that failed: "
for failed in failList:
print " " + failed
print ""
print "Keep in mind that you do not have to expect disconnected graphs."
print "In case of ties for lowest edge weights, they will accept any one of the possible MSTs.\n"
print "Any negative time differences mean your netplan was faster than refnetplan."
else:
print "All tests passed. You rock! Test log saved in 'pa3-tester-log'.\n"
print "Any negative time differences mean your netplan was faster than refnetplan."
testLog.write('\n')
testLog.close()
sys.exit()
@Kyly
Copy link
Author

Kyly commented Nov 17, 2014

Added time and cmd line file name for test directory.

@Kyly
Copy link
Author

Kyly commented Nov 22, 2014

Added rounding for times and list of failed test cases at the end.

@Kyly
Copy link
Author

Kyly commented Nov 22, 2014

Instead of writing output from netplan and refnetplan output is saved to string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment