Class: AtCoderFriends::TestRunner

Inherits:
Object
  • Object
show all
Includes:
PathUtil
Defined in:
lib/at_coder_friends/test_runner.rb

Overview

run tests for the specified program.

Direct Known Subclasses

JudgeTestRunner, SampleTestRunner

Constant Summary

Constants included from PathUtil

PathUtil::CASES_DIR, PathUtil::SMP_DIR

Instance Method Summary collapse

Methods included from PathUtil

cases_dir, contest_name, smp_dir, split_prg_path

Constructor Details

#initialize(path, config) ⇒ TestRunner

Returns a new instance of TestRunner.



10
11
12
13
14
# File 'lib/at_coder_friends/test_runner.rb', line 10

def initialize(path, config)
  @contest = contest_name(path)
  @path, @dir, @prg, @base, @ext, @q = split_prg_path(path)
  @config = config
end

Instance Method Details

#local_test(infile, outfile) ⇒ Object



51
52
53
# File 'lib/at_coder_friends/test_runner.rb', line 51

def local_test(infile, outfile)
  system("#{test_cmd} < #{infile} > #{outfile}")
end

#remote_test(infile, outfile) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/at_coder_friends/test_runner.rb', line 55

def remote_test(infile, outfile)
  agent = ScrapingAgent.new(@contest, @config)
  res = agent.code_test(@path, infile)
  unless res && res['Result']
    File.write(outfile, 'Remote test failed.')
    return false
  end
  puts "Exit code: #{res.dig('Result', 'ExitCode')}"
  puts "Time: #{res.dig('Result', 'TimeConsumption')}ms"
  puts "Memory: #{res.dig('Result', 'MemoryConsumption')}KB"
  if res.dig('Result', 'ExitCode') != 0
    File.write(outfile, res['Stderr'])
    return false
  end
  File.write(outfile, res['Stdout'])
  true
end

#run_test(id, infile, outfile, expfile) ⇒ Object

rubocop:disable Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/at_coder_friends/test_runner.rb', line 17

def run_test(id, infile, outfile, expfile)
  return false unless File.exist?(infile) && File.exist?(expfile)

  puts "==== #{id} (#{test_loc}) ===="

  success = send(test_mtd, infile, outfile)
  input, result, expected =
    [infile, outfile, expfile].map { |file| File.read(file) }

  puts '-- input --'
  print input
  puts '-- expected --'
  print expected
  puts '-- result --'
  print result
  if !success
    puts '!!!!! RE !!!!!'
  elsif result != expected
    puts '!!!!! WA !!!!!'
  else
    puts '<< OK >>'
  end
  true
end

#test_cmdObject



73
74
75
76
77
78
79
80
81
# File 'lib/at_coder_friends/test_runner.rb', line 73

def test_cmd
  @test_cmd ||= begin
    cmds = @config.dig('ext_settings', @ext, 'test_cmd')
    cmd = cmds && (cmds[which_os.to_s] || cmds['default'])
    return nil unless cmd

    cmd.gsub('{dir}', @dir).gsub('{base}', @base)
  end
end

#test_locObject

rubocop:enable Metrics/MethodLength



43
44
45
# File 'lib/at_coder_friends/test_runner.rb', line 43

def test_loc
  test_cmd ? 'local' : 'remote'
end

#test_mtdObject



47
48
49
# File 'lib/at_coder_friends/test_runner.rb', line 47

def test_mtd
  test_cmd ? :local_test : :remote_test
end

#which_osObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/at_coder_friends/test_runner.rb', line 83

def which_os
  @which_os ||= begin
    case RbConfig::CONFIG['host_os']
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      :windows
    when /darwin|mac os/
      :macosx
    when /linux/
      :linux
    when /solaris|bsd/
      :unix
    else
      :unknown
    end
  end
end