Description:
clean up run command to check for running time / memory merge check.float
Commit status:
[Not Reviewed]
References:
merge algo
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r161:d5a0f0a406b5 - - 4 files changed: 82 inserted, 9 deleted

@@ -0,0 +1,66
1 + #!/usr/bin/env ruby
2 +
3 + problem_home = ENV['PROBLEM_HOME']
4 + require "#{problem_home}/script/test_dsl.rb"
5 +
6 + if ARGV.length < 2
7 + puts "Usage: check <language> <test-number> [<output-file>]"
8 + exit(0)
9 + end
10 +
11 + language = ARGV[0]
12 + test_num = ARGV[1].to_i
13 + if ARGV.length >= 3
14 + output_file_name = ARGV[2]
15 + else
16 + output_file_name = "output.txt"
17 + end
18 +
19 + load "#{problem_home}/test_cases/all_tests.cfg"
20 + problem = Problem.get_instance
21 +
22 + output_file = File.new(output_file_name, "r")
23 + answer_file = File.new("#{problem_home}/test_cases/#{test_num}/answer-#{test_num}.txt")
24 + result_file = File.new("check_result", "w")
25 +
26 + output_file_content = output_file.read
27 + answer_file_content = answer_file.read
28 +
29 + report_correct = lambda {
30 + result_file.write "Correct\n"
31 + result_file.write problem.get_score(test_num)
32 + result_file.write "\n"
33 + result_file.close
34 + exit(0)
35 + }
36 +
37 + report_wrong = lambda {
38 + result_file.write "Incorrect\n"
39 + result_file.write "0\n"
40 + result_file.close
41 + exit(0)
42 + }
43 +
44 + ##################
45 + # Your code here #
46 + ##################
47 +
48 + ########### THIS IS FOR CHECKING FLOAT with EPSILON error ##########
49 +
50 + EPSILON = 0.000001
51 +
52 + out_items = output_file_content.split
53 + ans_items = answer_file_content.split
54 +
55 + if out_items.length != ans_items.length
56 + report_wrong.call
57 + else
58 + out_items.length.times do |i|
59 + out_value = out_items[i].to_f
60 + ans_value = ans_items[i].to_f
61 + if (out_value - ans_value).abs > EPSILON * [out_value.abs,ans_value.abs].max
62 + report_wrong.call
63 + end
64 + end
65 + report_correct.call
66 + end
@@ -138,24 +138,25
138 command = "#{PYTHON_CHECKER} #{params[:source_file]} > #{params[:message_file]}"
138 command = "#{PYTHON_CHECKER} #{params[:source_file]} > #{params[:message_file]}"
139 if system(command)
139 if system(command)
140 #compile to python bytecode
140 #compile to python bytecode
141 command = "#{PYTHON_INTERPRETER} -m py_compile #{params[:source_file]}"
141 command = "#{PYTHON_INTERPRETER} -m py_compile #{params[:source_file]}"
142 puts "compile: #{command}"
142 puts "compile: #{command}"
143 system(command)
143 system(command)
144 puts "pwd: " + Dir.pwd
144 puts "pwd: " + Dir.pwd
145 Dir.new('.').each {|file| puts file}
145 Dir.new('.').each {|file| puts file}
146 File.open(params[:output_file],"w") do |out_file|
146 File.open(params[:output_file],"w") do |out_file|
147 out_file.puts "#!#{PYTHON_INTERPRETER} #{params[:source_file]}c"
147 out_file.puts "#!#{PYTHON_INTERPRETER} #{params[:source_file]}c"
148 end
148 end
149 File.chmod(0755, params[:output_file])
149 File.chmod(0755, params[:output_file])
150 + FileUtils.cp("#{params[:source_file]}c",params[:output_file])
150 end
151 end
151
152
152 else
153 else
153 talk("ERROR: Invalid language specified!")
154 talk("ERROR: Invalid language specified!")
154 open(params[:message_file],"w") do |f|
155 open(params[:message_file],"w") do |f|
155 f.puts "ERROR: Invalid language specified!"
156 f.puts "ERROR: Invalid language specified!"
156 end
157 end
157 exit(127)
158 exit(127)
158 end
159 end
159
160
160 # Report success or failure.
161 # Report success or failure.
161 if FileTest.exists? params[:output_file]
162 if FileTest.exists? params[:output_file]
@@ -50,24 +50,25
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" && language != "java" && language != "ruby" && language != "python"
56 if language != "c" && language != "c++" && language != "pas" && language != "java" && language != "ruby" && language != "python"
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 + ENV['SOURCE_NAME'] = source_file
62 if File.exist?(source_file) == false
63 if File.exist?(source_file) == false
63 log "The source file does not exist."
64 log "The source file does not exist."
64 exit(127)
65 exit(127)
65 end
66 end
66
67
67 log "Making test result and sandbox directories..."
68 log "Making test result and sandbox directories..."
68
69
69 current_dir = FileUtils.pwd
70 current_dir = FileUtils.pwd
70 current_dir.strip!
71 current_dir.strip!
71
72
72 if ARGV.length >= 3
73 if ARGV.length >= 3
73 test_result_dir = ARGV[2]
74 test_result_dir = ARGV[2]
@@ -131,25 +132,25
131 $stdout.print "[#{test_num}]"
132 $stdout.print "[#{test_num}]"
132 $stdout.flush
133 $stdout.flush
133
134
134 log "Test number: #{test_num}"
135 log "Test number: #{test_num}"
135
136
136 call_and_log("Cannot copy the compiled program into #{sandbox_dir}") {
137 call_and_log("Cannot copy the compiled program into #{sandbox_dir}") {
137 FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true)
138 FileUtils.cp("#{test_result_dir}/a.out", sandbox_dir, :preserve => true)
138 if language == "java" then Dir["#{test_result_dir}/*.class"].each { |file| FileUtils.cp(file,sandbox_dir)} end
139 if language == "java" then Dir["#{test_result_dir}/*.class"].each { |file| FileUtils.cp(file,sandbox_dir)} end
139 if language == "python" then Dir["#{test_result_dir}/*.pyc"].each { |file| FileUtils.cp(file,sandbox_dir)} end
140 if language == "python" then Dir["#{test_result_dir}/*.pyc"].each { |file| FileUtils.cp(file,sandbox_dir)} end
140 }
141 }
141
142
142 begin
143 begin
143 - execute("#{problem_home}/script/run #{language} #{test_num}", "Error occured during execution of the run script")
144 + execute("#{problem_home}/script/run #{language} #{test_num} ", "Error occured during execution of the run script")
144 rescue
145 rescue
145 # do nothing
146 # do nothing
146 end
147 end
147
148
148 call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") {
149 call_and_log("Cannot create directory #{test_result_dir}/#{test_num}") {
149 FileUtils.mkdir "#{test_result_dir}/#{test_num}"
150 FileUtils.mkdir "#{test_result_dir}/#{test_num}"
150 }
151 }
151 call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") {
152 call_and_log("Cannot copy the result file into #{test_result_dir}/#{test_num}") {
152 FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}"
153 FileUtils.mv "#{sandbox_dir}/result", "#{test_result_dir}/#{test_num}"
153 }
154 }
154 call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") {
155 call_and_log("Cannot copy the comment file into #{test_result_dir}/#{test_num}") {
155 FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}"
156 FileUtils.mv "#{sandbox_dir}/comment", "#{test_result_dir}/#{test_num}"
@@ -34,28 +34,31
34 exit(127)
34 exit(127)
35 end
35 end
36
36
37 language = ARGV[0]
37 language = ARGV[0]
38 test_num = ARGV[1].to_i
38 test_num = ARGV[1].to_i
39 if ARGV.length > 2
39 if ARGV.length > 2
40 program_name = ARGV[2]
40 program_name = ARGV[2]
41 else
41 else
42 program_name = "a.out"
42 program_name = "a.out"
43 end
43 end
44
44
45 problem_home = ENV['PROBLEM_HOME']
45 problem_home = ENV['PROBLEM_HOME']
46 + source_name = ENV['SOURCE_NAME']
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
51 + sandbox_dir = Dir.getwd
52 +
50 if problem.well_formed? == false
53 if problem.well_formed? == false
51 log "The problem specification is not well formed."
54 log "The problem specification is not well formed."
52 exit(127)
55 exit(127)
53 end
56 end
54
57
55 # Check if the test number is okay.
58 # Check if the test number is okay.
56 if test_num <= 0 || test_num > problem.num_tests
59 if test_num <= 0 || test_num > problem.num_tests
57 log "You have specified a wrong test number."
60 log "You have specified a wrong test number."
58 exit(127)
61 exit(127)
59 end
62 end
60
63
61 #####################################
64 #####################################
@@ -72,55 +75,56
72 # Copy the input file.
75 # Copy the input file.
73 #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .`
76 #`cp #{problem_home}/test_cases/#{test_num}/#{input_file_name} .`
74
77
75 # check if box is there, if not, compile it!
78 # check if box is there, if not, compile it!
76 if !File.exists?("#{problem_home}/script/box")
79 if !File.exists?("#{problem_home}/script/box")
77 log "WARNING: Compiling box: to increase efficiency, it should be compile manually"
80 log "WARNING: Compiling box: to increase efficiency, it should be compile manually"
78 compile_box("#{problem_home}/script/box.cc",
81 compile_box("#{problem_home}/script/box.cc",
79 "#{problem_home}/script/box")
82 "#{problem_home}/script/box")
80 end
83 end
81
84
82 # Hide PROBLEM_HOME
85 # Hide PROBLEM_HOME
83 ENV['PROBLEM_HOME'] = nil
86 ENV['PROBLEM_HOME'] = nil
87 + ENV['SOURCE_NAME'] = nil
84
88
85 # Run the program.
89 # 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}"
90 #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 #
91 #
88
92
89 -
93 + JAVA_OPTION = "-s set_robust_list -s futex -s clone -s getppid -s clone -s wait4 -p /usr/bin/ -p ./"
90 -
94 + RUBY_OPTION = "-p /usr/lib64/ -p /lib64/ -p /dev/urandom -p #{sandbox_dir}/#{program_name} -s set_robust_list -s sched_getaffinity -s clock_gettime -s sigaltstack -s pipe2 -s clone -s futex -s openat"
95 + PYTHON_OPTION = "-p /usr/lib64/ -p /lib64/ -p /usr/bin/ -p /usr/local/lib64/ -p /usr/local/lib/ -p #{sandbox_dir}/#{program_name} -p ./#{program_name} -p #{sandbox_dir}/#{source_name} -s set_robust_list -s openat -s recvmsg -s connect -s socket -s sendto -E PYTHONNOUSERSITE=yes"
91
96
92 case language
97 case language
93 when "java"
98 when "java"
94 -
95 # for java, extract the classname
99 # for java, extract the classname
96 # wne have to add additional systemcall and we don't check the mem limit (dunno how to fix...)
100 # wne have to add additional systemcall and we don't check the mem limit (dunno how to fix...)
97 classname = 'DUMMY'
101 classname = 'DUMMY'
98 File.open(program_name,"r").each do |line|
102 File.open(program_name,"r").each do |line|
99 classname = line
103 classname = line
100 end
104 end
101 - 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 /usr/bin/java #{classname} 2>run_result"
105 + #for java, we cannot really check the memory limit...
106 + run_command = "#{problem_home}/script/box -a 3 -f -T -t #{time_limit} #{JAVA_OPTION} -i #{input_file_name} -o output.txt /usr/bin/java #{classname} 2>run_result"
102 when "ruby"
107 when "ruby"
103 - 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"
108 + run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} #{RUBY_OPTION} -i #{input_file_name} -o output.txt /usr/bin/ruby #{program_name} 2>run_result"
104 when "python"
109 when "python"
105 - #this code just run without any checking
110 + run_command = "#{problem_home}/script/box -a 2 -f -T -t #{time_limit} -m #{mem_limit} #{PYTHON_OPTION} -i #{input_file_name} -o output.txt /usr/bin/python #{program_name} 2>run_result"
106 - run_command = "#{problem_home}/script/box -T -t #{time_limit} -p #{problem_home}/ -i #{input_file_name} -o output.txt #{program_name} 2>run_result"
107 else # for c++, pascal, we do the normal checking
111 else # for c++, pascal, we do the normal checking
108 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"
112 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"
109 end
113 end
110
114
111
115
112 log "Running test #{test_num}..."
116 log "Running test #{test_num}..."
113 log run_command
117 log run_command
114 - log
118 + log
115 system(run_command)
119 system(run_command)
116
120
117 # Restore PROBLEM_HOME
121 # Restore PROBLEM_HOME
118 ENV['PROBLEM_HOME'] = problem_home
122 ENV['PROBLEM_HOME'] = problem_home
119
123
120 # Create the result file.
124 # Create the result file.
121 result_file = File.new("result", "w")
125 result_file = File.new("result", "w")
122 comment_file = File.new("comment", "w")
126 comment_file = File.new("comment", "w")
123
127
124 # Check if the program actually produced any output.
128 # Check if the program actually produced any output.
125 run_result_file = File.new("run_result", "r")
129 run_result_file = File.new("run_result", "r")
126 run_result = run_result_file.readlines
130 run_result = run_result_file.readlines
@@ -145,24 +149,25
145 # added for debuggin --- jittat
149 # added for debuggin --- jittat
146 comment_file.write "--run-result--\n"
150 comment_file.write "--run-result--\n"
147 run_result.each do |l|
151 run_result.each do |l|
148 comment_file.write l
152 comment_file.write l
149 end
153 end
150
154
151 comment_file.close
155 comment_file.close
152
156
153 log "Done!"
157 log "Done!"
154 exit(0)
158 exit(0)
155 }
159 }
156
160
161 +
157 if run_result[0][0,2] != "OK"
162 if run_result[0][0,2] != "OK"
158 log "There was a runtime error."
163 log "There was a runtime error."
159 report.call(run_result[0], 0, "No comment.\n")
164 report.call(run_result[0], 0, "No comment.\n")
160 end
165 end
161
166
162 if running_time[:user].to_f > time_limit
167 if running_time[:user].to_f > time_limit
163 log "Time limit exceeded."
168 log "Time limit exceeded."
164 report.call("Time limit exceeded", 0, "No comment.\n")
169 report.call("Time limit exceeded", 0, "No comment.\n")
165 end
170 end
166
171
167 # Run 'check' to evaluate the output.
172 # Run 'check' to evaluate the output.
168 #puts "There was no runtime error. Proceed to checking the output."
173 #puts "There was no runtime error. Proceed to checking the output."
You need to be logged in to leave comments. Login now