From 10a407cbccaedad254528439164a42d7c98149ef Mon Sep 17 00:00:00 2001 From: MaxOhn Date: Fri, 10 Nov 2023 21:56:36 +0100 Subject: [PATCH] test: parse maps only once --- tests/common/mod.rs | 74 +++++++++++++++++++++++++++++++++++++++------ tests/parse.rs | 10 +++--- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 28dff119..01e26dd3 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,30 +1,84 @@ mod mode; -use rosu_pp::Beatmap; +use rosu_pp::{Beatmap, GameMode}; +use std::sync::OnceLock; pub use self::mode::{Catch, Mania, Mode, Osu, Taiko}; +static MAPS: OnceLock<[Beatmap; 4]> = OnceLock::new(); + #[macro_export] #[rustfmt::skip] macro_rules! test_map { ($mode:ident) => {{ #[cfg(not(any(feature = "async_tokio", feature = "async_std")))] - { common::test_map::<$mode>() } + { common::test_map(GameMode::$mode) } #[cfg(any(feature = "async_tokio", feature = "async_std"))] - { common::test_map::<$mode>().await } + { common::test_map(GameMode::$mode).await } }}; } #[cfg(not(any(feature = "async_tokio", feature = "async_std")))] -pub fn test_map() -> Beatmap { - let path = format!("./maps/{}.osu", M::TEST_MAP_ID); +pub fn test_map(mode: GameMode) -> &'static Beatmap { + let maps = MAPS.get_or_init(|| { + let osu = Beatmap::from_path(path(GameMode::Osu)).unwrap(); + let taiko = Beatmap::from_path(path(GameMode::Taiko)).unwrap(); + let catch = Beatmap::from_path(path(GameMode::Catch)).unwrap(); + let mania = Beatmap::from_path(path(GameMode::Mania)).unwrap(); + + [osu, taiko, catch, mania] + }); + + &maps[mode as usize] +} + +#[cfg(feature = "async_tokio")] +pub async fn test_map(mode: GameMode) -> &'static Beatmap { + let maps = MAPS.get_or_init(|| { + let fut = async { + let osu = Beatmap::from_path(path(GameMode::Osu)).await.unwrap(); + let taiko = Beatmap::from_path(path(GameMode::Taiko)).await.unwrap(); + let catch = Beatmap::from_path(path(GameMode::Catch)).await.unwrap(); + let mania = Beatmap::from_path(path(GameMode::Mania)).await.unwrap(); + + [osu, taiko, catch, mania] + }; + + match tokio::runtime::Handle::try_current() { + Ok(h) => std::thread::spawn(move || h.block_on(fut)).join().unwrap(), + Err(_) => tokio::runtime::Builder::new_current_thread() + .build() + .unwrap() + .block_on(fut), + } + }); + + &maps[mode as usize] +} + +#[cfg(feature = "async_std")] +pub async fn test_map(mode: GameMode) -> &'static Beatmap { + let maps = MAPS.get_or_init(|| { + async_std::task::block_on(async { + let osu = Beatmap::from_path(path(GameMode::Osu)).await.unwrap(); + let taiko = Beatmap::from_path(path(GameMode::Taiko)).await.unwrap(); + let catch = Beatmap::from_path(path(GameMode::Catch)).await.unwrap(); + let mania = Beatmap::from_path(path(GameMode::Mania)).await.unwrap(); + + [osu, taiko, catch, mania] + }) + }); - Beatmap::from_path(path).unwrap() + &maps[mode as usize] } -#[cfg(any(feature = "async_tokio", feature = "async_std"))] -pub async fn test_map() -> Beatmap { - let path = format!("./maps/{}.osu", M::TEST_MAP_ID); +fn path(mode: GameMode) -> String { + let map_id = match mode { + GameMode::Osu => ::TEST_MAP_ID, + GameMode::Taiko => ::TEST_MAP_ID, + GameMode::Catch => ::TEST_MAP_ID, + GameMode::Mania => ::TEST_MAP_ID, + }; - Beatmap::from_path(path).await.unwrap() + format!("./maps/{map_id}.osu") } diff --git a/tests/parse.rs b/tests/parse.rs index f0bc6d24..24ace965 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -1,7 +1,5 @@ use rosu_pp::{Beatmap, GameMode}; -use crate::common::{Catch, Mania, Osu, Taiko}; - mod common; #[cfg(not(any(feature = "async_tokio", feature = "async_std")))] @@ -93,7 +91,7 @@ mod async_tokio { } } -fn assert_osu(map: Beatmap) { +fn assert_osu(map: &Beatmap) { assert_eq!(map.mode, GameMode::Osu); assert_eq!(map.version, 14); assert_eq!(map.n_circles, 307); @@ -114,7 +112,7 @@ fn assert_osu(map: Beatmap) { assert_eq!(map.breaks.len(), 1) } -fn assert_taiko(map: Beatmap) { +fn assert_taiko(map: &Beatmap) { assert_eq!(map.mode, GameMode::Taiko); assert_eq!(map.version, 14); assert_eq!(map.n_circles, 289); @@ -135,7 +133,7 @@ fn assert_taiko(map: Beatmap) { assert_eq!(map.breaks.len(), 0) } -fn assert_catch(map: Beatmap) { +fn assert_catch(map: &Beatmap) { assert_eq!(map.mode, GameMode::Catch); assert_eq!(map.version, 14); assert_eq!(map.n_circles, 249); @@ -156,7 +154,7 @@ fn assert_catch(map: Beatmap) { assert_eq!(map.breaks.len(), 0) } -fn assert_mania(map: Beatmap) { +fn assert_mania(map: &Beatmap) { assert_eq!(map.mode, GameMode::Mania); assert_eq!(map.version, 14); assert_eq!(map.n_circles, 473);