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
+1
View File
@@ -0,0 +1 @@
mod poc;
@@ -1,21 +1,37 @@
use trevors_utilities::data_to_text::DataToText;
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!(
"{}/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!("/home/tmerritt/Projects/trevors_utilities/resources/data_to_text/{}.display", source);
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,
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";
@@ -25,10 +41,7 @@ fn data_to_text() {
#[test]
fn quickbrownfoxtest() {
let test_params = vec![
"quickbrownfox",
"q"
];
let test_params = vec!["quickbrownfox", "q"];
for stub in test_params {
// load the content from quickbrownfox.bin
@@ -40,9 +53,7 @@ fn quickbrownfoxtest() {
// load the content from quickbrownfox.display
let expected = read_display(stub);
assert_eq!(
formatted, expected
);
assert_eq!(formatted, expected);
}
}
@@ -51,7 +62,5 @@ 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
);
assert_eq!(formatted, expected);
}
+3
View File
@@ -0,0 +1,3 @@
mod data_to_text;
mod number_system_conversion;
mod test_compression;
@@ -7,9 +7,10 @@ fn byte_to_bool_and_bool_to_byte() {
// **** 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,
]),
(
0x00,
[false, false, false, false, false, false, false, false],
),
(0xa0, [false, false, false, false, false, true, false, true]),
]);
@@ -118,17 +119,14 @@ fn clear_high_bits() {
(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)
(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);
assert_eq!(NumberSystemConversion::clear_high_bits(src), dst);
}
}
#[test]
@@ -137,29 +135,39 @@ fn clear_low_bits() {
(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)
(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);
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])
(
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));
assert_eq!(
*expected,
NumberSystemConversion::is_bit_set(base, index as u8)
);
}
}
}
@@ -167,14 +175,26 @@ fn is_bit_set_checks() {
#[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])
(
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));
assert_eq!(
*expected,
NumberSystemConversion::is_bit_clear(base, index as u8)
);
}
}
}
@@ -150,10 +150,9 @@ fn decompress_to_array() {
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");
let decompressed_text =
String::from_utf8(decompressed_data).expect("Decompressed bytes were not valid UTF-8");
assert_eq!(decompressed_text, original_text);
}
#[test]