Start of data_to_text family

This commit is contained in:
2025-06-05 14:19:45 -04:00
parent 37cc9e4fdb
commit f93131622f
11 changed files with 320 additions and 73 deletions
+27
View File
@@ -0,0 +1,27 @@
use trevors_utilities::data_to_text::DataToText;
#[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 ";
let actual_data = DataToText::data_to_text(data_to_display);
assert_eq!(actual_data, expected_data);
}
#[test]
fn quickbrownfoxtest() {
let test_params = vec![
"quickbrownfox"
];
// load the content from quickbrownfox.bin
// format it for display
// load the content from quickbrownfox.display
// compare the formatted data to the loaded content.
}
+77 -26
View File
@@ -7,38 +7,54 @@ 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]),
(0xa0, [false, false, false, false, false, true, false, 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
"Unable to convert [{}] to [{:?}]",
src,
dst
);
assert_eq!(
src,
NumberSystemConversion::bool_to_byte(dst),
"Unable to convert [{:?}] to [{}]", dst, src
"Unable to convert [{:?}] to [{}]",
dst,
src
);
assert_eq!(
src,
NumberSystemConversion::bool_to_byte(
NumberSystemConversion::byte_to_bool(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)];
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!(
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)));
assert_eq!(
dst,
NumberSystemConversion::swap_endian_u16(NumberSystemConversion::swap_endian_u16(dst))
);
}
}
@@ -47,16 +63,20 @@ fn swap_endinness_u32() {
let test_params: Vec<(u32, u32)> = vec![
(0xabcdef01, 0x01efcdab),
(0x12345678, 0x78563412),
(0xbadbeef0, 0xf0eedbba)
(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!(
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)));
assert_eq!(
dst,
NumberSystemConversion::swap_endian_u32(NumberSystemConversion::swap_endian_u32(dst))
);
}
}
@@ -66,19 +86,15 @@ fn split_bytes_u16_join_bytes_u16() {
(0x0000, (0x00, 0x00)),
(0xabcd, (0xab, 0xcd)),
(0xffff, (0xff, 0xff)),
(0xbeef, (0xbe, 0xef))
(0xbeef, (0xbe, 0xef)),
]);
for (joined, (high, low)) in test_params {
assert_eq!(
(high, low),
NumberSystemConversion::split_bytes_u16(joined));
assert_eq!((high, low), NumberSystemConversion::split_bytes_u16(joined));
assert_eq!(
joined,
NumberSystemConversion::join_bytes_u16_from_u8(high, low));
assert_eq!(
joined,
NumberSystemConversion::join_bytes_u16(high as u16, low as u16));
NumberSystemConversion::join_bytes_u16(high as u16, low as u16)
);
}
}
@@ -88,12 +104,47 @@ fn combine_u8_to_u16() {
((0xff, 0x00), 0x00ff),
((0x00, 0xff), 0xff00),
((0xbe, 0xef), 0xefbe),
((0xef, 0xbe), 0xbeef)
((0xef, 0xbe), 0xbeef),
];
for ((low, high), base) in test_params {
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!(
base, NumberSystemConversion::combine_u8_to_u16(low, high)
)
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);
}
}
+48 -28
View File
@@ -1,4 +1,3 @@
use std::fs::File;
use std::io::{Read, Write};
use tempfile::NamedTempFile;
use trevors_utilities::test_compression::TestCompression;
@@ -12,12 +11,8 @@ fn smoke() {
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
)).unwrap();
TestCompression::decompress_to_string(&TestCompression::compress_string(to_compress));
assert_eq!(to_compress, decompressed_text);
}
@@ -27,21 +22,23 @@ fn file_compression_round_trip() {
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 mut temp_reader = temp_target.reopen();
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");
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).expect("Unable to decompress string for test");
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
);
assert_eq!(string_to_compress, decompresed);
}
#[test]
@@ -51,7 +48,9 @@ fn file_compression_reader() {
// ...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");
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);
@@ -67,11 +66,11 @@ fn file_compression_writer() {
// 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).expect("Unable to decompress text");
assert_eq!(
decompressed_text,
to_compress);
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]
@@ -80,18 +79,22 @@ fn compress_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");
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.");
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 compressed_text = TestCompression::compress_string(test_text);
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).unwrap();
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);
}
@@ -105,15 +108,32 @@ fn compress_string_to_file() {
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.");
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 mut temp = NamedTempFile::new().unwrap();
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() {}