From f5e96da28e81fcb9b780d63b4b3bdf1d60a0468e Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 28 Sep 2022 19:02:38 +0200 Subject: [PATCH] add basic combat tests --- planetwars-rules/src/rules.rs | 83 ++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/planetwars-rules/src/rules.rs b/planetwars-rules/src/rules.rs index e339b4e..b4571f3 100644 --- a/planetwars-rules/src/rules.rs +++ b/planetwars-rules/src/rules.rs @@ -17,7 +17,7 @@ pub struct Player { pub alive: bool, } -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub struct Fleet { pub owner: Option, pub ship_count: u64, @@ -190,3 +190,84 @@ impl Planet { (dx.powi(2) + dy.powi(2)).sqrt().ceil() as u64 } } + +#[cfg(test)] +mod tests { + use super::*; + + fn get_test_planet() -> Planet { + Planet { + id: 0, + name: "test_planet".to_string(), + x: 0.0, + y: 0.0, + fleets: Vec::new(), + } + } + + #[test] + fn test_planet_basic_combat() { + let mut p = get_test_planet(); + p.orbit(Fleet { + owner: Some(0), + ship_count: 3, + }); + + p.orbit(Fleet { + owner: Some(1), + ship_count: 2, + }); + + p.resolve_combat(); + + assert_eq!(p.fleets.len(), 1); + assert_eq!(p.owner(), Some(0)); + assert_eq!(p.ship_count(), 1); + } + + #[test] + fn test_planet_combat_threeway() { + let mut p = get_test_planet(); + p.orbit(Fleet { + owner: Some(0), + ship_count: 5, + }); + p.orbit(Fleet { + owner: Some(1), + ship_count: 10, + }); + p.orbit(Fleet { + owner: Some(2), + ship_count: 12, + }); + + p.resolve_combat(); + + assert_eq!(p.fleets.len(), 1); + assert_eq!(p.owner(), Some(2)); + assert_eq!(p.ship_count(), 2); + } + + #[test] + fn test_planet_combat_threeway_tie() { + let mut p = get_test_planet(); + p.orbit(Fleet { + owner: Some(0), + ship_count: 5, + }); + p.orbit(Fleet { + owner: Some(1), + ship_count: 10, + }); + p.orbit(Fleet { + owner: Some(2), + ship_count: 10, + }); + + p.resolve_combat(); + + assert_eq!(p.fleets.len(), 0); + assert_eq!(p.owner(), None); + assert_eq!(p.ship_count(), 0); + } +}