gettc 1.10 → 2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/bin/gettc +60 -69
  3. data/dist/config.yml +1 -1
  4. data/dist/include/cpp/engine.rb +78 -86
  5. data/dist/include/cpp/topcoder +236 -236
  6. data/dist/include/go/engine.rb +53 -61
  7. data/dist/include/haskell/engine.rb +112 -122
  8. data/dist/include/java/engine.rb +187 -184
  9. data/dist/include/javascript/engine.rb +26 -30
  10. data/dist/include/javascript/topcoder.js +3 -3
  11. data/dist/include/javascript/topcoder/errors.js +5 -5
  12. data/dist/include/javascript/topcoder/reader.js +188 -165
  13. data/dist/include/javascript/topcoder/writer.js +37 -33
  14. data/dist/include/python/engine.rb +43 -52
  15. data/dist/include/python/topcoder/__init__.pyc +0 -0
  16. data/dist/include/python/topcoder/__pycache__/__init__.cpython-34.pyc +0 -0
  17. data/dist/include/python/topcoder/__pycache__/errors.cpython-34.pyc +0 -0
  18. data/dist/include/python/topcoder/__pycache__/reader.cpython-34.pyc +0 -0
  19. data/dist/include/python/topcoder/__pycache__/writer.cpython-34.pyc +0 -0
  20. data/dist/include/python/topcoder/errors.pyc +0 -0
  21. data/dist/include/python/topcoder/reader.pyc +0 -0
  22. data/dist/include/python/topcoder/writer.pyc +0 -0
  23. data/dist/include/ruby/engine.rb +47 -52
  24. data/dist/include/ruby/topcoder/reader.rb +205 -193
  25. data/dist/include/ruby/topcoder/writer.rb +39 -37
  26. data/dist/template/bin/runner.rb +146 -151
  27. data/dist/template/bin/runner.sh +96 -96
  28. data/dist/template/prob/{name}.html +1 -1
  29. data/dist/template/solve/cpp/{name}.cpp +3 -4
  30. data/dist/template/solve/cpp/{name}Solver.cpp +14 -14
  31. data/dist/template/solve/go/{name}/{name}.go +3 -3
  32. data/dist/template/solve/go/{name}Solver.go +2 -2
  33. data/dist/template/solve/haskell/{name}.hs +6 -6
  34. data/dist/template/solve/haskell/{name}Solver.hs +10 -10
  35. data/dist/template/solve/java/{name}.java +4 -4
  36. data/dist/template/solve/java/{name}Solver.java +4 -4
  37. data/dist/template/solve/javascript/{name}.js +4 -6
  38. data/dist/template/solve/javascript/{name}Solver.js +11 -9
  39. data/dist/template/solve/python/{name}.py +1 -1
  40. data/dist/template/solve/python/{name}Solver.py +2 -2
  41. data/dist/template/solve/ruby/{name}.rb +4 -4
  42. data/dist/template/solve/ruby/{name}Solver.rb +14 -17
  43. data/dist/template/util/check/check.cpp +19 -19
  44. data/{core/lib → lib}/gettc.rb +0 -0
  45. data/lib/gettc/account.rb +14 -0
  46. data/lib/gettc/download.rb +211 -0
  47. data/lib/gettc/generate.rb +156 -0
  48. data/lib/gettc/parse.rb +237 -0
  49. data/lib/gettc/print.rb +54 -0
  50. data/lib/gettc/problem.rb +39 -0
  51. data/lib/gettc/signature.rb +63 -0
  52. data/lib/gettc/types.rb +93 -0
  53. data/lib/version.rb +3 -0
  54. data/test/gettc/download_test.rb +61 -0
  55. data/test/gettc/generate_test.rb +70 -0
  56. data/test/gettc/parse_test.rb +78 -0
  57. data/test/gettc/signature_test.rb +71 -0
  58. data/test/gettc/types_test.rb +31 -0
  59. metadata +28 -23
  60. data/core/lib/gettc/download.rb +0 -130
  61. data/core/lib/gettc/generate.rb +0 -145
  62. data/core/lib/gettc/parse.rb +0 -233
  63. data/core/lib/gettc/print.rb +0 -56
  64. data/core/lib/gettc/problem.rb +0 -33
  65. data/core/lib/gettc/signature.rb +0 -55
  66. data/core/lib/gettc/types.rb +0 -83
  67. data/core/lib/version.rb +0 -3
  68. data/core/test/gettc/download_test.rb +0 -29
  69. data/core/test/gettc/generate_test.rb +0 -31
  70. data/core/test/gettc/parse_test.rb +0 -104
  71. data/core/test/gettc/signature_test.rb +0 -54
  72. data/core/test/gettc/types_test.rb +0 -28
