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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#![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))]
#![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)]
#[cfg(feature = "parallel")]
extern crate rayon;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use std::{env, io};
use std::ffi::OsString;
use std::fmt::{Arguments, Display};
use std::fs::{File, OpenOptions};
use std::io::{LineWriter, Read, Write};
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 InputReader {
input: String,
offset: usize,
}
struct OutputWriter(LineWriter<File>);
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,
}
}
#[cfg(not(feature = "parallel"))]
pub fn run<
D: Sized + Send + Sync,
R: Display + Sized + Send,
P: Fn(&mut InputReader) -> D,
S: Fn(&D) -> R + Sync
>
(
self,
p: P,
s: S,
) {
let mut reader = InputReader::new(self.input_file_path);
let mut writer = OutputWriter::new(self.output_file_path);
let mut current_case: usize = 1;
let case_count = reader.get_case_count();
while current_case <= case_count {
writer.write_test_result(current_case, (s)(&(p)(&mut reader)));
current_case += 1;
}
}
#[cfg(feature = "parallel")]
pub fn run<
D: Sized + Send + Sync,
R: Display + Sized + Send,
P: Fn(&mut InputReader) -> D,
S: Fn(&D) -> R + Sync
>
(
self,
p: P,
s: S,
) {
let mut reader = InputReader::new(self.input_file_path);
let mut writer = OutputWriter::new(self.output_file_path);
let case_count = reader.get_case_count();
let mut data = Vec::with_capacity(0);
data.reserve_exact(case_count);
for _ in 0..case_count {
data.push((p(&mut reader), None));
}
data.par_iter_mut().for_each(|d| d.1 = Some(s(&d.0)));
for (i, &(_, ref r)) in data.iter().enumerate() {
writer.write_test_result(
i + 1,
match *r {
Some(ref x) => x,
None => unreachable!(),
},
);
}
}
}
impl TestEngine<OsString, OsString> {
pub fn from_args() -> TestEngine<OsString, OsString> {
let mut args = env::args_os();
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<OsString, OsString> {
fn default() -> TestEngine<OsString, OsString> {
Self::from_args()
}
}
impl InputReader {
pub fn read_next_line(&mut self) -> &str {
if self.offset >= self.input.len() {
panic!("could not read line from input file: reached end of file");
}
let start = self.offset;
let end = self.input
.char_indices()
.skip(start)
.take_while(|&(_, c)| c != '\n')
.last()
.map_or(start, |(i, _)| i + 1);
let s = &self.input[start..end];
if s.is_empty() {
self.offset = end + 2;
} else {
self.offset = end + 1;
}
s
}
fn new<P: AsRef<Path>>(path: P) -> InputReader {
let mut file = OpenOptions::new()
.read(true)
.open(path)
.expect("could not open input file for reading");
let mut s = String::with_capacity(0);
let _ = file.read_to_string(&mut s)
.expect("could not read input file into string");
InputReader {
input: s,
offset: 0,
}
}
fn get_case_count(&mut self) -> usize {
usize::from_str_radix(self.read_next_line(), 10).expect("could not parse test case count")
}
}
impl OutputWriter {
fn new<P: AsRef<Path>>(path: P) -> OutputWriter {
OutputWriter(
LineWriter::new(
OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path)
.expect("could not open output file for writing"),
),
)
}
fn write_test_result<R: Display>(&mut self, case: usize, result: R) {
let case_prefix = "Case #";
let case_number = case.to_string();
let case_colon = ":";
let r = result.to_string();
let mut output = String::with_capacity(0);
output.reserve_exact(case_prefix.len() + case_number.len() + case_colon.len() + r.len(),);
output.push_str(case_prefix);
output.push_str(&case_number);
output.push_str(case_colon);
output.push_str(&r);
self.write_all(output.as_bytes())
.expect("could not write test result to output file");
}
}
impl Write for OutputWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.0.write_all(buf)
}
fn write_fmt(&mut self, fmt: Arguments) -> io::Result<()> {
self.0.write_fmt(fmt)
}
}