156 lines
6.0 KiB
Ruby
156 lines
6.0 KiB
Ruby
require 'rails_helper'
|
|
require_relative '../helpers/c_helper'
|
|
|
|
RSpec.configure do |c|
|
|
c.include Helpers::CFile
|
|
end
|
|
|
|
RSpec.describe 'Judge::Core::Executor' do
|
|
|
|
Executor = Judge::Core::Executor
|
|
|
|
describe "java #run" do
|
|
it "测试1+1可以正确输出" do
|
|
javaFile = File.expand_path('../../javafiles/add.java', __FILE__)
|
|
classFile = File.expand_path('../../javafiles/add.class', __FILE__)
|
|
compiler = Judge::Core::Compiler.new(source_file: javaFile, language: 'java', binary_file: classFile)
|
|
|
|
executor = Executor.new(program: "/usr/bin/java", program_arg: "-cp #{Pathname.new(classFile).dirname.to_s},add", program_param: "3 2")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("5")
|
|
|
|
end
|
|
|
|
it "结果不正确,可以正常报错" do
|
|
|
|
javaFile = File.expand_path('../../javafiles/exception.java', __FILE__)
|
|
classFile = File.expand_path('../../javafiles/exception.class', __FILE__)
|
|
compiler = Judge::Core::Compiler.new(source_file: javaFile, language: 'java', binary_file: classFile)
|
|
expect(compiler.compile).to be true
|
|
|
|
executor = Executor.new(program: "/usr/bin/java", program_arg: "-cp #{Pathname.new(classFile).dirname.to_s},exception", program_param: "4 2")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.success?).to be true
|
|
expect(executor.result).to match(/java.lang.Exception/)
|
|
end
|
|
|
|
end
|
|
|
|
describe "python #run" do
|
|
it "测试1+1可以正确输出" do
|
|
cFile = File.expand_path('../../pyfiles/add.py', __FILE__)
|
|
executor = Executor.new(program: "/usr/bin/python", program_arg: "#{cFile}", program_param: "4 2")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("6")
|
|
end
|
|
|
|
it "代码异常会报出错误信息" do
|
|
cFile = File.expand_path('../../pyfiles/error.py', __FILE__)
|
|
executor = Executor.new(program: "/usr/bin/python", program_arg: "#{cFile}", program_param: "4 2")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.success?).to be false
|
|
end
|
|
end
|
|
|
|
describe "c #run" do
|
|
it "测试x+1可以正确输出" do
|
|
compile_add_one_c
|
|
executor = Executor.new(program_param: '1')
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result == '2')
|
|
end
|
|
|
|
it "程序错误,执行失败" do
|
|
compile_bad_c
|
|
executor = Executor.new
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.success?).to be false
|
|
expect(rs).to eq(Executor::RS_RTE)
|
|
end
|
|
|
|
it "超过内存,执行失败" do
|
|
compile_big_memory_c
|
|
executor = Executor.new
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.success?).to be false
|
|
expect(rs).to eq(Executor::RS_MLE)
|
|
end
|
|
|
|
|
|
it "越权操作,执行失败" do
|
|
compile_permit_c
|
|
executor = Executor.new
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.success?).to be false
|
|
expect(rs).to eq(Executor::RS_SYS)
|
|
end
|
|
|
|
it "执行成功,给出时间,内存,结果" do
|
|
compile_valid_c
|
|
executor = Executor.new(program: '/tmp/test', program_param: '4 2')
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(rs).not_to be nil
|
|
expect(option).not_to be nil
|
|
expect(time_used).not_to be nil
|
|
expect(memory_used).not_to be nil
|
|
expect(executor.success?).to be true
|
|
expect(executor.result).to eq("6")
|
|
end
|
|
|
|
it "超过时间,执行失败" do
|
|
compile_timeout_c
|
|
executor = Executor.new
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.success?).to be false
|
|
expect(rs).to eq(Executor::RS_TLE)
|
|
end
|
|
|
|
describe "执行老师的6个题" do
|
|
it "第一题" do
|
|
cFile = File.expand_path('../../cfiles/1.c', __FILE__)
|
|
Judge::Core::Compiler.new(source_file: cFile, out_file: "/tmp/test").compile
|
|
executor = Executor.new(program: '/tmp/test', program_param: '3')
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("3")
|
|
end
|
|
it "第二题" do
|
|
cFile = File.expand_path('../../cfiles/2.c', __FILE__)
|
|
Judge::Core::Compiler.new(source_file: cFile, out_file: "/tmp/test").compile
|
|
executor = Executor.new(program: '/tmp/test', program_param: "4\n23.45 35.7 54.89 47")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("11.862")
|
|
end
|
|
it "第三题" do
|
|
cFile = File.expand_path('../../cfiles/3.cpp', __FILE__)
|
|
Judge::Core::Compiler.new(source_file: cFile, out_file: "/tmp/test", language: 'cpp').compile
|
|
executor = Executor.new(program: '/tmp/test', program_param: "2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2\n2 2 2 2")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("yes")
|
|
end
|
|
it "第四题" do
|
|
cFile = File.expand_path('../../cfiles/4.c', __FILE__)
|
|
Judge::Core::Compiler.new(source_file: cFile, out_file: "/tmp/test").compile
|
|
executor = Executor.new(program: '/tmp/test', program_param: "6\n10 1 10 20 30 20")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("10")
|
|
end
|
|
it "第五题" do
|
|
cFile = File.expand_path('../../cfiles/5.c', __FILE__)
|
|
Judge::Core::Compiler.new(source_file: cFile, out_file: "/tmp/test").compile
|
|
executor = Executor.new(program: '/tmp/test', program_param: "2\n1 1 4 4\n2 3 6 5")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("15")
|
|
end
|
|
it "第六题" do
|
|
cFile = File.expand_path('../../cfiles/6.c', __FILE__)
|
|
Judge::Core::Compiler.new(source_file: cFile, out_file: "/tmp/test").compile
|
|
executor = Executor.new(program: '/tmp/test', program_param: "0-670-82162-0")
|
|
rs,option,time_used,memory_used = executor.run
|
|
expect(executor.result).to eq("0-670-82162-4")
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
end
|