@@ -2,43 +2,45 @@ require "gettc/types"
2
2
  include Gettc
3
3
 
4
4
  module TopCoder
5
- class Writer
6
- def initialize
7
- @text = ""
8
- end
9
- def next value = nil, typ = nil
10
- if value.nil? || typ.nil?
11
- @text << ", "
12
- return self
13
- end
14
-
15
- if typ.is_a? TArray
16
- @text << ?[
17
- value.each_index do |i|
18
- self.next value[i], typ.subtype
19
- if i < value.size - 1
20
- self.next
21
- end
22
- end
23
- @text << ?]
24
- return self
25
- end
26
-
27
- case typ
28
- when TBoolean, TInt, TLong, TFloat, TDouble
29
- @text << value.to_s
30
- when TChar
31
- @text << ?' << value << ?'
32
- when TString
33
- @text << ?" << value << ?"
34
- else
35
- raise UnsupportedType.new typ
36
- end
37
-
38
- return self
39
- end
40
- def to_s
41
- return @text
5
+ class Writer
6
+ def initialize
7
+ @text = ""
8
+ end
9
+
10
+ def next(value = nil, type = nil)
11
+ if value.nil? || type.nil?
12
+ @text << ", "
13
+ return self
14
+ end
15
+
16
+ if type.is_a?(TArray)
17
+ @text << ?[
18
+
19
+ value.each_index do |i|
20
+ self.next(value[i], type.subtype)
21
+ self.next if i < value.size - 1
42
22
  end
23
+
24
+ @text << ?]
25
+ return self
26
+ end
27
+
28
+ case type
29
+ when TBoolean, TInt, TLong, TFloat, TDouble
30
+ @text << value.to_s
31
+ when TChar
32
+ @text << ?' << value << ?'
33
+ when TString
34
+ @text << ?" << value << ?"
35
+ else
36
+ raise UnsupportedType.new(type)
37
+ end
38
+
39
+ return self
40
+ end
41
+
42
+ def to_s
43
+ @text
43
44
  end
45
+ end
44
46
  end
@@ -3,185 +3,180 @@ require "logger"
3
3
  require "pathname"
4
4
 
5
5
  class SysUtil
6
- def self.with_dir dir
7
- saved = FileUtils.pwd
8
- FileUtils.chdir dir
9
- yield
10
- FileUtils.chdir saved
11
- end
6
+ def self.with_dir dir
7
+ saved = FileUtils.pwd
8
+ FileUtils.chdir dir
9
+ yield
10
+ FileUtils.chdir saved
11
+ end
12
12
  end
13
13
 
14
14
  class StopWatch
15
- def self.measure
16
- before = Time.now
17
- yield
18
- after = Time.now
19
- return ((after - before) * 1000).round
20
- end
15
+ def self.measure
16
+ before = Time.now
17
+ yield
18
+ after = Time.now
19
+ return ((after - before) * 1000).round
20
+ end
21
21
  end
22
22
 
23
23
  class GettcRunner
24
- def initialize mode, solver, data_d, output_d
25
- @verbose = (mode == "verbose")
26
- @solver = solver
27
- @data_d = data_d
28
- @output_d = output_d
24
+ def initialize(mode, solver, data_d, output_d)
25
+ @verbose = (mode == "verbose")
26
+ @solver = solver
27
+ @data_d = data_d
29
28
 
30
- @dir = File.dirname __FILE__
29
+ @output_d = output_d
30
+ FileUtils.mkdir_p(@output_d)
31
31
 
