More number conversion code
100% coverage
This commit is contained in:
parent
60232b958f
commit
12d738389b
@ -1,3 +1,2 @@
|
||||
pub mod test_loader;
|
||||
pub mod test_compression;
|
||||
pub mod number_system_conversion;
|
||||
|
||||
@ -81,17 +81,20 @@ impl NumberSystemConversion {
|
||||
low | high << 8
|
||||
}
|
||||
|
||||
/// combine_u8_to_u16
|
||||
///
|
||||
/// Combines a pair of u8 values into a single u16 with high
|
||||
/// and low bytes specified
|
||||
///
|
||||
/// ex: (0xff, 0x00) -> 0x00ff
|
||||
/// (0x00, 0xff) -> 0xff00
|
||||
/// (0xbe, 0xef) -> 0xefbe
|
||||
/// (0xef, 0xbe) -> 0xbeef
|
||||
pub fn combine_u8_to_u16(low: u8, high: u8) -> u16 {
|
||||
(high as u16) << 8 | low as u16
|
||||
}
|
||||
|
||||
pub fn join_bytes_u16_from_u8(high: u8, low: u8) -> u16 {
|
||||
low as u16 | ((high as u16) << 8)
|
||||
}
|
||||
|
||||
pub fn split_bytes_u32(from: u32) -> (u16, u16) {
|
||||
let mut bytes = vec![];
|
||||
bytes[0] = (from & 0xffff) as u16;
|
||||
bytes[1] = ((from & 0xffff0000) >> 16) as u16;
|
||||
(bytes[0], bytes[1])
|
||||
}
|
||||
pub fn split_bytes_u32_4(from: u32) -> (u8, u8, u8, u8) {
|
||||
(0, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use std::fs::File;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
use flate2::Compression;
|
||||
@ -16,6 +16,7 @@ impl TestCompression {
|
||||
.expect("Failed to write data");
|
||||
encoder.finish().expect("Failed to finish compression")
|
||||
}
|
||||
|
||||
pub fn decompress_to_string(compressed_data: &[u8]) -> Result<String, std::io::Error> {
|
||||
let mut decoder = DeflateDecoder::new(compressed_data);
|
||||
let mut decompressed = String::new();
|
||||
@ -34,14 +35,23 @@ impl TestCompression {
|
||||
|
||||
/// Save a str to a specified file as compressed data
|
||||
pub fn save_string_as_compressed_data(source: &str, target_file: &Path) {
|
||||
let mut compressed_file = File::open(&target_file).expect(format!("Unable to open target file [{}]", target_file.display()).as_str());
|
||||
let mut compressed_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.truncate(true) // optional: clear existing contents
|
||||
.create(true)
|
||||
.open(target_file)
|
||||
.expect(&format!("Unable to open target file [{}] for writing", target_file.display()));
|
||||
|
||||
let compressed_data = TestCompression::compress_string(source);
|
||||
compressed_file.write_all(&compressed_data).expect(format!("Unable to write compressed data to [{}]", target_file.display()).as_str());
|
||||
compressed_file
|
||||
.write_all(&compressed_data)
|
||||
.expect(&format!("Unable to write compressed data to [{}]", target_file.display()));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tempfile::NamedTempFile;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@ -63,10 +73,51 @@ mod tests {
|
||||
// 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 temp_target = tempfile().unwrap();
|
||||
let mut temp_target = NamedTempFile::new().unwrap();
|
||||
let mut 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).expect("Unable to decompress string for test");
|
||||
//
|
||||
// ...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).expect("Unable to decompress text");
|
||||
assert_eq!(
|
||||
decompressed_text,
|
||||
to_compress);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,15 +0,0 @@
|
||||
use std::path::Path;
|
||||
|
||||
pub fn load_test_result(base_path: &Path, suffix: String) -> String {
|
||||
let full_path = format!("{}/{}", base_path.display(), suffix);
|
||||
std::fs::read_to_string(full_path).unwrap()
|
||||
}
|
||||
|
||||
pub fn load_compressed_test_result(base_path: &Path, suffix: String) -> String {
|
||||
// first load the data...
|
||||
|
||||
// ...then decompress it...
|
||||
// let raw_result = decompress_to_string(compressed_result);
|
||||
// ...finally return it
|
||||
String::new()
|
||||
}
|
||||
@ -81,3 +81,19 @@ fn split_bytes_u16_join_bytes_u16() {
|
||||
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)
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user