From 0ddace7c22e2e33cefdc0c90ff903cbbdea9d6e5 Mon Sep 17 00:00:00 2001 From: Patrick Marks Date: Sat, 5 Mar 2022 16:33:02 -0800 Subject: [PATCH] add test for favor_dec_speed, run fmt --- src/block/mod.rs | 30 ++++++++++++++++-------------- src/encoder.rs | 26 +++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/block/mod.rs b/src/block/mod.rs index 7e7ca2a35..620926a91 100644 --- a/src/block/mod.rs +++ b/src/block/mod.rs @@ -33,7 +33,7 @@ pub enum CompressionMode { DEFAULT, } -/// Returns the size of the buffer that is guaranteed to hold the result of +/// Returns the size of the buffer that is guaranteed to hold the result of /// compressing `uncompressed_size` bytes of in data. Returns std::io::Error /// with ErrorKind::InvalidInput if input data is too long to be compressed by lz4. pub fn compress_bound(uncompressed_size: usize) -> Result { @@ -50,7 +50,6 @@ pub fn compress_bound(uncompressed_size: usize) -> Result { Ok(compress_bound as usize) } - /// Compresses the full src buffer using the specified CompressionMode, where None and Some(Default) /// are treated equally. If prepend_size is set, the source length will be prepended to the output /// buffer. @@ -91,8 +90,12 @@ pub fn compress(src: &[u8], mode: Option, prepend_size: bool) - /// # Errors /// Returns std::io::Error with ErrorKind::InvalidInput if the src buffer is too long. /// Returns std::io::Error with ErrorKind::Other if the compression data does not find in `buffer`. -pub fn compress_to_buffer(src: &[u8], mode: Option, prepend_size: bool, buffer: &mut [u8]) -> Result { - +pub fn compress_to_buffer( + src: &[u8], + mode: Option, + prepend_size: bool, + buffer: &mut [u8], +) -> Result { // check that src isn't too big for lz4 let max_len: i32 = unsafe { LZ4_compressBound(src.len() as i32) }; @@ -151,11 +154,7 @@ pub fn compress_to_buffer(src: &[u8], mode: Option, prepend_siz return Err(Error::new(ErrorKind::Other, "Compression failed")); } - let written_size = if prepend_size { - dec_size + 4 - } else { - dec_size - }; + let written_size = if prepend_size { dec_size + 4 } else { dec_size }; Ok(written_size as usize) } @@ -216,7 +215,11 @@ pub fn decompress(src: &[u8], uncompressed_size: Option) -> Result> Ok(buffer) } -pub fn decompress_to_buffer(mut src: &[u8], uncompressed_size: Option, buffer: &mut [u8]) -> Result { +pub fn decompress_to_buffer( + mut src: &[u8], + uncompressed_size: Option, + buffer: &mut [u8], +) -> Result { let size; if let Some(s) = uncompressed_size { @@ -255,8 +258,8 @@ pub fn decompress_to_buffer(mut src: &[u8], uncompressed_size: Option, buff if size as usize > buffer.len() { return Err(Error::new( ErrorKind::InvalidInput, - "buffer isn't large enough to hold decompressed data" - )) + "buffer isn't large enough to hold decompressed data", + )); } let dec_bytes = unsafe { @@ -280,7 +283,7 @@ pub fn decompress_to_buffer(mut src: &[u8], uncompressed_size: Option, buff #[cfg(test)] mod test { - use crate::block::{compress, decompress, CompressionMode, decompress_to_buffer}; + use crate::block::{compress, decompress, decompress_to_buffer, CompressionMode}; use super::compress_to_buffer; @@ -358,7 +361,6 @@ mod test { let r = compress_to_buffer(&data[..], None, true, &mut small_buf); assert!(r.is_err()); - let mut big_buf = vec![0; 1000]; let r = compress_to_buffer(&data[..], None, true, &mut big_buf).unwrap(); assert_eq!(big_buf[r], 0); diff --git a/src/encoder.rs b/src/encoder.rs index 0714474dd..affe1ec60 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -201,7 +201,7 @@ impl Drop for EncoderContext { #[cfg(test)] mod test { use super::EncoderBuilder; - use std::io::Write; + use std::io::{Read, Write}; #[test] fn test_encoder_smoke() { @@ -232,4 +232,28 @@ mod test { let enc = EncoderBuilder::new().build(Vec::new()); check_send(&enc); } + + #[test] + fn test_favor_dec_speed() { + let mut encoder = EncoderBuilder::new() + .level(11) + .favor_dec_speed(true) + .build(Vec::new()) + .unwrap(); + let mut input = Vec::new(); + let mut rnd: u32 = 42; + for _ in 0..1024 * 1024 { + input.push((rnd & 0xFF) as u8); + rnd = ((1664525 as u64) * (rnd as u64) + (1013904223 as u64)) as u32; + } + encoder.write(&input).unwrap(); + let (compressed, result) = encoder.finish(); + result.unwrap(); + + let mut dec = crate::decoder::Decoder::new(&compressed[..]).unwrap(); + + let mut output = Vec::new(); + dec.read_to_end(&mut output).unwrap(); + assert_eq!(input, output); + } }