added async_tokio feature

This commit is contained in:
MaxOhn
2021-02-23 19:50:44 +01:00
parent 8a3bb912b0
commit e7ac4d7401
7 changed files with 1132 additions and 601 deletions
+2
View File
@@ -1,5 +1,7 @@
## Upcoming
- Async beatmap parsing
# v0.1.1
- parse:
+20 -5
View File
@@ -8,20 +8,35 @@ readme = "README.md"
repository = "https://github.com/MaxOhn/rosu-pp"
documentation = "https://docs.rs/rosu-pp/"
description = "osu! difficulty and pp calculation for all modes"
keywords = ["osu", "pp", "stars", "async", "async-std"]
keywords = ["osu", "pp", "stars", "async"]
[features]
default = ["osu", "taiko", "fruits", "mania", "no_leniency", "async_std"]
default = ["osu", "taiko", "fruits", "mania", "no_leniency"]
# game modes
osu = []
taiko = []
fruits = []
mania = []
# osu!standard version
all_included = []
no_leniency = []
no_sliders_no_leniency = []
async_std = ["async-std"]
[dependencies]
async-std = { version = "1.9.0", optional = true }
# async version
async_std = ["async-std"]
async_tokio = ["tokio"]
[dependencies.async-std]
version = "1.9"
optional = true
[dependencies.tokio]
version = "1.2"
optional = true
default-features = false
features = ["io-util"]
[dev-dependencies.tokio]
features = ["fs", "rt-multi-thread"]
+840 -332
View File
File diff suppressed because it is too large Load Diff
+65 -63
View File
@@ -3,7 +3,6 @@
extern crate rosu_pp;
use rosu_pp::Beatmap;
use std::fs::File;
struct MapResult {
map_id: u32,
@@ -12,8 +11,28 @@ struct MapResult {
pp: f32,
}
macro_rules! assert_result {
($kind:expr => $result:expr, $margin:expr, $expected:ident, $map_id:ident, $mods:ident) => {
assert!(
($result - $expected).abs() < $margin * $expected,
"\n{kind}:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
kind = $kind,
calculated = $result,
expected = $expected,
margin = ($result - $expected).abs(),
allowed = $margin * $expected,
map = $map_id,
mods = $mods
);
};
}
#[cfg(not(any(feature = "async_std", feature = "async_tokio")))]
#[test]
fn fruits() {
fn fruits_sync() {
let star_margin = 0.0001;
let pp_margin = 0.0001;
@@ -25,7 +44,7 @@ fn fruits() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)) {
let file = match std::fs::File::open(format!("./maps/{}.osu", map_id)) {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
@@ -37,42 +56,50 @@ fn fruits() {
let result = rosu_pp::FruitsPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
}
#[cfg(feature = "async_tokio")]
#[test]
fn fruits_async_tokio() {
tokio::runtime::Runtime::new()
.expect("could not start runtime")
.block_on(async {
let star_margin = 0.0001;
let pp_margin = 0.0001;
for result in RESULTS {
let MapResult {
map_id,
mods,
stars,
pp,
} = result;
let file = match tokio::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::FruitsPP::new(&map).mods(*mods).calculate();
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
});
}
#[cfg(feature = "async_std")]
#[test]
fn fruits_async() {
use async_std::{fs::File, task};
task::block_on(async {
fn fruits_async_std() {
async_std::task::block_on(async {
let star_margin = 0.0001;
let pp_margin = 0.0001;
@@ -84,45 +111,20 @@ fn fruits_async() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)).await {
let file = match async_std::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse_async(file).await {
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::FruitsPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
})
}
+65 -63
View File
@@ -3,7 +3,6 @@
extern crate rosu_pp;
use rosu_pp::Beatmap;
use std::fs::File;
struct MapResult {
map_id: u32,
@@ -12,8 +11,28 @@ struct MapResult {
pp: f32,
}
macro_rules! assert_result {
($kind:expr => $result:expr, $margin:expr, $expected:ident, $map_id:ident, $mods:ident) => {
assert!(
($result - $expected).abs() < $margin * $expected,
"\n{kind}:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
kind = $kind,
calculated = $result,
expected = $expected,
margin = ($result - $expected).abs(),
allowed = $margin * $expected,
map = $map_id,
mods = $mods
);
};
}
#[cfg(not(any(feature = "async_std", feature = "async_tokio")))]
#[test]
fn mania() {
fn mania_sync() {
let star_margin = 0.00001;
let pp_margin = 0.00001;
@@ -25,7 +44,7 @@ fn mania() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)) {
let file = match std::fs::File::open(format!("./maps/{}.osu", map_id)) {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
@@ -37,42 +56,50 @@ fn mania() {
let result = rosu_pp::ManiaPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
}
#[cfg(feature = "async_tokio")]
#[test]
fn mania_async_tokio() {
tokio::runtime::Runtime::new()
.expect("could not start runtime")
.block_on(async {
let star_margin = 0.00001;
let pp_margin = 0.00001;
for result in RESULTS {
let MapResult {
map_id,
mods,
stars,
pp,
} = result;
let file = match tokio::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::ManiaPP::new(&map).mods(*mods).calculate();
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
});
}
#[cfg(feature = "async_std")]
#[test]
fn mania_async() {
use async_std::{fs::File, task};
task::block_on(async {
fn mania_async_std() {
async_std::task::block_on(async {
let star_margin = 0.00001;
let pp_margin = 0.00001;
@@ -84,45 +111,20 @@ fn mania_async() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)).await {
let file = match async_std::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse_async(file).await {
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::ManiaPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
})
}
+75 -75
View File
@@ -3,7 +3,6 @@
extern crate rosu_pp;
use rosu_pp::Beatmap;
use std::fs::File;
struct MapResult {
map_id: u32,
@@ -12,9 +11,27 @@ struct MapResult {
pp: f32,
}
#[test]
fn osu() {
let margin = if cfg!(feature = "no_sliders_no_leniency") {
macro_rules! assert_result {
($kind:expr => $result:expr, $margin:expr, $expected:ident, $map_id:ident, $mods:ident) => {
assert!(
($result - $expected).abs() < $margin * $expected,
"\n{kind}:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
kind = $kind,
calculated = $result,
expected = $expected,
margin = ($result - $expected).abs(),
allowed = $margin * $expected,
map = $map_id,
mods = $mods
);
};
}
fn margin() -> f32 {
if cfg!(feature = "no_sliders_no_leniency") {
0.0075
} else if cfg!(feature = "no_leniency") {
0.0025
@@ -22,7 +39,13 @@ fn osu() {
0.001
} else {
unreachable!()
};
}
}
#[cfg(not(any(feature = "async_std", feature = "async_tokio")))]
#[test]
fn osu_sync() {
let margin = margin();
let star_margin = margin;
let pp_margin = margin;
@@ -35,7 +58,7 @@ fn osu() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)) {
let file = match std::fs::File::open(format!("./maps/{}.osu", map_id)) {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
@@ -47,51 +70,53 @@ fn osu() {
let result = rosu_pp::OsuPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
}
#[cfg(feature = "async_tokio")]
#[test]
fn osu_async_tokio() {
tokio::runtime::Runtime::new()
.expect("could not start runtime")
.block_on(async {
let margin = margin();
let star_margin = margin;
let pp_margin = margin;
for result in RESULTS {
let MapResult {
map_id,
mods,
stars,
pp,
} = result;
let file = match tokio::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::OsuPP::new(&map).mods(*mods).calculate();
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
});
}
#[cfg(feature = "async_std")]
#[test]
fn osu_async() {
use async_std::{fs::File, task};
task::block_on(async {
let margin = if cfg!(feature = "no_sliders_no_leniency") {
0.0075
} else if cfg!(feature = "no_leniency") {
0.0025
} else if cfg!(feature = "all_included") {
0.001
} else {
unreachable!()
};
fn osu_async_std() {
async_std::task::block_on(async {
let margin = margin();
let star_margin = margin;
let pp_margin = margin;
@@ -104,45 +129,20 @@ fn osu_async() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)).await {
let file = match async_std::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse_async(file).await {
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::OsuPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
})
}
+65 -63
View File
@@ -3,7 +3,6 @@
extern crate rosu_pp;
use rosu_pp::Beatmap;
use std::fs::File;
struct MapResult {
map_id: u32,
@@ -12,8 +11,28 @@ struct MapResult {
pp: f32,
}
macro_rules! assert_result {
($kind:expr => $result:expr, $margin:expr, $expected:ident, $map_id:ident, $mods:ident) => {
assert!(
($result - $expected).abs() < $margin * $expected,
"\n{kind}:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
kind = $kind,
calculated = $result,
expected = $expected,
margin = ($result - $expected).abs(),
allowed = $margin * $expected,
map = $map_id,
mods = $mods
);
};
}
#[cfg(not(any(feature = "async_std", feature = "async_tokio")))]
#[test]
fn taiko() {
fn taiko_sync() {
let star_margin = 0.0001;
let pp_margin = 0.0001;
@@ -25,7 +44,7 @@ fn taiko() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)) {
let file = match std::fs::File::open(format!("./maps/{}.osu", map_id)) {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
@@ -37,42 +56,50 @@ fn taiko() {
let result = rosu_pp::TaikoPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
}
#[cfg(feature = "async_tokio")]
#[test]
fn taiko_async_tokio() {
tokio::runtime::Runtime::new()
.expect("could not start runtime")
.block_on(async {
let star_margin = 0.0001;
let pp_margin = 0.0001;
for result in RESULTS {
let MapResult {
map_id,
mods,
stars,
pp,
} = result;
let file = match tokio::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::TaikoPP::new(&map).mods(*mods).calculate();
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
});
}
#[cfg(feature = "async_std")]
#[test]
fn taiko_async() {
use async_std::{fs::File, task};
task::block_on(async {
fn taiko_async_std() {
async_std::task::block_on(async {
let star_margin = 0.0001;
let pp_margin = 0.0001;
@@ -84,45 +111,20 @@ fn taiko_async() {
pp,
} = result;
let file = match File::open(format!("./maps/{}.osu", map_id)).await {
let file = match async_std::fs::File::open(format!("./maps/{}.osu", map_id)).await {
Ok(file) => file,
Err(why) => panic!("Could not open file {}.osu: {}", map_id, why),
};
let map = match Beatmap::parse_async(file).await {
let map = match Beatmap::parse(file).await {
Ok(map) => map,
Err(why) => panic!("Error while parsing map {}: {}", map_id, why),
};
let result = rosu_pp::TaikoPP::new(&map).mods(*mods).calculate();
assert!(
(result.stars() - stars).abs() < star_margin * stars,
"\nStars:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.stars(),
expected = stars,
margin = (result.stars() - stars).abs(),
allowed = star_margin * stars,
map = map_id,
mods = mods
);
assert!(
(result.pp() - pp).abs() < pp_margin * pp,
"\nPP:\n\
Calculated: {calculated} | Expected: {expected}\n \
=> {margin} margin ({allowed} allowed)\n\
[map {map} | mods {mods}]\n",
calculated = result.pp(),
expected = pp,
margin = (result.pp() - pp).abs(),
allowed = pp_margin * pp,
map = map_id,
mods = mods
);
assert_result!("Stars" => result.stars(), star_margin, stars, map_id, mods);
assert_result!("PP" => result.pp(), pp_margin, pp, map_id, mods);
}
})
}