From 66be2185f989a0a04a236e1ce60b0f13f0ff07f0 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 24 Apr 2025 12:58:06 -0400 Subject: [PATCH] add tests for MoveValueStats --- src/logic/mvs.rs | 69 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/src/logic/mvs.rs b/src/logic/mvs.rs index b58afcd..8e63221 100644 --- a/src/logic/mvs.rs +++ b/src/logic/mvs.rs @@ -17,6 +17,36 @@ pub struct MoveValueStats { } impl MoveValueStats { + #[cfg(test)] + pub const fn new_from_wins_losses(wins: u16, losses: u16) -> Self { + Self { + state: None, + wins, + losses, + value: 0, + } + } + + #[cfg(test)] + pub const fn new_from_value(value: i32) -> Self { + Self { + state: None, + wins: 0, + losses: 0, + value, + } + } + + #[cfg(test)] + pub const fn new_from_state(state: Option) -> Self { + Self { + state, + wins: 0, + losses: 0, + value: 0, + } + } + fn chance_win(&self) -> Option { let sum = self.losses + self.wins; if sum == 0 { @@ -55,16 +85,39 @@ impl Ord for MoveValueStats { return self.state.cmp(&other.state); } - if let Some(s_cw) = self.chance_win() { - if let Some(o_cw) = other.chance_win() { - if s_cw > o_cw { - return Ordering::Greater; - } else if o_cw > s_cw { - return Ordering::Less; - } - } + let (s_cw, o_cw) = (self.chance_win(), other.chance_win()); + if s_cw > o_cw { + return Ordering::Greater; + } else if o_cw > s_cw { + return Ordering::Less; } self.value.cmp(&other.value) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn two_prob() { + let one = MoveValueStats::new_from_wins_losses(10, 4); + let two = MoveValueStats::new_from_wins_losses(4, 6); + assert!(one > two); + } + + #[test] + fn one_prob_one_non() { + let one = MoveValueStats::new_from_wins_losses(10, 4); + let two = MoveValueStats::new_from_value(10); + assert!(one > two); + } + + #[test] + fn one_prob_one_win() { + let one = MoveValueStats::new_from_wins_losses(10, 4); + let two = MoveValueStats::new_from_state(Some(MVSGameState::Win)); + assert!(one < two); + } +}