From b1142c12d9e25827fcd94bf3b00a36a5b664a680 Mon Sep 17 00:00:00 2001 From: RumovZ Date: Mon, 15 Nov 2021 06:41:43 +0100 Subject: [PATCH] Fix `constrained_fuzz_bounds()` (#1490) ... for cases where the entire fuzz range is above `maximum`. Also improve hanling if the entire range is below `minimum` and readability. --- rslib/src/scheduler/states/mod.rs | 14 ++++++++++---- rslib/src/scheduler/states/review.rs | 9 +++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/rslib/src/scheduler/states/mod.rs b/rslib/src/scheduler/states/mod.rs index 831db55c9..3bd977183 100644 --- a/rslib/src/scheduler/states/mod.rs +++ b/rslib/src/scheduler/states/mod.rs @@ -151,12 +151,18 @@ impl<'a> StateContext<'a> { /// Ensure the upper bound is larger than the lower bound, if `maximum` allows /// it and it is larger than 1. fn constrained_fuzz_bounds(interval: f32, minimum: u32, maximum: u32) -> (u32, u32) { - let (lower, mut upper) = fuzz_bounds(interval); - let lower = lower.max(minimum); - if upper == lower && upper != 1 { + let (mut lower, mut upper) = fuzz_bounds(interval); + + // minimum <= maximum and lower <= upper are assumed + // now ensure minimum <= lower <= upper <= maximum + lower = lower.max(minimum).min(maximum); + upper = upper.max(minimum).min(maximum); + + if upper == lower && upper > 1 && upper < maximum { upper = lower + 1; }; - (lower, upper.min(maximum).max(lower)) + + (lower, upper) } fn fuzz_bounds(interval: f32) -> (u32, u32) { diff --git a/rslib/src/scheduler/states/review.rs b/rslib/src/scheduler/states/review.rs index 2aa6774b2..986fec254 100644 --- a/rslib/src/scheduler/states/review.rs +++ b/rslib/src/scheduler/states/review.rs @@ -255,7 +255,7 @@ mod test { } #[test] - fn low_multiplier_fuzz() { + fn extreme_multiplier_fuzz() { let mut ctx = StateContext::defaults_for_testing(); // our calculations should work correctly with a low ease or non-default multiplier let state = ReviewState { @@ -272,6 +272,11 @@ mod test { ctx.interval_multiplier = 0.1; assert_eq!(state.passing_review_intervals(&ctx), (2, 3, 4)); ctx.fuzz_factor = Some(0.99); - assert_eq!(state.passing_review_intervals(&ctx), (2, 3, 4)); + assert_eq!(state.passing_review_intervals(&ctx), (3, 5, 7)); + + // maximum must be respected no matter what + ctx.interval_multiplier = 10.0; + ctx.maximum_review_interval = 5; + assert_eq!(state.passing_review_intervals(&ctx), (5, 5, 5)); } }