52 lines
1.8 KiB
Rust
52 lines
1.8 KiB
Rust
use std::fs::File;
|
|
use std::io::{Read, Write};
|
|
use std::path::Path;
|
|
use flate2::Compression;
|
|
use flate2::read::DeflateDecoder;
|
|
use flate2::write::{DeflateEncoder};
|
|
|
|
use log::debug;
|
|
|
|
const TEST_OUTPUT_SAMPLE_DIR: &str = "../resources/test/";
|
|
|
|
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 {
|
|
let full_path = std::env::current_dir().unwrap().display().to_string() + "/" + &*TEST_OUTPUT_SAMPLE_DIR.to_owned() + suffix + ".deflated";
|
|
println!("LOADING {}", full_path);
|
|
let mut compressed_file = File::open(full_path).expect(format!("Unable to load test result [{}]", suffix).as_str());
|
|
let mut compressed_data = Vec::new();
|
|
compressed_file.read_to_end(&mut compressed_data).expect("Unable to compress data");
|
|
|
|
decompress_to_string(&compressed_data).unwrap()
|
|
}
|
|
|
|
pub fn load_compressed_result(suffix: &str) -> String {
|
|
read_compressed_test_result(suffix)
|
|
}
|
|
|
|
pub fn read_test_result(suffix: &str) -> String {
|
|
std::fs::read_to_string(TEST_OUTPUT_SAMPLE_DIR.to_owned() + suffix).unwrap()
|
|
}
|
|
|
|
|
|
pub fn load_result(to_load: &str) -> String {
|
|
std::fs::read_to_string(format!("../resources/test/{}", to_load)).unwrap()
|
|
}
|
|
|
|
pub fn load_rom(to_load: &str) -> Vec<u8> {
|
|
std::fs::read(format!("../resources/roms/{}", to_load)).unwrap()
|
|
} |