119 lines
4.4 KiB
Rust
119 lines
4.4 KiB
Rust
use std::fs::File;
|
|
use std::io::{Read, Write};
|
|
use tempfile::NamedTempFile;
|
|
use trevors_utilities::test_compression::TestCompression;
|
|
|
|
#[test]
|
|
fn smoke() {
|
|
assert!(true)
|
|
}
|
|
|
|
#[test]
|
|
fn compression_round_trip() {
|
|
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
;
|
|
let decompressed_text =
|
|
TestCompression::decompress_to_string(
|
|
&TestCompression::compress_string(
|
|
to_compress
|
|
)).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 mut temp_target = NamedTempFile::new().unwrap();
|
|
let mut temp_reader = temp_target.reopen();
|
|
|
|
// ...write the compressed version to a file...
|
|
temp_target.write_all(&compressed_string).expect("Unable to write compressed file for test");
|
|
//
|
|
// ...decompress from file...
|
|
let mut compressed_read = Vec::new();
|
|
temp_reader.unwrap().read_to_end(&mut compressed_read).expect("Unable to read compressed data for test");
|
|
let decompresed = TestCompression::decompress_to_string(&compressed_read).expect("Unable to decompress string for test");
|
|
//
|
|
// ...verify its the same.
|
|
assert_eq!(
|
|
string_to_compress,
|
|
decompresed
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn file_compression_reader() {
|
|
// Get the 'sample text'
|
|
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
// ...write it to the temp file...
|
|
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test");
|
|
temp_file.write_all(&*TestCompression::compress_string(to_compress)).expect("Unable to write compressed data to temp file for test");
|
|
let temp2_path = temp_file.path();
|
|
let uncompressed_text = TestCompression::load_compressed_file_to_string(temp2_path);
|
|
|
|
assert_eq!(uncompressed_text, to_compress);
|
|
}
|
|
|
|
#[test]
|
|
fn file_compression_writer() {
|
|
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
|
|
|
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test.");
|
|
TestCompression::save_string_as_compressed_data(to_compress, temp_file.path());
|
|
|
|
// read back the compressed text
|
|
let mut read_buffer = Vec::new();
|
|
temp_file.read_to_end(&mut read_buffer).expect("Unable to read compressed data back.");
|
|
let decompressed_text = TestCompression::decompress_to_string(&read_buffer).expect("Unable to decompress text");
|
|
assert_eq!(
|
|
decompressed_text,
|
|
to_compress);
|
|
}
|
|
|
|
#[test]
|
|
fn compress_file() {
|
|
// create our test file.
|
|
let test_text = "The quick brown fox jumps over the lazy dog.";
|
|
let mut original_file = NamedTempFile::new().unwrap();
|
|
let compressed_file = NamedTempFile::new().unwrap();
|
|
original_file.write(test_text.as_bytes()).expect("Unable to write original file");
|
|
|
|
// run the method being tested
|
|
TestCompression::compress_file(original_file.path(), compressed_file.path()).expect("Unable to compress temp file.");
|
|
|
|
// verify the data in the new file matches what we expect
|
|
let compressed_text = TestCompression::compress_string(test_text);
|
|
let mut reader_file = compressed_file.reopen().unwrap();
|
|
|
|
let mut result_text = Vec::new();
|
|
reader_file.read_to_end(&mut result_text).expect("Unable to read compressed data back to verify");
|
|
let decompressed_text = TestCompression::decompress_to_string(&result_text).unwrap();
|
|
|
|
assert_eq!(decompressed_text, test_text);
|
|
}
|
|
|
|
#[test]
|
|
fn compress_string_to_file() {
|
|
let to_compress = "The quick brown fox jumped over the lazy dog.";
|
|
let compressed_text = TestCompression::compress_string(to_compress);
|
|
let target_file = NamedTempFile::new().unwrap();
|
|
let mut duplicate = target_file.reopen().unwrap();
|
|
TestCompression::compress_string_to_file(to_compress, target_file.path());
|
|
|
|
let mut file_contents = Vec::new();
|
|
duplicate.read_to_end(&mut file_contents).expect("Unable to read compressed file.");
|
|
assert_eq!(file_contents, compressed_text);
|
|
}
|
|
|
|
#[test]
|
|
fn decompress_file_to_string() {
|
|
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
|
let mut temp = NamedTempFile::new().unwrap();
|
|
TestCompression::compress_string_to_file(to_compress, temp.path());
|
|
let result = TestCompression::decompress_file_to_string(temp.path());
|
|
assert_eq!(to_compress, result);
|
|
} |