From 4e8ae60b3cdb7b346cc1f9034fd3eab240c98141 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Fri, 13 Dec 2024 15:12:29 +0200 Subject: [PATCH] add unit tests for load, store memory, bcd convert, add to index --- src/emulator.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++++ src/instruction.rs | 4 +-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/emulator.rs b/src/emulator.rs index 7af3380..94847a2 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -854,4 +854,76 @@ mod tests { assert_eq!(emulator.sound_timer, 0xEE); } + + #[test] + fn test_execute_add_to_index() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.registers[0xA] = 0xEE; + + emulator + .execute_instruction(Instruction::new([0xFA, 0x1E])) + .expect("Failed to execute"); + + assert_eq!(emulator.index_register, 0xEE); + } + + #[test] + fn test_execute_get_font_character() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.registers[0xA] = 0xEE; + + emulator + .execute_instruction(Instruction::new([0xFA, 0x29])) + .expect("Failed to execute"); + + assert_eq!(emulator.index_register, 0x136); + } + + #[test] + fn test_execute_bcd_convert() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.registers[0xA] = 0xFE; + + emulator + .execute_instruction(Instruction::new([0xFA, 0x33])) + .expect("Failed to execute"); + + assert_eq!(emulator.memory[emulator.index_register as usize], 2); + assert_eq!(emulator.memory[emulator.index_register as usize + 1], 5); + assert_eq!(emulator.memory[emulator.index_register as usize + 2], 4); + } + + #[test] + fn test_execute_store_memory() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.index_register = 0x20; + for i in 0..0xF { + emulator.registers[i] = (0xF + i) as u8; + } + + emulator + .execute_instruction(Instruction::new([0xFF, 0x55])) + .expect("Failed to execute"); + + for i in 0..0xF { + assert_eq!(emulator.memory[(emulator.index_register + i) as usize], (0xF + i) as u8); + } + } + + #[test] + fn test_execute_load_memory() { + let mut emulator = Emulator::new(TerminalDisplay::new(), TerminalSound, NoInput); + emulator.index_register = 0x20; + for i in 0..0xF { + emulator.memory[(emulator.index_register + i) as usize] = (0xF + i) as u8; + } + + emulator + .execute_instruction(Instruction::new([0xFF, 0x65])) + .expect("Failed to execute"); + + for i in 0..0xF { + assert_eq!(emulator.registers[i], (0xF + i) as u8); + } + } } diff --git a/src/instruction.rs b/src/instruction.rs index 4b06bd7..e573a7b 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -611,7 +611,7 @@ mod tests { } #[test] - fn test_instruction_load_memory() { + fn test_instruction_store_memory() { let instruction = Instruction::new([0xFA, 0x55]); assert_eq!( instruction.processor_instruction, @@ -620,7 +620,7 @@ mod tests { } #[test] - fn test_instruction_store_memory() { + fn test_instruction_load_memory() { let instruction = Instruction::new([0xFA, 0x65]); assert_eq!( instruction.processor_instruction,