1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy))]
#![cfg_attr(feature = "clippy", forbid(clippy))]
#![cfg_attr(feature = "clippy", forbid(clippy_internal))]
#![cfg_attr(feature = "clippy", forbid(clippy_pedantic))]
#![cfg_attr(feature = "clippy", forbid(clippy_restrictions))]
#![forbid(warnings)]
#![forbid(box_pointers)]
#![forbid(fat_ptr_transmutes)]
#![forbid(missing_copy_implementations)]
#![forbid(missing_debug_implementations)]
#![forbid(missing_docs)]
#![forbid(trivial_casts)]
#![forbid(trivial_numeric_casts)]
#![forbid(unsafe_code)]
#![forbid(unused_extern_crates)]
#![forbid(unused_import_braces)]
#![deny(unused_qualifications)]
#![forbid(unused_results)]
#![forbid(variant_size_differences)]
use std::{env, io};
use std::fmt::Arguments;
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, LineWriter, Lines, Write};
use std::ops::{Add, AddAssign};
use std::path::Path;
#[derive(Debug)]
pub struct TestEngine<I: AsRef<Path>, O: AsRef<Path>> {
input_file_path: I,
output_file_path: O,
}
#[derive(Debug)]
pub struct IoHelper {
input: Lines<BufReader<File>>,
output: LineWriter<File>,
current_case: usize,
case_count: usize,
}
impl<I: AsRef<Path>, O: AsRef<Path>> TestEngine<I, O> {
pub fn new(input_file_path: I, output_file_path: O) -> TestEngine<I, O> {
TestEngine {
input_file_path: input_file_path,
output_file_path: output_file_path,
}
}
pub fn run<F: Fn(&mut IoHelper)>(self, f: F) {
let io_helper = IoHelper::new(self);
io_helper.run(f);
}
}
impl TestEngine<String, String> {
pub fn from_args() -> TestEngine<String, String> {
let mut args = env::args();
let input_file_path = args.nth(1).expect("input file path not specified");
let output_file_path = args.next().expect("output file path not specified");
Self::new(input_file_path, output_file_path)
}
}
impl Default for TestEngine<String, String> {
fn default() -> TestEngine<String, String> {
Self::from_args()
}
}
impl IoHelper {
pub fn read_line(&mut self) -> String {
self.input
.next()
.expect("reached end of file")
.expect("could not read from input file")
}
pub fn current_case(&self) -> usize {
self.current_case
}
pub fn case_count(&self) -> usize {
self.case_count
}
fn new<I: AsRef<Path>, O: AsRef<Path>>(test_engine: TestEngine<I, O>) -> IoHelper {
let mut io_helper = IoHelper {
input: Self::open_input_file(test_engine.input_file_path),
output: Self::open_output_file(test_engine.output_file_path),
current_case: 1,
case_count: 0,
};
io_helper.init_test_case_count();
io_helper
}
fn run<F: Fn(&mut IoHelper)>(mut self, f: F) {
while self.current_case <= self.case_count {
let current_case_s = self.current_case_string();
let _ = self.write(current_case_s.as_bytes())
.expect("could not write current test case string to output file");
(f)(&mut self);
self.current_case.add_assign(1);
}
}
fn open_input_file<P: AsRef<Path>>(path: P) -> Lines<BufReader<File>> {
BufReader::new(OpenOptions::new()
.read(true)
.open(path)
.expect("could not open input file for reading")).lines()
}
fn open_output_file<P: AsRef<Path>>(path: P) -> LineWriter<File> {
LineWriter::new(OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path)
.expect("could not open output file for writing"))
}
fn init_test_case_count(&mut self) {
let line = self.read_line();
self.case_count =
usize::from_str_radix(&line, 10).expect("could not parse test case count");
}
fn current_case_string(&mut self) -> String {
let case_str = "Case #";
let case_s = self.current_case.to_string();
let case_str_colon = ":";
let mut out = String::with_capacity(0);
out.reserve_exact(case_str.len().add(case_s.len()).add(case_str_colon.len()));
out.push_str(case_str);
out.push_str(&case_s);
out.push_str(case_str_colon);
out
}
}
impl Write for IoHelper {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.output.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.output.flush()
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.output.write_all(buf)
}
fn write_fmt(&mut self, fmt: Arguments) -> io::Result<()> {
self.output.write_fmt(fmt)
}
}