Skip to content

Commit

Permalink
Migrate test-collection to judger-core
Browse files Browse the repository at this point in the history
Move postman folder

Perf README

Update test-collection

Migrate test-collection to judger-core

Co-authored-by: KanadeSiina [email protected]
Co-authored-by: cubercsl [email protected]

Fix CI

Remove test-collection gitmodules

Delete test-collection

Fix compiling failure while target folder is not exist

Fix test case path
  • Loading branch information
slhmy authored and akamya997 committed Dec 30, 2023
1 parent 3cfbcc5 commit 7fd12e6
Show file tree
Hide file tree
Showing 44 changed files with 535 additions and 286 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/rust_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ jobs:

steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y libseccomp-dev protobuf-compiler gcc-11 g++-11
- name: Setup gcc/g++ 11
Expand All @@ -26,7 +24,7 @@ jobs:
- name: Check ENV
run: echo $(rustup --version && g++ -v)
- name: Build test dist
run: cd test-collection && ./build.sh && cd ..
run: cd judge-core/tests/data/built-in-programs && ./build.sh && cd ../../../../
- name: Run tests
# Currently use --test-threads=1 to avoid log mess
# Should only be used in judge-core later
Expand Down
4 changes: 0 additions & 4 deletions .gitmodules

This file was deleted.

3 changes: 2 additions & 1 deletion docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
⚠️ Do not use VSCode's `Build Image..` usage.

Run the following command under project root instead.

```sh
docker build --pull --rm -f "docker/judge-server.dockerfile" -t oj-lab/judge-server:latest .
```
```
51 changes: 7 additions & 44 deletions judge-core/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::JudgeCoreError;
use crate::utils::get_pathbuf_str;
use anyhow::anyhow;
use serde_derive::{Serialize, Deserialize};
use std::fmt;
use std::{fmt, fs};
use std::path::PathBuf;
use std::{process::Command, str::FromStr};

Expand Down Expand Up @@ -89,8 +89,8 @@ impl FromStr for Language {
///
/// 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"),
/// &PathBuf::from("tests/data/built-in-programs/src/programs/infinite_loop.cpp"),
/// &PathBuf::from("tests/temp/infinite_loop_test"),
/// ) {
/// Ok(out) => {
/// log::info!("compiled with output: {}", out);
Expand Down Expand Up @@ -151,6 +151,10 @@ impl Compiler {
target_path_string
);

if let Some(target_parent) = target_path.parent() {
fs::create_dir_all(target_parent)?;
}

if PathBuf::from(target_path).exists() {
std::fs::remove_file(target_path)?;
}
Expand All @@ -175,44 +179,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
Loading

0 comments on commit 7fd12e6

Please sign in to comment.