32
- FileUtils.mkdir_p @output_d
32
+ @dir = File.dirname(__FILE__)
33
33
 
34
- @log = Logger.new STDOUT
35
- @log.formatter = proc do |severity, datetime, progname, message|
36
- message
37
- end
38
- @log.level = Logger::INFO
39
- if @verbose
40
- @log.level = Logger::DEBUG
41
- end
34
+ @log = Logger.new(STDOUT)
35
+ @log.formatter = proc do |severity, datetime, progname, message|
36
+ message
42
37
  end
38
+ @log.level = @verbose ? Logger::DEBUG : Logger::INFO
39
+ end
43
40
 
44
- def compile_checker
45
- puts "[gettc] Compile checker"
46
- @checker = File.join @dir, "../build/check"
47
- unless File.exists? @checker
48
- checker_src = File.join @dir, "../util/check"
49
- SysUtil::with_dir checker_src do
50
- system "make"
51
- end
52
- end
53
- end
41
+ def compile_checker
42
+ puts "[gettc] Compile checker"
43
+ @checker = File.join(@dir, "../build/check")
54
44
 
55
- def populate_filenames n
56
- input = File.join @data_d, "#{n}.in"
57
- expected = File.join @data_d, "#{n}.out"
58
- received = File.join @output_d, "#{n}.out"
59
- return input, expected, received
45
+ unless File.exists?(@checker)
46
+ SysUtil::with_dir(File.join(@dir, "../util/check")) { system "make "}
60
47
  end
61
-
62
- def print_file_content label, filename
63
- print " #{label}: <"
64
- print File.read(filename)
65
- puts ">"
48
+ end
49
+
50
+ def populate_filenames(case_id)
51
+ input = File.join(@data_d, "#{case_id}.in")
52
+ expected = File.join(@data_d, "#{case_id}.out")
53
+ received = File.join(@output_d, "#{case_id}.out")
54
+ return input, expected, received
55
+ end
56
+
57
+ def print_file_content(label, filename)
58
+ print " #{label}: <"
59
+ print File.read(filename)
60
+ puts ">"
61
+ end
62
+
63
+ def print_case(case_id)
64
+ input, expected, received = populate_filenames(case_id)
65
+ print_file_content("Input", input)
66
+ print_file_content("Expected", expected)
67
+ print_file_content("Received", received)
68
+ end
69
+
70
+ def run_case(case_id)
71
+ input, expected, received = populate_filenames(case_id)
72
+
73
+ @log.debug "Case #{case_id} ... "
74
+ ret = true
75
+
76
+ elapsed = StopWatch::measure do
77
+ ret = system "#{@solver} #{input} #{received}"
66
78
  end
67
79
 
68
- def print_case n
69
- input, expected, received = populate_filenames n
70
- print_file_content "Input", input
71
- print_file_content "Expected", expected
72
- print_file_content "Received", received
80
+ @elapsed[case_id] = elapsed
81
+ @log.debug "#{elapsed}ms "
82
+
83
+ if ret != true
84
+ @errors << case_id
85
+ @log.debug "Error (cannot execute solver)\n"
86
+ elsif !File.exists?(received)
87
+ @errors << case_id
88
+ @log.debug "Error (solver did not produce an output file)\n"
89
+ else
90
+ system "#{@checker} #{expected} #{received}"
91
+ ret = $?.exitstatus
92
+ case ret
93
+ when 0
94
+ @log.debug "Passed\n"
95
+ when 1
96
+ @log.debug "Failed\n"
97
+ @failures << case_id
98
+ print_case(case_id) if @verbose
99
+ when 2
100
+ @log.debug "Error (checker reported error)\n"
101
+ @errors << case_id
102
+ else
103
+ @log.debug "Error (error executing checker)\n"
104
+ @errors << case_id
105
+ end
73
106
  end
107
+ end
74
108
 
75
- def run_case n
76
- input, expected, received = populate_filenames n
77
-
78
- @log.debug "Case #{n} ... "
79
- ret = true
109
+ def run_all_cases
110
+ puts "[gettc] Run test cases"
80
111
 
