Skip to content

Commit

Permalink
add test for favor_dec_speed, run fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarks committed Mar 6, 2022
1 parent e46e5e6 commit 0ddace7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
30 changes: 16 additions & 14 deletions src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
Expand All @@ -50,7 +50,6 @@ pub fn compress_bound(uncompressed_size: usize) -> Result<usize> {
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.
Expand Down Expand Up @@ -91,8 +90,12 @@ pub fn compress(src: &[u8], mode: Option<CompressionMode>, 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<CompressionMode>, prepend_size: bool, buffer: &mut [u8]) -> Result<usize> {

pub fn compress_to_buffer(
src: &[u8],
mode: Option<CompressionMode>,
prepend_size: bool,
buffer: &mut [u8],
) -> Result<usize> {
// check that src isn't too big for lz4
let max_len: i32 = unsafe { LZ4_compressBound(src.len() as i32) };

Expand Down Expand Up @@ -151,11 +154,7 @@ pub fn compress_to_buffer(src: &[u8], mode: Option<CompressionMode>, 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)
}
Expand Down Expand Up @@ -216,7 +215,11 @@ pub fn decompress(src: &[u8], uncompressed_size: Option<i32>) -> Result<Vec<u8>>
Ok(buffer)
}

pub fn decompress_to_buffer(mut src: &[u8], uncompressed_size: Option<i32>, buffer: &mut [u8]) -> Result<usize> {
pub fn decompress_to_buffer(
mut src: &[u8],
uncompressed_size: Option<i32>,
buffer: &mut [u8],
) -> Result<usize> {
let size;

if let Some(s) = uncompressed_size {
Expand Down Expand Up @@ -255,8 +258,8 @@ pub fn decompress_to_buffer(mut src: &[u8], uncompressed_size: Option<i32>, 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 {
Expand All @@ -280,7 +283,7 @@ pub fn decompress_to_buffer(mut src: &[u8], uncompressed_size: Option<i32>, 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;

Expand Down Expand Up @@ -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);
Expand Down
26 changes: 25 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 0ddace7

Please sign in to comment.