working on moving the 'TestCompression' code into its own crate
This commit is contained in:
@@ -15,4 +15,6 @@ flate2.workspace = true
|
||||
clap.workspace = true
|
||||
hex.workspace = true
|
||||
log.workspace = true
|
||||
# Testing
|
||||
tempfile.workspace = true
|
||||
lipsum.workspace = true
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
/// - Uncompressed files must only contain printable ASCII characters
|
||||
/// - Stuff I don't know about yet
|
||||
|
||||
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use clap::{Parser, Subcommand};
|
||||
@@ -33,6 +32,7 @@ use std::io::prelude::*;
|
||||
use std::io;
|
||||
use std::ptr::write;
|
||||
use log::{debug, info};
|
||||
use gemma::test_compression::TestCompression;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about = "Compress or decompress a string", long_about = None)]
|
||||
@@ -56,52 +56,6 @@ enum Commands {
|
||||
}
|
||||
}
|
||||
|
||||
fn compress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let compressed_bytes= compress_file_to_array(input_path);
|
||||
let output_filename = input_path.with_extension("deflated");
|
||||
let mut output_file = File::create(&output_filename)?;
|
||||
output_file.write_all(&compressed_bytes.unwrap())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compress_file_to_array(input_path: &Path) -> Result<Vec<u8>, String> {
|
||||
let mut file = File::open(input_path)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to open input path [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut read_buffer = Vec::new();
|
||||
let mut write_buffer = Vec::new();
|
||||
file.unwrap().read_to_end(&mut read_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to read input file [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut encoder = DeflateEncoder::new(read_buffer, Compression::default());
|
||||
encoder.write_all(&write_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to compress data for [{}]: {}", input_path.display(), e)));
|
||||
|
||||
debug!("Compressed file from path: {}", input_path.display());
|
||||
Ok(write_buffer)
|
||||
}
|
||||
|
||||
fn decompress_file_to_array(input_path: &Path) -> Vec<u8> {
|
||||
let file = File::open(input_path)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to open input path [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut decoder = DeflateDecoder::new(file.unwrap());
|
||||
let mut return_vec = Vec::new();
|
||||
decoder.read_to_end(&mut return_vec)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to decompress data for [{}]: {}", input_path.display(), e)));
|
||||
|
||||
debug!("Decompressed file from path: {}", input_path.display());
|
||||
return_vec
|
||||
}
|
||||
|
||||
fn decompress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let decompressed_bytes = decompress_file_to_array(input_path);
|
||||
let output_path = input_path.with_extension("inflated");
|
||||
let mut output_file = File::create(&output_path)?;
|
||||
output_file.write_all(&decompressed_bytes).expect(format!("Unable to write compressed data to [{}]", output_path.display()).as_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
pretty_env_logger::init();
|
||||
|
||||
@@ -109,10 +63,10 @@ fn main() {
|
||||
|
||||
match cli.command {
|
||||
Commands::Compress { input } => {
|
||||
compress_file(&input).expect(format!("Unable to compress {}", input.display()).as_str());
|
||||
TestCompression::compress_file(&input).expect(format!("Unable to compress {}", input.display()).as_str());
|
||||
}
|
||||
Commands::Decompress { input } => {
|
||||
decompress_file(&input).expect(format!("Unable to decompress {}", input.display()).as_str());
|
||||
TestCompression::decompress_file(&input).expect(format!("Unable to decompress {}", input.display()).as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -15,4 +15,5 @@ pub mod chip8 {
|
||||
pub mod quirk_modes;
|
||||
}
|
||||
|
||||
pub mod constants;
|
||||
pub mod constants;
|
||||
pub mod test_compression;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use flate2::Compression;
|
||||
use flate2::read::DeflateDecoder;
|
||||
use flate2::write::DeflateEncoder;
|
||||
use log::debug;
|
||||
|
||||
pub struct TestCompression {}
|
||||
|
||||
impl TestCompression {
|
||||
|
||||
pub fn compress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let compressed_bytes= TestCompression::compress_file_to_array(input_path);
|
||||
let output_filename = input_path.with_extension("deflated");
|
||||
let mut output_file = File::create(&output_filename)?;
|
||||
output_file.write_all(&compressed_bytes.unwrap())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compress_file_to_array(input_path: &Path) -> Result<Vec<u8>, String> {
|
||||
let mut file = File::open(input_path)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to open input path [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut read_buffer = Vec::new();
|
||||
let mut write_buffer = Vec::new();
|
||||
file.unwrap().read_to_end(&mut read_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to read input file [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut encoder = DeflateEncoder::new(read_buffer, Compression::default());
|
||||
encoder.write_all(&write_buffer)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to compress data for [{}]: {}", input_path.display(), e)));
|
||||
|
||||
debug!("Compressed file from path: {}", input_path.display());
|
||||
Ok(write_buffer)
|
||||
}
|
||||
|
||||
pub fn decompress_file_to_array(input_path: &Path) -> Vec<u8> {
|
||||
let file = File::open(input_path)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to open input path [{}]: {}", input_path.display(), e)));
|
||||
|
||||
let mut decoder = DeflateDecoder::new(file.unwrap());
|
||||
let mut return_vec = Vec::new();
|
||||
decoder.read_to_end(&mut return_vec)
|
||||
.map_err(|e| std::io::Error::new(e.kind(), format!("Unable to decompress data for [{}]: {}", input_path.display(), e)));
|
||||
|
||||
debug!("Decompressed file from path: {}", input_path.display());
|
||||
return_vec
|
||||
}
|
||||
|
||||
pub fn decompress_file(input_path: &PathBuf) -> io::Result<()> {
|
||||
let decompressed_bytes = TestCompression::decompress_file_to_array(input_path);
|
||||
let output_path = input_path.with_extension("inflated");
|
||||
let mut output_file = File::create(&output_path)?;
|
||||
output_file.write_all(&decompressed_bytes).expect(format!("Unable to write compressed data to [{}]", output_path.display()).as_str());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compress_string(input: &str) -> Vec<u8> {
|
||||
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
|
||||
encoder
|
||||
.write_all(input.as_bytes())
|
||||
.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();
|
||||
decoder.read_to_string(&mut decompressed)?;
|
||||
Ok(decompressed)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -92,7 +92,7 @@ fn level3_compressed_test() {
|
||||
|
||||
assert_eq!(
|
||||
x.dump_video_to_string(),
|
||||
load_compressed_result("gemma_integration.corax_plus")
|
||||
load_compressed_result("gemma_integration_corax_plus")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -132,7 +132,6 @@ fn level4_test() {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn partial_eq_chip8computer() {
|
||||
let x = Chip8Computer::new();
|
||||
@@ -140,6 +139,19 @@ fn partial_eq_chip8computer() {
|
||||
assert_eq!(x, y)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_to_json() {
|
||||
let x = Chip8Computer::new();
|
||||
|
||||
let expected = load_compressed_result("gemma_integration_state_to_json1");
|
||||
let actual = x.dump_state_to_json();
|
||||
println!("ACTUAL:: [{}]", actual);
|
||||
|
||||
assert_eq!(
|
||||
actual,
|
||||
expected
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tick_when_not_ready() {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
mod test_utils;
|
||||
use gemma::chip8::keypad::Keypad;
|
||||
use crate::test_utils::read_compressed_test_result;
|
||||
use gemma::chip8::keypad::Keypad;
|
||||
|
||||
#[test]
|
||||
fn keypad_keys_check() {
|
||||
@@ -20,17 +20,12 @@ fn keypad_keys_check() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn keypad_string_format_test() {
|
||||
let k = Keypad::new();
|
||||
|
||||
let expected_result = read_compressed_test_result("gemma_keypad_string_result");
|
||||
let actual_result = k.format_as_string();
|
||||
|
||||
let actual_result = Keypad::new().format_as_string();
|
||||
|
||||
println!("EXPECTING [{}]", expected_result);
|
||||
println!("GOT [{}]", actual_result);
|
||||
assert_eq!(
|
||||
k.format_as_string(),
|
||||
read_compressed_test_result("gemma_keypad_string_result")
|
||||
);
|
||||
assert_eq!(actual_result, expected_result);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use crate::test_utils::{compress_string, decompress_to_string};
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use tempfile::{NamedTempFile, tempfile};
|
||||
use gemma::test_compression::TestCompression;
|
||||
|
||||
mod test_utils;
|
||||
|
||||
@@ -10,9 +13,42 @@ fn smoke() {
|
||||
#[test]
|
||||
fn round_trip() {
|
||||
let text_to_process = "The quick brown fox jumps over the lazy dog.";
|
||||
let compressed = compress_string(text_to_process);
|
||||
let decompressed = decompress_to_string(&compressed).unwrap();
|
||||
let compressed = TestCompression::compress_string(text_to_process);
|
||||
let decompressed = TestCompression::decompress_to_string(&compressed).unwrap();
|
||||
|
||||
assert_eq!(text_to_process, decompressed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_file_to_array() {
|
||||
// create a file with plain text...
|
||||
|
||||
// ...compress it to in-memory data
|
||||
|
||||
// ...confirm that matches predefined data
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compress_file() {
|
||||
// create a file with plain text...
|
||||
|
||||
// ...compress it to a temp file...
|
||||
|
||||
// ...read the compressed data...
|
||||
|
||||
// ...verify it matches initial plain text
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decompress_file_to_array() {
|
||||
// crate a file with plain text...
|
||||
|
||||
// ...write the compressed version
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn decompress_file() {
|
||||
|
||||
}
|
||||
@@ -4,28 +4,16 @@ use flate2::Compression;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use tempfile::tempfile;
|
||||
const TEST_OUTPUT_SAMPLE_DIR: &str = "../resources/test/";
|
||||
use gemma::test_compression::TestCompression;
|
||||
|
||||
const TEST_OUTPUT_SAMPLE_DIR: &str = "../resources/test/";
|
||||
const TEST_ROMS_SAMPLE_DIR: &str = "../resources/roms/";
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
assert!(true)
|
||||
}
|
||||
|
||||
pub fn compress_string(input: &str) -> Vec<u8> {
|
||||
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
|
||||
encoder
|
||||
.write_all(input.as_bytes())
|
||||
.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();
|
||||
decoder.read_to_string(&mut decompressed)?;
|
||||
Ok(decompressed)
|
||||
}
|
||||
|
||||
/// Read a compressed test result and return the expected result
|
||||
/// as a string
|
||||
pub fn read_compressed_test_result(suffix: &str) -> String {
|
||||
@@ -42,7 +30,7 @@ pub fn read_compressed_test_result(suffix: &str) -> String {
|
||||
.read_to_end(&mut compressed_data)
|
||||
.expect("Unable to compress data");
|
||||
|
||||
decompress_to_string(&compressed_data).unwrap()
|
||||
TestCompression::decompress_to_string(&compressed_data).unwrap()
|
||||
}
|
||||
|
||||
pub fn load_compressed_result(suffix: &str) -> String {
|
||||
@@ -50,39 +38,5 @@ pub fn load_compressed_result(suffix: &str) -> String {
|
||||
}
|
||||
|
||||
pub fn load_rom(to_load: &str) -> Vec<u8> {
|
||||
std::fs::read(format!("../resources/roms/{}.ch8", to_load)).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn smoke() {
|
||||
assert!(true)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compression_round_trip() {
|
||||
let to_compress = "The quick brown fox jumps over the lazy dog.";
|
||||
|
||||
let compressed_text = compress_string(to_compress);
|
||||
let decompressed_text = decompress_to_string(&compressed_text).unwrap();
|
||||
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 = compress_string(string_to_compress);
|
||||
let temp_target = tempfile().unwrap();
|
||||
|
||||
//
|
||||
// ...decompress from file...
|
||||
//
|
||||
// ...verify its the same.
|
||||
}
|
||||
|
||||
|
||||
std::fs::read(format!("{}{}.ch8", TEST_ROMS_SAMPLE_DIR, to_load)).unwrap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user