Class: TestCase

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

Instance Method Summary collapse

Constructor Details

#initialize(problem_name) ⇒ TestCase

Returns a new instance of TestCase.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/atcoder_greedy/lib/test_case.rb', line 7

def initialize(problem_name)
  language = File.extname(problem_name)
  if language.size == 0
    language = '.' + AtcoderGreedy.config[:language]
    problem_name = problem_name + language
  end
  puts "Running a test for problem #{problem_name}..."

  @problem_name = File.basename(problem_name, '.*')
  @input_file = File.open("./input_#{@problem_name}.txt", 'r')
  @exec_file = "./#{@problem_name}#{language}"
  get_in_out
end

Instance Method Details

#get_in_outObject



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
# File 'lib/atcoder_greedy/lib/test_case.rb', line 21

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

#get_solve(solve_file) ⇒ Object

HACK: move to Languages



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/atcoder_greedy/lib/test_case.rb', line 92

def get_solve(solve_file)
  case File.extname(solve_file)
    when '.rb'
      Rb.new(solve_file)
    when '.cpp'
      Cpp.new(solve_file)
    when '.c'
      C.new(solve_file)
    else
      raise 'Unknown Language'
  end
end

#validateObject



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
# File 'lib/atcoder_greedy/lib/test_case.rb', line 49

def validate
  my_solve = get_solve(@exec_file)
  passed = 0
  begin
    if my_solve.compile(@problem_name)
      puts '-------------------- Compile Done --------------------'
    else
      raise 'Compile Error'
    end

    @input.size.times do |j|
      myout_file = Tempfile.new(['myout', '.txt'], './')
      myout_file.open
      result = Benchmark.realtime do
        unless my_solve.execute(@input[j].path, myout_file.path)
          raise "Runtime Error"
        end
        @input[j].close
        myout_file.close(false)
      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
        passed += 1
        puts "-------------------- Testcase ##{j} -------------------- PASSED! Time: #{sprintf("%.5f", result)}s"
      else
        puts "-------------------- Testcase ##{j} -------------------- FAILED! Time: #{sprintf("%.5f", result)}s"
        puts 'Your Output:'
        puts "#{myout}\n"
        puts 'Correct Answer:'
        puts "#{correct}\n"
      end
    end
    puts "Test done. #{passed}/#{@input.size} passed."
  rescue => e
    puts e
  end
end