applies rustfmt to cleanup

uses std::env::current_dir to find out where the tests are running from
This commit is contained in:
Trevor Merritt 2025-08-14 07:48:23 -04:00
parent 6d51a4d3a6
commit e7e20f1d36
9 changed files with 173 additions and 65 deletions

View File

@ -1,38 +1,113 @@
use std::time::Duration;
use crate::duration_to_string::duration_to_string::duration_to_string;
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::duration_to_string::duration_to_string::duration_to_string;
use crate::duration_to_string::duration_to_string::*; use crate::duration_to_string::duration_to_string::*;
use std::time::Duration;
#[test] #[test]
fn multi_duration_to_string() { fn multi_duration_to_string() {
let params: Vec<(&str, Duration, Option<bool>)> = vec![ let params: Vec<(&str, Duration, Option<bool>)> = vec![
("1 second", Duration::from_secs(1), Some(false)), ("1 second", Duration::from_secs(1), Some(false)),
("4 seconds", Duration::from_secs(4), Some(false)), ("4 seconds", Duration::from_secs(4), Some(false)),
("1 minute", Duration::from_secs(SECONDS_IN_MINUTE as u64), Some(false)), (
("4 minutes", Duration::from_secs((SECONDS_IN_MINUTE * 4) as u64), Some(false)), "1 minute",
("1 hour", Duration::from_secs(SECONDS_IN_HOUR as u64), Some(false)), Duration::from_secs(SECONDS_IN_MINUTE as u64),
("4 hours", Duration::from_secs((SECONDS_IN_HOUR * 4) as u64), Some(false)), Some(false),
("4 hours 5 minutes", Duration::from_secs((SECONDS_IN_HOUR * 4 + SECONDS_IN_MINUTE * 5) as u64), Some(false)), ),
("1 day", Duration::from_secs(SECONDS_IN_DAY as u64), Some(false)), (
("0 days 0 hours 0 minutes 1 second", Duration::from_secs(1), Some(true)), "4 minutes",
("0 days 0 hours 0 minutes 4 seconds", Duration::from_secs(4), Some(true)), Duration::from_secs((SECONDS_IN_MINUTE * 4) as u64),
("0 days 0 hours 1 minute 0 seconds", Duration::from_secs(SECONDS_IN_MINUTE as u64), Some(true)), Some(false),
("0 days 0 hours 4 minutes 0 seconds", Duration::from_secs((SECONDS_IN_MINUTE * 4) as u64), Some(true)), ),
("0 days 1 hour 0 minutes 0 seconds", Duration::from_secs(SECONDS_IN_HOUR as u64), Some(true)), (
("0 days 4 hours 0 minutes 0 seconds", Duration::from_secs((SECONDS_IN_HOUR * 4) as u64), Some(true)), "1 hour",
("0 days 4 hours 5 minutes 0 seconds", Duration::from_secs((SECONDS_IN_HOUR * 4 + SECONDS_IN_MINUTE * 5) as u64), Some(true)), Duration::from_secs(SECONDS_IN_HOUR as u64),
("1 day 0 hours 0 minutes 0 seconds", Duration::from_secs(SECONDS_IN_DAY as u64), Some(true)), Some(false),
("1 day 0 hours 0 minutes 1 second", Duration::from_secs((SECONDS_IN_DAY + 1) as u64), Some(true)), ),
(
"4 hours",
Duration::from_secs((SECONDS_IN_HOUR * 4) as u64),
Some(false),
),
(
"4 hours 5 minutes",
Duration::from_secs((SECONDS_IN_HOUR * 4 + SECONDS_IN_MINUTE * 5) as u64),
Some(false),
),
(
"1 day",
Duration::from_secs(SECONDS_IN_DAY as u64),
Some(false),
),
(
"0 days 0 hours 0 minutes 1 second",
Duration::from_secs(1),
Some(true),
),
(
"0 days 0 hours 0 minutes 4 seconds",
Duration::from_secs(4),
Some(true),
),
(
"0 days 0 hours 1 minute 0 seconds",
Duration::from_secs(SECONDS_IN_MINUTE as u64),
Some(true),
),
(
"0 days 0 hours 4 minutes 0 seconds",
Duration::from_secs((SECONDS_IN_MINUTE * 4) as u64),
Some(true),
),
(
"0 days 1 hour 0 minutes 0 seconds",
Duration::from_secs(SECONDS_IN_HOUR as u64),
Some(true),
),
(
"0 days 4 hours 0 minutes 0 seconds",
Duration::from_secs((SECONDS_IN_HOUR * 4) as u64),
Some(true),
),
(
"0 days 4 hours 5 minutes 0 seconds",
Duration::from_secs((SECONDS_IN_HOUR * 4 + SECONDS_IN_MINUTE * 5) as u64),
Some(true),
),
(
"1 day 0 hours 0 minutes 0 seconds",
Duration::from_secs(SECONDS_IN_DAY as u64),
Some(true),
),
(
"1 day 0 hours 0 minutes 1 second",
Duration::from_secs((SECONDS_IN_DAY + 1) as u64),
Some(true),
),
("1 second", Duration::from_secs(1), None), ("1 second", Duration::from_secs(1), None),
("4 seconds", Duration::from_secs(4), None), ("4 seconds", Duration::from_secs(4), None),
("1 minute", Duration::from_secs(SECONDS_IN_MINUTE as u64), None), (
("4 minutes", Duration::from_secs((SECONDS_IN_MINUTE * 4) as u64), None), "1 minute",
Duration::from_secs(SECONDS_IN_MINUTE as u64),
None,
),
(
"4 minutes",
Duration::from_secs((SECONDS_IN_MINUTE * 4) as u64),
None,
),
("1 hour", Duration::from_secs(SECONDS_IN_HOUR as u64), None), ("1 hour", Duration::from_secs(SECONDS_IN_HOUR as u64), None),
("4 hours", Duration::from_secs((SECONDS_IN_HOUR * 4) as u64), None), (
("4 hours 5 minutes", Duration::from_secs((SECONDS_IN_HOUR * 4 + SECONDS_IN_MINUTE * 5) as u64), None), "4 hours",
Duration::from_secs((SECONDS_IN_HOUR * 4) as u64),
None,
),
(
"4 hours 5 minutes",
Duration::from_secs((SECONDS_IN_HOUR * 4 + SECONDS_IN_MINUTE * 5) as u64),
None,
),
("1 day", Duration::from_secs(SECONDS_IN_DAY as u64), None), ("1 day", Duration::from_secs(SECONDS_IN_DAY as u64), None),
]; ];

View File

@ -1,4 +1,4 @@
pub mod number_system_conversion;
pub mod test_compression;
pub mod data_to_text; pub mod data_to_text;
pub mod duration_to_string; pub mod duration_to_string;
pub mod number_system_conversion;
pub mod test_compression;

View File

@ -1,5 +1,4 @@
pub struct NumberSystemConversion {} pub struct NumberSystemConversion {}
impl NumberSystemConversion { impl NumberSystemConversion {
/// byte_to_bool /// byte_to_bool
/// ///
@ -94,7 +93,6 @@ impl NumberSystemConversion {
(high as u16) << 8 | low as u16 (high as u16) << 8 | low as u16
} }
/// clear_high_bits /// clear_high_bits
/// ///
/// Clear the 8 MSB of the value /// Clear the 8 MSB of the value

View File

@ -100,10 +100,13 @@ impl TestCompression {
} }
pub fn decompress_file_to_array(to_decompress: &Path) -> Vec<u8> { pub fn decompress_file_to_array(to_decompress: &Path) -> Vec<u8> {
// println!("DECOMPRESS_FILE_TO_ARRAY--> {:?}", to_decompress);
let file = File::open(to_decompress).expect("Failed to open file"); let file = File::open(to_decompress).expect("Failed to open file");
let mut decoder = DeflateDecoder::new(file); let mut decoder = DeflateDecoder::new(file);
let mut output = Vec::new(); let mut output = Vec::new();
decoder.read_to_end(&mut output).expect("Failed to decompress"); decoder
.read_to_end(&mut output)
.expect("Failed to decompress");
output output
} }
} }

