Class: AtcoderGreedy::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/atcoder_greedy/command/test.rb

Instance Method Summary collapse

Constructor Details

#initialize(problem_name) ⇒ TestCase

Returns a new instance of TestCase.



21
22
23
24
25
# File 'lib/atcoder_greedy/command/test.rb', line 21

def initialize(problem_name)
  @input_file = File.open("./input_#{problem_name}.txt", 'r')
  @exec_file = "./#{problem_name}.rb"
  get_in_out
end

Instance Method Details

#get_in_outObject



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
# File 'lib/atcoder_greedy/command/test.rb', line 27

def get_in_out
  i = 0
  @input = []
  @output = []
  now = nil
  while (t = @input_file.gets) != nil
    example_string = "-- Example #{i}"
    answer_string = "-- Answer #{i}"

    if t.chomp == example_string
      now.close(false) unless now.nil?
      now = Tempfile.new(['in', '.txt'], './')
      @input.push(now)
      now.open
    elsif t.chomp == answer_string
      now.close(false) unless now.nil?
      now = Tempfile.new(['out', '.txt'], './')
      @output.push(now)
      now.open
      i += 1
    else
      now.puts t
    end
  end
  @input[-1].close(false)
  @output[-1].close(false)
end

#validateObject



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
# File 'lib/atcoder_greedy/command/test.rb', line 55

def validate
  @input.size.times do |j|
    myout_file = Tempfile.new(['myout', '.txt'], './')
    myout_file.open
    result = Benchmark.realtime do
      pid = Process.fork do
        exec "ruby #{@exec_file} < #{@input[j].path} > #{myout_file.path}"
        @input[j].close
        myout_file.close(false)
      end
      Process.waitpid pid
    end

    myout = myout_file.open.read
    myout_file.close
    correct = File.open("#{@output[j].path}").read
    diffs = Diff::LCS.diff(myout, correct)
    if diffs.size == 0
      puts "Testcase ##{j} ... PASSED! Time: #{result}s"
    else
      puts "Testcase ##{j} ... FAILED! Time: #{result}s"
      puts "Your Output:"
      puts "#{myout}\n"
      puts "Correct Answer:"
      puts "#{correct}\n"
    end
  end
end