81
- elapsed = StopWatch::measure do
82
- ret = system "#{@solver} #{input} #{received}"
83
- end
84
- @elapsed[n] = elapsed
85
- @log.debug "#{elapsed}ms "
86
-
87
- if ret != true
88
- @errors << n
89
- @log.debug "Error (cannot execute solver)\n"
90
- elsif !File.exists? received
91
- @errors << n
92
- @log.debug "Error (solver did not produce an output file)\n"
93
- else
94
- system "#{@checker} #{expected} #{received}"
95
- ret = $?.exitstatus
96
- case ret
97
- when 0
98
- @log.debug "Passed\n"
99
- when 1
100
- @log.debug "Failed\n"
101
- @failures << n
102
- print_case n if @verbose
103
- when 2
104
- @log.debug "Error (checker reported error)\n"
105
- @errors << n
106
- else
107
- @log.debug "Error (error executing checker)\n"
108
- @errors << n
109
- end
110
- end
111
- end
112
+ inputs = Dir.glob("#{@data_d}/*.in").sort
112
113
 
113
- def run_all_cases
114
- puts "[gettc] Run test cases"
114
+ @total = inputs.size
115
+ @elapsed = Hash.new
116
+ @failures = Array.new
117
+ @errors = Array.new
115
118
 
116
- inputs = Dir.glob "#{@data_d}/*.in"
117
- inputs.sort!
118
-
119
- @total = inputs.size
120
- @elapsed = Hash.new
121
- @failures = Array.new
122
- @errors = Array.new
123
-
124
- inputs.each do |input|
125
- basename = Pathname.new(input).basename.to_s
126
- run_case basename[0..-4]
127
- end
119
+ inputs.each do |input|
120
+ run_case(Pathname.new(input).basename.to_s[0..-4])
128
121
  end
122
+ end
129
123
 
130
- def print_array label, arr
131
- if arr.size > 0
132
- print "#{label}: "
133
- puts arr.join(", ")
134
- end
124
+ def print_array(label, arr)
125
+ unless arr.empty?
126
+ print "#{label}: "
127
+ puts arr.join(", ")
135
128
  end
129
+ end
130
+
131
+ def summarize
132
+ puts "[gettc] Summary"
133
+ puts "#{@total} cases checked, #{@failures.size} failures, #{@errors.size} errors"
134
+ print_array("Failures", @failures)
135
+ print_array("Errors", @errors)
136
+
137
+ if @total > 0
138
+ sum = 0
139
+ maxv = 0
140
+ maxk = nil
141
+
142
+ @elapsed.each do |key, value|
143
+ sum += value
144
+ if value > maxv
145
+ maxv = value
146
+ maxk = key
147
+ end
148
+ end
136
149
 
137
- def summarize
138
- puts "[gettc] Summary"
139
- puts "#{@total} cases checked, #{@failures.size} failures, #{@errors.size} errors"
140
- print_array "Failures", @failures
141
- print_array "Errors", @errors
142
-
143
- if @total > 0
144
- sum = 0
145
- maxv = 0
146
- maxk = nil
147
- @elapsed.each do |key, value|
148
- sum += value
149
- if value > maxv
150
- maxv = value
151
- maxk = key
152
- end
153
- end
154
- avg = sum / @total
155
-
156
- puts " Total time taken: #{sum} ms"
157
- puts " Average time taken: #{avg} ms"
158
- puts " Slowest running case: #{maxv} ms (case #{maxk})"
159
- end
150
+ avg = sum / @total
151
+
152
+ puts " Total time taken: #{sum} ms"
153
+ puts " Average time taken: #{avg} ms"
154
+ puts " Slowest running case: #{maxv} ms (case #{maxk})"
160
155
  end
156
+ end
161
157
 
162
- def print_failures
163
- if @failures.empty? || @verbose
164
- return
165
- end
166
- n = @failures.size
167
- if n > 5
168
- puts "Here are a few failed case for your debugging purposes"
169
- n = 5
170
- end
171
- n.times do |i|
172
- j = @failures[i]
173
- puts "Case #{j}:"
174
- print_case j
175
- end
158
+ def print_failures
159
+ return if @failures.empty? || @verbose
160
+
161
+ n = @failures.size
162
+ if n > 5
163
+ puts "Here are a few failed case for your debugging purposes"
164
+ n = 5
176
165
  end
