//! The desktop frontend, as megadrive-python's `tests/test_frontend.py`. //! //! Its conversions need no window and are tested everywhere. The window runs headless //! under SDL's dummy drivers with the `frontend` feature //! (`cargo test --features frontend`); CI builds without it. use megadrive_rust::frontend::{ frame_bytes, layout, machine_for, pad_for, sound_samples, stamp, where_, KEYS, LAYOUTS, }; use megadrive_rust::io::{BUTTONS, DOWN, LEFT, RIGHT, UP}; #[test] fn frame_bytes_are_opaque_bgra() { let mut want = vec![0x56, 0x34, 0x12, 0xFF]; want.extend([0xFF; 4]); assert_eq!(frame_bytes(&[vec![0x123456, 0xFFFFFF]]), want); } #[test] fn sound_samples_interleave_and_clamp() { // At 0 dB, below the knee: the mix as it is, interleaved. assert_eq!( sound_samples(&[(100, -100), (-23_000, 23_000)], 0.0), [100, -100, -23_000, 23_000] ); // +12 dB is x3.98. assert_eq!(sound_samples(&[(1_000, -1_000)], 12.0), [3_981, -3_981]); // Over the knee it bends, keeps its order and never reaches past full scale. let loud: Vec<(i32, i32)> = (0..=40).map(|k| (k * 2_000, -k * 2_000)).collect(); let out = sound_samples(&loud, 12.0); for pair in out.chunks(2).collect::>().windows(2) { assert!(pair[1][0] >= pair[0][0] && pair[1][1] <= pair[0][1]); } // 8,000 at +12 dB would be 0.97 of full scale; bent, it is 0.92. assert_eq!(out[8], 30_072); assert_eq!( sound_samples(&[(i32::MAX, i32::MIN)], 24.0), [32767, -32768] ); } fn header(country: &str) -> Vec { let mut image = vec![0u8; 0x200]; image[0x1F0..0x1F0 + country.len()].copy_from_slice(country.as_bytes()); image } #[test] fn the_machine_follows_the_headers_country_codes() { assert_eq!(machine_for(&header("JUE")), "genesis"); // the USA's NTSC console first assert_eq!(machine_for(&header("E")), "megadrive"); assert_eq!(machine_for(&header("J")), "megadrive-japan"); assert_eq!(machine_for(&header("4")), "genesis"); // a later code the Manual lacks assert_eq!(machine_for(b""), "genesis"); } #[test] fn every_pad_button_has_a_key() { let mut keys: Vec = KEYS.iter().map(|(button, _)| *button).collect(); let mut buttons: Vec = BUTTONS.iter().map(|(_, bit)| *bit).collect(); keys.sort_unstable(); buttons.sort_unstable(); assert_eq!(keys, buttons); // Each controller layout has every button but the d-pad, which comes from the // controller's own, once each, on eight different controller buttons. let rest: Vec = buttons .iter() .copied() .filter(|b| ![UP, DOWN, LEFT, RIGHT].contains(b)) .collect(); for (name, map) in LAYOUTS { let mut pad: Vec = map.iter().map(|(button, _)| *button).collect(); pad.sort_unstable(); assert_eq!(pad, rest, "{name}"); let mut names: Vec<&str> = map.iter().map(|(_, n)| *n).collect(); names.sort_unstable(); names.dedup(); assert_eq!(names.len(), 8, "{name}"); assert!(layout(name).is_ok()); } assert!(layout("xbox").is_err()); } #[test] fn the_pad_follows_the_headers_io_support() { let io = |field: &str| { let mut image = vec![0u8; 0x200]; image[0x190..0x190 + field.len()].copy_from_slice(field.as_bytes()); image }; assert_eq!(pad_for(&io("J6")), "6button"); // Super Street Fighter II assert_eq!(pad_for(&io("J64")), "6button"); // Mortal Kombat 3 assert_eq!(pad_for(&io("J")), "3button"); assert_eq!(pad_for(b""), "3button"); } #[test] fn the_title_bar_names_the_frame_and_the_emulated_time() { assert_eq!(where_(6800, 49.70), "frame 6,800 (2:16)"); assert_eq!(where_(0, 59.92), "frame 0 (0:00)"); assert_eq!(where_(1_234_567, 59.92), "frame 1,234,567 (343:23)"); } #[test] fn screenshots_are_stamped_in_utc() { assert_eq!(stamp(0), "19700101-000000"); assert_eq!(stamp(1_791_806_400 + 3723), "20261012-130203"); } /// The window, the loop, the pads and a change of mode, headless. One test, as SDL is /// initialised once per process. #[cfg(feature = "frontend")] #[test] fn the_window_runs_headless_and_survives_a_resolution_change() { use megadrive_rust::play::{Frontend, Options}; std::env::set_var("SDL_VIDEODRIVER", "dummy"); std::env::set_var("SDL_AUDIODRIVER", "dummy"); // A cartridge that loops for ever: the window, the loop and the pads, not a game. let mut rom = vec![0u8; 0x10000]; rom[0..4].copy_from_slice(&0x00FF_FE00u32.to_be_bytes()); rom[4..8].copy_from_slice(&0x0000_0200u32.to_be_bytes()); rom[0x100..0x110].copy_from_slice(b"SEGA GENESIS "); rom[0x200..0x202].copy_from_slice(&[0x60, 0xFE]); // bra.s * let path = std::env::temp_dir().join(format!("md-play-loop-{}.bin", std::process::id())); std::fs::write(&path, &rom).unwrap(); let sdl = sdl2::init().unwrap(); let mut frontend = Frontend::new( &sdl, Options { rom: path.clone(), machine: String::from("genesis"), scale: 1, audio: true, // the dummy driver: the audio-paced path runs fullscreen: false, fm: true, skip: 0, pads: [String::from("3button"), String::from("3button")], layout: String::from("snes"), save: None, tmss_rom: None, volume_db: megadrive_rust::frontend::VOLUME_DB, }, ) .unwrap(); frontend.run(Some(3)).unwrap(); frontend.fast = true; // Tab held: five console frames to each shown let before = frontend.system.board().vdp.s.frame; frontend.run(Some(2)).unwrap(); frontend.fast = false; assert_eq!(frontend.system.board().vdp.s.frame - before, 10); assert_eq!( frontend.frame_size, (frontend.frame[0].len() as u32, frontend.frame.len() as u32) ); frontend.apply_inputs(); // no key held: the pad reads nothing pressed let pad = frontend.system.board().dev.io.ports[0] .device .as_ref() .unwrap(); assert_eq!(pad.pressed(), 0); // H32/H40, V28/V30 or interlace changing mid-game resizes the window. for (width, height) in [(256, 224), (352, 240), (320, 224), (320, 448), (256, 224)] { frontend.frame = vec![vec![0; width]; height]; frontend.draw().unwrap(); assert_eq!(frontend.frame_size, (width as u32, height as u32)); } frontend.close().unwrap(); let _ = std::fs::remove_file(&path); }