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
use crate::utils::scanner::IO;
use std::collections::vec_deque::VecDeque;
use std::error::Error;
use std::fmt;
use std::fs;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::str;

pub(crate) struct Tester {
    input: Vec<Vec<u8>>,
    output: Vec<Vec<u8>>,
}

impl Tester {
    pub(crate) fn new(input_directory: &str, output_directory: &str) -> Tester {
        let input = read_from_directory(input_directory);
        let output = read_from_directory(output_directory);
        Tester { input, output }
    }

    pub(crate) fn test_solution<F>(self, solution: F)
    where
        F: Fn(&mut IO<&[u8], &mut Vec<u8>>),
    {
        for (input, output) in self.input.into_iter().zip(self.output) {
            let mut writer = vec![];
            {
                let mut sc = IO::new(&input[..], &mut writer);
                solution(&mut sc);
            }
            assert_eq!(writer, output);
        }
    }
}

fn read_file(filepath: &str) -> Vec<u8> {
    let mut file = File::open(filepath).unwrap();
    let mut buf = Vec::new();
    file.read_to_end(&mut buf).unwrap();
    buf
}

fn read_from_directory(directory_path: &str) -> Vec<Vec<u8>> {
    let mut filenames: Vec<String> = fs::read_dir(directory_path)
        .unwrap()
        .map(|result| result.unwrap().path().display().to_string())
        .collect();
    filenames.sort();
    filenames.into_iter().map(|file| read_file(&file)).collect()
}