|
|
|
@@ -0,0 +1,72 @@
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use flate2::Compression;
|
|
|
|
|
use flate2::read::DeflateDecoder;
|
|
|
|
|
use flate2::write::DeflateEncoder;
|
|
|
|
|
use tempfile::tempfile;
|
|
|
|
|
|
|
|
|
|
pub struct TestCompression {}
|
|
|
|
|
|
|
|
|
|
impl TestCompression {
|
|
|
|
|
pub fn compress_string(input: &str) -> Vec<u8> {
|
|
|
|
|
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
|
|
|
|
|
encoder
|
|
|
|
|
.write_all(input.as_bytes())
|
|
|
|
|
.expect("Failed to write data");
|
|
|
|
|
encoder.finish().expect("Failed to finish compression")
|
|
|
|
|
}
|
|
|
|
|
pub fn decompress_to_string(compressed_data: &[u8]) -> Result<String, std::io::Error> {
|
|
|
|
|
let mut decoder = DeflateDecoder::new(compressed_data);
|
|
|
|
|
let mut decompressed = String::new();
|
|
|
|
|
decoder.read_to_string(&mut decompressed)?;
|
|
|
|
|
Ok(decompressed)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Load a pre-compressed test result to a string for comparison in an assert macro
|
|
|
|
|
pub fn load_compressed_file_to_string(source: &Path) -> String {
|
|
|
|
|
let mut compressed_file = File::open(&source).expect(format!("Unable to open compressed file at [{}]", source.display()).as_str());
|
|
|
|
|
let mut compressed_data = Vec::new();
|
|
|
|
|
compressed_file.read_to_end(&mut compressed_data)
|
|
|
|
|
.expect(format!("Unable to read compressed data from [{}]", source.display()).as_str());
|
|
|
|
|
TestCompression::decompress_to_string(&compressed_data).unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Save a str to a specified file as compressed data
|
|
|
|
|
pub fn save_string_as_compressed_data(source: &str, target_file: &Path) {
|
|
|
|
|
let mut compressed_file = File::open(&target_file).expect(format!("Unable to open target file [{}]", target_file.display()).as_str());
|
|
|
|
|
let compressed_data = TestCompression::compress_string(source);
|
|
|
|
|
compressed_file.write_all(&compressed_data).expect(format!("Unable to write compressed data to [{}]", target_file.display()).as_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn smoke() {
|
|
|
|
|
assert!(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn compression_round_trip() {
|
|
|
|
|
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
|
|
|
|
|
|
let compressed_text = TestCompression::compress_string(to_compress);
|
|
|
|
|
let decompressed_text = TestCompression::decompress_to_string(&compressed_text).unwrap();
|
|
|
|
|
assert_eq!(to_compress, decompressed_text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn file_compression_round_trip() {
|
|
|
|
|
// compress string to file...
|
|
|
|
|
let string_to_compress = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
|
let compressed_string = TestCompression::compress_string(string_to_compress);
|
|
|
|
|
let temp_target = tempfile().unwrap();
|
|
|
|
|
//
|
|
|
|
|
// ...decompress from file...
|
|
|
|
|
//
|
|
|
|
|
// ...verify its the same.
|
|
|
|
|
}
|
|
|
|
|
}
|