tests: use anyhow

This commit is contained in:
Alex Janka 2024-06-16 11:48:05 +10:00
parent 21e767525e
commit 695bb06757
2 changed files with 15 additions and 13 deletions

View file

@ -6,14 +6,14 @@ use gb_emu_lib::connect::{EmulatorCoreTrait, RomFile};
mod common;
#[test]
fn cpu_instrs() -> Result<(), String> {
fn cpu_instrs() -> anyhow::Result<()> {
run_blargg_test("cpu_instrs\n\n01:ok 02:ok 03:ok 04:ok 05:ok 06:ok 07:ok 08:ok 09:ok 10:ok 11:ok \n\nPassed all tests", include_bytes!(
"../../test-roms/blargg/cpu_instrs/cpu_instrs.gb"
),None)
}
#[test]
fn instr_timing() -> Result<(), String> {
fn instr_timing() -> anyhow::Result<()> {
run_blargg_test(
"instr_timing\n\n\nPassed",
include_bytes!("../../test-roms/blargg/instr_timing/instr_timing.gb"),
@ -22,7 +22,7 @@ fn instr_timing() -> Result<(), String> {
}
#[test]
fn mem_timing() -> Result<(), String> {
fn mem_timing() -> anyhow::Result<()> {
run_blargg_test(
"mem_timing\n\n\nPassed",
include_bytes!("../../test-roms/blargg/mem_timing/mem_timing.gb"),
@ -30,11 +30,17 @@ fn mem_timing() -> Result<(), String> {
)
}
#[derive(Debug, thiserror::Error)]
enum TestError {
#[error("Timeout")]
Timeout(String),
}
fn run_blargg_test<const N: usize>(
correct_output: &str,
rom: &[u8; N],
extra_end: Option<Vec<&str>>,
) -> Result<(), String> {
) -> anyhow::Result<()> {
let mut emu = emulator_setup(RomFile::Raw(rom.to_vec()))?;
let mut end_strings = extra_end.unwrap_or_default();
@ -54,7 +60,7 @@ fn run_blargg_test<const N: usize>(
}
}
if began.elapsed() > timeout {
return Err(format!("Test timed out: output was {chars}"));
return Err(TestError::Timeout(format!("Test timed out: output was {chars}")).into());
}
}

View file

@ -13,11 +13,9 @@ pub struct TestEmulator {
pub serial_rx: Receiver<u8>,
}
pub fn emulator_setup(rom_file: RomFile) -> Result<TestEmulator, String> {
pub fn emulator_setup(rom_file: RomFile) -> anyhow::Result<TestEmulator> {
let (sender, receiver) = channel::<EmulatorMessage<[u8; 4]>>();
let rom = rom_file
.load(gb_emu_lib::connect::SramType::None)
.map_err(|_e| String::from("Error reading ROM: {_e:?}"))?;
let rom = rom_file.load(gb_emu_lib::connect::SramType::None)?;
let (audio_output, audio_rx) = AudioOutput::new(
48000.,
1,
@ -40,10 +38,8 @@ pub fn emulator_setup(rom_file: RomFile) -> Result<TestEmulator, String> {
include_bytes!("../../../sameboy-bootroms/dmg_boot.bin").to_vec(),
)));
let core = EmulatorCore::init(true, receiver, options);
sender
.send(EmulatorMessage::Start)
.map_err(|_e| String::from("Error sending message: {_e:?}"))?;
let core = EmulatorCore::init(true, receiver, options)?;
sender.send(EmulatorMessage::Start)?;
Ok(TestEmulator {
core,
_sender: sender,