From 4fa179244f189365b50ba972e9cc0d7189852639 Mon Sep 17 00:00:00 2001 From: Aram Ebtekar Date: Sun, 12 Apr 2020 01:11:59 -0700 Subject: [PATCH] cleaned up I/O examples --- src/graph/connectivity.rs | 4 ++-- src/scanner.rs | 50 +++++++++++++++++++-------------------- tests/codeforces343d.rs | 27 ++++++++++----------- 3 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/graph/connectivity.rs b/src/graph/connectivity.rs index 8404268..4402e7d 100644 --- a/src/graph/connectivity.rs +++ b/src/graph/connectivity.rs @@ -17,8 +17,8 @@ impl ConnectivityData { time: 0, vis: vec![0; num_v].into_boxed_slice(), low: vec![0; num_v].into_boxed_slice(), - v_stack: Vec::new(), - e_stack: Vec::new(), + v_stack: vec![], + e_stack: vec![], } } diff --git a/src/scanner.rs b/src/scanner.rs index ae6f5a9..a57b408 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -13,7 +13,7 @@ impl Scanner { pub fn new(reader: R) -> Self { Self { reader, - buffer: Vec::new(), + buffer: vec![], } } @@ -46,7 +46,7 @@ impl UnsafeScanner { pub fn new(reader: R) -> Self { Self { reader, - buf_str: Vec::new(), + buf_str: vec![], buf_iter: "".split_ascii_whitespace(), } } @@ -84,32 +84,36 @@ pub fn writer_to_file(filename: &str) -> io::BufWriter { mod test { use super::*; - #[test] - fn test_in_memory_io() { - let input = "50 8".as_bytes(); - let mut scan = Scanner::new(input); - let mut out = String::new(); - use std::fmt::Write; // needed for writeln!() + fn solve(scan: &mut Scanner, out: &mut W) { + let x = scan.token::(); + let y = scan.token::(); + writeln!(out, "{} - {} = {}", x, y, x - y).ok(); + } + fn unsafe_solve(scan: &mut UnsafeScanner, out: &mut W) { let x = scan.token::(); let y = scan.token::(); - writeln!(out, "Test {}", x - y).ok(); + writeln!(out, "{} - {} = {}", x, y, x - y).ok(); + } + + #[test] + fn test_in_memory_io() { + let input: &[u8] = b"50 8"; + let mut scan = Scanner::new(input); + let mut out = vec![]; - assert_eq!(out, "Test 42\n"); + solve(&mut scan, &mut out); + assert_eq!(out, b"50 - 8 = 42\n"); } #[test] fn test_in_memory_unsafe() { - let input = "50 8".as_bytes(); + let input: &[u8] = b"50 8"; let mut scan = UnsafeScanner::new(input); - let mut out = String::new(); - use std::fmt::Write; // needed for writeln!() - - let x = scan.token::(); - let y = scan.token::(); - writeln!(out, "Test {}", x - y).ok(); + let mut out = vec![]; - assert_eq!(out, "Test 42\n"); + unsafe_solve(&mut scan, &mut out); + assert_eq!(out, b"50 - 8 = 42\n"); } #[test] @@ -117,12 +121,9 @@ mod test { let (stdin, stdout) = (io::stdin(), io::stdout()); let mut scan = Scanner::new(stdin.lock()); let mut out = io::BufWriter::new(stdout.lock()); - use io::Write; // needed for writeln!() if false { - let x = scan.token::(); - let y = scan.token::(); - writeln!(out, "Test {}", x - y).ok(); + solve(&mut scan, &mut out); } } @@ -131,10 +132,7 @@ mod test { fn test_panic_file() { let mut scan = scanner_from_file("input_file.txt"); let mut out = writer_to_file("output_file.txt"); - use io::Write; // needed for writeln!() - let x = scan.token::(); - let y = scan.token::(); - writeln!(out, "Test {}", x - y).ok(); + solve(&mut scan, &mut out); } } diff --git a/tests/codeforces343d.rs b/tests/codeforces343d.rs index b4f1d94..27cac59 100644 --- a/tests/codeforces343d.rs +++ b/tests/codeforces343d.rs @@ -6,8 +6,9 @@ extern crate contest_algorithms; use contest_algorithms::graph::Graph; use contest_algorithms::range_query::{specs::AssignSum, StaticArq}; use contest_algorithms::scanner::Scanner; +use std::io; -const SAMPLE_INPUT: &str = "\ +const SAMPLE_INPUT: &[u8] = b"\ 5 1 2 5 1 @@ -27,7 +28,7 @@ const SAMPLE_INPUT: &str = "\ 3 4 3 5 "; -const SAMPLE_OUTPUT: &str = "\ +const SAMPLE_OUTPUT: &[u8] = b"\ 0 0 0 @@ -59,18 +60,7 @@ fn dfs( r[u] = *time; } -#[test] -fn main() { - use std::fmt::Write; - let mut scan = Scanner::new(SAMPLE_INPUT.as_bytes()); - let mut out = String::new(); - /* To read/write with stdin/stdout instead: - use std::io::{self, Write}; - let (stdin, stdout) = (io::stdin(), io::stdout()); - let mut scan = Scanner::new(stdin.lock()); - let mut out = io::BufWriter::new(stdout.lock()); - */ - +fn solve(scan: &mut Scanner, out: &mut W) { let n = scan.token::(); let mut tree = Graph::new(n, 2 * (n - 1)); for _ in 1..n { @@ -102,6 +92,13 @@ fn main() { writeln!(out, "{}", ans).ok(); } } +} + +#[test] +fn main() { + let mut scan = Scanner::new(SAMPLE_INPUT); + let mut out = vec![]; + solve(&mut scan, &mut out); assert_eq!(out, SAMPLE_OUTPUT); -} +} \ No newline at end of file