ferris-boi/tests/cpu/jsmoo/tests.rs

61 lines
2.4 KiB
Rust

use std::fs::File;
use ferris_boi::cpu::Processor;
use test_generator::test_resources;
use super::TestCase;
#[test_resources("tests/cpu/jsmoo/testdata/*.json")]
fn test_jsmoo_test(filename: &str) {
let testdata_file = File::open(filename).expect("failed to open test data file");
let test_cases = serde_json::from_reader::<_, Vec<TestCase>>(testdata_file)
.expect("failed to parse test data file");
for test_case in test_cases {
let test_name = test_case.name;
println!("{test_name}");
let mut processor = Processor::default();
processor.registers.a = test_case.initial.a;
processor.registers.b = test_case.initial.b;
processor.registers.c = test_case.initial.c;
processor.registers.d = test_case.initial.d;
processor.registers.e = test_case.initial.e;
processor.registers.flags = test_case.initial.f;
processor.registers.h = test_case.initial.h;
processor.registers.l = test_case.initial.l;
processor.registers.program_counter = test_case.initial.pc;
processor.registers.stack_pointer = test_case.initial.sp;
for (addr, value) in test_case.initial.ram {
processor
.memory
.set(addr.into(), value)
.unwrap_or_else(|err| {
panic!("failed to set up initial memory for test {test_name}: {err}")
});
}
processor.run_next_instruction();
assert_eq!(processor.registers.a, test_case.r#final.a);
assert_eq!(processor.registers.b, test_case.r#final.b);
assert_eq!(processor.registers.c, test_case.r#final.c);
assert_eq!(processor.registers.d, test_case.r#final.d);
assert_eq!(processor.registers.e, test_case.r#final.e);
assert_eq!(processor.registers.flags, test_case.r#final.f);
assert_eq!(processor.registers.h, test_case.r#final.h);
assert_eq!(processor.registers.l, test_case.r#final.l);
assert_eq!(processor.registers.program_counter, test_case.r#final.pc);
assert_eq!(processor.registers.stack_pointer, test_case.r#final.sp);
for (addr, value) in test_case.r#final.ram {
let stored_val = processor
.memory
.get(addr.into())
.unwrap_or_else(|_| panic!("failed to set up final memory for test {test_name}"));
assert_eq!(stored_val, value);
}
}
}