Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: d6c16c600c
Fetching contributors…

Cannot retrieve contributors at this time

file 124 lines (106 sloc) 3.137 kb
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
task :default => 'test'

# Assumes that if you have somethign like sample.in && sample.out or B-small-attempt0.in && B-small-attempt1.out, they are a valid pair
desc 'Tests your program!'
task :test do
  pwd = Rake.original_dir
  Dir.chdir(pwd)
  valid_tests = {}
  invalid_tests = {}

  # Determine the program filename
  files = Dir.entries(pwd)
  files = files.delete_if{|x| (x == '.' || x == '..') }
  files.delete_if{|x| x == "test.rb"}
  prog = files.find{|x| x.index(/.rb$/) }

  files = Dir.glob("works/*")
  files = files.delete_if{|x| (x == '.' || x == '..') }

  # Look for the working files and add it to the map
  inputs = files.select{ |x| x.index(/.in$/) }
  inputs.each do |input|
    output = input.sub(".in", ".out")
    if files.include?(output)
      valid_tests[input] = output
    end
  end

  files = Dir.glob("fails/*")
  files = files.delete_if{|x| (x == '.' || x == '..') }

  # Look for the failing files and add it to the map
  inputs = files.select{ |x| x.index(/\d+.in$/) }
  inputs.each do |input|
    output = input.sub(".in", ".out")
    if files.include?(output)
      invalid_tests[input] = output
    end
  end
  
  ["/tmp/works", "/tmp/fails"].each do |dir|
    Dir.mkdir(dir) unless Dir.exists?(dir)
  end

  valid_tests.each do |input_name, output_name|
    # Read the output of the file
    reference_output = ""
    file = File.new(output_name, "r")
    while (line = file.gets)
      reference_output += line
    end
    file.close

    puts "Testing #{prog} against #{input_name}..."
    cmd = "ruby #{prog} #{input_name}"
    result = `#{cmd}`
    if result != reference_output
      File.open("/tmp/#{output_name}", 'w'){ |f| f.write(result)}
      print `diff /tmp/#{output_name} #{output_name}`
    else
      puts "Great success!"
    end
  end

  invalid_tests.each do |input_name, output_name|
    # Read the output of the file
    reference_output = ""
    file = File.new(output_name, "r")
    while (line = file.gets)
      reference_output += line
    end
    file.close

    puts "Testing #{prog} against #{input_name}..."
    cmd = "ruby #{prog} #{input_name}"
    result = `#{cmd}`
    if result != reference_output
      puts "Not a total failure!"
    else
      puts "FAIL. Current program matched a known bad output!"
    end
  end

end

desc "Takes one argument, creates a new folder for that problem w/ starting .rb file and test folders"
task :create do
  puts ARGV.inspect
  dir = ARGV[1]
  raise 'Error: must provide a problem to create!' unless dir

  if Dir.exists?(dir)
    puts "Directory #{dir} already exists"
  else
    Dir.mkdir(dir)
    puts "Created #{dir}"
  end

  filename = dir + "/" + dir + ".rb"

  if File.exists?(filename)
    puts "#{filename} already exists!"
  else
    program = <<END
ARGF.readline
count = 0
ARGF.each do |input|
puts input
end
END
    File.open(filename, 'w'){ |f| f.write(program)}
    puts "Created #{filename}"
  end

  ["/works", "/fails"].each do |sub_dir|
    if Dir.exists?(dir + sub_dir)
      puts "Directory #{dir} already exists."
    else
      Dir.mkdir(dir + sub_dir)
      puts "Created #{dir}"
    end
  end

end
Something went wrong with that request. Please try again.