Skip to content

Commit

Permalink
Migrate test-collection to judger-core
Browse files Browse the repository at this point in the history
Co-authored-by: KanadeSiina [email protected]
Co-authored-by: cubercsl [email protected]
  • Loading branch information
slhmy committed Dec 30, 2023
1 parent 7eca114 commit cbdbff2
Show file tree
Hide file tree
Showing 38 changed files with 526 additions and 277 deletions.
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

41 changes: 0 additions & 41 deletions judge-core/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,44 +175,3 @@ impl Compiler {
}
}
}

#[cfg(test)]
pub mod compiler {
use std::path::PathBuf;

use super::{Compiler, Language};

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}

#[test]
fn test_compile_cpp() {
init();
let compiler = Compiler::new(Language::Cpp, vec!["-std=c++17".to_string()]);
match compiler.compile(
&PathBuf::from("../test-collection/src/programs/infinite_loop.cpp"),
&PathBuf::from("../tmp/infinite_loop_test"),
) {
Ok(out) => {
log::info!("{}", out);
}
Err(e) => panic!("{:?}", e),
}
}

#[test]
fn test_compile_py() {
init();
let compiler = Compiler::new(Language::Python, vec![]);
match compiler.compile(
&PathBuf::from("../test-collection/src/programs/read_and_write.py"),
&PathBuf::from("../tmp/read_and_write"),
) {
Ok(out) => {
log::info!("{}", out);
}
Err(e) => panic!("{:?}", e),
}
}
}
43 changes: 0 additions & 43 deletions judge-core/src/judge/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,46 +77,3 @@ impl JudgeBuilder {
})
}
}

#[cfg(test)]
pub mod builder {
use crate::judge::{common::run_judge, JudgeConfig};

use super::{JudgeBuilder, JudgeBuilderInput, Language, PackageType};
use std::path::PathBuf;

fn init() {
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.try_init();
}

#[test]
fn test_build_icpc() {
init();
let builder = JudgeBuilder::new(JudgeBuilderInput {
package_type: PackageType::ICPC,
package_path: PathBuf::from("../test-collection/packages/icpc/hello_world"),
runtime_path: PathBuf::from("../tmp/icpc"),
src_language: Language::Cpp,
src_path: PathBuf::from("../test-collection/src/programs/read_and_write.cpp"),
})
.unwrap();
log::info!("builder: {:?}", builder);
for idx in 0..builder.testdata_configs.len() {
log::info!("runing testdata {}", idx);
let judge_config = JudgeConfig {
test_data: builder.testdata_configs[idx].clone(),
program: builder.program_config.clone(),
checker: builder.checker_config.clone(),
runtime: builder.runtime_config.clone(),
};

let res = run_judge(&judge_config);
match res {
Ok(info) => log::info!("{:?}", info),
Err(e) => panic!("{:?}", e),
}
}
}
}
97 changes: 0 additions & 97 deletions judge-core/src/judge/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,100 +130,3 @@ pub fn run_judge(config: &JudgeConfig) -> Result<JudgeResultInfo, JudgeCoreError
})
}
}

#[cfg(test)]
pub mod common_judge_tests {
use std::path::PathBuf;

use crate::{
compiler::Language,
judge::{
result::JudgeVerdict, CheckerConfig, JudgeConfig, ProgramConfig, RuntimeConfig,
TestdataConfig,
},
run::{executor::Executor, RlimitConfigs},
};

use super::run_judge;

const TEST_CONFIG: RlimitConfigs = RlimitConfigs {
stack_limit: Some((64 * 1024 * 1024, 64 * 1024 * 1024)),
as_limit: Some((64 * 1024 * 1024, 64 * 1024 * 1024)),
cpu_limit: Some((1, 2)),
nproc_limit: Some((1, 1)),
fsize_limit: Some((1024, 1024)),
};

fn init() {
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.try_init();
}

fn build_test_config(program_executor: Executor) -> JudgeConfig {
JudgeConfig {
runtime: RuntimeConfig {
rlimit_configs: TEST_CONFIG,
},
test_data: TestdataConfig {
input_file_path: PathBuf::from("../tmp/in"),
answer_file_path: PathBuf::from("../tmp/ans"),
},
checker: CheckerConfig {
executor: None,
output_file_path: PathBuf::from("../tmp/check"),
},
program: ProgramConfig {
executor: program_executor,
output_file_path: PathBuf::from("../tmp/out"),
},
}
}

#[test]
fn test_run_judge() {
init();
let program_path = PathBuf::from("./../test-collection/dist/programs/read_and_write");
let program_executor = Executor::new(Language::Cpp, program_path).unwrap();

let runner_config = build_test_config(program_executor);
let result = run_judge(&runner_config);
if let Ok(result) = result {
log::debug!("{:?}", result);
assert_eq!(result.verdict, JudgeVerdict::Accepted);
} else {
log::debug!("{:?}", result);
assert!(false)
}
}

#[test]
fn test_run_tle() {
init();
let program_path = PathBuf::from("./../test-collection/dist/programs/infinite_loop");
let program_executor = Executor::new(Language::Cpp, program_path).unwrap();

let runner_config = build_test_config(program_executor);
let result = run_judge(&runner_config);
assert!(result.is_ok());
if let Ok(result) = result {
log::debug!("{:?}", result);
assert_eq!(result.verdict, JudgeVerdict::TimeLimitExceeded);
}
}

#[test]
fn test_run_mle() {
init();
let program_path = PathBuf::from("./../test-collection/dist/programs/memory_limit");
let program_executor = Executor::new(Language::Cpp, program_path).unwrap();

let runner_config = build_test_config(program_executor);
let result = run_judge(&runner_config);
assert!(result.is_ok());
if let Ok(result) = result {
log::debug!("{:?}", result);
assert_eq!(result.verdict, JudgeVerdict::RuntimeError);
}
}
}
93 changes: 1 addition & 92 deletions judge-core/src/judge/interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn add_epoll_fd(epoll: &Epoll, fd: RawFd) -> Result<(), JudgeCoreError> {
pub fn run_interact(
config: &JudgeConfig,
mut interactor_executor: Executor,
output_path: &String,
output_path: &PathBuf,
) -> Result<Option<JudgeResultInfo>, JudgeCoreError> {
log::debug!("Creating epoll");
let epoll = Epoll::new(EpollCreateFlags::EPOLL_CLOEXEC)?;
Expand Down Expand Up @@ -231,94 +231,3 @@ pub fn run_interact(
}))
}
}

