Skip to content

Commit

Permalink
test: parse maps only once
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxOhn committed Nov 10, 2023
1 parent f8c246b commit 10a407c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
74 changes: 64 additions & 10 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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<M: Mode>() -> 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<M: Mode>() -> Beatmap {
let path = format!("./maps/{}.osu", M::TEST_MAP_ID);
fn path(mode: GameMode) -> String {
let map_id = match mode {
GameMode::Osu => <Osu as Mode>::TEST_MAP_ID,
GameMode::Taiko => <Taiko as Mode>::TEST_MAP_ID,
GameMode::Catch => <Catch as Mode>::TEST_MAP_ID,
GameMode::Mania => <Mania as Mode>::TEST_MAP_ID,
};

Beatmap::from_path(path).await.unwrap()
format!("./maps/{map_id}.osu")
}
10 changes: 4 additions & 6 deletions tests/parse.rs
Original file line number Diff line number Diff line change
@@ -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")))]
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 10a407c

Please sign in to comment.