adds smoke tests to everything

adds deflated test examples
This commit is contained in:
2025-05-28 11:32:09 -04:00
parent e45d269828
commit 6c902de16f
37 changed files with 185 additions and 82 deletions
+53 -60
View File
@@ -21,16 +21,18 @@
use std::fs;
use std::fs::File;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::io::prelude::*;
use std::io::Read;
use std::io::Write;
use flate2::Compression;
use flate2::write::{GzEncoder, ZlibEncoder};
use flate2::write::{DeflateEncoder, GzEncoder, ZlibEncoder};
use gemma::constants::{TESTS_ROOT, TEST_ROM_ROOT};
use flate2::read::{DeflateDecoder, DeflateEncoder, GzDecoder};
use flate2::read::{DeflateDecoder};
use std::io::prelude::*;
use std::io;
use std::ptr::write;
use log::{debug, info};
#[derive(Parser)]
#[command(author, version, about = "Compress or decompress a string", long_about = None)]
@@ -54,72 +56,63 @@ enum Commands {
}
}
fn compress_file(input_path: &PathBuf) {
let file = File::open(&input_path).unwrap();
let mut compressed_bytes= Vec::new();
let mut deflater = DeflateEncoder::new(file, Compression::fast());
deflater.read_to_end(&mut compressed_bytes).expect("Unable to write compressed data to buffer ");
let output_filename = format!("{}.deflated", input_path.display());
let output_file = File::create(&output_filename);
output_file.unwrap().write_all(&compressed_bytes).expect("Unable to write compressed version");
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 decompress_file(input_path: &PathBuf) {
let file = File::open(&input_path).unwrap();
let mut uncompressed_bytes = Vec::new();
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 inflater = DeflateDecoder::new(file);
inflater.read_to_end(&mut uncompressed_bytes).expect("Unable to inflate.");
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 target_file_name = format!("{}.inflated", input_path.display());
println!("Writing decompressed data to {}", target_file_name);
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)));
let mut target_file = File::create(&target_file_name)
.expect("Unable to create uncompressed output file");
target_file
.write_all(&uncompressed_bytes)
.expect("Unable to write decompressed file");
debug!("Compressed file from path: {}", input_path.display());
Ok(write_buffer)
}
// let decompressed_data = decompress_data(input_data);
// let mut target_file = File::create(&target_file_name).expect(format!("Unable to create uncompressed file -> {}", target_file_name.clone()).as_str());
//target_file.write_all(&decompressed_data).expect(format!("Unable to write uncompressed file -> {}", target_file_name).as_str());
//println!("Decompressed: {:?}", String::from_utf8_lossy(&decompressed_data));
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() {
env_logger::init();
let cli = Cli::parse();
let raw_file = format!("{}/gemma/{}test_scroll_down_10_hd.asc", std::env::current_dir().unwrap().display(), TESTS_ROOT);
let compressed_file = format!("{}.deflated", &raw_file);
let uncompressed_file = format!("{}.inflated", &compressed_file);
println!("[ + ] Writing compressed file from {}", &raw_file);
compress_file(&raw_file.clone().into());
decompress_file(&compressed_file.into());
///////// COMPRESSION COMPLETE. TIME TO DECOMPRESS AND COMPARE
//
//
//
// match cli.command {
// Commands::Compress { input } => {
// let compressed = compress_string(&input);
// // Convert to hex format for easier readability in the terminal
// println!("Compressed (hex): {:?} / from {}b to {}b", hex::encode(compressed.clone()), input.len(), compressed.len());
// }
// Commands::Decompress { input } => {
// // Decode hex string back to bytes
// let compressed_bytes = hex::decode(input).expect("Invalid hex input");
// let decompressed = decompress_string(&compressed_bytes);
// println!("Decompressed: {}", decompressed);
// }
// Commands::DecompressFile { input } => {
// decompress_file(&input);
// }
// Commands::CompressFile { input } => {
// compress_file(&input);
// }
// }
match cli.command {
Commands::Compress { input } => {
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());
}
}
}