1 | pub mod bufread; |
2 | pub mod read; |
3 | pub mod write; |
4 | |
5 | #[cfg (test)] |
6 | mod tests { |
7 | use std::io::prelude::*; |
8 | |
9 | use rand::{thread_rng, Rng}; |
10 | |
11 | use super::{read, write}; |
12 | use crate::Compression; |
13 | |
14 | #[test ] |
15 | fn roundtrip() { |
16 | let mut real = Vec::new(); |
17 | let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); |
18 | let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); |
19 | for _ in 0..200 { |
20 | let to_write = &v[..thread_rng().gen_range(0..v.len())]; |
21 | real.extend(to_write.iter().copied()); |
22 | w.write_all(to_write).unwrap(); |
23 | } |
24 | let result = w.finish().unwrap(); |
25 | let mut r = read::DeflateDecoder::new(&result[..]); |
26 | let mut ret = Vec::new(); |
27 | r.read_to_end(&mut ret).unwrap(); |
28 | assert_eq!(ret, real); |
29 | } |
30 | |
31 | #[test ] |
32 | fn drop_writes() { |
33 | let mut data = Vec::new(); |
34 | write::DeflateEncoder::new(&mut data, Compression::default()) |
35 | .write_all(b"foo" ) |
36 | .unwrap(); |
37 | let mut r = read::DeflateDecoder::new(&data[..]); |
38 | let mut ret = Vec::new(); |
39 | r.read_to_end(&mut ret).unwrap(); |
40 | assert_eq!(ret, b"foo" ); |
41 | } |
42 | |
43 | #[test ] |
44 | fn total_in() { |
45 | let mut real = Vec::new(); |
46 | let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); |
47 | let v = crate::random_bytes().take(1024).collect::<Vec<_>>(); |
48 | for _ in 0..200 { |
49 | let to_write = &v[..thread_rng().gen_range(0..v.len())]; |
50 | real.extend(to_write.iter().copied()); |
51 | w.write_all(to_write).unwrap(); |
52 | } |
53 | let mut result = w.finish().unwrap(); |
54 | |
55 | let result_len = result.len(); |
56 | |
57 | for _ in 0..200 { |
58 | result.extend(v.iter().copied()); |
59 | } |
60 | |
61 | let mut r = read::DeflateDecoder::new(&result[..]); |
62 | let mut ret = Vec::new(); |
63 | r.read_to_end(&mut ret).unwrap(); |
64 | assert_eq!(ret, real); |
65 | assert_eq!(r.total_in(), result_len as u64); |
66 | } |
67 | |
68 | #[test ] |
69 | fn roundtrip2() { |
70 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
71 | let mut r = |
72 | read::DeflateDecoder::new(read::DeflateEncoder::new(&v[..], Compression::default())); |
73 | let mut ret = Vec::new(); |
74 | r.read_to_end(&mut ret).unwrap(); |
75 | assert_eq!(ret, v); |
76 | } |
77 | |
78 | #[test ] |
79 | fn roundtrip3() { |
80 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
81 | let mut w = write::DeflateEncoder::new( |
82 | write::DeflateDecoder::new(Vec::new()), |
83 | Compression::default(), |
84 | ); |
85 | w.write_all(&v).unwrap(); |
86 | let w = w.finish().unwrap().finish().unwrap(); |
87 | assert_eq!(w, v); |
88 | } |
89 | |
90 | #[test ] |
91 | fn reset_writer() { |
92 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
93 | let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); |
94 | w.write_all(&v).unwrap(); |
95 | let a = w.reset(Vec::new()).unwrap(); |
96 | w.write_all(&v).unwrap(); |
97 | let b = w.finish().unwrap(); |
98 | |
99 | let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); |
100 | w.write_all(&v).unwrap(); |
101 | let c = w.finish().unwrap(); |
102 | assert!(a == b && b == c); |
103 | } |
104 | |
105 | #[test ] |
106 | fn reset_reader() { |
107 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
108 | let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); |
109 | let mut r = read::DeflateEncoder::new(&v[..], Compression::default()); |
110 | r.read_to_end(&mut a).unwrap(); |
111 | r.reset(&v[..]); |
112 | r.read_to_end(&mut b).unwrap(); |
113 | |
114 | let mut r = read::DeflateEncoder::new(&v[..], Compression::default()); |
115 | r.read_to_end(&mut c).unwrap(); |
116 | assert!(a == b && b == c); |
117 | } |
118 | |
119 | #[test ] |
120 | fn reset_decoder() { |
121 | let v = crate::random_bytes().take(1024 * 1024).collect::<Vec<_>>(); |
122 | let mut w = write::DeflateEncoder::new(Vec::new(), Compression::default()); |
123 | w.write_all(&v).unwrap(); |
124 | let data = w.finish().unwrap(); |
125 | |
126 | { |
127 | let (mut a, mut b, mut c) = (Vec::new(), Vec::new(), Vec::new()); |
128 | let mut r = read::DeflateDecoder::new(&data[..]); |
129 | r.read_to_end(&mut a).unwrap(); |
130 | r.reset(&data); |
131 | r.read_to_end(&mut b).unwrap(); |
132 | |
133 | let mut r = read::DeflateDecoder::new(&data[..]); |
134 | r.read_to_end(&mut c).unwrap(); |
135 | assert!(a == b && b == c && c == v); |
136 | } |
137 | |
138 | { |
139 | let mut w = write::DeflateDecoder::new(Vec::new()); |
140 | w.write_all(&data).unwrap(); |
141 | let a = w.reset(Vec::new()).unwrap(); |
142 | w.write_all(&data).unwrap(); |
143 | let b = w.finish().unwrap(); |
144 | |
145 | let mut w = write::DeflateDecoder::new(Vec::new()); |
146 | w.write_all(&data).unwrap(); |
147 | let c = w.finish().unwrap(); |
148 | assert!(a == b && b == c && c == v); |
149 | } |
150 | } |
151 | |
152 | #[test ] |
153 | fn zero_length_read_with_data() { |
154 | let m = vec![3u8; 128 * 1024 + 1]; |
155 | let mut c = read::DeflateEncoder::new(&m[..], Compression::default()); |
156 | |
157 | let mut result = Vec::new(); |
158 | c.read_to_end(&mut result).unwrap(); |
159 | |
160 | let mut d = read::DeflateDecoder::new(&result[..]); |
161 | let mut data = Vec::new(); |
162 | assert_eq!(d.read(&mut data).unwrap(), 0); |
163 | } |
164 | |
165 | #[test ] |
166 | fn qc_reader() { |
167 | ::quickcheck::quickcheck(test as fn(_) -> _); |
168 | |
169 | fn test(v: Vec<u8>) -> bool { |
170 | let mut r = read::DeflateDecoder::new(read::DeflateEncoder::new( |
171 | &v[..], |
172 | Compression::default(), |
173 | )); |
174 | let mut v2 = Vec::new(); |
175 | r.read_to_end(&mut v2).unwrap(); |
176 | v == v2 |
177 | } |
178 | } |
179 | |
180 | #[test ] |
181 | fn qc_writer() { |
182 | ::quickcheck::quickcheck(test as fn(_) -> _); |
183 | |
184 | fn test(v: Vec<u8>) -> bool { |
185 | let mut w = write::DeflateEncoder::new( |
186 | write::DeflateDecoder::new(Vec::new()), |
187 | Compression::default(), |
188 | ); |
189 | w.write_all(&v).unwrap(); |
190 | v == w.finish().unwrap().finish().unwrap() |
191 | } |
192 | } |
193 | } |
194 | |