fix emulator tests

This commit is contained in:
Denis-Cosmin Nutiu 2025-01-20 21:49:31 +02:00
parent 9a360be045
commit 86acbf6fc8
3 changed files with 58 additions and 46 deletions

View file

@ -11,3 +11,12 @@ pub trait Display {
/// Renders the display data on screen. /// Renders the display data on screen.
fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]); fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]);
} }
/// Simple display module for testing. Does nothing.
pub(crate) struct TestingDisplay;
impl Display for TestingDisplay {
fn clear(&mut self) {}
fn render(&mut self, display_data: &[bool; DISPLAY_WIDTH * DISPLAY_HEIGHT]) {}
}

View file

@ -485,16 +485,16 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::display::TerminalDisplay; use crate::display::TestingDisplay;
use crate::input::NoInput; use crate::input::NoInput;
use crate::sound::TerminalSound; use crate::sound::TestingSound;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use std::fs::File; use std::fs::File;
use std::io::{Seek, SeekFrom}; use std::io::{Seek, SeekFrom};
#[test] #[test]
fn test_load_font_data() { fn test_load_font_data() {
let emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
assert_eq!(emulator.memory[0xf0..0xf0 + 80], FONT_SPRITES) assert_eq!(emulator.memory[0xf0..0xf0 + 80], FONT_SPRITES)
} }
@ -509,7 +509,7 @@ mod tests {
let _ = file.seek(SeekFrom::Start(0)); let _ = file.seek(SeekFrom::Start(0));
// Test // Test
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.load_rom(file).expect("failed to load ROM"); emulator.load_rom(file).expect("failed to load ROM");
// Assert // Assert
@ -519,7 +519,7 @@ mod tests {
#[test] #[test]
fn test_execute_clear_screen_instruction() { fn test_execute_clear_screen_instruction() {
// Setup // Setup
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
for i in 10..30 { for i in 10..30 {
emulator.display_data[i] = true; emulator.display_data[i] = true;
} }
@ -538,7 +538,7 @@ mod tests {
#[test] #[test]
fn test_execute_jump() { fn test_execute_jump() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator emulator
.execute_instruction(Instruction::new([0x1A, 0xBC])) .execute_instruction(Instruction::new([0x1A, 0xBC]))
@ -549,7 +549,7 @@ mod tests {
#[test] #[test]
fn test_execute_set_register() { fn test_execute_set_register() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
for i in 0x0..=0xF { for i in 0x0..=0xF {
let random_data: u8 = rand::thread_rng().gen_range(0x00..0xFF); let random_data: u8 = rand::thread_rng().gen_range(0x00..0xFF);
@ -562,7 +562,7 @@ mod tests {
#[test] #[test]
fn test_execute_add_value_to_register() { fn test_execute_add_value_to_register() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator emulator
.execute_instruction(Instruction::new([0x71, 0xCC])) .execute_instruction(Instruction::new([0x71, 0xCC]))
@ -582,7 +582,7 @@ mod tests {
#[test] #[test]
fn test_execute_set_index_register() { fn test_execute_set_index_register() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator emulator
.execute_instruction(Instruction::new([0xAA, 0xBC])) .execute_instruction(Instruction::new([0xAA, 0xBC]))
@ -593,7 +593,7 @@ mod tests {
#[test] #[test]
fn test_execute_draw() { fn test_execute_draw() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.index_register = 0xF0; emulator.index_register = 0xF0;
emulator emulator
@ -608,7 +608,7 @@ mod tests {
#[test] #[test]
fn test_execute_call() { fn test_execute_call() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.program_counter = 0x200; emulator.program_counter = 0x200;
emulator emulator
@ -621,7 +621,7 @@ mod tests {
#[test] #[test]
fn test_execute_return() { fn test_execute_return() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.stack.push(0x269); emulator.stack.push(0x269);
emulator emulator
@ -633,7 +633,7 @@ mod tests {
#[test] #[test]
fn test_execute_set() { fn test_execute_set() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator emulator
.execute_instruction(Instruction::new([0x61, 0x40])) .execute_instruction(Instruction::new([0x61, 0x40]))
@ -644,7 +644,7 @@ mod tests {
#[test] #[test]
fn test_execute_binary_or() { fn test_execute_binary_or() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x6; emulator.registers[0x1] = 0x6;
emulator.registers[0xF] = 0x9; emulator.registers[0xF] = 0x9;
@ -657,7 +657,7 @@ mod tests {
#[test] #[test]
fn test_execute_binary_and() { fn test_execute_binary_and() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x6; emulator.registers[0x1] = 0x6;
emulator.registers[0xF] = 0x9; emulator.registers[0xF] = 0x9;
@ -670,7 +670,7 @@ mod tests {
#[test] #[test]
fn test_execute_logical_xor() { fn test_execute_logical_xor() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x7; emulator.registers[0x1] = 0x7;
emulator.registers[0xF] = 0x9; emulator.registers[0xF] = 0x9;
@ -683,7 +683,7 @@ mod tests {
#[test] #[test]
fn test_execute_logical_add() { fn test_execute_logical_add() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x7; emulator.registers[0x1] = 0x7;
emulator.registers[0xF] = 0x9; emulator.registers[0xF] = 0x9;
@ -696,7 +696,7 @@ mod tests {
#[test] #[test]
fn test_execute_logical_subtract_vx() { fn test_execute_logical_subtract_vx() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x7; emulator.registers[0x1] = 0x7;
emulator.registers[0xE] = 0x9; emulator.registers[0xE] = 0x9;
@ -709,7 +709,7 @@ mod tests {
#[test] #[test]
fn test_execute_logical_subtract_vy() { fn test_execute_logical_subtract_vy() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x7; emulator.registers[0x1] = 0x7;
emulator.registers[0xF] = 0x9; emulator.registers[0xF] = 0x9;
@ -722,7 +722,7 @@ mod tests {
#[test] #[test]
fn test_execute_logical_shift_left() { fn test_execute_logical_shift_left() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x7; emulator.registers[0x1] = 0x7;
emulator.registers[0x2] = 0x9; emulator.registers[0x2] = 0x9;
@ -735,7 +735,7 @@ mod tests {
#[test] #[test]
fn test_execute_logical_shift_right() { fn test_execute_logical_shift_right() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x1] = 0x7; emulator.registers[0x1] = 0x7;
emulator.registers[0x2] = 0x9; emulator.registers[0x2] = 0x9;
@ -748,7 +748,7 @@ mod tests {
#[test] #[test]
fn test_execute_jump_with_offset() { fn test_execute_jump_with_offset() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0x0] = 0x2; emulator.registers[0x0] = 0x2;
emulator emulator
@ -760,7 +760,7 @@ mod tests {
#[test] #[test]
fn test_execute_random_number() { fn test_execute_random_number() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator emulator
.execute_instruction(Instruction::new([0xCA, 0xBC])) .execute_instruction(Instruction::new([0xCA, 0xBC]))
@ -771,7 +771,7 @@ mod tests {
#[test] #[test]
fn test_execute_skip_equal_vx_data() { fn test_execute_skip_equal_vx_data() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xBC; emulator.registers[0xA] = 0xBC;
emulator emulator
@ -783,7 +783,7 @@ mod tests {
#[test] #[test]
fn test_execute_skip_not_equal_vx_data() { fn test_execute_skip_not_equal_vx_data() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xB1; emulator.registers[0xA] = 0xB1;
emulator emulator
@ -795,7 +795,7 @@ mod tests {
#[test] #[test]
fn test_execute_skip_equal_vx_vy() { fn test_execute_skip_equal_vx_vy() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xBC; emulator.registers[0xA] = 0xBC;
emulator.registers[0xB] = 0xBC; emulator.registers[0xB] = 0xBC;
@ -808,7 +808,7 @@ mod tests {
#[test] #[test]
fn test_execute_skip_not_equal_vx_vy() { fn test_execute_skip_not_equal_vx_vy() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xBD; emulator.registers[0xA] = 0xBD;
emulator.registers[0xB] = 0xBC; emulator.registers[0xB] = 0xBC;
@ -821,7 +821,7 @@ mod tests {
#[test] #[test]
fn test_execute_set_vx_to_delay_timer() { fn test_execute_set_vx_to_delay_timer() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.delay_timer = 0xEE; emulator.delay_timer = 0xEE;
emulator emulator
@ -833,7 +833,7 @@ mod tests {
#[test] #[test]
fn test_execute_set_delay_timer() { fn test_execute_set_delay_timer() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xEE; emulator.registers[0xA] = 0xEE;
emulator emulator
@ -845,7 +845,7 @@ mod tests {
#[test] #[test]
fn test_execute_set_sound_timer() { fn test_execute_set_sound_timer() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xEE; emulator.registers[0xA] = 0xEE;
emulator emulator
@ -857,7 +857,7 @@ mod tests {
#[test] #[test]
fn test_execute_add_to_index() { fn test_execute_add_to_index() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xEE; emulator.registers[0xA] = 0xEE;
emulator emulator
@ -869,7 +869,7 @@ mod tests {
#[test] #[test]
fn test_execute_get_font_character() { fn test_execute_get_font_character() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xEE; emulator.registers[0xA] = 0xEE;
emulator emulator
@ -881,7 +881,7 @@ mod tests {
#[test] #[test]
fn test_execute_bcd_convert() { fn test_execute_bcd_convert() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0xFE; emulator.registers[0xA] = 0xFE;
emulator emulator
@ -895,7 +895,7 @@ mod tests {
#[test] #[test]
fn test_execute_store_memory() { fn test_execute_store_memory() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.index_register = 0x20; emulator.index_register = 0x20;
for i in 0..0xF { for i in 0..0xF {
emulator.registers[i] = (0xF + i) as u8; emulator.registers[i] = (0xF + i) as u8;
@ -915,7 +915,7 @@ mod tests {
#[test] #[test]
fn test_execute_load_memory() { fn test_execute_load_memory() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.index_register = 0x20; emulator.index_register = 0x20;
for i in 0..0xF { for i in 0..0xF {
emulator.memory[(emulator.index_register + i) as usize] = (0xF + i) as u8; emulator.memory[(emulator.index_register + i) as usize] = (0xF + i) as u8;
@ -932,7 +932,7 @@ mod tests {
#[test] #[test]
fn test_execute_get_key_blocking() { fn test_execute_get_key_blocking() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.program_counter = 0x10; emulator.program_counter = 0x10;
emulator emulator
@ -950,7 +950,7 @@ mod tests {
#[test] #[test]
fn test_execute_skip_key_pressed() { fn test_execute_skip_key_pressed() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.program_counter = 0; emulator.program_counter = 0;
emulator.registers[0xA] = 0x1; emulator.registers[0xA] = 0x1;
@ -968,7 +968,7 @@ mod tests {
#[test] #[test]
fn test_execute_skip_key_not_pressed() { fn test_execute_skip_key_not_pressed() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.program_counter = 0; emulator.program_counter = 0;
emulator.registers[0xA] = 0x1; emulator.registers[0xA] = 0x1;
@ -986,7 +986,7 @@ mod tests {
#[test] #[test]
fn test_execute_set_vx_to_vy() { fn test_execute_set_vx_to_vy() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.registers[0xA] = 0; emulator.registers[0xA] = 0;
emulator.registers[0xB] = 0xEF; emulator.registers[0xB] = 0xEF;
@ -999,7 +999,7 @@ mod tests {
#[test] #[test]
fn test_fetch_instruction() { fn test_fetch_instruction() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.program_counter = 0x200; emulator.program_counter = 0x200;
emulator.memory[0x200] = 0x00; emulator.memory[0x200] = 0x00;
emulator.memory[0x201] = 0xEE; emulator.memory[0x201] = 0xEE;
@ -1017,7 +1017,7 @@ mod tests {
#[test] #[test]
fn test_handle_timers() { fn test_handle_timers() {
let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); let mut emulator = Emulator::new(TestingDisplay, TestingSound, NoInput);
emulator.sound_timer = 10; emulator.sound_timer = 10;
emulator.delay_timer = 12; emulator.delay_timer = 12;
@ -1039,11 +1039,7 @@ mod tests {
} }
} }
let mut emulator = Emulator::new( let mut emulator = Emulator::new(TestingDisplay, TestSound { did_beep: false }, NoInput);
TerminalDisplay::new(),
TestSound { did_beep: false },
NoInput,
);
emulator.sound_timer = 0; emulator.sound_timer = 0;
emulator.handle_timers(); emulator.handle_timers();

View file

@ -3,3 +3,10 @@ pub trait SoundModule {
/// beep makes a beep sound. /// beep makes a beep sound.
fn beep(&mut self); fn beep(&mut self);
} }
/// A simple module for testing the sound.
pub(crate) struct TestingSound;
impl SoundModule for TestingSound {
fn beep(&mut self) {}
}