177
166
 
178
- def run
179
- compile_checker
180
- run_all_cases
181
- print_failures
182
- summarize
167
+ n.times do |i|
168
+ case_id = @failures[i]
169
+ puts "Case #{case_id}:"
170
+ print_case j
183
171
  end
172
+ end
173
+
174
+ def run
175
+ compile_checker
176
+ run_all_cases
177
+ print_failures
178
+ summarize
179
+ end
184
180
  end
185
181
 
186
- runner = GettcRunner.new ARGV[0], ARGV[1], ARGV[2], ARGV[3]
187
- runner.run
182
+ GettcRunner.new(ARGV[0], ARGV[1], ARGV[2], ARGV[3]).run
@@ -7,126 +7,126 @@ data_d="$3"
7
7
  output_d="$4"
8
8
 
9
9
  command_exists () {
10
- type "$1" > /dev/null 2>&1 ;
10
+ type "$1" > /dev/null 2>&1 ;
11
11
  }
12
12
 
13
13
  timeit () {
14
- if [ $mode = "quiet" ]; then
15
- "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
16
- elif command_exists /usr/bin/time ; then
17
- /usr/bin/time "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
18
- elif command_exists time ; then
19
- time "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
20
- else
21
- "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
22
- fi
14
+ if [ $mode = "quiet" ]; then
15
+ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
16
+ elif command_exists /usr/bin/time ; then
17
+ /usr/bin/time "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
18
+ elif command_exists time ; then
19
+ time "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
20
+ else
21
+ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
22
+ fi
23
23
  }
24
24
 
25
25
  puts () {
26
- if [ $mode = "verbose" ]; then
27
- echo "$1"
28
- fi
26
+ if [ $mode = "verbose" ]; then
27
+ echo "$1"
28
+ fi
29
29
  }
30
30
 
31
31
  write () {
32
- if [ $mode = "verbose" ]; then
33
- echo -n "$1"
34
- fi
32
+ if [ $mode = "verbose" ]; then
33
+ echo -n "$1"
34
+ fi
35
35
  }
36
36
 
37
37
  view_file () {
38
- filename="$1"
39
- write "<"
40
- cat "$filename"
41
- puts ">"
38
+ filename="$1"
39
+ write "<"
40
+ cat "$filename"
41
+ puts ">"
42
42
  }
43
43
 
44
44
  report_case () {
45
- if [ $mode = "quiet" ]; then
46
- return
47
- fi
48
- write " Input: "
49
- view_file "$data_d/$test_case.in"
50
- write " Expected: "
51
- view_file "$data_d/$test_case.out"
52
- write " Received: "
53
- view_file "$output_d/$test_case.out"
45
+ if [ $mode = "quiet" ]; then
46
+ return
47
+ fi
48
+ write " Input: "
49
+ view_file "$data_d/$test_case.in"
50
+ write " Expected: "
51
+ view_file "$data_d/$test_case.out"
52
+ write " Received: "
53
+ view_file "$output_d/$test_case.out"
54
54
  }
55
55
 
56
56
  delegate () {
57
- if command_exists ruby ; then
58
- runner_rb="$(dirname $0)/runner.rb"
59
- ruby "$runner_rb" "$mode" "$solver" "$data_d" "$output_d"
60
- exit 0
61
- fi
57
+ if command_exists ruby ; then
58
+ runner_rb="$(dirname $0)/runner.rb"
59
+ ruby "$runner_rb" "$mode" "$solver" "$data_d" "$output_d"
60
+ exit 0
61
+ fi
62
62
  }
63
63
 
