Skip to content

Commit 4fa1792

Browse files
committed
cleaned up I/O examples
1 parent e4fd17d commit 4fa1792

File tree

3 files changed

+38
-43
lines changed

3 files changed

+38
-43
lines changed

src/graph/connectivity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ impl ConnectivityData {
1717
time: 0,
1818
vis: vec![0; num_v].into_boxed_slice(),
1919
low: vec![0; num_v].into_boxed_slice(),
20-
v_stack: Vec::new(),
21-
e_stack: Vec::new(),
20+
v_stack: vec![],
21+
e_stack: vec![],
2222
}
2323
}
2424

src/scanner.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl<R: io::BufRead> Scanner<R> {
1313
pub fn new(reader: R) -> Self {
1414
Self {
1515
reader,
16-
buffer: Vec::new(),
16+
buffer: vec![],
1717
}
1818
}
1919

@@ -46,7 +46,7 @@ impl<R: io::BufRead> UnsafeScanner<R> {
4646
pub fn new(reader: R) -> Self {
4747
Self {
4848
reader,
49-
buf_str: Vec::new(),
49+
buf_str: vec![],
5050
buf_iter: "".split_ascii_whitespace(),
5151
}
5252
}
@@ -84,45 +84,46 @@ pub fn writer_to_file(filename: &str) -> io::BufWriter<std::fs::File> {
8484
mod test {
8585
use super::*;
8686

87-
#[test]
88-
fn test_in_memory_io() {
89-
let input = "50 8".as_bytes();
90-
let mut scan = Scanner::new(input);
91-
let mut out = String::new();
92-
use std::fmt::Write; // needed for writeln!()
87+
fn solve<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) {
88+
let x = scan.token::<i32>();
89+
let y = scan.token::<i32>();
90+
writeln!(out, "{} - {} = {}", x, y, x - y).ok();
91+
}
9392

93+
fn unsafe_solve<R: io::BufRead, W: io::Write>(scan: &mut UnsafeScanner<R>, out: &mut W) {
9494
let x = scan.token::<i32>();
9595
let y = scan.token::<i32>();
96-
writeln!(out, "Test {}", x - y).ok();
96+
writeln!(out, "{} - {} = {}", x, y, x - y).ok();
97+
}
98+
99+
#[test]
100+
fn test_in_memory_io() {
101+
let input: &[u8] = b"50 8";
102+
let mut scan = Scanner::new(input);
103+
let mut out = vec![];
97104

98-
assert_eq!(out, "Test 42\n");
105+
solve(&mut scan, &mut out);
106+
assert_eq!(out, b"50 - 8 = 42\n");
99107
}
100108

101109
#[test]
102110
fn test_in_memory_unsafe() {
103-
let input = "50 8".as_bytes();
111+
let input: &[u8] = b"50 8";
104112
let mut scan = UnsafeScanner::new(input);
105-
let mut out = String::new();
106-
use std::fmt::Write; // needed for writeln!()
107-
108-
let x = scan.token::<i32>();
109-
let y = scan.token::<i32>();
110-
writeln!(out, "Test {}", x - y).ok();
113+
let mut out = vec![];
111114

112-
assert_eq!(out, "Test 42\n");
115+
unsafe_solve(&mut scan, &mut out);
116+
assert_eq!(out, b"50 - 8 = 42\n");
113117
}
114118

115119
#[test]
116120
fn test_compile_stdio() {
117121
let (stdin, stdout) = (io::stdin(), io::stdout());
118122
let mut scan = Scanner::new(stdin.lock());
119123
let mut out = io::BufWriter::new(stdout.lock());
120-
use io::Write; // needed for writeln!()
121124

122125
if false {
123-
let x = scan.token::<i32>();
124-
let y = scan.token::<i32>();
125-
writeln!(out, "Test {}", x - y).ok();
126+
solve(&mut scan, &mut out);
126127
}
127128
}
128129

@@ -131,10 +132,7 @@ mod test {
131132
fn test_panic_file() {
132133
let mut scan = scanner_from_file("input_file.txt");
133134
let mut out = writer_to_file("output_file.txt");
134-
use io::Write; // needed for writeln!()
135135

136-
let x = scan.token::<i32>();
137-
let y = scan.token::<i32>();
138-
writeln!(out, "Test {}", x - y).ok();
136+
solve(&mut scan, &mut out);
139137
}
140138
}

tests/codeforces343d.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ extern crate contest_algorithms;
66
use contest_algorithms::graph::Graph;
77
use contest_algorithms::range_query::{specs::AssignSum, StaticArq};
88
use contest_algorithms::scanner::Scanner;
9+
use std::io;
910

10-
const SAMPLE_INPUT: &str = "\
11+
const SAMPLE_INPUT: &[u8] = b"\
1112
5
1213
1 2
1314
5 1
@@ -27,7 +28,7 @@ const SAMPLE_INPUT: &str = "\
2728
3 4
2829
3 5
2930
";
30-
const SAMPLE_OUTPUT: &str = "\
31+
const SAMPLE_OUTPUT: &[u8] = b"\
3132
0
3233
0
3334
0
@@ -59,18 +60,7 @@ fn dfs(
5960
r[u] = *time;
6061
}
6162

62-
#[test]
63-
fn main() {
64-
use std::fmt::Write;
65-
let mut scan = Scanner::new(SAMPLE_INPUT.as_bytes());
66-
let mut out = String::new();
67-
/* To read/write with stdin/stdout instead:
68-
use std::io::{self, Write};
69-
let (stdin, stdout) = (io::stdin(), io::stdout());
70-
let mut scan = Scanner::new(stdin.lock());
71-
let mut out = io::BufWriter::new(stdout.lock());
72-
*/
73-
63+
fn solve<R: io::BufRead, W: io::Write>(scan: &mut Scanner<R>, out: &mut W) {
7464
let n = scan.token::<usize>();
7565
let mut tree = Graph::new(n, 2 * (n - 1));
7666
for _ in 1..n {
@@ -102,6 +92,13 @@ fn main() {
10292
writeln!(out, "{}", ans).ok();
10393
}
10494
}
95+
}
96+
97+
#[test]
98+
fn main() {
99+
let mut scan = Scanner::new(SAMPLE_INPUT);
100+
let mut out = vec![];
101+
solve(&mut scan, &mut out);
105102

106103
assert_eq!(out, SAMPLE_OUTPUT);
107-
}
104+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy