diff --git a/brro-compressor/tests/e2e.rs b/brro-compressor/tests/e2e.rs index e69de29..a18876a 100644 --- a/brro-compressor/tests/e2e.rs +++ b/brro-compressor/tests/e2e.rs @@ -0,0 +1,109 @@ +use std::fs; +use std::path::{Path, PathBuf}; +use wavbrro::wavbrro::WavBrro; + +const TEST_FILE_NAME: &str = "go_gc_heap_goal_bytes.wbro"; +const TEST_COMPRESSED_FILE_NAME: &str = "go_gc_heap_goal_bytes.bro"; +const TEST_WBRO_PATH: &str = "tests/wbros/go_gc_heap_goal_bytes.wbro"; + +#[test] +fn test_compressor_idw() { + test_lossless_compression("idw") +} + +#[test] +fn test_compressor_polynomial() { + test_lossless_compression("polynomial") +} + +#[test] +fn test_compressor_auto() { + test_lossless_compression("auto") +} + +#[test] +fn test_compressor_fft() { + test_lossless_compression("fft") +} + +#[test] +fn test_compressor_noop() { + test_lossless_compression("noop") +} + +fn test_lossless_compression(compressor: &str) { + test_compression(compressor) +} + +#[test] +fn test_compressor_constant() { + // tests/wbros/uptime.wbro constant data which can be compressed by constant compressor + let test_dir = tempfile::tempdir().unwrap().into_path(); + fs::copy("tests/wbros/uptime.wbro", test_dir.join("uptime.wbro")).unwrap(); + + run_compressor(&[ + "--compressor", + "noop", + test_dir.join("uptime.wbro").to_str().unwrap(), + ]); + + run_compressor(&["-u", test_dir.join("uptime.bro").to_str().unwrap()]); + + compare_samples( + &PathBuf::from("tests/wbros/uptime.wbro"), + &test_dir.join("uptime.wbro"), + ) +} + +/// Runs compression and decompression test for a specified compressor. +/// max_error is an error level, compression speed is set as the lowest (0). +/// +/// It compresses tests/wbros/go_gc_duration_count.wbro file, decompresses +/// got .bro file, and compares the original .wbro and the decompressed one. +fn test_compression(compressor: &str) { + let test_dir = prepare_test_dir(); + + // Running data compression + run_compressor(&[ + "--compressor", + compressor, + "--error", + "0", + test_dir.join(TEST_FILE_NAME).to_str().unwrap(), + ]); + + // Running data decompression + run_compressor(&[ + "-u", + test_dir.join(TEST_COMPRESSED_FILE_NAME).to_str().unwrap(), + ]); + + compare_samples( + &PathBuf::from(TEST_WBRO_PATH), + &test_dir.join(TEST_FILE_NAME), + ) +} + +/// Prepares test directory and copies test wbro file there. +fn prepare_test_dir() -> PathBuf { + let test_dir = tempfile::tempdir().unwrap().into_path(); + fs::copy(TEST_WBRO_PATH, test_dir.join(TEST_FILE_NAME)).unwrap(); + test_dir +} + +/// Runs compressor binary with provided arguments. +fn run_compressor(args: &[&str]) { + let compressor_bin = env!("CARGO_BIN_EXE_brro-compressor"); + let exit_status = std::process::Command::new(compressor_bin) + .args(args) + .status() + .unwrap(); + + assert!(exit_status.success()); +} + +fn compare_samples(original: &Path, uncompressed: &Path) { + let original_samples = WavBrro::from_file(original).unwrap(); + let uncompressed_samples = WavBrro::from_file(uncompressed).unwrap(); + assert_eq!(original_samples, uncompressed_samples); +} diff --git a/brro-compressor/tests/wbros/go_gc_duration_count.wbro b/brro-compressor/tests/wbros/go_gc_duration_count.wbro deleted file mode 100644 index 71e88ae..0000000 Binary files a/brro-compressor/tests/wbros/go_gc_duration_count.wbro and /dev/null differ diff --git a/brro-compressor/tests/wbros/go_gc_heap_goal_bytes.wbro b/brro-compressor/tests/wbros/go_gc_heap_goal_bytes.wbro new file mode 100644 index 0000000..d6741a9 Binary files /dev/null and b/brro-compressor/tests/wbros/go_gc_heap_goal_bytes.wbro differ