#[cfg(test)]
pub mod interact_judge_test {
use std::path::PathBuf;

use crate::{
compiler::Language,
judge::{
result::JudgeVerdict, CheckerConfig, JudgeConfig, ProgramConfig, RuntimeConfig,
TestdataConfig,
},
run::{executor::Executor, RlimitConfigs},
};

use super::run_interact;

const TEST_CONFIG: RlimitConfigs = RlimitConfigs {
stack_limit: Some((64 * 1024 * 1024, 64 * 1024 * 1024)),
as_limit: Some((64 * 1024 * 1024, 64 * 1024 * 1024)),
cpu_limit: Some((1, 2)),
nproc_limit: Some((1, 1)),
fsize_limit: Some((1024, 1024)),
};

fn init() {
let _ = env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.try_init();
}

fn build_test_config(program_executor: Executor) -> JudgeConfig {
JudgeConfig {
runtime: RuntimeConfig {
rlimit_configs: TEST_CONFIG,
},
test_data: TestdataConfig {
input_file_path: PathBuf::from("../tmp/in"),
answer_file_path: PathBuf::from("../tmp/ans"),
},
checker: CheckerConfig {
executor: Some(
Executor::new(
Language::Cpp,
PathBuf::from("./../test-collection/dist/checkers/lcmp"),
)
.unwrap(),
),
output_file_path: PathBuf::from("../tmp/check"),
},
program: ProgramConfig {
executor: program_executor,
output_file_path: PathBuf::from("../tmp/out"),
},
}
}

#[test]
fn test_run_interact() {
init();

let interactor_executor = Executor::new(
Language::Cpp,
PathBuf::from("./../test-collection/dist/checkers/interactor-echo"),
)
.unwrap();
let program_executor = Executor::new(
Language::Cpp,
PathBuf::from("./../test-collection/dist/programs/read_and_write"),
)
.unwrap();
let runner_config = build_test_config(program_executor);
let result = run_interact(
&runner_config,
interactor_executor,
&String::from("../tmp/interactor"),
);
match result {
Ok(Some(result)) => {
log::debug!("{:?}", result);
assert!(result.verdict == JudgeVerdict::Accepted);
}
Ok(None) => {
log::debug!("Ignoring this result, for it's from a fork child process");
}
Err(e) => {
log::error!("meet error: {:?}", e);
assert!(false);
}
}
}
}
3 changes: 3 additions & 0 deletions judge-core/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data/built-in-programs/build
data/built-in-programs/include/testlib.h
temp
40 changes: 40 additions & 0 deletions judge-core/tests/compile_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::path::PathBuf;

use judge_core::compiler::{Compiler, Language};

const TEST_DATA_PATH: &str = "tests/data";
const TEST_TEMP_PATH: &str = "tests/temp";

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}

#[test]
fn test_compile_cpp() {
init();
let compiler = Compiler::new(Language::Cpp, vec!["-std=c++17".to_string()]);
match compiler.compile(
&PathBuf::from(TEST_DATA_PATH).join("built-in-programs/src/programs/infinite_loop.cpp"),
&PathBuf::from(TEST_TEMP_PATH).join("infinite_loop_test.o"),
) {
Ok(out) => {
log::info!("{}", out);
}
Err(e) => panic!("{:?}", e),
}
}

#[test]
fn test_compile_py() {
init();
let compiler = Compiler::new(Language::Python, vec![]);
match compiler.compile(
&PathBuf::from(TEST_DATA_PATH).join("built-in-programs/src/programs/read_and_write.py"),
&PathBuf::from(TEST_TEMP_PATH).join("read_and_write.o"),
) {
Ok(out) => {
log::info!("{}", out);
}
Err(e) => panic!("{:?}", e),
}
}
31 changes: 31 additions & 0 deletions judge-core/tests/data/built-in-programs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.0)

project(judger-test VERSION 1.0)

set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")

set(CMAKE_C_FLAGS "-Wall -O2 -std=gnu11 -static -lm")

if (CMAKE_COMPILER_IS_GNUCC)
execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpfullversion -dumpversion
OUTPUT_VARIABLE GCC_VERSION)
string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
list(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR)

message(STATUS "CMAKE version is ${CMAKE_VERSION}")

set(GCC_VERSION "${GCC_MAJOR}.${GCC_MINOR}")
message(STATUS "GCC version is ${GCC_VERSION}")

if (GCC_VERSION GREATER_EQUAL "11")
set(CMAKE_CXX_FLAGS "-Wall -O2 -std=gnu++20 -static")
else()
set(CMAKE_CXX_FLAGS "-Wall -O2 -std=gnu++2a -static")
endif()
endif()

include(include/testlib.cmake)

add_subdirectory(src)
Loading

0 comments on commit cbdbff2

Please sign in to comment.