April bump

This commit is contained in:
2026-04-04 13:14:43 -04:00
parent e7e20f1d36
commit 9852ef687d
13 changed files with 318 additions and 539 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ fn data_to_text() {
0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x20, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x20,
];
let expected_data: &str = "0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x20 fghijklmnopqrst \n0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x20 vwxyz{|}~. \n";
let expected_data: &str = "0x66 0x67 0x68 0x69 0x6a 0x6b 0x6c 0x6d 0x6e 0x6f 0x70 0x71 0x72 0x73 0x74 0x20 fghijklmnopqrst \n0x76 0x77 0x78 0x79 0x7a 0x7b 0x7c 0x7d 0x7e 0x7f 0x20 vwxyz{|}~..\n";
let actual_data = DataToText::data_to_text(data_to_display);
assert_eq!(actual_data, expected_data);
+16
View File
@@ -0,0 +1,16 @@
use trevors_utilities::format_with_commas::format_with_commas;
#[test]
fn format_with_commas_none_needed() {
assert_eq!("0", format_with_commas(0));
}
#[test]
fn format_thousands() {
assert_eq!("1,000", format_with_commas(1000));
}
#[test]
fn format_millions() {
assert_eq!("1,000,000", format_with_commas(1000000));
}
+1
View File
@@ -1,3 +1,4 @@
mod data_to_text;
mod number_system_conversion;
mod test_compression;
mod format_with_commas;
+177 -158
View File
@@ -2,162 +2,181 @@ 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));
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 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);
//
// ...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);
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 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);
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 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);
}
#[test]
fn compress_file_to_array() {
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");
temp_file.write_all(to_compress.as_bytes()).unwrap();
let result = TestCompression::compress_file_to_array(temp_file.path());
let decompressed_result = TestCompression::decompress_to_string(&result);
assert_eq!(to_compress, decompressed_result);
}
#[test]
fn decompress_to_array() {
let original_text = "The quick brown fox jumps over the lazy dog.";
let compressed_data = TestCompression::compress_string(original_text);
// Write the compressed data to a temp file
let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
temp_file
.write_all(&compressed_data)
.expect("Failed to write compressed data to file");
// Run the method under test
let decompressed_data = TestCompression::decompress_file_to_array(temp_file.path());
// Convert to string to compare
let decompressed_text =
String::from_utf8(decompressed_data).expect("Decompressed bytes were not valid UTF-8");
assert_eq!(decompressed_text, original_text);
}
#[test]
#[should_panic(expected = "No such file or directory")]
fn decompress_file_panics_on_missing_path() {
let bad_path = std::path::Path::new("/definitely/invalid/path");
TestCompression::decompress_file_to_array(bad_path);
#[cfg(test)]
mod tests {
const SAMPLE_TEXT: &str = "The quick brown fox jumps over the lazy dog.";
use super::*;
#[test]
fn smoke() {
assert!(true)
}
#[test]
fn compression_round_trip() {
let to_compress = SAMPLE_TEXT;
let decompressed_text =
TestCompression::decompress_to_string(&TestCompression::compress_string(to_compress));
assert_eq!(to_compress, decompressed_text);
}
#[test]
fn file_compression_round_trip() {
// compress string to file...
let string_to_compress = SAMPLE_TEXT;
let compressed_string = TestCompression::compress_string(string_to_compress);
let mut temp_target = NamedTempFile::new().unwrap();
let 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);
//
// ...verify its the same.
assert_eq!(string_to_compress, decompresed);
}
#[test]
fn file_compression_reader() {
// Get the 'sample text'
let to_compress = SAMPLE_TEXT;
// ...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 = SAMPLE_TEXT;
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);
assert_eq!(decompressed_text, to_compress);
}
#[test]
fn compress_file() {
// create our test file.
let test_text = SAMPLE_TEXT;
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 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);
assert_eq!(decompressed_text, test_text);
}
#[test]
fn compress_string_to_file() {
let to_compress = SAMPLE_TEXT;
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 = SAMPLE_TEXT;
let 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);
}
#[test]
fn compress_file_to_array() {
let to_compress = SAMPLE_TEXT;
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test");
temp_file.write_all(to_compress.as_bytes()).unwrap();
let result = TestCompression::compress_file_to_array(temp_file.path());
let decompressed_result = TestCompression::decompress_to_string(&result);
assert_eq!(to_compress, decompressed_result);
}
#[test]
fn compress_file_to_array_with_trailing_newline() {
let to_compress = format!("{}\n", SAMPLE_TEXT);
let mut temp_file = NamedTempFile::new().expect("Unable to get temp file for test");
temp_file.write_all(to_compress.as_bytes()).unwrap();
let result = TestCompression::compress_file_to_array(temp_file.path());
let decompressed_result = TestCompression::decompress_to_string(&result);
assert_eq!(to_compress, decompressed_result);
}
#[test]
fn decompress_to_array() {
let original_text = SAMPLE_TEXT;
let compressed_data = TestCompression::compress_string(original_text);
// Write the compressed data to a temp file
let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
temp_file
.write_all(&compressed_data)
.expect("Failed to write compressed data to file");
// Run the method under test
let decompressed_data = TestCompression::decompress_file_to_array(temp_file.path());
// Convert to string to compare
let decompressed_text =
String::from_utf8(decompressed_data).expect("Decompressed bytes were not valid UTF-8");
assert_eq!(decompressed_text, original_text);
}
#[test]
#[should_panic(expected = "No such file or directory")]
fn decompress_file_panics_on_missing_path() {
let bad_path = std::path::Path::new("/definitely/invalid/path");
TestCompression::decompress_file_to_array(bad_path);
}
}