update tests

This commit is contained in:
Trevor Merritt 2025-06-12 14:33:43 -04:00
parent 703ae38814
commit 0b11b91fe4
6 changed files with 52 additions and 28 deletions

View File

@ -1,3 +1,2 @@
[alias] [alias]
coverage = "tarpaulin --out Html --skip-clean --output-dir coverage" coverage = "tarpaulin --out Html --skip-clean --output-dir coverage"

View File

@ -5,7 +5,14 @@ impl DataToText {
/// ///
/// Convert a block of u8 data to text for user display skipping specified bytes /// Convert a block of u8 data to text for user display skipping specified bytes
pub fn data_to_text_window(to_convert: &[u8], offset: usize, length: usize) -> String { pub fn data_to_text_window(to_convert: &[u8], offset: usize, length: usize) -> String {
DataToText::data_to_text(&to_convert[offset..(offset + length)]) let convert_len = to_convert.len();
if offset >= convert_len {
return String::from("");
}
let end_of_window = offset.saturating_add(length).min(convert_len);
DataToText::data_to_text(&to_convert[offset..end_of_window])
} }
/// data_to_text /// data_to_text

View File

@ -100,27 +100,10 @@ impl TestCompression {
} }
pub fn decompress_file_to_array(to_decompress: &Path) -> Vec<u8> { pub fn decompress_file_to_array(to_decompress: &Path) -> Vec<u8> {
let mut read_buffer = Vec::new(); let file = File::open(to_decompress).expect("Failed to open file");
let mut file = File::open(to_decompress).expect( let mut decoder = DeflateDecoder::new(file);
format!( let mut output = Vec::new();
"Unable to open file to decompress [{}]", decoder.read_to_end(&mut output).expect("Failed to decompress");
to_decompress.display() output
)
.as_str(),
);
file.read_to_end(&mut read_buffer)
.expect(format!("Unable to read from [{}]", to_decompress.display()).as_str());
read_buffer
} }
// let file = File::open(input_path)
// .map_err(|e| std::io::Error::new(e.kind(), format!("Unable to open input path [{}]: {}", input_path.display(), e)));
//
// let mut decoder = DeflateDecoder::new(file.unwrap());
// let mut return_vec = Vec::new();
// decoder.read_to_end(&mut return_vec)
// .map_err(|e| std::io::Error::new(e.kind(), format!("Unable to decompress data for [{}]: {}", input_path.display(), e)));
//
// debug!("Decompressed file from path: {}", input_path.display());
// return_vec
} }

View File

@ -2,13 +2,13 @@ use trevors_utilities::data_to_text::DataToText;
fn read_bin(source: &str) -> Vec<u8> { fn read_bin(source: &str) -> Vec<u8> {
let full_path = format!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.bin", source); let full_path = format!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.bin", source);
println!("FULL PATH BIN: [{}]", full_path); // println!("FULL PATH BIN: [{}]", full_path);
std::fs::read(full_path).unwrap() std::fs::read(full_path).unwrap()
} }
fn read_display(source: &str) -> String { fn read_display(source: &str) -> String {
let full_path = format!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.display", source); let full_path = format!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.display", source);
println!("FULL PATH DIS: [{}]", full_path); // println!("FULL PATH DIS: [{}]", full_path);
std::fs::read_to_string(full_path).unwrap() std::fs::read_to_string(full_path).unwrap()
} }
@ -46,3 +46,13 @@ fn quickbrownfoxtest() {
); );
} }
} }
#[test]
fn data_to_text_window_all() {
let bin_data = read_bin("quickbrownfox");
let formatted = DataToText::data_to_text_window(&bin_data, 0, bin_data.len());
let expected = read_display("quickbrownfox");
assert_eq!(
formatted, expected
);
}

View File

@ -136,4 +136,29 @@ fn compress_file_to_array() {
} }
#[test] #[test]
fn decompress_to_array() {} 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);
}