Created
February 6, 2012 14:57
-
-
Save sentientwaffle/1752538 to your computer and use it in GitHub Desktop.
CS235 Test runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'term/ansicolor' | |
include Term::ANSIColor | |
dir = ARGV[0].gsub /\/$/, "" | |
def compile(d) | |
`(cd #{d} && g++ *.cpp)` # -Wall | |
return $? == 0 | |
end | |
class TestCase | |
attr_reader :output, :dir, :num | |
def initialize(num, dir) | |
@num = num | |
@dir = dir | |
@inFiles = [] | |
@outFile = nil | |
end | |
def in(file) | |
@inFiles.push file | |
end | |
def out(file) | |
@outFile = file | |
end | |
def expected | |
@expected ||= File.open(@outFile, "rb").read | |
end | |
def run(executable) | |
resultPath = "#{@dir}/results.txt" | |
puts "(./#{executable} #{ @inFiles.sort().join(" ") } #{resultPath})" | |
`(./#{executable} #{ @inFiles.sort().join(" ") } #{resultPath})` | |
@output = File.open(resultPath, "rb").read | |
if expected == @output | |
pass | |
else | |
fail | |
end | |
end | |
private | |
def pass | |
print green "[#{num}]" | |
end | |
def fail | |
puts "" | |
print "[#{num}] " | |
puts (("=" * 25) + " Expected " + ("=" * 30)).red | |
puts expected | |
print "[#{num}] " | |
puts (("=" * 25) + " Actual " + ("=" * 30)).red | |
puts @output | |
puts ("=" * 70).red | |
end | |
end | |
def tests(d) | |
files = {} | |
Dir["#{d}/*"].each do |file| | |
if match = file.match(/\bout([0-9]{2})\.txt$/) | |
num = match[1].to_i | |
if !files[num] | |
files[num] = TestCase.new(num, d) | |
end | |
files[num].out file | |
elsif match = file.match(/\bin([0-9]{2})([a-z])\.txt$/) | |
num = match[1].to_i | |
if !files[num] | |
files[num] = TestCase.new(num, d) | |
end | |
files[num].in file | |
end | |
end | |
return files.values.sort_by(&:num) | |
end | |
def check(d) | |
`(cd #{d} && cppcheck --enable=all *.cpp)` | |
end | |
if compile dir | |
puts "\n\n" | |
tests(dir).each do |test| | |
test.run "#{dir}/a.out" | |
end | |
puts "" | |
check dir | |
else | |
puts "\nCompile failed\n" | |
end | |
puts "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment