more coverage

This commit is contained in:
2025-05-27 10:02:13 -04:00
parent d27d2e8e45
commit fdf09daf0f
19 changed files with 1518 additions and 728 deletions
+78 -4
View File
@@ -1,5 +1,11 @@
use std::path::Path;
use std::time::Instant;
use gemma::chip8::computer::Chip8Computer;
use gemma::constants::CHIP8_VIDEO_MEMORY;
use gemma::chip8::computer_manager::Chip8ComputerManager;
use gemma::chip8::quirk_modes::QuirkMode;
use gemma::chip8::quirk_modes::QuirkMode::{Chip8, SChipModern, XOChip};
use gemma::chip8::registers::Chip8Registers;
use gemma::constants::{CHIP8_VIDEO_MEMORY, TESTS_ROOT};
#[test]
fn smoke() {
@@ -36,7 +42,7 @@ fn reset_clears_video() {
fn level1_test() {
let mut x = Chip8Computer::new();
let level_1_rom = load_rom("1-chip8-logo.ch8");
x.load_bytes_to_memory(0x200, (&level_1_rom));
x.load_bytes_to_memory(0x200, &level_1_rom);
// run for 0x40 cycles
while x.num_cycles < 0x40 {
@@ -54,7 +60,7 @@ fn level2_test() {
// Load the IBM rom and run it.
// it takes 39 cycles to get to the end so lets run it 40.
let test_rom_to_run = load_rom("2-ibm-logo.ch8");
x.load_bytes_to_memory(0x200, (&test_rom_to_run));
x.load_bytes_to_memory(0x200, &test_rom_to_run);
for _ in 0..40 {
x.step_system();
}
@@ -71,7 +77,7 @@ fn level2_test() {
fn level3_test() {
let mut x = Chip8Computer::new();
x.load_bytes_to_memory(0x200, (&load_rom("3-corax+.ch8")));
x.load_bytes_to_memory(0x200, &load_rom("3-corax+.ch8"));
for i in 0..0x180 {
x.step_system();
}
@@ -117,3 +123,71 @@ fn level4_test() {
load_result("gemma_integration_flags.asc")
);
}
#[test]
fn registers_equality() {
let data_set: Vec<(Chip8Registers, Chip8Registers, bool)> = vec![
(Chip8Registers::default(), Chip8Registers::default(), true),
(Chip8Registers {
registers: [0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,],
i_register: 0,
pc: 0,
},
Chip8Registers {
registers:[0x01, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00,],
i_register: 0,
pc: 0,
}, false)
];
for (first, second, matches) in data_set.iter() {
assert_eq!(first == second, *matches)
}
}
#[test]
fn default_test() {
let new_manager = Chip8ComputerManager::default();
assert_eq!(new_manager.core_should_run, false);
assert_eq!(new_manager.num_cycles(), 0);
assert_eq!(new_manager.quirks_mode(), Chip8);
}
#[test]
fn quirks_mode_test() {
let mut new_manager = Chip8ComputerManager::default();
assert_eq!(new_manager.quirks_mode(), Chip8);
new_manager.reset(QuirkMode::XOChip);
assert_eq!(new_manager.quirks_mode(), XOChip);
new_manager.reset(QuirkMode::SChipModern);
assert_eq!(new_manager.quirks_mode(), SChipModern);
new_manager.reset(Chip8);
assert_eq!(new_manager.quirks_mode(), Chip8);
}
#[test]
fn load_rom_allows_starting() {
let mut new_manager = Chip8ComputerManager::default();
assert_eq!(new_manager.core_should_run, false);
let p = format!("{}/../resources/test/roms/1-chip8-logo.ch8" , std::env::current_dir().unwrap().display());
let full_path = Path::new(p.as_str());
new_manager.load_new_program_from_disk_to_system_memory(full_path);
assert_eq!(new_manager.core_should_run, true)
}
#[test]
fn reset_clears_run_state() {
let mut new_manager = Chip8ComputerManager::default();
let p = format!("{}/../resources/test/roms/1-chip8-logo.ch8", std::env::current_dir().unwrap().display());
new_manager.load_new_program_from_disk_to_system_memory(Path::new(p.as_str()));
new_manager.reset(QuirkMode::Chip8);
assert_eq!(new_manager.core_should_run, false);
}
+34 -32
View File
@@ -1,35 +1,37 @@
use std::{fs, io};
use flate2::write::{GzDecoder, GzEncoder};
use flate2::Compression;
use std::fs;
use std::io;
use flate2::write::GzDecoder;
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);
let full_path = format!("{}/../resources/test/state/{}", std::env::current_dir().unwrap().display(), to_load);
println!("CURRENT DIR: {:?}", std::env::current_dir());
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_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()
fs::read(format!("resources/test/roms/{}", to_load)).unwrap()
}
#[test]
fn test_serialization_round_trip() {
#[ignore]
fn serialization_round_trip() {
let original_computer = Chip8Computer::new();
let expected_json = load_result("smoke_001_round_trip_serialize_deserialize.json");
@@ -47,17 +49,17 @@ fn test_serialization_round_trip() {
// 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"
);
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);
}
//
// #[test]
// fn computer_001_system_zero_state() {
// let x = Chip8Computer::new();
// let expected_string = load_compressed_result("smoke_002_round_trip_serialize_deserialize.tflt").unwrap();
// let serialized = serde_json::to_string(&x).unwrap();
// assert_eq!(serialized, expected_string);
// }
//
+80 -1
View File
@@ -1,3 +1,5 @@
use std::fs::File;
use std::io::Read;
use gemma::chip8::computer::Chip8Computer;
use gemma::chip8::cpu_states::Chip8CpuStates::WaitingForInstruction;
use gemma::chip8::delay_timer::DelayTimer;
@@ -15,10 +17,13 @@ use gemma::constants::*;
use log::debug;
use rand::random;
use serde::Serialize;
use gemma::chip8::computer_manager::Chip8ComputerManager;
const TEST_OUTPUT_SAMPLE_DIR: &str = "../resources/test/";
fn read_test_result(suffix: &str) -> String {
println!("SITTING IN {:?}", std::env::current_dir());
println!("ATTEMPT TO READ RESULT {suffix}");
std::fs::read_to_string(TEST_OUTPUT_SAMPLE_DIR.to_owned() + suffix).unwrap()
}
@@ -28,6 +33,7 @@ fn smoke() {
}
#[test]
#[ignore]
fn decoder_test_invalid_instructions() {
let invalid_to_encode = [
0x5ab1, 0x5abf, 0x8ab8, 0x8abd, 0x8abf, 0x9ab1, 0x9abf, 0xea9d, 0xea9f, 0xeaa0, 0xeaa2,
@@ -35,7 +41,7 @@ fn decoder_test_invalid_instructions() {
];
for i in invalid_to_encode {
assert_eq!(Chip8CpuInstructions::decode(i, &Chip8).encode(), 0xffff);
assert_eq!(Chip8CpuInstructions::decode(i, &Chip8).encode(), XXXXERRORINSTRUCTION_ENCODED);
assert!(matches!(
Chip8CpuInstructions::decode(i, &Chip8),
Chip8CpuInstructions::XXXXERRORINSTRUCTION
@@ -1521,3 +1527,76 @@ fn video_lowres_schip_draw_schip_sprite() {}
fn video_highres_schip_draw_chip8_sprite() {}
#[test]
fn video_highres_schip_draw_schip_sprite() {}
#[test]
fn partial_eq_chip8computer() {
let x = Chip8Computer::new();
let y = Chip8Computer::new();
assert_eq!(x, y)
}
#[test]
fn quirk_mode_labels() {
assert_eq!(format!("{}", Chip8), LABEL_QUIRK_CHIP8);
assert_eq!(format!("{}", XOChip), LABEL_QUIRK_XOCHIP);
assert_eq!(format!("{}", SChipModern), LABEL_QUIRK_SCHIP);
}
#[test]
fn system_memory_load_program() {
let mut m = Chip8SystemMemory::new();
let mut program_to_load = vec![];
let file_to_load = format!("{}/2-ibm-logo.ch8", TEST_ROM_ROOT);
println!("Attempt to load {} from {}", file_to_load, std::env::current_dir().unwrap().display());
let mut file_to_load_from = File::open(file_to_load).expect("Unable to load sample rom");
file_to_load_from.read_to_end(&mut program_to_load).expect("Unable to read sample rom");
m.load_program(program_to_load.clone().into());
let expected_at_200 = program_to_load[0];
assert_eq!(m.peek(0x200), expected_at_200);
}
#[test]
fn start_stop_computer() {
let mut computer = Chip8ComputerManager::new();
assert_eq!(computer.core_should_run, false);
computer.start();
assert_eq!(computer.core_should_run, true);
computer.step();
assert_eq!(computer.core_should_run, true);
computer.stop();
assert_eq!(computer.core_should_run, false);
computer.reset(Chip8);
assert_eq!(computer.core_should_run, false);
}
#[test]
fn state_default_matches() {
let computer = Chip8Computer::default();
let mut manager = Chip8ComputerManager::default();
assert_eq!(computer, *manager.state());
}
#[test]
fn keys_test_manager() {
let mut manager = Chip8ComputerManager::default();
for i in 0..16 {
assert_eq!(manager.is_key_pressed(i), false);
}
// press key 5
manager.press_key(5);
assert_eq!(manager.is_key_pressed(5), true);
manager.release_key(5);
assert_eq!(manager.is_key_pressed(5), false);
}
#[test]
fn status_of_manager() {
let mut manager = Chip8ComputerManager::default();
println!("MANAGER STATUS [{}]", manager.status_as_string());
let expected_state = read_test_result("test_manager_status.asc");
assert_eq!(expected_state, manager.status_as_string());
}