[gnome-nibbles] Regularly assert each worm is the proper length



commit 70ec5ddb8c5fcf0588663e3aa583fbc1a8c0dd88
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sat Jul 19 18:17:33 2014 -0500

    Regularly assert each worm is the proper length
    
    No bug can withstand the awesome power of invariants! Yeah!
    
    https://bugzilla.gnome.org/show_bug.cgi?id=733429

 src/board.c    |   20 ++++++++++++++++++++
 src/board.h    |    1 +
 src/gnibbles.c |    5 +++++
 3 files changed, 26 insertions(+), 0 deletions(-)
---
diff --git a/src/board.c b/src/board.c
index d261ab0..2e8d67d 100644
--- a/src/board.c
+++ b/src/board.c
@@ -440,3 +440,23 @@ gnibbles_board_level_add_bonus (GnibblesBoard *board, gint regular)
     }
   }
 }
+
+/* Gasp, it's an invariant. The count of the worm's segments
+ * found on the board needs to equal its length. Or else bad. */
+gint
+gnibbles_board_count_worm_segments_for_worm (GnibblesBoard *board, GnibblesWorm *worm) {
+  gint i, j;
+  gint count = 0;
+
+  for (i = 0; i < BOARDHEIGHT; i++) {
+    for (j = 0; j < BOARDWIDTH; j++) {
+      if (board->walls[j][i] == worm->number + WORMCHAR)
+        count++;
+    }
+  }
+
+  if (count > 0)
+    g_assert_cmpint (count, ==, worm->length);
+
+  return count;
+}
diff --git a/src/board.h b/src/board.h
index ea522a6..3b5897d 100644
--- a/src/board.h
+++ b/src/board.h
@@ -46,5 +46,6 @@ GnibblesBoard* gnibbles_board_new (void);
 void gnibbles_board_rescale (GnibblesBoard *board, gint tilesize);
 void gnibbles_board_level_new (GnibblesBoard *board, gint level);
 void gnibbles_board_level_add_bonus (GnibblesBoard *board, gint regular);
+gint gnibbles_board_count_worm_segments_for_worm (GnibblesBoard *board, GnibblesWorm *worm);
 
 #endif
diff --git a/src/gnibbles.c b/src/gnibbles.c
index cf17edd..d0283cd 100644
--- a/src/gnibbles.c
+++ b/src/gnibbles.c
@@ -291,6 +291,11 @@ gnibbles_move_worms (void)
   }
    /* Noone died, so the round can continue. */
 
+  for (i = 0; i < properties->numworms; i++) {
+    /* Verify that worm length is as expected */
+    (void) gnibbles_board_count_worm_segments_for_worm (board, worms[i]);
+  }
+
   return CONTINUE;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]