Skip to content

Commit

Permalink
cleaned up I/O examples
Browse files Browse the repository at this point in the history
  • Loading branch information
EbTech committed Apr 12, 2020
1 parent e4fd17d commit 4fa1792
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/graph/connectivity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
}
}

Expand Down
50 changes: 24 additions & 26 deletions src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<R: io::BufRead> Scanner<R> {
pub fn new(reader: R) -> Self {
Self {
reader,
buffer: Vec::new(),
buffer: vec![],
}
}

Expand Down Expand Up @@ -46,7 +46,7 @@ impl<R: io::BufRead> UnsafeScanner<R> {
pub fn new(reader: R) -> Self {
Self {
reader,
buf_str: Vec::new(),
buf_str: vec![],
buf_iter: "".split_ascii_whitespace(),
}
}
Expand Down Expand Up @@ -84,45 +84,46 @@ pub fn writer_to_file(filename: &str) -> io::BufWriter<std::fs::File> {
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<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) {
let x = scan.token::<i32>();
let y = scan.token::<i32>();
writeln!(out, "{} - {} = {}", x, y, x - y).ok();
}

fn unsafe_solve<R: io::BufRead, W: io::Write>(scan: &mut UnsafeScanner<R>, out: &mut W) {
let x = scan.token::<i32>();
let y = scan.token::<i32>();
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::<i32>();
let y = scan.token::<i32>();
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]
fn test_compile_stdio() {
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::<i32>();
let y = scan.token::<i32>();
writeln!(out, "Test {}", x - y).ok();
solve(&mut scan, &mut out);
}
}

Expand All @@ -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::<i32>();
let y = scan.token::<i32>();
writeln!(out, "Test {}", x - y).ok();
solve(&mut scan, &mut out);
}
}
27 changes: 12 additions & 15 deletions tests/codeforces343d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,7 +28,7 @@ const SAMPLE_INPUT: &str = "\
3 4
3 5
";
const SAMPLE_OUTPUT: &str = "\
const SAMPLE_OUTPUT: &[u8] = b"\
0
0
0
Expand Down Expand Up @@ -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<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) {
let n = scan.token::<usize>();
let mut tree = Graph::new(n, 2 * (n - 1));
for _ in 1..n {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4fa1792

Please sign in to comment.