1#[cfg(test)]
2use alloc::vec;
3
4#[cfg(test)]
5use alloc::vec::Vec;
6
7#[cfg(test)]
8extern crate std;
9
10#[cfg(all(test, not(feature = "std")))]
11impl crate::io_nostd::Read for std::fs::File {
12 fn read(&mut self, buf: &mut [u8]) -> Result<usize, crate::io_nostd::Error> {
13 std::io::Read::read(self, buf).map_err(|e| {
14 if e.get_ref().is_none() {
15 crate::io_nostd::Error::from(crate::io_nostd::ErrorKind::Other)
16 } else {
17 crate::io_nostd::Error::new(
18 crate::io_nostd::ErrorKind::Other,
19 alloc::boxed::Box::new(e.into_inner().unwrap()),
20 )
21 }
22 })
23 }
24}
25
26#[cfg(all(test, feature = "std"))]
27#[allow(dead_code)]
28fn assure_error_impl() {
29 // not a real test just there to throw an compiler error if Error is not derived correctly
30
31 use crate::frame_decoder::FrameDecoderError;
32 let _err: &dyn std::error::Error = &FrameDecoderError::NotYetInitialized;
33}
34
35#[test]
36fn skippable_frame() {
37 use crate::frame;
38
39 let mut content = vec![];
40 content.extend_from_slice(&0x184D2A50u32.to_le_bytes());
41 content.extend_from_slice(&300u32.to_le_bytes());
42 assert_eq!(8, content.len());
43 let err = frame::read_frame_header(content.as_slice());
44 assert!(matches!(
45 err,
46 Err(frame::ReadFrameHeaderError::SkipFrame(0x184D2A50u32, 300))
47 ));
48
49 content.clear();
50 content.extend_from_slice(&0x184D2A5Fu32.to_le_bytes());
51 content.extend_from_slice(&0xFFFFFFFFu32.to_le_bytes());
52 assert_eq!(8, content.len());
53 let err = frame::read_frame_header(content.as_slice());
54 assert!(matches!(
55 err,
56 Err(frame::ReadFrameHeaderError::SkipFrame(
57 0x184D2A5Fu32,
58 0xFFFFFFFF
59 ))
60 ));
61}
62
63#[cfg(test)]
64#[test]
65fn test_frame_header_reading() {
66 use crate::frame;
67 use std::fs;
68
69 let mut content = fs::File::open("./decodecorpus_files/z000088.zst").unwrap();
70 let (_frame, _) = frame::read_frame_header(&mut content).unwrap();
71}
72
73#[test]
74fn test_block_header_reading() {
75 use crate::decoding;
76 use crate::frame;
77 use std::fs;
78
79 let mut content: File = fs::File::open(path:"./decodecorpus_files/z000088.zst").unwrap();
80 let (_frame: Frame, _) = frame::read_frame_header(&mut content).unwrap();
81
82 let mut block_dec: BlockDecoder = decoding::block_decoder::new();
83 let block_header: (BlockHeader, u8) = block_dec.read_block_header(&mut content).unwrap();
84 let _ = block_header; //TODO validate blockheader in a smart way
85}
86
87#[test]
88fn test_frame_decoder() {
89 use crate::frame_decoder;
90 use std::fs;
91
92 let mut content: File = fs::File::open(path:"./decodecorpus_files/z000088.zst").unwrap();
93
94 struct NullWriter(());
95 impl std::io::Write for NullWriter {
96 fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
97 Ok(buf.len())
98 }
99 fn flush(&mut self) -> Result<(), std::io::Error> {
100 Ok(())
101 }
102 }
103 let mut _null_target: NullWriter = NullWriter(());
104
105 let mut frame_dec: FrameDecoder = frame_decoder::FrameDecoder::new();
106 frame_dec.reset(&mut content).unwrap();
107 frame_decResult
108 .decode_blocks(&mut content, strat:frame_decoder::BlockDecodingStrategy::All)
109 .unwrap();
110}
111
112#[test]
113fn test_decode_from_to() {
114 use crate::frame_decoder;
115 use std::fs::File;
116 use std::io::Read;
117 let f = File::open("./decodecorpus_files/z000088.zst").unwrap();
118 let mut frame_dec = frame_decoder::FrameDecoder::new();
119
120 let content: Vec<u8> = f.bytes().map(|x| x.unwrap()).collect();
121
122 let mut target = vec![0u8; 1024 * 1024];
123
124 // first part
125 let source1 = &content[..50 * 1024];
126 let (read1, written1) = frame_dec
127 .decode_from_to(source1, target.as_mut_slice())
128 .unwrap();
129
130 //second part explicitely without checksum
131 let source2 = &content[read1..content.len() - 4];
132 let (read2, written2) = frame_dec
133 .decode_from_to(source2, &mut target[written1..])
134 .unwrap();
135
136 //must have decoded until checksum
137 assert!(read1 + read2 == content.len() - 4);
138
139 //insert checksum separatly to test that this is handled correctly
140 let chksum_source = &content[read1 + read2..];
141 let (read3, written3) = frame_dec
142 .decode_from_to(chksum_source, &mut target[written1 + written2..])
143 .unwrap();
144
145 //this must result in these values because just the checksum was processed
146 assert!(read3 == 4);
147 assert!(written3 == 0);
148
149 let read = read1 + read2 + read3;
150 let written = written1 + written2;
151
152 let result = &target.as_slice()[..written];
153
154 if read != content.len() {
155 panic!(
156 "Byte counter: {} was wrong. Should be: {}",
157 read,
158 content.len()
159 );
160 }
161
162 match frame_dec.get_checksum_from_data() {
163 Some(chksum) => {
164 if frame_dec.get_calculated_checksum().unwrap() != chksum {
165 std::println!(
166 "Checksum did not match! From data: {}, calculated while decoding: {}\n",
167 chksum,
168 frame_dec.get_calculated_checksum().unwrap()
169 );
170 } else {
171 std::println!("Checksums are ok!\n");
172 }
173 }
174 None => std::println!("No checksums to test\n"),
175 }
176
177 let original_f = File::open("./decodecorpus_files/z000088").unwrap();
178 let original: Vec<u8> = original_f.bytes().map(|x| x.unwrap()).collect();
179
180 if original.len() != result.len() {
181 panic!(
182 "Result has wrong length: {}, should be: {}",
183 result.len(),
184 original.len()
185 );
186 }
187
188 let mut counter = 0;
189 let min = if original.len() < result.len() {
190 original.len()
191 } else {
192 result.len()
193 };
194 for idx in 0..min {
195 if original[idx] != result[idx] {
196 counter += 1;
197 //std::println!(
198 // "Original {:3} not equal to result {:3} at byte: {}",
199 // original[idx], result[idx], idx,
200 //);
201 }
202 }
203 if counter > 0 {
204 panic!("Result differs in at least {} bytes from original", counter);
205 }
206}
207
208#[test]
209fn test_specific_file() {
210 use crate::frame_decoder;
211 use std::fs;
212 use std::io::Read;
213
214 let path = "./decodecorpus_files/z000068.zst";
215 let mut content = fs::File::open(path).unwrap();
216
217 struct NullWriter(());
218 impl std::io::Write for NullWriter {
219 fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
220 Ok(buf.len())
221 }
222 fn flush(&mut self) -> Result<(), std::io::Error> {
223 Ok(())
224 }
225 }
226 let mut _null_target = NullWriter(());
227
228 let mut frame_dec = frame_decoder::FrameDecoder::new();
229 frame_dec.reset(&mut content).unwrap();
230 frame_dec
231 .decode_blocks(&mut content, frame_decoder::BlockDecodingStrategy::All)
232 .unwrap();
233 let result = frame_dec.collect().unwrap();
234
235 let original_f = fs::File::open("./decodecorpus_files/z000088").unwrap();
236 let original: Vec<u8> = original_f.bytes().map(|x| x.unwrap()).collect();
237
238 std::println!("Results for file: {}", path);
239
240 if original.len() != result.len() {
241 std::println!(
242 "Result has wrong length: {}, should be: {}",
243 result.len(),
244 original.len()
245 );
246 }
247
248 let mut counter = 0;
249 let min = if original.len() < result.len() {
250 original.len()
251 } else {
252 result.len()
253 };
254 for idx in 0..min {
255 if original[idx] != result[idx] {
256 counter += 1;
257 //std::println!(
258 // "Original {:3} not equal to result {:3} at byte: {}",
259 // original[idx], result[idx], idx,
260 //);
261 }
262 }
263 if counter > 0 {
264 std::println!("Result differs in at least {} bytes from original", counter);
265 }
266}
267
268#[test]
269#[cfg(feature = "std")]
270fn test_streaming() {
271 use std::fs;
272 use std::io::Read;
273
274 let mut content = fs::File::open("./decodecorpus_files/z000088.zst").unwrap();
275 let mut stream = crate::streaming_decoder::StreamingDecoder::new(&mut content).unwrap();
276
277 let mut result = Vec::new();
278 Read::read_to_end(&mut stream, &mut result).unwrap();
279
280 let original_f = fs::File::open("./decodecorpus_files/z000088").unwrap();
281 let original: Vec<u8> = original_f.bytes().map(|x| x.unwrap()).collect();
282
283 if original.len() != result.len() {
284 panic!(
285 "Result has wrong length: {}, should be: {}",
286 result.len(),
287 original.len()
288 );
289 }
290
291 let mut counter = 0;
292 let min = if original.len() < result.len() {
293 original.len()
294 } else {
295 result.len()
296 };
297 for idx in 0..min {
298 if original[idx] != result[idx] {
299 counter += 1;
300 //std::println!(
301 // "Original {:3} not equal to result {:3} at byte: {}",
302 // original[idx], result[idx], idx,
303 //);
304 }
305 }
306 if counter > 0 {
307 panic!("Result differs in at least {} bytes from original", counter);
308 }
309
310 // Test resetting to a new file while keeping the old decoder
311
312 let mut content = fs::File::open("./decodecorpus_files/z000068.zst").unwrap();
313 let mut stream =
314 crate::streaming_decoder::StreamingDecoder::new_with_decoder(&mut content, stream.inner())
315 .unwrap();
316
317 let mut result = Vec::new();
318 Read::read_to_end(&mut stream, &mut result).unwrap();
319
320 let original_f = fs::File::open("./decodecorpus_files/z000068").unwrap();
321 let original: Vec<u8> = original_f.bytes().map(|x| x.unwrap()).collect();
322
323 std::println!("Results for file:");
324
325 if original.len() != result.len() {
326 panic!(
327 "Result has wrong length: {}, should be: {}",
328 result.len(),
329 original.len()
330 );
331 }
332
333 let mut counter = 0;
334 let min = if original.len() < result.len() {
335 original.len()
336 } else {
337 result.len()
338 };
339 for idx in 0..min {
340 if original[idx] != result[idx] {
341 counter += 1;
342 //std::println!(
343 // "Original {:3} not equal to result {:3} at byte: {}",
344 // original[idx], result[idx], idx,
345 //);
346 }
347 }
348 if counter > 0 {
349 panic!("Result differs in at least {} bytes from original", counter);
350 }
351}
352
353#[test]
354#[cfg(not(feature = "std"))]
355fn test_streaming_no_std() {
356 use crate::io::Read;
357
358 let content = include_bytes!("../../decodecorpus_files/z000088.zst");
359 let mut content = content.as_slice();
360 let mut stream = crate::streaming_decoder::StreamingDecoder::new(&mut content).unwrap();
361
362 let original = include_bytes!("../../decodecorpus_files/z000088");
363 let mut result = vec![0; original.len()];
364 Read::read_exact(&mut stream, &mut result).unwrap();
365
366 if original.len() != result.len() {
367 panic!(
368 "Result has wrong length: {}, should be: {}",
369 result.len(),
370 original.len()
371 );
372 }
373
374 let mut counter = 0;
375 let min = if original.len() < result.len() {
376 original.len()
377 } else {
378 result.len()
379 };
380 for idx in 0..min {
381 if original[idx] != result[idx] {
382 counter += 1;
383 //std::println!(
384 // "Original {:3} not equal to result {:3} at byte: {}",
385 // original[idx], result[idx], idx,
386 //);
387 }
388 }
389 if counter > 0 {
390 panic!("Result differs in at least {} bytes from original", counter);
391 }
392
393 // Test resetting to a new file while keeping the old decoder
394
395 let content = include_bytes!("../../decodecorpus_files/z000068.zst");
396 let mut content = content.as_slice();
397 let mut stream =
398 crate::streaming_decoder::StreamingDecoder::new_with_decoder(&mut content, stream.inner())
399 .unwrap();
400
401 let original = include_bytes!("../../decodecorpus_files/z000068");
402 let mut result = vec![0; original.len()];
403 Read::read_exact(&mut stream, &mut result).unwrap();
404
405 std::println!("Results for file:");
406
407 if original.len() != result.len() {
408 panic!(
409 "Result has wrong length: {}, should be: {}",
410 result.len(),
411 original.len()
412 );
413 }
414
415 let mut counter = 0;
416 let min = if original.len() < result.len() {
417 original.len()
418 } else {
419 result.len()
420 };
421 for idx in 0..min {
422 if original[idx] != result[idx] {
423 counter += 1;
424 //std::println!(
425 // "Original {:3} not equal to result {:3} at byte: {}",
426 // original[idx], result[idx], idx,
427 //);
428 }
429 }
430 if counter > 0 {
431 panic!("Result differs in at least {} bytes from original", counter);
432 }
433}
434
435pub mod bit_reader;
436pub mod decode_corpus;
437pub mod dict_test;
438pub mod fuzz_regressions;
439