add unit tests for load, store memory, bcd convert, add to index

This commit is contained in:
Denis-Cosmin Nutiu 2024-12-13 15:12:29 +02:00
parent ef98349586
commit 4e8ae60b3c
2 changed files with 74 additions and 2 deletions

View file

@ -854,4 +854,76 @@ mod tests {
assert_eq!(emulator.sound_timer, 0xEE); 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);
}
}
} }

View file

@ -611,7 +611,7 @@ mod tests {
} }
#[test] #[test]
fn test_instruction_load_memory() { fn test_instruction_store_memory() {
let instruction = Instruction::new([0xFA, 0x55]); let instruction = Instruction::new([0xFA, 0x55]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,
@ -620,7 +620,7 @@ mod tests {
} }
#[test] #[test]
fn test_instruction_store_memory() { fn test_instruction_load_memory() {
let instruction = Instruction::new([0xFA, 0x65]); let instruction = Instruction::new([0xFA, 0x65]);
assert_eq!( assert_eq!(
instruction.processor_instruction, instruction.processor_instruction,