Description:
add more languages, java and ruby add bookmark algo-bm and branch algo
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r147:9e3cee7d454d - - 5 files changed: 54 inserted, 7 deleted

@@ -1,113 +1,142
1 #!/usr/bin/env ruby
1 #!/usr/bin/env ruby
2
2
3 require 'fileutils'
3 require 'fileutils'
4
4
5 ##############################
5 ##############################
6 #
6 #
7 # Standard Compile Script
7 # Standard Compile Script
8 #
8 #
9 # Supported compilers:
9 # Supported compilers:
10 # gcc, g++, and fpc.
10 # gcc, g++, and fpc.
11 #
11 #
12 ##############################
12 ##############################
13
13
14 def talk(msg)
14 def talk(msg)
15 if ENV['TALKATIVE']!=nil
15 if ENV['TALKATIVE']!=nil
16 puts str
16 puts str
17 end
17 end
18 if ENV['GRADER_LOGGING']!=nil
18 if ENV['GRADER_LOGGING']!=nil
19 log_fname = ENV['GRADER_LOGGING']
19 log_fname = ENV['GRADER_LOGGING']
20 fp = File.open(log_fname,"a")
20 fp = File.open(log_fname,"a")
21 fp.puts("run: #{Time.new.strftime("%H:%M")} #{msg}")
21 fp.puts("run: #{Time.new.strftime("%H:%M")} #{msg}")
22 fp.close
22 fp.close
23 end
23 end
24 end
24 end
25
25
26 C_COMPILER = "/usr/bin/gcc"
26 C_COMPILER = "/usr/bin/gcc"
27 CPLUSPLUS_COMPILER = "/usr/bin/g++"
27 CPLUSPLUS_COMPILER = "/usr/bin/g++"
28 PASCAL_COMPILER = "/usr/bin/fpc"
28 PASCAL_COMPILER = "/usr/bin/fpc"
29 + JAVA_COMPILER = "/usr/bin/javac"
30 + RUBY_INTEPRETER = "/home/dae/.rvm/rubies/ruby-1.9.2-p320/bin/ruby"
29
31
30 C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall"
32 C_OPTIONS = "-O2 -s -static -std=c99 -DCONTEST -lm -Wall"
31 CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall"
33 CPLUSPLUS_OPTIONS = "-O2 -s -static -DCONTEST -lm -Wall"
32 PASCAL_OPTIONS = "-O1 -XS -dCONTEST"
34 PASCAL_OPTIONS = "-O1 -XS -dCONTEST"
35 + JAVA_OPTIONS = ""
33
36
34 # Check for the correct number of arguments. Otherwise, print usage.
37 # Check for the correct number of arguments. Otherwise, print usage.
35 if ARGV.length == 0 or ARGV.length > 4
38 if ARGV.length == 0 or ARGV.length > 4
36 puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]"
39 puts "Usage: compile <language> [<source-file>] [<output-file>] [<message-file>]"
37 puts
40 puts
38 puts "<source-file> is defaulted to \"source\"."
41 puts "<source-file> is defaulted to \"source\"."
39 puts "<output-file> is defaulted to \"a.out\"."
42 puts "<output-file> is defaulted to \"a.out\"."
40 puts "<message-file> is defaulted to \"compiler_message\"."
43 puts "<message-file> is defaulted to \"compiler_message\"."
41 puts
44 puts
42 exit(127)
45 exit(127)
43 end
46 end
44
47
45 PARAMS = {
48 PARAMS = {
46 :source_file => [1,'source'],
49 :source_file => [1,'source'],
47 :output_file => [2,'a.out'],
50 :output_file => [2,'a.out'],
48 :message_file => [3,'compiler_message']
51 :message_file => [3,'compiler_message']
49 }
52 }
50
53
51 params = {}
54 params = {}
52 params[:prog_lang] = ARGV[0]
55 params[:prog_lang] = ARGV[0]
53 PARAMS.each_key do |param_name|
56 PARAMS.each_key do |param_name|
54 index, default = PARAMS[param_name]
57 index, default = PARAMS[param_name]
55 if ARGV.length > index
58 if ARGV.length > index
56 params[param_name] = ARGV[index]
59 params[param_name] = ARGV[index]
57 else
60 else
58 params[param_name] = default
61 params[param_name] = default
59 end
62 end
60 talk "#{param_name}: #{params[param_name]}"
63 talk "#{param_name}: #{params[param_name]}"
61 end
64 end
62
65
63 # Remove any remaining output files or message files.
66 # Remove any remaining output files or message files.
64 if FileTest.exists? params[:output_file]
67 if FileTest.exists? params[:output_file]
65 FileUtils.rm(params[:output_file])
68 FileUtils.rm(params[:output_file])
66 end
69 end
67 if FileTest.exists? params[:message_file]
70 if FileTest.exists? params[:message_file]
68 FileUtils.rm(params[:message_file])
71 FileUtils.rm(params[:message_file])
69 end
72 end
70
73
71 # Check if the source file exists before attempt compiling.
74 # Check if the source file exists before attempt compiling.
72 if !FileTest.exists? params[:source_file]
75 if !FileTest.exists? params[:source_file]
73 talk("ERROR: The source file does not exist!")
76 talk("ERROR: The source file does not exist!")
74 open(params[:message_file],"w") do |f|
77 open(params[:message_file],"w") do |f|
75 f.puts "ERROR: The source file did not exist."
78 f.puts "ERROR: The source file did not exist."
76 end
79 end
77 exit(127)
80 exit(127)
78 end
81 end
79
82
80 if params[:prog_lang]=='cpp'
83 if params[:prog_lang]=='cpp'
81 params[:prog_lang] = 'c++'
84 params[:prog_lang] = 'c++'
82 end
85 end
83
86
84 # Compile.
87 # Compile.
85 case params[:prog_lang]
88 case params[:prog_lang]
86
89
87 when "c"
90 when "c"
88 command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}"
91 command = "#{C_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{C_OPTIONS} 2> #{params[:message_file]}"
89 system(command)
92 system(command)
90
93
91 when "c++"
94 when "c++"
92 command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}"
95 command = "#{CPLUSPLUS_COMPILER} #{params[:source_file]} -o #{params[:output_file]} #{CPLUSPLUS_OPTIONS} 2> #{params[:message_file]}"
93 system(command)
96 system(command)
94
97
95 when "pas"
98 when "pas"
96 command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}"
99 command = "#{PASCAL_COMPILER} #{params[:source_file]} -ooutpas #{PASCAL_OPTIONS} > #{params[:message_file]}"
97 system(command)
100 system(command)
98 FileUtils.mv("output", params[:output_file])
101 FileUtils.mv("output", params[:output_file])
102 +
103 + when "java"
104 + #rename the file to the public class name
105 +
106 + #get the class name
107 + classname = 'DUMMY'
108 + File.foreach(params[:source_file]) do |line|
109 + md = /\s*public\s*class\s*(\w*)/.match(line)
110 + classname=md[1] if md
111 + end
112 + system("cp #{params[:source_file]} #{classname}.java")
113 + command = "#{JAVA_COMPILER} #{classname}.java > #{params[:message_file]}"
114 + system(command)
115 + File.open(params[:output_file],"w") {|file| file.write("#!/bin/sh\n/usr/bin/java #{classname}\n")}
116 + File.chmod(0755, params[:output_file])
117 +
118 + when "ruby"
119 + command = "#{RUBY_INTEPRETER} -c #{params[:source_file]} > #{params[:message_file]}"
120 + system(command)
121 + File.open(params[:output_file],"w") do |out_file|
122 + out_file.puts "#!#{RUBY_INTEPRETER}"
123 + File.open(params[:source_file],"r").each do |line|
124 + out_file.print line
125 + end
126 + end
127 + File.chmod(0755, params[:output_file])
99
128
100 else
129 else
101 talk("ERROR: Invalid language specified!")
130 talk("ERROR: Invalid language specified!")
102 open(params[:message_file],"w") do |f|
131 open(params[:message_file],"w") do |f|
103 f.puts "ERROR: Invalid language specified!"
132 f.puts "ERROR: Invalid language specified!"
104 end
133 end
105 exit(127)
134 exit(127)
106 end
135 end
107
136
108 # Report success or failure.
137 # Report success or failure.
109 if FileTest.exists? params[:output_file]
138 if FileTest.exists? params[:output_file]
110 talk "Compilation was successful!"
139 talk "Compilation was successful!"
111 else
140 else
112 talk "ERROR: Something was wrong during the compilation!"
141 talk "ERROR: Something was wrong during the compilation!"
113 end
142 end
@@ -8,168 +8,170
8 end
8 end
9 if ENV['GRADER_LOGGING']!=nil
9 if ENV['GRADER_LOGGING']!=nil
10 log_fname = ENV['GRADER_LOGGING']
10 log_fname = ENV['GRADER_LOGGING']
11 fp = File.open(log_fname,"a")
11 fp = File.open(log_fname,"a")
12 fp.puts("judge: #{Time.new.strftime("%H:%M")} #{str}")
12 fp.puts("judge: #{Time.new.strftime("%H:%M")} #{str}")
13 fp.close
13 fp.close
14 end
14 end
15 end
15 end
16
16
17 problem_home = ENV['PROBLEM_HOME']
17 problem_home = ENV['PROBLEM_HOME']
18
18
19 def execute(command, error_message="")
19 def execute(command, error_message="")
20 if not system(command)
20 if not system(command)
21 msg = "ERROR: #{error_message}"
21 msg = "ERROR: #{error_message}"
22 log msg
22 log msg
23 raise(msg)
23 raise(msg)
24 end
24 end
25 end
25 end
26
26
27 def call_and_log(error_message)
27 def call_and_log(error_message)
28 begin
28 begin
29 yield
29 yield
30 rescue
30 rescue
31 msg = "ERROR: #{error_message}"
31 msg = "ERROR: #{error_message}"
32 log msg
32 log msg
33 raise msg
33 raise msg
34 end
34 end
35 end
35 end
36
36
37 def clear_and_create_empty_dir(dir)
37 def clear_and_create_empty_dir(dir)
38 FileUtils.rm_rf(dir, :secure => true)
38 FileUtils.rm_rf(dir, :secure => true)
39 call_and_log("Cannot make directory #{dir}.") { FileUtils.mkdir(dir) }
39 call_and_log("Cannot make directory #{dir}.") { FileUtils.mkdir(dir) }
40 end
40 end
41
41
42 # ARGV[0] --- language
42 # ARGV[0] --- language
43 # ARGV[1] --- program source file
43 # ARGV[1] --- program source file
44 # ARGV[2] --- test result directory
44 # ARGV[2] --- test result directory
45 # ARGV[3] --- sandbox directory
45 # ARGV[3] --- sandbox directory
46
46
47 if ARGV.length < 2 || ARGV.length > 4
47 if ARGV.length < 2 || ARGV.length > 4
48 puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]"
48 puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]"
49 puts " <sandbox-directory> is defaulted to ./sandbox"
49 puts " <sandbox-directory> is defaulted to ./sandbox"
50 puts " <test-result-directory> is defaulted to ./test-result"
50 puts " <test-result-directory> is defaulted to ./test-result"
51 puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it."
51 puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it."
52 exit(127)
52 exit(127)
53 end
53 end
54
54
55 language = ARGV[0]
55 language = ARGV[0]
56 - if language != "c" && language != "c++" && language != "pas"
56 + if language != "c" && language != "c++" && language != "pas" && language != "java" && language != "ruby"
57 log "You specified a language that is not supported: #{language}."
57 log "You specified a language that is not supported: #{language}."
58 exit(127)
58 exit(127)
59 end
59 end
60
60
61 source_file = ARGV[1]
61 source_file = ARGV[1]
62 if File.exist?(source_file) == false
62 if File.exist?(source_file) == false
63 log "The source file does not exist."
63 log "The source file does not exist."
64 exit(127)
64 exit(127)
65 end
65 end
66
66
67 log "Making test result and sandbox directories..."
67 log "Making test result and sandbox directories..."
68
68
69 current_dir = FileUtils.pwd
69 current_dir = FileUtils.pwd
70 current_dir.strip!
70 current_dir.strip!
71
71
72 if ARGV.length >= 3
72 if ARGV.length >= 3
73 test_result_dir = ARGV[2]
73 test_result_dir = ARGV[2]
74 else
74 else
75 test_result_dir = "#{current_dir}/test-result"
75 test_result_dir = "#{current_dir}/test-result"
76 end
76 end
77
77
78 log "Test result directory: #{test_result_dir}"
78 log "Test result directory: #{test_result_dir}"
79 clear_and_create_empty_dir(test_result_dir)
79 clear_and_create_empty_dir(test_result_dir)
80
80
81 if ARGV.length >= 4
81 if ARGV.length >= 4
82 sandbox_dir = ARGV[3]
82 sandbox_dir = ARGV[3]
83 else
83 else
84 sandbox_dir = "#{current_dir}/sandbox"
84 sandbox_dir = "#{current_dir}/sandbox"
85 end
85 end
86 log "Sandbox directory: #{sandbox_dir}"
86 log "Sandbox directory: #{sandbox_dir}"
87 clear_and_create_empty_dir(sandbox_dir)
87 clear_and_create_empty_dir(sandbox_dir)
88
88
89 # Compile
89 # Compile
90 log
90 log
91 log "Compiling..."
91 log "Compiling..."
92 call_and_log("Cannot copy the source file to #{sandbox_dir}") {
92 call_and_log("Cannot copy the source file to #{sandbox_dir}") {
93 FileUtils.cp(source_file, sandbox_dir)
93 FileUtils.cp(source_file, sandbox_dir)
94 }
94 }
95 begin
95 begin
96 Dir.chdir sandbox_dir
96 Dir.chdir sandbox_dir
97 rescue
97 rescue
98 log "ERROR: Cannot change directory to #{sandbox_dir}."
98 log "ERROR: Cannot change directory to #{sandbox_dir}."
99 exit(127)
99 exit(127)
100 end
100 end
101 execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!")
101 execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!")
102 compile_message = open("compiler_message").read
102 compile_message = open("compiler_message").read
103 compile_message.strip!
103 compile_message.strip!
104 call_and_log("Cannot move the compiler message to #{test_result_dir}.") {
104 call_and_log("Cannot move the compiler message to #{test_result_dir}.") {
105 FileUtils.mv("compiler_message", test_result_dir)
105 FileUtils.mv("compiler_message", test_result_dir)
106 }
106 }
107 if !FileTest.exist?("a.out")
107 if !FileTest.exist?("a.out")
108 log "Cannot compile the source code. See message in #{test_result_dir}/compile_message"
108 log "Cannot compile the source code. See message in #{test_result_dir}/compile_message"
109 exit(127)
109 exit(127)
110 else
110 else
111 call_and_log("Cannot move the compiled program to #{test_result_dir}") {
111 call_and_log("Cannot move the compiled program to #{test_result_dir}") {
112 FileUtils.mv("a.out",test_result_dir)
112 FileUtils.mv("a.out",test_result_dir)
113 + if language == "java" then Dir["*.class"].each { |file| FileUtils.mv(file,test_result_dir)} end
113 }
114 }
114 FileUtils.rm_rf("#{sandbox_dir}/.")
115 FileUtils.rm_rf("#{sandbox_dir}/.")
115 end
116 end
116
117
117 require "#{problem_home}/script/test_dsl.rb"
118 require "#{problem_home}/script/test_dsl.rb"
118 load "#{problem_home}/test_cases/all_tests.cfg"
119 load "#{problem_home}/test_cases/all_tests.cfg"
119 problem = Problem.get_instance
120 problem = Problem.get_instance
120
121
121 if problem.well_formed? == false
122 if problem.well_formed? == false
122 log "The problem specification is not well formed."
123 log "The problem specification is not well formed."
123 exit(127)
124 exit(127)
124 end
125 end
125
126
126 # Doing the testing.
127 # Doing the testing.
127 (1..(problem.num_tests)).each do |test_num|
128 (1..(problem.num_tests)).each do |test_num|
128
129
129 $stdout.print "[#{test_num}]"
130 $stdout.print "[#{test_num}]"
130 $stdout.flush
131 $stdout.flush
131
132
132 log "Test number: #{test_num}"
133 log "Test number: #{test_num}"
133
134
134 call_and_log("Cannot copy the compiled program into #{sandbox_dir}") {
135 call_and_log("Cannot copy the compiled program into #{sandbox_dir}") {
135 FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true)
136 FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true)
137 + if language == "java" then Dir["#{test_result_dir}/*.class"].each { |file| FileUtils.cp(file,sandbox_dir)} end
136 }
138 }
137
139
138 begin
140 begin
139 execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script")
141 execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script")
140 rescue
142 rescue
141 # do nothing
143 # do nothing
142 end
144 end
143
145
144 call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") {
146 call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") {
145 FileUtils.mkdir "#{test_result_dir}/#{test_num}"
147 FileUtils.mkdir "#{test_result_dir}/#{test_num}"
146 }
148 }
147 call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") {
149 call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") {
148 FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}"
150 FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}"
149 }
151 }
150 call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") {
152 call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") {
151 FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}"
153 FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}"
152 }
154 }
153 call_and_log("Cannot copy the output file into #{test_result_dir}/#{test_num}") {
155 call_and_log("Cannot copy the output file into #{test_result_dir}/#{test_num}") {
154 FileUtils.mv "#{sandbox_dir}/output.txt", "#{test_result_dir}/#{test_num}"
156 FileUtils.mv "#{sandbox_dir}/output.txt", "#{test_result_dir}/#{test_num}"
155 }
157 }
156 call_and_log("Cannot clear #{sandbox_dir}") {
158 call_and_log("Cannot clear #{sandbox_dir}") {
157 FileUtils.rm_rf(Dir.glob("#{sandbox_dir}/*"), :secure => true)
159 FileUtils.rm_rf(Dir.glob("#{sandbox_dir}/*"), :secure => true)
158 }
160 }
159 end
161 end
160
162
161 $stdout.print "[done]\n"
163 $stdout.print "[done]\n"
162
164
163 # Grade
165 # Grade
164 log
166 log
165 log "Grading..."
167 log "Grading..."
166 begin
168 begin
167 Dir.chdir test_result_dir
169 Dir.chdir test_result_dir
168 rescue
170 rescue
169 log "ERROR: Cannot change directory to #{test_result_dir}."
171 log "ERROR: Cannot change directory to #{test_result_dir}."
170 exit(127)
172 exit(127)
171 end
173 end
172 execute("#{problem_home}/script/grade", "An error occured during grading!")
174 execute("#{problem_home}/script/grade", "An error occured during grading!")
173
175
174 log
176 log
175 log "All done!"
177 log "All done!"
@@ -1,159 +1,175
1 #!/usr/bin/env ruby
1 #!/usr/bin/env ruby
2
2
3 require 'fileutils'
3 require 'fileutils'
4
4
5 def log(str='')
5 def log(str='')
6 if ENV['TALKATIVE']!=nil
6 if ENV['TALKATIVE']!=nil
7 puts str
7 puts str
8 end
8 end
9 if ENV['GRADER_LOGGING']!=nil
9 if ENV['GRADER_LOGGING']!=nil
10 log_fname = ENV['GRADER_LOGGING']
10 log_fname = ENV['GRADER_LOGGING']
11 fp = File.open(log_fname,"a")
11 fp = File.open(log_fname,"a")
12 fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}")
12 fp.puts("run: #{Time.new.strftime("%H:%M")} #{str}")
13 fp.close
13 fp.close
14 end
14 end
15 end
15 end
16
16
17 def extract_time(t)
17 def extract_time(t)
18 - # puts "TIME: #{t}"
18 + #puts "TIME: #{t}"
19 - if (result=/^(.*)r(.*)u(.*)s/.match(t))
19 + #if (result=/^(.*)real(.*)wall(.*)s/.match(t))
20 + if (result=/^OK \((.*) sec real\, (.*) sec wall,.*MB, (.*) syscalls/.match(t))
20 {:real => result[1], :user => result[2], :sys => result[3]}
21 {:real => result[1], :user => result[2], :sys => result[3]}
21 else
22 else
22 #{:real => 0, :user => 0, :sys => 0}
23 #{:real => 0, :user => 0, :sys => 0}
23 #puts "ERROR READING RUNNING TIME: #{t}"
24 #puts "ERROR READING RUNNING TIME: #{t}"
24 raise "Error reading running time: #{t}"
25 raise "Error reading running time: #{t}"
25 end
26 end
26 end
27 end
27
28
28 def compile_box(source,bin)
29 def compile_box(source,bin)
29 system("g++ #{source} -o #{bin}")
30 system("g++ #{source} -o #{bin}")
30 end
31 end
31
32
32 if ARGV.length < 2 || ARGV.length > 3
33 if ARGV.length < 2 || ARGV.length > 3
33 puts "Usage: run <language> <test-num> [<program-name>]"
34 puts "Usage: run <language> <test-num> [<program-name>]"
34 exit(127)
35 exit(127)
35 end
36 end
36
37
37 language = ARGV[0]
38 language = ARGV[0]
38 test_num = ARGV[1].to_i
39 test_num = ARGV[1].to_i
39 if ARGV.length > 2
40 if ARGV.length > 2
40 program_name = ARGV[2]
41 program_name = ARGV[2]
41 else
42 else
42 program_name = "a.out"
43 program_name = "a.out"
43 end
44 end
44
45
45 problem_home = ENV['PROBLEM_HOME']
46 problem_home = ENV['PROBLEM_HOME']
46 require "#{problem_home}/script/test_dsl.rb"
47 require "#{problem_home}/script/test_dsl.rb"
47 load "#{problem_home}/test_cases/all_tests.cfg"
48 load "#{problem_home}/test_cases/all_tests.cfg"
48 problem = Problem.get_instance
49 problem = Problem.get_instance
49
50
50 if problem.well_formed? == false
51 if problem.well_formed? == false
51 log "The problem specification is not well formed."
52 log "The problem specification is not well formed."
52 exit(127)
53 exit(127)
53 end
54 end
54
55
55 # Check if the test number is okay.
56 # Check if the test number is okay.
56 if test_num <= 0 || test_num > problem.num_tests
57 if test_num <= 0 || test_num > problem.num_tests
57 log "You have specified a wrong test number."
58 log "You have specified a wrong test number."
58 exit(127)
59 exit(127)
59 end
60 end
60
61
61 #####################################
62 #####################################
62 # Set the relavant file names here. #
63 # Set the relavant file names here. #
63 #####################################
64 #####################################
64
65
65 input_file_name = "#{problem_home}/test_cases/#{test_num}/input-#{test_num}.txt"
66 input_file_name = "#{problem_home}/test_cases/#{test_num}/input-#{test_num}.txt"
66
67
67 #####################################
68 #####################################
68
69
69 time_limit = problem.get_time_limit test_num
70 time_limit = problem.get_time_limit test_num
70 mem_limit = problem.get_mem_limit(test_num) * 1024
71 mem_limit = problem.get_mem_limit(test_num) * 1024
71
72
72 # Copy the input file.
73 # Copy the input file.
73 #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .`
74 #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .`
74
75
75 # check if box is there, if not, compile it!
76 # check if box is there, if not, compile it!
76 if !File.exists?("#{problem_home}/script/box")
77 if !File.exists?("#{problem_home}/script/box")
77 log "WARNING: Compiling box: to increase efficiency, it should be compile manually"
78 log "WARNING: Compiling box: to increase efficiency, it should be compile manually"
78 compile_box("#{problem_home}/script/box.cc",
79 compile_box("#{problem_home}/script/box.cc",
79 "#{problem_home}/script/box")
80 "#{problem_home}/script/box")
80 end
81 end
81
82
82 # Hide PROBLEM_HOME
83 # Hide PROBLEM_HOME
83 ENV['PROBLEM_HOME'] = nil
84 ENV['PROBLEM_HOME'] = nil
84
85
85 # Run the program.
86 # Run the program.
86 #run_command = "/usr/bin/time -f \"#{time_output_format}\" 2>run_result #{problem_home}/script/box_new -a 2 -f -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name}"
87 #run_command = "/usr/bin/time -f \"#{time_output_format}\" 2>run_result #{problem_home}/script/box_new -a 2 -f -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name}"
88 + #
87
89
88 - run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name} 2>run_result"
90 +
91 +
92 +
93 + case language
94 + when "java"
95 + # for java, we have to add additional systemcall and we don't check the mem limit (dunno how to fix...)
96 + run_command = "#{problem_home}/script/box -T -t #{time_limit} -s getppid -s clone -s wait4 -p /usr/bin/ -p ./ -i #{input_file_name} -o output.txt #{program_name} 2>run_result"
97 + when "ruby"
98 + run_command = "#{problem_home}/script/box -T -t #{time_limit} -s getppid -s wait4 -s clone -s set_robust_list -s futex -s sigaltstack -p /dev/urandom -p ./ -p /home/dae/.rvm/rubies/ruby-1.9.2-p320/ -p #{problem_home}/ -i #{input_file_name} -o output.txt #{program_name} 2>run_result"
99 + when "c++"
100 + run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name} 2>run_result"
101 + else
102 + run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} -i #{input_file_name} -o output.txt #{program_name} 2>run_result"
103 + end
104 +
89
105
90 log "Running test #{test_num}..."
106 log "Running test #{test_num}..."
91 log run_command
107 log run_command
92 log
108 log
93 system(run_command)
109 system(run_command)
94
110
95 # Restore PROBLEM_HOME
111 # Restore PROBLEM_HOME
96 ENV['PROBLEM_HOME'] = problem_home
112 ENV['PROBLEM_HOME'] = problem_home
97
113
98 # Create the result file.
114 # Create the result file.
99 result_file = File.new("result", "w")
115 result_file = File.new("result", "w")
100 comment_file = File.new("comment", "w")
116 comment_file = File.new("comment", "w")
101
117
102 # Check if the program actually produced any output.
118 # Check if the program actually produced any output.
103 run_result_file = File.new("run_result", "r")
119 run_result_file = File.new("run_result", "r")
104 run_result = run_result_file.readlines
120 run_result = run_result_file.readlines
105 run_result_file.close
121 run_result_file.close
106
122
107 run_stat = run_result[run_result.length-1]
123 run_stat = run_result[run_result.length-1]
108 running_time = extract_time(run_stat)
124 running_time = extract_time(run_stat)
109
125
110 report = lambda{ |status, points, comment|
126 report = lambda{ |status, points, comment|
111 result_file.write status.strip
127 result_file.write status.strip
112 result_file.write "\n"
128 result_file.write "\n"
113 result_file.write points.to_s.strip
129 result_file.write points.to_s.strip
114 result_file.write "\n"
130 result_file.write "\n"
115 result_file.write run_stat.strip
131 result_file.write run_stat.strip
116 result_file.write "\n"
132 result_file.write "\n"
117 result_file.close
133 result_file.close
118 FileUtils.rm "run_result"
134 FileUtils.rm "run_result"
119 # `rm output.txt` --- keep the output
135 # `rm output.txt` --- keep the output
120
136
121 comment_file.write comment
137 comment_file.write comment
122
138
123 # added for debuggin --- jittat
139 # added for debuggin --- jittat
124 comment_file.write "--run-result--\n"
140 comment_file.write "--run-result--\n"
125 run_result.each do |l|
141 run_result.each do |l|
126 comment_file.write l
142 comment_file.write l
127 end
143 end
128
144
129 comment_file.close
145 comment_file.close
130
146
131 log "Done!"
147 log "Done!"
132 exit(0)
148 exit(0)
133 }
149 }
134
150
135 if run_result[0][0,2] != "OK"
151 if run_result[0][0,2] != "OK"
136 log "There was a runtime error."
152 log "There was a runtime error."
137 report.call(run_result[0], 0, "No comment.\n")
153 report.call(run_result[0], 0, "No comment.\n")
138 end
154 end
139
155
140 - if running_time[:user].to_f + running_time[:sys].to_f > time_limit
156 + if running_time[:user].to_f > time_limit
141 log "Time limit exceeded."
157 log "Time limit exceeded."
142 report.call("Time limit exceeded", 0, "No comment.\n")
158 report.call("Time limit exceeded", 0, "No comment.\n")
143 end
159 end
144
160
145 # Run 'check' to evaluate the output.
161 # Run 'check' to evaluate the output.
146 #puts "There was no runtime error. Proceed to checking the output."
162 #puts "There was no runtime error. Proceed to checking the output."
147 check_command = "#{problem_home}/script/check #{language} #{test_num}"
163 check_command = "#{problem_home}/script/check #{language} #{test_num}"
148 log "Checking the output..."
164 log "Checking the output..."
149 log check_command
165 log check_command
150 if not system(check_command)
166 if not system(check_command)
151 log "Problem with check script"
167 log "Problem with check script"
152 report.call("Incorrect",0,"Check script error.\n")
168 report.call("Incorrect",0,"Check script error.\n")
153 exit(127)
169 exit(127)
154 end
170 end
155
171
156 check_file = File.new("check_result", "r")
172 check_file = File.new("check_result", "r")
157 check_file_lines = check_file.readlines
173 check_file_lines = check_file.readlines
158
174
159 report.call(check_file_lines[0], check_file_lines[1], "No comment.\n")
175 report.call(check_file_lines[0], check_file_lines[1], "No comment.\n")
@@ -1,49 +1,49
1 - #!/usr/bin/ruby
1 + #!/usr/bin/env ruby
2
2
3 problem_home = ENV['PROBLEM_HOME']
3 problem_home = ENV['PROBLEM_HOME']
4 require "#{problem_home}/script/test_dsl.rb"
4 require "#{problem_home}/script/test_dsl.rb"
5
5
6 if ARGV.length < 2
6 if ARGV.length < 2
7 puts "Usage: check <language> <test-number> [<output-file>]"
7 puts "Usage: check <language> <test-number> [<output-file>]"
8 exit(0)
8 exit(0)
9 end
9 end
10
10
11 language = ARGV[0]
11 language = ARGV[0]
12 test_num = ARGV[1].to_i
12 test_num = ARGV[1].to_i
13 if ARGV.length >= 3
13 if ARGV.length >= 3
14 output_file_name = ARGV[2]
14 output_file_name = ARGV[2]
15 else
15 else
16 output_file_name = "output.txt"
16 output_file_name = "output.txt"
17 end
17 end
18
18
19 load "#{problem_home}/test_cases/all_tests.cfg"
19 load "#{problem_home}/test_cases/all_tests.cfg"
20 problem = Problem.get_instance
20 problem = Problem.get_instance
21
21
22 output_file = File.new(output_file_name, "r")
22 output_file = File.new(output_file_name, "r")
23 answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt")
23 answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt")
24 result_file = File.new("check_result", "w")
24 result_file = File.new("check_result", "w")
25
25
26 output_file_content = output_file.read
26 output_file_content = output_file.read
27 answer_file_content = answer_file.read
27 answer_file_content = answer_file.read
28
28
29 report_correct = lambda {
29 report_correct = lambda {
30 result_file.write "Correct\n"
30 result_file.write "Correct\n"
31 result_file.write problem.get_score(test_num)
31 result_file.write problem.get_score(test_num)
32 result_file.write "\n"
32 result_file.write "\n"
33 result_file.close
33 result_file.close
34 exit(0)
34 exit(0)
35 }
35 }
36
36
37 report_wrong = lambda {
37 report_wrong = lambda {
38 result_file.write "Incorrect\n"
38 result_file.write "Incorrect\n"
39 result_file.write "0\n"
39 result_file.write "0\n"
40 result_file.close
40 result_file.close
41 exit(0)
41 exit(0)
42 }
42 }
43
43
44 ##################
44 ##################
45 # Your code here #
45 # Your code here #
46 ##################
46 ##################
47
47
48 ########### THIS IS FOR CHECKING TEXT ##########
48 ########### THIS IS FOR CHECKING TEXT ##########
49
49
@@ -1,47 +1,47
1 - #!/usr/bin/ruby
1 + #!/usr/bin/env ruby
2
2
3 problem_home = ENV['PROBLEM_HOME']
3 problem_home = ENV['PROBLEM_HOME']
4 require "#{problem_home}/script/test_dsl.rb"
4 require "#{problem_home}/script/test_dsl.rb"
5
5
6 if ARGV.length < 2
6 if ARGV.length < 2
7 puts "Usage: check <language> <test-number> [<output-file>]"
7 puts "Usage: check <language> <test-number> [<output-file>]"
8 exit(0)
8 exit(0)
9 end
9 end
10
10
11 language = ARGV[0]
11 language = ARGV[0]
12 test_num = ARGV[1].to_i
12 test_num = ARGV[1].to_i
13 if ARGV.length >= 3
13 if ARGV.length >= 3
14 output_file_name = ARGV[2]
14 output_file_name = ARGV[2]
15 else
15 else
16 output_file_name = "output.txt"
16 output_file_name = "output.txt"
17 end
17 end
18
18
19 load "#{problem_home}/test_cases/all_tests.cfg"
19 load "#{problem_home}/test_cases/all_tests.cfg"
20 problem = Problem.get_instance
20 problem = Problem.get_instance
21
21
22 output_file = File.new(output_file_name, "r")
22 output_file = File.new(output_file_name, "r")
23 answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt")
23 answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt")
24 result_file = File.new("check_result", "w")
24 result_file = File.new("check_result", "w")
25
25
26 output_file_content = output_file.read
26 output_file_content = output_file.read
27 answer_file_content = answer_file.read
27 answer_file_content = answer_file.read
28
28
29 report_correct = lambda {
29 report_correct = lambda {
30 result_file.write "Correct\n"
30 result_file.write "Correct\n"
31 result_file.write problem.get_score(test_num)
31 result_file.write problem.get_score(test_num)
32 result_file.write "\n"
32 result_file.write "\n"
33 result_file.close
33 result_file.close
34 exit(0)
34 exit(0)
35 }
35 }
36
36
37 report_wrong = lambda {
37 report_wrong = lambda {
38 result_file.write "Incorrect\n"
38 result_file.write "Incorrect\n"
39 result_file.write "0\n"
39 result_file.write "0\n"
40 result_file.close
40 result_file.close
41 exit(0)
41 exit(0)
42 }
42 }
43
43
44 ##################
44 ##################
45 # Your code here #
45 # Your code here #
46 ##################
46 ##################
47 report_correct.call
47 report_correct.call
You need to be logged in to leave comments. Login now