Skip to content
This repository has been archived by the owner on May 18, 2020. It is now read-only.

Commit

Permalink
v0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Mar 12, 2019
1 parent 1394732 commit a32d185
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tester"
version = "0.1.0"
version = "0.1.1"
authors = ["Nugine <Nugine@163.com>"]
edition = "2018"

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
time: 0 ms
memory: 1384 KB

$ tester ~/a+b < a+b.in
3
code: 0
time: 0 ms
memory: 1660 KB

$ tester ~/a+b < a+b.in > a+b.out
code: 0
time: 0 ms
memory: 1692 KB

## Usage

tester 0.1.0
Expand Down
39 changes: 15 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ mod sys;
#[cfg(test)]
mod test;

use sys::*;

use std::path::{Path, PathBuf};

use structopt::StructOpt;
Expand All @@ -15,35 +17,24 @@ struct Opt {
target: PathBuf,
}

pub fn run(path: &Path) -> Result<(), ()> {
let pid = sys::exec(&path).map_err(|e| {
eprintln!("{:?}", e);
})?;

let output = sys::wait(pid).map_err(|e| {
eprintln!("{:?}", e);
})?;

eprintln!("code: {}", output.code);

if let Some(sig) = output.signal {
eprintln!("signal: {}", sig);
pub fn run(path: &Path) -> Result<WaitOutput, String> {
if !path.is_file() {
return Err(format!("No such file: {:?}", path));
}

eprintln!("time: {} ms", output.time);

if output.memory > 10240 {
eprintln!("memory: {} MB", output.memory / 1024);
} else {
eprintln!("memory: {} KB", output.memory);
}

Ok(())
exec(&path)
.map_err(|e| format!("{:?}\n", e))
.and_then(|pid| wait(pid).map_err(|e| format!("{:?}\n", e)))
}

fn main() {
let opt = Opt::from_args();
if run(&opt.target).is_err() {
std::process::exit(1)

match run(&opt.target) {
Err(msg) => {
eprintln!("{}", msg);
std::process::exit(1);
}
Ok(out) => eprintln!("{}", out),
}
}
30 changes: 28 additions & 2 deletions src/sys.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::Error;

use std::ffi::CString;
use std::fmt::Display;
use std::path::Path;

use nix::sys::signal::Signal;
Expand All @@ -18,19 +19,44 @@ pub fn exec(path: &Path) -> Result<Pid, Error> {
match r {
Parent { child } => Ok(child),
Child => {
let _ = nix::unistd::execve(&p, &[], &[]);
std::process::exit(0);
let r = nix::unistd::execve(&p, &[], &[]);

if r.is_err() {
eprintln!("fail to execute path: {:?}", p);
std::process::exit(127);
} else {
std::process::exit(0);
}
}
}
}

#[derive(Debug)]
pub struct WaitOutput {
pub code: i32,
pub signal: Option<Signal>,
pub time: u64, // in ms
pub memory: usize, // in kb
}

impl Display for WaitOutput {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(f, "code: {}", self.code)?;

if let Some(sig) = self.signal {
writeln!(f, "signal: {}", sig)?;
}

writeln!(f, "time: {} ms", self.time)?;

if self.memory > 4096 {
write!(f, "memory: {} MB", (self.memory as f64) / 1024.0)
} else {
write!(f, "memory: {} KB", self.memory)
}
}
}

pub fn wait(pid: Pid) -> Result<WaitOutput, Error> {
use libc::{c_int, rusage, WSTOPPED};

Expand Down
30 changes: 28 additions & 2 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@ use crate::run;
use std::path::PathBuf;

#[test]
fn test() {
fn test_ok() {
let r = run(&PathBuf::from("./test/hello"));
assert_eq!(r, Ok(()));
assert!(r.is_ok());
let out = r.unwrap();
dbg!(&out);
assert_eq!(out.code, 0);
assert_eq!(out.signal, None);
assert!(out.time < 10);
assert!(out.memory < 2000);
}

#[test]
fn test_err() {
let r = run(&PathBuf::from("./test/"));
assert!(r.is_err());
let e = r.unwrap_err();
assert_eq!(e, "No such file: \"./test/\"".to_string());
}

#[test]
fn test_mem() {
let r = run(&PathBuf::from("./test/mem"));
assert!(r.is_ok());
let out = r.unwrap();
dbg!(&out);
assert_eq!(out.code, 0);
assert_eq!(out.signal, None);
assert!(out.time < 20);
assert!(out.memory > 9000 && out.memory < 10000);
}
Binary file added test/mem
Binary file not shown.
7 changes: 7 additions & 0 deletions test/mem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <cstdio>

int main() {
long long a[1001][1001] = {0};
printf("a: %lu\n", sizeof(a));
return 0;
}

0 comments on commit a32d185

Please sign in to comment.