64 lines
2.1 KiB
Rust
64 lines
2.1 KiB
Rust
use std::{fs, io};
|
|
use flate2::write::{GzDecoder, GzEncoder};
|
|
use flate2::Compression;
|
|
use gemma::chip8::computer::Chip8Computer;
|
|
use std::io::prelude::*;
|
|
|
|
fn load_result(to_load: &str) -> String {
|
|
let full_path = format!("resources/test/state/{}", to_load);
|
|
println!("Loading state => (([{}]))", full_path);
|
|
std::fs::read_to_string(full_path).unwrap()
|
|
}
|
|
|
|
fn load_compressed_result(file_path: &str) -> io::Result<String> {
|
|
// Load the compressed file contents
|
|
let compressed_data = fs::read(file_path)?;
|
|
|
|
// Create a GzDecoder to uncompress the data
|
|
let mut decoder = GzDecoder::new(&mut compressed_data[..]);
|
|
let mut decompressed_data = String::new();
|
|
|
|
// Read the decompressed data directly into a String
|
|
decoder.read_to_string(&mut decompressed_data)?;
|
|
|
|
Ok(decompressed_data)
|
|
}
|
|
|
|
fn load_rom(to_load: &str) -> Vec<u8> {
|
|
std::fs::read(format!("resources/test/roms/{}", to_load)).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn test_serialization_round_trip() {
|
|
let original_computer = Chip8Computer::new();
|
|
let expected_json = load_result("smoke_001_round_trip_serialize_deserialize.json");
|
|
|
|
// Serialize the Chip8Computer instance
|
|
let serialized = serde_json::to_string(&original_computer).expect("Serialization failed");
|
|
|
|
// Compare the serialized output to the expected JSON
|
|
println!("Serialized Output: [{}]", serialized);
|
|
assert_eq!(
|
|
serialized.trim(),
|
|
expected_json.trim(),
|
|
"Serialized output does not match expected JSON"
|
|
);
|
|
|
|
// Deserialize back to Chip8Computer and assert equality
|
|
let deserialized_computer: Chip8Computer =
|
|
serde_json::from_str(&serialized).expect("Deserialization failed");
|
|
assert_eq!(
|
|
deserialized_computer, original_computer,
|
|
"Deserialized instance does not match the original"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn computer_001_system_zero_state() {
|
|
let x = Chip8Computer::new();
|
|
let expected_string = load_compressed_result("smoke_002_round_trip_serialize_deserialize.tflt");
|
|
let serialized = serde_json::to_string(&x).unwrap();
|
|
assert_eq!(serialized, expected_string);
|
|
}
|
|
|