Skip to content

Commit

Permalink
add compression roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Nov 8, 2024
1 parent b445efa commit 2820025
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 157 additions & 5 deletions tests/quick.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
use std::env;
use std::path::Path;
use std::process::{Command, Stdio};

fn run_test(compressed: &str, expected: &[u8]) {
let mut cmd;
/// Useful to test with the C binary
fn bzip2_binary() -> &'static str {
env!("CARGO_BIN_EXE_bzip2")
}

fn command() -> Command {
match env::var("RUNNER") {
Ok(runner) if !runner.is_empty() => {
let mut runner_args = runner.split(' ');
cmd = Command::new(runner_args.next().unwrap());
let mut cmd = Command::new(runner_args.next().unwrap());
cmd.args(runner_args);
cmd.arg(env!("CARGO_BIN_EXE_bzip2"));
cmd.arg(bzip2_binary());

cmd
}
_ => cmd = Command::new(env!("CARGO_BIN_EXE_bzip2")),
_ => Command::new(bzip2_binary()),
}
}

fn run_test(compressed: &str, expected: &[u8]) {
let mut cmd = command();

let output = match cmd
.arg("-d")
.arg(compressed)
Expand Down Expand Up @@ -54,3 +66,143 @@ fn sample3() {
include_bytes!("input/quick/sample3.ref"),
);
}

#[test]
fn test_comp_decomp_sample_ref1() {
let sample = Path::new("tests/input/quick/sample1.ref");

for block_size in ["-1", "-2", "-3"] {
let mut cmd = command();
cmd.arg("--compress")
.arg(block_size)
.arg("--keep")
.arg("--stdout")
.arg(sample)
.stdout(Stdio::piped());

let output = cmd.output().unwrap();

assert!(output.status.success());

let tmpdir = tempfile::tempdir().unwrap();

let tempfile_path = tmpdir
.path()
.with_file_name(sample.file_name().unwrap())
.with_extension("bz2");

std::fs::write(&tempfile_path, output.stdout).unwrap();

let mut cmd = command();
cmd.arg("--decompress")
.arg("--stdout")
.arg(tempfile_path)
.stdout(Stdio::piped());

let output = cmd.output().unwrap();

assert!(output.status.success());

let out_hash = crc32fast::hash(&output.stdout);
let ref_file = std::fs::read(sample).unwrap();
let ref_hash = crc32fast::hash(&ref_file);

assert_eq!(out_hash, ref_hash);
}
}

#[test]
fn test_comp_decomp_sample_ref2() {
let sample = Path::new("tests/input/quick/sample2.ref");

for block_size in ["-1", "-2", "-3"] {
let mut cmd = command();
cmd.arg("--compress")
.arg(block_size)
.arg("--keep")
.arg("--stdout")
.arg(sample)
.stdout(Stdio::piped());

let output = cmd.output().unwrap();

assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);

// let tmpdir = tempfile::tempdir().unwrap();
let tmpdir_path = Path::new("/tmp/foo");

let tempfile_path = tmpdir_path
.with_file_name(sample.file_name().unwrap())
.with_extension("bz2");

std::fs::write(&tempfile_path, output.stdout).unwrap();

let mut cmd = command();
cmd.arg("--decompress")
.arg("--stdout")
.arg(tempfile_path)
.stdout(Stdio::piped());

let output = cmd.output().unwrap();

assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);

let out_hash = crc32fast::hash(&output.stdout);
let ref_file = std::fs::read(sample).unwrap();
let ref_hash = crc32fast::hash(&ref_file);

assert_eq!(out_hash, ref_hash);
}
}

#[test]
fn test_comp_decomp_sample_ref3() {
let sample = Path::new("tests/input/quick/sample3.ref");

for block_size in ["-1", "-2", "-3"] {
let mut cmd = command();
cmd.arg("--compress")
.arg(block_size)
.arg("--keep")
.arg("--stdout")
.arg(sample)
.stdout(Stdio::piped());

let output = cmd.output().unwrap();

assert!(output.status.success());

let tmpdir = tempfile::tempdir().unwrap();

let tempfile_path = tmpdir
.path()
.with_file_name(sample.file_name().unwrap())
.with_extension("bz2");

std::fs::write(&tempfile_path, output.stdout).unwrap();

let mut cmd = command();
cmd.arg("--decompress")
.arg("--stdout")
.arg(tempfile_path)
.stdout(Stdio::piped());

let output = cmd.output().unwrap();

assert!(output.status.success());

let out_hash = crc32fast::hash(&output.stdout);
let ref_file = std::fs::read(sample).unwrap();
let ref_hash = crc32fast::hash(&ref_file);

assert_eq!(out_hash, ref_hash);
}
}

0 comments on commit 2820025

Please sign in to comment.