Skip to content

Commit

Permalink
Test compat with flate2 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesgranger authored Sep 25, 2024
1 parent 33401b5 commit e6b3375
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions src/igzip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub fn read_gzip_header(
}

#[cfg(test)]
mod tests {
pub mod tests {

use io::Cursor;
use md5;
Expand All @@ -312,14 +312,14 @@ mod tests {
use super::*;

// Default testing data
fn gen_large_data() -> Vec<u8> {
(0..1_000_000)
pub fn gen_large_data() -> Vec<u8> {
(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)
}

Expand Down
48 changes: 39 additions & 9 deletions src/igzip/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,9 @@ impl<R: io::Read> io::Read for Decoder<R> {
mod tests {

use super::*;
use crate::igzip::tests::{gen_large_data, same_same};
use std::io::{self, Cursor};

fn gen_large_data() -> Vec<u8> {
(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();
Expand All @@ -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]
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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));
}
}
61 changes: 52 additions & 9 deletions src/igzip/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,12 @@ impl<W: io::Write> io::Write for Decoder<W> {

#[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<u8> {
(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();
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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));
}
}

0 comments on commit e6b3375

Please sign in to comment.