Created
April 11, 2017 12:44
-
-
Save qryxip/798c792aef4a2f02753dd8c1e2803a10 to your computer and use it in GitHub Desktop.
競プロ用のテスター(未完成)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env run-cargo-script | |
//!```cargo | |
//![package] | |
//!authors = ["wariuni <wariuni@gmail.com>"] | |
//!name = tester | |
//!version = "0.1.0" | |
//! | |
//![dependencies] | |
//!toml = "0.3.2" | |
//!``` | |
extern crate toml; | |
use std::fs::File; | |
use std::io::Read; | |
use std::process::Command; | |
fn main() { | |
let test_inputs = read_toml("./tests.toml").unwrap(); | |
println!("{:?}", test_inputs); | |
} | |
fn read_toml(path: &str) -> Result<toml::Value, String> { | |
let mut input = String::new(); | |
File::open(path).and_then(|mut file| file.read_to_string(&mut input)) | |
.map_err(|_| format!("Failed to read {}.", path))?; | |
input | |
.parse() | |
.map_err(|_| format!("Failed to parse {}.", path)) | |
} | |
fn apply_tests(test_inputs: toml::Value, program: &str, timeout: usize) -> Result<String, String> { | |
let (num_passed_tests, num_tests) = match test_inputs { | |
toml::Value::Table(table) => { | |
let num_tests = table.len(); | |
let mut num_passed_tests = 0; | |
table | |
.into_iter() | |
.map(|(case_name, pair)| if let (&toml::Value::String(ref expected), | |
&toml::Value::String(ref input)) = | |
(pair.get("expected").unwrap(), pair.get("input").unwrap()) { | |
let result = Command::new("timeout") | |
.args(&[timeout.to_string().as_str(), program]) | |
.spawn(); | |
if let Ok(child) = result { | |
if let Some(mut stdout) = child.stdout { | |
let mut actual = String::new(); | |
let _ = stdout.read_to_string(&mut actual); | |
if &actual == expected { | |
num_passed_tests += 1; | |
} | |
} | |
} | |
}); | |
(num_passed_tests, num_tests) | |
} | |
_ => return Err(format!("Invalid format.")), | |
}; | |
let text = format!("{}/{} test(s) passed.", num_passed_tests, num_tests); | |
if num_passed_tests == num_tests { | |
Ok(text) | |
} else { | |
Err(text) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment