File: proptest.rs

package info (click to toggle)
rust-async-compression 0.4.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 928 kB
  • sloc: makefile: 2
file content (142 lines) | stat: -rw-r--r-- 4,416 bytes parent folder | download
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
use async_compression::Level;

use ::proptest::{
    arbitrary::any,
    prop_oneof,
    strategy::{Just, Strategy},
};

mod utils;

#[allow(dead_code)]
fn any_level() -> impl Strategy<Value = Level> {
    prop_oneof![
        Just(Level::Fastest),
        Just(Level::Best),
        Just(Level::Default),
        any::<i32>().prop_map(Level::Precise),
    ]
}

#[allow(unused_macros)]
macro_rules! io_tests {
    ($impl:ident, $variant:ident) => {
        mod $impl {
            mod bufread {
                use crate::utils::{algos::$variant::{$impl::{read, bufread}, sync}, InputStream};
                use proptest::{prelude::{any, ProptestConfig}, proptest};
                use std::iter::FromIterator;

                proptest! {
                    #[test]
                    fn compress(ref input in any::<InputStream>()) {
                        let compressed = bufread::compress(bufread::from(input));
                        let output = sync::decompress(&compressed);
                        assert_eq!(output, input.bytes());
                    }

                    #[test]
                    fn decompress(
                        ref bytes in any::<Vec<u8>>(),
                        chunk_size in 1..20usize,
                    ) {
                        let compressed = sync::compress(bytes);
                        let input = InputStream::from(Vec::from_iter(compressed.chunks(chunk_size).map(Vec::from)));
                        let output = bufread::decompress(bufread::from(&input));
                        assert_eq!(&output, bytes);
                    }
                }

                proptest! {
                    #![proptest_config(ProptestConfig::with_cases(32))]

                    #[test]
                    fn compress_with_level(
                        ref input in any::<InputStream>(),
                        level in crate::any_level(),
                    ) {
                        let encoder = bufread::Encoder::with_quality(bufread::from(input), level);
                        let compressed = read::to_vec(encoder);
                        let output = sync::decompress(&compressed);
                        assert_eq!(output, input.bytes());
                    }
                }
            }

            mod write {
                use crate::utils::{algos::$variant::{$impl::write, sync}, InputStream};
                use proptest::{prelude::{any, ProptestConfig}, proptest};

                proptest! {
                    #[test]
                    fn compress(
                        ref input in any::<InputStream>(),
                        limit in 1..20usize,
                    ) {
                        let compressed = write::compress(input.as_ref(), limit);
                        let output = sync::decompress(&compressed);
                        assert_eq!(output, input.bytes());
                    }
                }

                proptest! {
                    #![proptest_config(ProptestConfig::with_cases(32))]

                    #[test]
                    fn compress_with_level(
                        ref input in any::<InputStream>(),
                        limit in 1..20usize,
                        level in crate::any_level(),
                    ) {
                        let compressed = write::to_vec(
                            input.as_ref(),
                            |input| Box::pin(write::Encoder::with_quality(input, level)),
                            limit,
                        );
                        let output = sync::decompress(&compressed);
                        assert_eq!(output, input.bytes());
                    }
                }
            }
        }
    }
}

#[allow(unused_macros)]
macro_rules! tests {
    ($variant:ident) => {
        mod $variant {
            #[cfg(feature = "futures-io")]
            io_tests!(futures, $variant);

            #[cfg(feature = "tokio")]
            io_tests!(tokio, $variant);
        }
    };
}

mod proptest {
    #[cfg(feature = "brotli")]
    tests!(brotli);

    #[cfg(feature = "bzip2")]
    tests!(bzip2);

    #[cfg(feature = "deflate")]
    tests!(deflate);

    #[cfg(feature = "gzip")]
    tests!(gzip);

    #[cfg(feature = "lzma")]
    tests!(lzma);

    #[cfg(feature = "xz")]
    tests!(xz);

    #[cfg(feature = "zlib")]
    tests!(zlib);

    #[cfg(feature = "zstd")]
    tests!(zstd);
}