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
@@ -144,12 +144,13
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!"
@@ -56,12 +56,13
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..."
@@ -137,13 +138,13
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}"
@@ -40,16 +40,19
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.
@@ -78,43 +81,44
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.
@@ -151,12 +155,13
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
You need to be logged in to leave comments. Login now