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

95 lines
3.2 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
.set_raw_flag_bits(test_case.initial.f)
.unwrap();
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,
"register a value was incorrect"
);
assert_eq!(
processor.registers.b, test_case.r#final.b,
"register b value was incorrect"
);
assert_eq!(
processor.registers.c, test_case.r#final.c,
"register c value was incorrect"
);
assert_eq!(
processor.registers.d, test_case.r#final.d,
"register d value was incorrect"
);
assert_eq!(
processor.registers.e, test_case.r#final.e,
"register e value was incorrect"
);
assert_eq!(
processor.registers.get_raw_flag_bits(),
test_case.r#final.f,
"register f value was incorrect"
);
assert_eq!(
processor.registers.h, test_case.r#final.h,
"register h value was incorrect"
);
assert_eq!(
processor.registers.l, test_case.r#final.l,
"register l value was incorrect"
);
assert_eq!(
processor.registers.program_counter, test_case.r#final.pc,
"program counter was incorrect"
);
assert_eq!(
processor.registers.stack_pointer, test_case.r#final.sp,
"stack pointer was incorrect"
);
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);
}
}
}