Skip to content

Add unit tests of the different flush kinds #498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,6 @@ mod tests {
use crate::write;
use crate::{Compression, Decompress, FlushDecompress};

#[cfg(feature = "any_zlib")]
use crate::{Compress, FlushCompress};

#[test]
Expand Down Expand Up @@ -783,4 +782,66 @@ mod tests {

assert_eq!(err.message(), Some("invalid stored block lengths"));
}

fn compress_with_flush(flush: FlushCompress) -> Vec<u8> {
let incompressible = (0..=255).collect::<Vec<u8>>();
let mut output = vec![0; 1024];

// Feed in the incompressible data followed by the indicated flush type.
let mut w = Compress::new(Compression::default(), false);
w.compress(&incompressible, &mut output, flush).unwrap();

if flush != FlushCompress::None {
// The first instance of incompressible input should have been written uncompressed.
assert!(w.total_out() >= 261);
assert_eq!(&output[0..5], &[0, 0, 1, 0xff, !1]);
assert_eq!(&output[5..261], &incompressible);
}

// Feed in the same data again.
let len = w.total_out() as usize;
w.compress(&incompressible, &mut output[len..], FlushCompress::Finish)
.unwrap();

if flush != FlushCompress::Full {
// This time, the data should have been compressed (because it is an exact duplicate of
// the earlier block).
assert!(w.total_out() < 300);
}

// Assert that all input has been processed.
assert_eq!(w.total_in(), 256 * 2);

output.resize(w.total_out() as usize, 0);
output
}

#[test]
fn test_partial_flush() {
let output = compress_with_flush(FlushCompress::Partial);

// Check for partial flush marker.
assert_eq!(output[261], 0x2);
assert_eq!(output[262] & 0x7, 0x4);
}

#[test]
fn test_sync_flush() {
let output = compress_with_flush(FlushCompress::Sync);

// Check for sync flush marker.
assert_eq!(&output[261..][..5], &[0, 0, 0, 0xff, 0xff]);
}

#[test]
fn test_full_flush() {
let output = compress_with_flush(FlushCompress::Full);
assert_eq!(output.len(), 527);

// Check for flush flush marker.
assert_eq!(&output[261..][..5], &[0, 0, 0, 0xff, 0xff]);

// Check that the second instance of incompressible input was also written uncompressed.
assert_eq!(&output[266..][..5], &[1, 0, 1, 0xff, !1]);
}
}
Loading