trevors_chip8_toy/gemma/tests/state_tests.rs
2024-11-04 10:38:46 -05:00

41 lines
1.6 KiB
Rust

use flate2::write::{GzEncoder, GzDecoder};
use flate2::Compression;
use std::io::prelude::*;
use gemma::chip8::computer::Chip8Computer;
fn load_result(to_load: &str) -> String {
std::fs::read_to_string(format!("resources/test/state/{}", to_load)).unwrap()
}
fn load_compressed_result(to_load: &str) -> String {
// load the file...
// ...then uncompress the string.
let mut decoder = GzDecoder::new(Vec::new());
decoder.write_all(load_result(to_load).as_ref()).expect("Decompression failed");
let decompressed_data = decoder.finish().expect("Failed to finish decompression");
String::from_utf8(decompressed_data).expect("Invalid UTF-8")
}
fn load_rom(to_load: &str) -> Vec<u8> {
std::fs::read(format!("resources/roms/{}", to_load)).unwrap()
}
#[test]
fn smoke_round_trip_serialize_deserialize() {
let x = Chip8Computer::new();
let expected_string = load_result("smoke_001_round_trip_serialize_deserialize.json");
let serialized = serde_json::to_string(&x).unwrap();
let deserialized: Chip8Computer = serde_json::from_str(&expected_string).unwrap();
println!("SERIALIZED [{}]", serialized);
// TODO: using trim here is a hack to handle editors adding a newline at the end of a file...even a 1 line file
assert_eq!(serialized.trim(), expected_string.trim());
assert_eq!(deserialized, x);
}
#[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();
}