Skip to content

Commit

Permalink
feat(judge): judge each line of expected output (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 authored Nov 30, 2024
1 parent 985d50c commit 20bb292
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/judge-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eval-stack": patch:feat
---

Judge outputs for each line of expected outputs.
4 changes: 4 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
"rlimit",
"rustc",
"rustup",
"serde",
"setrlimit",
"SIGBUS",
"SIGILL",
"SIGSEGV",
"statm",
"sysconf",
"unistd"
],
"ignorePaths": [
"Cargo.lock"
]
}
43 changes: 32 additions & 11 deletions src/judge.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::{
fs, future::Future, io::Read, os::unix::process::ExitStatusExt, path::PathBuf, task::Poll,
fs,
future::Future,
io::{BufRead, BufReader},
os::unix::process::ExitStatusExt,
path::PathBuf,
task::Poll,
time::Duration,
};

Expand Down Expand Up @@ -51,18 +56,34 @@ impl Future for Judge {
drop(self.child.stdin.take());
drop(self.child.stdout.take());
if status.success() {
let mut stdout = fs::File::open(&self.stdout_file)?;
let mut expected_out = fs::File::open(&self.expected_output_file)?;
let stdout = BufReader::new(fs::File::open(&self.stdout_file)?);
let expected_out = BufReader::new(fs::File::open(&self.expected_output_file)?);

let mut output = String::new();
let mut expected_output = String::new();
stdout.read_to_string(&mut output)?;
expected_out.read_to_string(&mut expected_output)?;

if output.trim_end_matches(|c: char| c.is_whitespace() || c == '\n')
== expected_output
.trim_end_matches(|c: char| c.is_whitespace() || c == '\n')
let mut stdout_lines = stdout.lines();
let mut expected_out_lines = expected_out.lines();
let mut matched = true;
while let (Some(output), Some(expected_output)) =
(stdout_lines.next(), expected_out_lines.next())
{
if output?.trim_end_matches(|c: char| c.is_whitespace() || c == '\n')
!= expected_output?
.trim_end_matches(|c: char| c.is_whitespace() || c == '\n')
{
matched = false;
break;
}
}

while let Some(extra_line) = stdout_lines.next() {
if extra_line?.trim_end_matches(|c: char| c.is_whitespace() || c == '\n')
!= ""
{
matched = false;
break;
}
}

if matched {
Poll::Ready(Ok(JudgeResult {
status: JudgeStatus::Accepted,
time_used: self.time_used,
Expand Down
2 changes: 1 addition & 1 deletion tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ int main()
{
i64 a, b;
cin >> a >> b;
cout << a + b << endl;
cout << a + b << endl << endl << endl;
return 0;
}

0 comments on commit 20bb292

Please sign in to comment.