aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-09-28 19:02:38 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-09-28 19:02:38 +0200
commitf5e96da28e81fcb9b780d63b4b3bdf1d60a0468e (patch)
tree387faafdb5be4382772e8de8afee8106b3b8f053
parentc4f78f2dadf5de918a40dfd825940fadaf009b47 (diff)
downloadplanetwars.dev-f5e96da28e81fcb9b780d63b4b3bdf1d60a0468e.tar.xz
planetwars.dev-f5e96da28e81fcb9b780d63b4b3bdf1d60a0468e.zip
add basic combat tests
-rw-r--r--planetwars-rules/src/rules.rs83
1 files changed, 82 insertions, 1 deletions
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<usize>,
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);
+ }
+}