64
64
  init () {
65
- export TIME="Time: %es - Memory: %MK"
66
- export TIMEFORMAT=%3lR
67
-
68
- if [ ! -d "$output_d" ]; then
69
- mkdir "$output_d"
70
- fi
71
-
72
- dir=`dirname $0`
73
- echo "[gettc] Compile checker"
74
- checker="$dir/../build/check"
75
- if [ ! -x "$checker" ]; then
76
- undo=`pwd`
77
- cd "$dir/../util/check"
78
- make
79
- cd "$undo"
80
- fi
81
- }
65
+ export TIME="Time: %es - Memory: %MK"
66
+ export TIMEFORMAT=%3lR
67
+
68
+ if [ ! -d "$output_d" ]; then
69
+ mkdir "$output_d"
70
+ fi
71
+
72
+ dir=`dirname $0`
73
+ echo "[gettc] Compile checker"
74
+ checker="$dir/../build/check"
75
+ if [ ! -x "$checker" ]; then
76
+ undo=`pwd`
77
+ cd "$dir/../util/check"
78
+ make
79
+ cd "$undo"
80
+ fi
81
+ }
82
82
 
83
83
  main () {
84
- echo "[gettc] Run test cases"
85
-
86
- cases=0
87
- fails=0
88
- errors=0
89
- failstr=""
90
- errorstr=""
91
-
92
- for input in "$data_d"/*.in; do
93
- if [ -f "$input" ]; then
94
- cases=$(( cases + 1 ))
95
-
96
- test_case=`basename "$input"`
97
- test_case=${test_case%.in}
98
-
99
- write "Case $test_case ... "
100
- timeit "$solver" "$data_d/$test_case.in" "$output_d/$test_case.out"
101
- "$checker" "$data_d/$test_case.out" "$output_d/$test_case.out"
102
- retcode=$?
103
-
104
- if [ $retcode -eq 0 ]; then
105
- puts "Passed"
106
-
107
- elif [ $retcode -eq 1 ]; then
108
- fails=$(( fails + 1 ))
109
- failstr="$failstr $test_case"
110
- puts "Failed"
111
- report_case "$test_case"
112
-
113
- else
114
- errors=$(( errors + 1 ))
115
- errorstr="$errorstr $test_case"
116
- puts "Error"
117
- report_case "$test_case"
118
- fi
119
- fi
120
- done
121
-
122
- echo "[gettc] Summary"
123
- echo "$cases cases checked, $fails failures, $errors errors"
124
- if [ $fails -gt 0 ]; then
125
- echo "Failures:${failstr}"
126
- fi
127
- if [ $errors -gt 0 ]; then
128
- echo "Errors:${errorstr}"
84
+ echo "[gettc] Run test cases"
85
+
86
+ cases=0
87
+ fails=0
88
+ errors=0
89
+ failstr=""
90
+ errorstr=""
91
+
92
+ for input in "$data_d"/*.in; do
93
+ if [ -f "$input" ]; then
94
+ cases=$(( cases + 1 ))
95
+
96
+ test_case=`basename "$input"`
97
+ test_case=${test_case%.in}
98
+
99
+ write "Case $test_case ... "
100
+ timeit "$solver" "$data_d/$test_case.in" "$output_d/$test_case.out"
101
+ "$checker" "$data_d/$test_case.out" "$output_d/$test_case.out"
102
+ retcode=$?
103
+
104
+ if [ $retcode -eq 0 ]; then
105
+ puts "Passed"
106
+
107
+ elif [ $retcode -eq 1 ]; then
108
+ fails=$(( fails + 1 ))
109
+ failstr="$failstr $test_case"
110
+ puts "Failed"
111
+ report_case "$test_case"
112
+
113
+ else
114
+ errors=$(( errors + 1 ))
115
+ errorstr="$errorstr $test_case"
116
+ puts "Error"
117
+ report_case "$test_case"
118
+ fi
129
119
  fi
120
+ done
121
+
122
+ echo "[gettc] Summary"
123
+ echo "$cases cases checked, $fails failures, $errors errors"
124
+ if [ $fails -gt 0 ]; then
125
+ echo "Failures:${failstr}"
126
+ fi
127
+ if [ $errors -gt 0 ]; then
128
+ echo "Errors:${errorstr}"
129
+ fi
130
130
  }
131
131
 
132
132
  delegate