applies rustfmt to cleanup

uses std::env::current_dir to find out where the tests are running from
This commit is contained in:
2025-08-14 07:48:23 -04:00
parent 6d51a4d3a6
commit e7e20f1d36
9 changed files with 173 additions and 65 deletions
+66
View File
@@ -0,0 +1,66 @@
use trevors_utilities::data_to_text::DataToText;
fn read_bin(source: &str) -> Vec<u8> {
let full_path = format!(
"{}/resources/data_to_text/{}.bin",
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
source
);
// println!("FULL PATH BIN: [{}]", full_path);
std::fs::read(full_path).unwrap()
}
fn read_display(source: &str) -> String {
let full_path = format!(
"{}/resources/data_to_text/{}.display",
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
source
);
std::fs::read_to_string(full_path).unwrap()
}
#[test]
fn data_to_text() {
let data_to_display: &[u8] = &[
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 actual_data = DataToText::data_to_text(data_to_display);
assert_eq!(actual_data, expected_data);
}
#[test]
fn quickbrownfoxtest() {
let test_params = vec!["quickbrownfox", "q"];
for stub in test_params {
// load the content from quickbrownfox.bin
let bin_data = read_bin(stub);
// format it for display
let formatted = DataToText::data_to_text(&bin_data);
// load the content from quickbrownfox.display
let expected = read_display(stub);
assert_eq!(formatted, expected);
}
}
#[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);
}
+3
View File
@@ -0,0 +1,3 @@
mod data_to_text;
mod number_system_conversion;
mod test_compression;
+200
View File
@@ -0,0 +1,200 @@
use std::collections::BTreeMap;
use trevors_utilities::number_system_conversion::NumberSystemConversion;
#[test]
fn byte_to_bool_and_bool_to_byte() {
// confirm a byte becomes a valid bool
// **** NOTE THAT THIS VISUALLY IS BACKWARDS AS INDEX 0 IS FIRST AND LSB IS LAST ****
let test_params: BTreeMap<u8, [bool; 8]> = BTreeMap::from([
(0xff, [true, true, true, true, true, true, true, true]),
(
0x00,
[false, false, false, false, false, false, false, false],
),
(0xa0, [false, false, false, false, false, true, false, true]),
]);
for (src, dst) in test_params {
assert_eq!(
dst,
NumberSystemConversion::byte_to_bool(src),
"Unable to convert [{}] to [{:?}]",
src,
dst
);
assert_eq!(
src,
NumberSystemConversion::bool_to_byte(dst),
"Unable to convert [{:?}] to [{}]",
dst,
src
);
assert_eq!(
src,
NumberSystemConversion::bool_to_byte(NumberSystemConversion::byte_to_bool(src))
);
}
}
#[test]
fn swap_endianness_u16() {
let test_params: Vec<(u16, u16)> = vec![
(0xabcd, 0xcdab),
(0x0000, 0x0000),
(0xffff, 0xffff),
(0x00ff, 0xff00),
];
for (src, dst) in test_params {
assert_eq!(src, NumberSystemConversion::swap_endian_u16(dst));
assert_eq!(
src,
NumberSystemConversion::swap_endian_u16(NumberSystemConversion::swap_endian_u16(src))
);
assert_eq!(dst, NumberSystemConversion::swap_endian_u16(src));
assert_eq!(
dst,
NumberSystemConversion::swap_endian_u16(NumberSystemConversion::swap_endian_u16(dst))
);
}
}
#[test]
fn swap_endinness_u32() {
let test_params: Vec<(u32, u32)> = vec![
(0xabcdef01, 0x01efcdab),
(0x12345678, 0x78563412),
(0xbadbeef0, 0xf0eedbba),
];
for (src, dst) in test_params {
assert_eq!(src, NumberSystemConversion::swap_endian_u32(dst));
assert_eq!(
src,
NumberSystemConversion::swap_endian_u32(NumberSystemConversion::swap_endian_u32(src))
);
assert_eq!(dst, NumberSystemConversion::swap_endian_u32(src));
assert_eq!(
dst,
NumberSystemConversion::swap_endian_u32(NumberSystemConversion::swap_endian_u32(dst))
);
}
}
#[test]
fn split_bytes_u16_join_bytes_u16() {
let test_params = BTreeMap::from([
(0x0000, (0x00, 0x00)),
(0xabcd, (0xab, 0xcd)),
(0xffff, (0xff, 0xff)),
(0xbeef, (0xbe, 0xef)),
]);
for (joined, (high, low)) in test_params {
assert_eq!((high, low), NumberSystemConversion::split_bytes_u16(joined));
assert_eq!(
joined,
NumberSystemConversion::join_bytes_u16(high as u16, low as u16)
);
}
}
#[test]
fn combine_u8_to_u16() {
let test_params: Vec<((u8, u8), u16)> = vec![
((0xff, 0x00), 0x00ff),
((0x00, 0xff), 0xff00),
((0xbe, 0xef), 0xefbe),
((0xef, 0xbe), 0xbeef),
];
for ((low, high), base) in test_params {
assert_eq!(base, NumberSystemConversion::combine_u8_to_u16(low, high))
}
}
#[test]
fn clear_high_bits() {
let test_params = vec![
(0b1111_1111_0101_1010, 0b0000_0000_0101_1010),
(0b1111_1111_1111_1111, 0b0000_0000_1111_1111),
(0b0000_0000_1111_1111, 0b0000_0000_1111_1111),
(0b0101_1010_1001_0110, 0b0000_0000_1001_0110),
];
for (src, dst) in test_params {
let result = NumberSystemConversion::clear_high_bits(src);
println!("Rolled {src:016b} to {result:016b} / {dst:016b}");
assert_eq!(NumberSystemConversion::clear_high_bits(src), dst);
}
}
#[test]
fn clear_low_bits() {
let test_params = vec![
(0b1111_1111_0101_1010, 0b1111_1111_0000_0000),
(0b1111_1111_1111_1111, 0b1111_1111_0000_0000),
(0b0000_0000_1111_1111, 0b0000_0000_0000_0000),
(0b0101_1010_1001_0110, 0b0101_1010_0000_0000),
];
for (src, dst) in test_params {
let result = NumberSystemConversion::clear_low_bits(src);
println!("Rolled {src:08b} to {result:08b} / {dst:08b}");
assert_eq!(NumberSystemConversion::clear_low_bits(src), dst);
}
}
#[test]
fn is_bit_set_checks() {
let params = vec![
(
0b0000_0001,
vec![true, false, false, false, false, false, false, false],
),
(
0b1111_1111,
vec![true, true, true, true, true, true, true, true],
),
(
0b1010_1010,
vec![false, true, false, true, false, true, false, true],
),
];
for (base, options) in params {
for (index, expected) in options.iter().enumerate() {
assert_eq!(
*expected,
NumberSystemConversion::is_bit_set(base, index as u8)
);
}
}
}
#[test]
fn is_bit_clear_checks() {
let params = vec![
(
0b0000_0001,
vec![false, true, true, true, true, true, true, true],
),
(
0b1111_1111,
vec![false, false, false, false, false, false, false, false],
),
(
0b1010_1010,
vec![true, false, true, false, true, false, true, false],
),
];
for (base, options) in params {
for (index, expected) in options.iter().enumerate() {
assert_eq!(
*expected,
NumberSystemConversion::is_bit_clear(base, index as u8)
);
}
}
}
+163
View File
@@ -0,0 +1,163 @@
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);
}