incpa_byte/
testutils.rs

1//! Common parser testing utilities
2
3use crate::ByteParser;
4
5/// Run the given parser with many different initial buffer sizes, calling `check` on the results each time
6///
7/// This test function helps catch bounds errors in `Parser` / `BufferManager` implementations
8pub fn test_buffer_windows_res<B, I, F, E>(bfmt: B, input: I, check: F) -> Result<(), E>
9where
10    B: Clone + ByteParser,
11    I: AsRef<[u8]>,
12    F: Fn(Result<B::Output, E>) -> Result<(), E>,
13    E: From<B::Error> + From<std::io::Error>,
14{
15    for initsize in [0, 1, 2, 3, 5, 7, 1 << 10, 1 << 14] {
16        eprintln!("Checking parser.parse_reader_with_initial_buffer_size(..., {initsize})");
17
18        let res = bfmt
19            .clone()
20            .parse_reader_with_initial_buffer_size(input.as_ref(), initsize);
21
22        check(res)?;
23    }
24
25    eprintln!("Checking parser.parse(...)");
26    check(bfmt.parse_all(input.as_ref()).map_err(E::from))
27}
28
29/// Run the given parser with many different initial buffer sizes, calling `check` on the outputs each time
30///
31/// This test function helps catch bounds errors in `Parser` / `BufferManager` implementations
32pub fn test_buffer_windows_outputs<B, I, F, E>(bfmt: B, input: I, check: F) -> Result<(), E>
33where
34    B: Clone + ByteParser,
35    I: AsRef<[u8]>,
36    F: Fn(B::Output) -> Result<(), E>,
37    E: From<B::Error> + From<std::io::Error>,
38{
39    test_buffer_windows_res(bfmt, input, |res| {
40        let output = res?;
41        check(output)
42    })
43}
44
45/// Run the given parser with many different initial buffer sizes, calling `check` on the outputs each time, which must panic to cause a test failure
46///
47/// This test function helps catch bounds errors in `Parser` / `BufferManager` implementations
48pub fn test_buffer_windows_output_no_res<B, I, F, E>(bfmt: B, input: I, check: F) -> Result<(), E>
49where
50    B: Clone + ByteParser,
51    I: AsRef<[u8]>,
52    F: Fn(B::Output),
53    E: From<B::Error> + From<std::io::Error>,
54{
55    test_buffer_windows_res(bfmt, input, |res| {
56        let output = res?;
57        check(output);
58        Ok(())
59    })
60}