diff --git a/Cargo.toml b/Cargo.toml index 07bbd68..637efac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ isal-sys = { path = "isal-sys", version = "0.3.1+496255c" } [dev-dependencies] criterion = "0.3" md5 = "0.7.0" +flate2 = "^1" [[bench]] name = "igzip" diff --git a/src/igzip/mod.rs b/src/igzip/mod.rs index ee841ad..4a0c7ba 100644 --- a/src/igzip/mod.rs +++ b/src/igzip/mod.rs @@ -303,7 +303,7 @@ pub fn read_gzip_header( } #[cfg(test)] -mod tests { +pub mod tests { use io::Cursor; use md5; @@ -312,14 +312,14 @@ mod tests { use super::*; // Default testing data - fn gen_large_data() -> Vec { - (0..1_000_000) + pub fn gen_large_data() -> Vec { + (0..(BUF_SIZE * 2) + 1) .map(|_| b"oh what a beautiful morning, oh what a beautiful day!!".to_vec()) .flat_map(|v| v) .collect() } - fn same_same(a: &[u8], b: &[u8]) -> bool { + pub fn same_same(a: &[u8], b: &[u8]) -> bool { md5::compute(a) == md5::compute(b) } diff --git a/src/igzip/read.rs b/src/igzip/read.rs index 3ac1cec..efc2202 100644 --- a/src/igzip/read.rs +++ b/src/igzip/read.rs @@ -251,15 +251,9 @@ impl io::Read for Decoder { mod tests { use super::*; + use crate::igzip::tests::{gen_large_data, same_same}; use std::io::{self, Cursor}; - fn gen_large_data() -> Vec { - (0..1_000_000) - .map(|_| b"oh what a beautiful morning, oh what a beautiful day!!".to_vec()) - .flat_map(|v| v) - .collect() - } - #[test] fn large_roundtrip() { let input = gen_large_data(); @@ -274,6 +268,7 @@ mod tests { let nbytes = io::copy(&mut decoder, &mut decompressed).unwrap(); assert_eq!(nbytes as usize, input.len()); + assert!(same_same(&input, &decompressed)); } #[test] @@ -306,7 +301,7 @@ mod tests { let n = io::copy(&mut encoder, &mut output)? as usize; let decompressed = decompress(&output[..n])?; - assert_eq!(input, decompressed.as_slice()); + assert!(same_same(&input, decompressed.as_slice())); Ok(()) } @@ -342,8 +337,43 @@ mod tests { let n = io::copy(&mut decoder, &mut decompressed)? as usize; assert_eq!(n, decompressed.len()); - assert_eq!(input, decompressed.as_slice()); + assert!(same_same(&input, decompressed.as_slice())); Ok(()) } + + #[test] + fn flate2_gzip_compat_encoder_out() { + let data = gen_large_data(); + + // our encoder + let mut encoder = Encoder::new(data.as_slice(), CompressionLevel::Three, true); + let mut compressed = vec![]; + io::copy(&mut encoder, &mut compressed).unwrap(); + + // their decoder + let mut decoder = flate2::read::GzDecoder::new(compressed.as_slice()); + let mut decompressed = vec![]; + io::copy(&mut decoder, &mut decompressed).unwrap(); + + assert!(same_same(&data, &decompressed)); + } + + #[test] + fn flate2_gzip_compat_decoder_out() { + let data = gen_large_data(); + + // their encoder + let mut encoder = + flate2::read::GzEncoder::new(data.as_slice(), flate2::Compression::fast()); + let mut compressed = vec![]; + io::copy(&mut encoder, &mut compressed).unwrap(); + + // our decoder + let mut decoder = Decoder::new(compressed.as_slice()); + let mut decompressed = vec![]; + io::copy(&mut decoder, &mut decompressed).unwrap(); + + assert!(same_same(&data, &decompressed)); + } } diff --git a/src/igzip/write.rs b/src/igzip/write.rs index a0ffccb..5cd8817 100644 --- a/src/igzip/write.rs +++ b/src/igzip/write.rs @@ -280,16 +280,12 @@ impl io::Write for Decoder { #[cfg(test)] pub mod tests { + use io::Cursor; + use super::*; + use crate::igzip::tests::{gen_large_data, same_same}; use std::io::Write; - fn gen_large_data() -> Vec { - (0..1_000_000) - .map(|_| b"oh what a beautiful morning, oh what a beautiful day!!".to_vec()) - .flat_map(|v| v) - .collect() - } - #[test] fn test_encoder_basic() { let data = gen_large_data(); @@ -314,7 +310,7 @@ pub mod tests { // and can be decompressed let decompressed = crate::igzip::decompress(io::Cursor::new(&compressed)).unwrap(); - assert_eq!(decompressed.len(), data.len()); + assert!(same_same(&decompressed, &data)); } #[test] @@ -348,7 +344,7 @@ pub mod tests { let mut decoder = Decoder::new(&mut decompressed); let nbytes = io::copy(&mut io::Cursor::new(&compressed), &mut decoder).unwrap(); assert_eq!(nbytes, compressed.len() as u64); - assert_eq!(decompressed.len(), data.len()); + assert!(same_same(&decompressed, &data)); } #[test] @@ -370,4 +366,51 @@ pub mod tests { assert_eq!(nbytes, compressed.len() as _); assert_eq!(&decompressed, b"foobar"); } + + #[test] + fn flate2_gzip_compat_encoder_out() { + let data = gen_large_data(); + + // our encoder + let mut compressed = vec![]; + { + let mut encoder = Encoder::new(&mut compressed, CompressionLevel::Three, true); + io::copy(&mut Cursor::new(&data), &mut encoder).unwrap(); + encoder.flush().unwrap(); + } + + // their decoder + let mut decompressed = vec![]; + { + let mut decoder = flate2::write::GzDecoder::new(&mut decompressed); + io::copy(&mut Cursor::new(&compressed), &mut decoder).unwrap(); + decoder.flush().unwrap(); + } + + assert!(same_same(&data, &decompressed)); + } + + #[test] + fn flate2_gzip_compat_decoder_out() { + let data = gen_large_data(); + + // their encoder + let mut compressed = vec![]; + { + let mut encoder = + flate2::write::GzEncoder::new(&mut compressed, flate2::Compression::fast()); + io::copy(&mut Cursor::new(&data), &mut encoder).unwrap(); + encoder.flush().unwrap(); + } + + // our decoder + let mut decompressed = vec![]; + { + let mut decoder = Decoder::new(&mut decompressed); + io::copy(&mut Cursor::new(&compressed), &mut decoder).unwrap(); + decoder.flush().unwrap(); + } + + assert!(same_same(&data, &decompressed)); + } }