chiark / gitweb /
hidden: Add a consistency check function
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Mar 2021 19:02:21 +0000 (19:02 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 13 Mar 2021 19:58:01 +0000 (19:58 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/hidden.rs

index bdb8ce989c87770e44c7b8024131359c803ffde5..becb108322ab3e83acc30174c008279112cd0a75 100644 (file)
@@ -208,6 +208,7 @@ mod vpid {
   type NotchPtr = Option<Notch>;
 
   #[derive(Clone,Copy,Debug,Serialize,Deserialize)]
+  #[derive(Eq,PartialEq)]
   enum NotchRecord {
     Free(NotchPtr),
     Piece(PieceId),
@@ -496,9 +497,42 @@ mod vpid {
     occ.notches.table = new_notches;
     dbgc!(&occ);
   }
+
+  #[cfg(debug_assertions)]
+  pub fn consistency_check(
+    gplayers: &GPlayers,
+    gpieces: &GPieces,
+    goccults: &GameOccults,
+  ) {
+    for (_player, gpl) in gplayers.iter() {
+      for (piece, &vpid) in gpl.idmap.f.iter() {
+        if let Some(&rpiece) = gpl.idmap.r.get(vpid) {
+          assert_eq!(piece, rpiece);
+        }
+      }
+      for (vpid, &piece) in gpl.idmap.r.iter() {
+        if let Some(&fvpid) = gpl.idmap.f.get(piece) {
+          assert_eq!(vpid, fvpid);
+        }
+      }
+    }
+    
+    for (piece, gpc) in gpieces.iter() {
+      if let Some(occid) = gpc.occult.active {
+        let occ = goccults.occults.get(occid).unwrap();
+        assert_eq!(occ.occulter, piece);
+        assert_eq!(&gpc.occult.passive, &None);
+      }
+      
+      if let Some(Passive { occid, notch }) = gpc.occult.passive {
+        let occ = goccults.occults.get(occid).unwrap();
+        assert_eq!(occ.notches.table[notch], NR::Piece(piece));
+      }
+    }
+  }
 }
 
-pub use vpid::{PerPlayerIdMap, NotchNumber, Notch, Notches};
+pub use vpid::{PerPlayerIdMap, NotchNumber, Notch, Notches, consistency_check};
 
 // ========== public entrypoints ==========
 
@@ -894,6 +928,10 @@ mod recompute {
         }
       }
 
+      if cfg!(debug_assertions) {
+        consistency_check(gplayers, gpieces, goccults);
+      }
+
       Implemented { }
     }
   }