1
tests/mod.rs Normal file
View File

@ -0,0 +1 @@
mod poc;

View File

@ -1,21 +1,37 @@
use trevors_utilities::data_to_text::DataToText; 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!(
"{}/resources/data_to_text/{}.bin",
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
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!(
"{}/resources/data_to_text/{}.display",
std::env::current_dir()
.unwrap()
.to_str()
.unwrap()
.to_string(),
source
);
std::fs::read_to_string(full_path).unwrap() std::fs::read_to_string(full_path).unwrap()
} }
#[test] #[test]
fn data_to_text() { fn data_to_text() {
let data_to_display: &[u8] = &[ let data_to_display: &[u8] = &[
0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74,
0x74, 0x20, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x20, 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";
@ -25,10 +41,7 @@ fn data_to_text() {
#[test] #[test]
fn quickbrownfoxtest() { fn quickbrownfoxtest() {
let test_params = vec![ let test_params = vec!["quickbrownfox", "q"];
"quickbrownfox",
"q"
];
for stub in test_params { for stub in test_params {
// load the content from quickbrownfox.bin // load the content from quickbrownfox.bin
@ -40,9 +53,7 @@ fn quickbrownfoxtest() {
// load the content from quickbrownfox.display // load the content from quickbrownfox.display
let expected = read_display(stub); let expected = read_display(stub);
assert_eq!( assert_eq!(formatted, expected);
formatted, expected
);
} }
} }
@ -51,7 +62,5 @@ fn data_to_text_window_all() {
let bin_data = read_bin("quickbrownfox"); let bin_data = read_bin("quickbrownfox");
let formatted = DataToText::data_to_text_window(&bin_data, 0, bin_data.len()); let formatted = DataToText::data_to_text_window(&bin_data, 0, bin_data.len());
let expected = read_display("quickbrownfox"); let expected = read_display("quickbrownfox");
assert_eq!( assert_eq!(formatted, expected);
formatted, expected
);
} }

3
tests/poc/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod data_to_text;
mod number_system_conversion;
mod test_compression;

View File

@ -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 **** // **** NOTE THAT THIS VISUALLY IS BACKWARDS AS INDEX 0 IS FIRST AND LSB IS LAST ****
let test_params: BTreeMap<u8, [bool; 8]> = BTreeMap::from([ let test_params: BTreeMap<u8, [bool; 8]> = BTreeMap::from([
(0xff, [true, true, true, true, true, true, true, true]), (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]), (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_0101_1010, 0b0000_0000_0101_1010),
(0b1111_1111_1111_1111, 0b0000_0000_1111_1111), (0b1111_1111_1111_1111, 0b0000_0000_1111_1111),
(0b0000_0000_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 { for (src, dst) in test_params {
let result = NumberSystemConversion::clear_high_bits(src); let result = NumberSystemConversion::clear_high_bits(src);
println!("Rolled {src:016b} to {result:016b} / {dst:016b}"); println!("Rolled {src:016b} to {result:016b} / {dst:016b}");
assert_eq!( assert_eq!(NumberSystemConversion::clear_high_bits(src), dst);
NumberSystemConversion::clear_high_bits(src),
dst);
} }
} }
#[test] #[test]
@ -137,29 +135,39 @@ fn clear_low_bits() {
(0b1111_1111_0101_1010, 0b1111_1111_0000_0000), (0b1111_1111_0101_1010, 0b1111_1111_0000_0000),
(0b1111_1111_1111_1111, 0b1111_1111_0000_0000), (0b1111_1111_1111_1111, 0b1111_1111_0000_0000),
(0b0000_0000_1111_1111, 0b0000_0000_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 { for (src, dst) in test_params {
let result = NumberSystemConversion::clear_low_bits(src); let result = NumberSystemConversion::clear_low_bits(src);
println!("Rolled {src:08b} to {result:08b} / {dst:08b}"); println!("Rolled {src:08b} to {result:08b} / {dst:08b}");
assert_eq!( assert_eq!(NumberSystemConversion::clear_low_bits(src), dst);
NumberSystemConversion::clear_low_bits(src),
dst);
} }
} }
#[test] #[test]
fn is_bit_set_checks() { fn is_bit_set_checks() {
let params = vec![ 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]), 0b0000_0001,
(0b1010_1010, vec![false, true, false, true, false, true, false, true]) 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 (base, options) in params {
for (index, expected) in options.iter().enumerate() { 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] #[test]
fn is_bit_clear_checks() { fn is_bit_clear_checks() {
let params = vec![ 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]), 0b0000_0001,
(0b1010_1010, vec![true, false, true, false, true, false, true, false]) 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 (base, options) in params {
for (index, expected) in options.iter().enumerate() { 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)
);
} }
} }
} }

View File

@ -150,10 +150,9 @@ fn decompress_to_array() {
let decompressed_data = TestCompression::decompress_file_to_array(temp_file.path()); let decompressed_data = TestCompression::decompress_file_to_array(temp_file.path());
// Convert to string to compare // Convert to string to compare
let decompressed_text = String::from_utf8(decompressed_data) let decompressed_text =
.expect("Decompressed bytes were not valid UTF-8"); String::from_utf8(decompressed_data).expect("Decompressed bytes were not valid UTF-8");
assert_eq!(decompressed_text, original_text); assert_eq!(decompressed_text, original_text);
} }
#[test] #[test]