Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2004:
[Freeciv-Dev] (PR#10318) [Patch] fc_assert()
Home

[Freeciv-Dev] (PR#10318) [Patch] fc_assert()

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#10318) [Patch] fc_assert()
From: "Marko Lindqvist" <marko.lindqvist@xxxxxxxxxxx>
Date: Sat, 25 Sep 2004 03:02:55 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10318 >


  In order to get more information out from asserts I need wrapper 
around them. I first tried something like this...

#include <assert.h>
#undef assert
#define assert(x) fc_assert(x)

  ...but it doesn't work as <assert.h> has no guards against multiple 
inclusion and will replace my wrapper with normal assert again.

  So I really have to use my wrapper with its real name 'fc_assert()'. 
This means that I have to maintain huge patches for my own testings. If 
freeciv proper would change its asserts into fc_asserts, changing assert 
behavior would require touching only that one place; fc_assert() 
implementation.

  First attached patch, fcassert.diff, is one possible implementation 
for fc_assert(). Second patch, sanitycheck_fcassert.diff, changes 
asserts in sanitycheck.c into fc_asserts.


  - Caz


diff -Nurd -X.diff_ignore freeciv/utility/Makefile.am 
freeciv/utility/Makefile.am
--- freeciv/utility/Makefile.am 2004-09-24 22:50:12.546875000 +0300
+++ freeciv/utility/Makefile.am 2004-09-25 12:55:47.468750000 +0300
@@ -17,6 +17,8 @@
                fciconv.h       \
                distribute.c    \
                distribute.h    \
+               fcassert.c      \
+               fcassert.h      \
                fcintl.c        \
                fcintl.h        \
                genlist.c       \
diff -Nurd -X.diff_ignore freeciv/utility/fcassert.c freeciv/utility/fcassert.c
--- freeciv/utility/fcassert.c  1970-01-01 02:00:00.000000000 +0200
+++ freeciv/utility/fcassert.c  2004-09-25 12:59:51.093750000 +0300
@@ -0,0 +1,28 @@
+/********************************************************************** 
+   Copyright (C) 2004 - Freeciv team
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif /* HAVE_CONFIG_H */
+
+#include "fcassert.h"
+#include "log.h"
+
+#ifdef DEBUG
+
+void failing_assert(const char *check, const char *filename, int line)
+{
+  freelog(LOG_FATAL, "Assert \"%s\" at %s:%d should fail.", check, filename, 
line);
+}
+
+#endif /* DEBUG */
diff -Nurd -X.diff_ignore freeciv/utility/fcassert.h freeciv/utility/fcassert.h
--- freeciv/utility/fcassert.h  1970-01-01 02:00:00.000000000 +0200
+++ freeciv/utility/fcassert.h  2004-09-25 12:59:09.546875000 +0300
@@ -0,0 +1,35 @@
+/********************************************************************** 
+   Copyright (C) 2004 - Freeciv team
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+***********************************************************************/
+#ifndef H_FCASSERT
+#define H_FCASSERT
+
+#include <assert.h>
+
+#ifdef DEBUG
+
+void failing_assert(const char *check, const char *filename, int line);
+
+#define fc_assert(x)                                                     \
+  { if (! (x)) {                                                         \
+      failing_assert( #x, __FILE__, __LINE__ );                          \
+    }                                                                    \
+    assert(x);                                                           \
+  }
+
+#else  /* DEBUG */
+
+#define fc_assert(x) assert(x)
+
+#endif /* DEBUG */
+#endif /* H_FCASSERT */
diff -Nurd -X.diff_ignore freeciv/server/sanitycheck.c 
freeciv/server/sanitycheck.c
--- freeciv/server/sanitycheck.c        2004-09-24 22:50:12.531250000 +0300
+++ freeciv/server/sanitycheck.c        2004-09-25 12:32:50.750000000 +0300
@@ -18,6 +18,7 @@
 #include <assert.h>
 
 #include "city.h"
+#include "fcassert.h"
 #include "game.h"
 #include "log.h"
 #include "map.h"
@@ -42,18 +43,18 @@
     enum tile_special_type special = map_get_special(x, y);
 
     if (contains_special(special, S_RAILROAD))
-      assert(contains_special(special, S_ROAD));
+      fc_assert(contains_special(special, S_ROAD));
     if (contains_special(special, S_FARMLAND))
-      assert(contains_special(special, S_IRRIGATION));
+      fc_assert(contains_special(special, S_IRRIGATION));
     if (contains_special(special, S_SPECIAL_1))
-      assert(!contains_special(special,  S_SPECIAL_2));
+      fc_assert(!contains_special(special,  S_SPECIAL_2));
 
     if (contains_special(special, S_MINE))
-      assert(get_tile_type(terrain)->mining_result == terrain);
+      fc_assert(get_tile_type(terrain)->mining_result == terrain);
     if (contains_special(special, S_IRRIGATION))
-      assert(get_tile_type(terrain)->irrigation_result == terrain);
+      fc_assert(get_tile_type(terrain)->irrigation_result == terrain);
 
-    assert(terrain >= T_FIRST && terrain < T_COUNT);
+    fc_assert(terrain >= T_FIRST && terrain < T_COUNT);
   } whole_map_iterate_end;
 }
 
@@ -66,13 +67,13 @@
     players_iterate(pplayer) {
       struct player_tile *plr_tile = map_get_player_tile(x, y, pplayer);
       /* underflow of unsigned int */
-      assert(plr_tile->seen < 60000);
-      assert(plr_tile->own_seen < 60000);
-      assert(plr_tile->pending_seen < 60000);
+      fc_assert(plr_tile->seen < 60000);
+      fc_assert(plr_tile->own_seen < 60000);
+      fc_assert(plr_tile->pending_seen < 60000);
 
-      assert(plr_tile->own_seen <= plr_tile->seen);
+      fc_assert(plr_tile->own_seen <= plr_tile->seen);
       if (map_is_known(x, y, pplayer)) {
-       assert(plr_tile->pending_seen == 0);
+       fc_assert(plr_tile->pending_seen == 0);
       }
     } players_iterate_end;
   } whole_map_iterate_end;
@@ -89,9 +90,9 @@
       nbarbs++;
     }
   } players_iterate_end;
-  assert(nbarbs == game.nbarbarians);
+  fc_assert(nbarbs == game.nbarbarians);
 
-  assert(game.nplayers <= MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS);
+  fc_assert(game.nplayers <= MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS);
 }
 
 /**************************************************************************
@@ -105,34 +106,34 @@
     int cont = map_get_continent(x, y);
 
     if (is_ocean(map_get_terrain(x, y))) {
-      assert(cont < 0);
+      fc_assert(cont < 0);
       adjc_iterate(x, y, x1, y1) {
        if (is_ocean(map_get_terrain(x1, y1))) {
-         assert(map_get_continent(x1, y1) == cont);
+         fc_assert(map_get_continent(x1, y1) == cont);
        }
       } adjc_iterate_end;
     } else {
-      assert(cont > 0);
+      fc_assert(cont > 0);
       adjc_iterate(x, y, x1, y1) {
        if (!is_ocean(map_get_terrain(x1, y1))) {
-         assert(map_get_continent(x1, y1) == cont);
+         fc_assert(map_get_continent(x1, y1) == cont);
        }
       } adjc_iterate_end;
     }
 
     if (pcity) {
-      assert(same_pos(pcity->x, pcity->y, x, y));
+      fc_assert(same_pos(pcity->x, pcity->y, x, y));
     }
 
     unit_list_iterate(ptile->units, punit) {
-      assert(same_pos(punit->x, punit->y, x, y));
+      fc_assert(same_pos(punit->x, punit->y, x, y));
 
       /* Check diplomatic status of stacked units. */
       unit_list_iterate(ptile->units, punit2) {
-       assert(pplayers_allied(unit_owner(punit), unit_owner(punit2)));
+       fc_assert(pplayers_allied(unit_owner(punit), unit_owner(punit2)));
       } unit_list_iterate_end;
       if (pcity) {
-       assert(pplayers_allied(unit_owner(punit), city_owner(pcity)));
+       fc_assert(pplayers_allied(unit_owner(punit), city_owner(pcity)));
       }
     } unit_list_iterate_end;
   } whole_map_iterate_end;
@@ -146,14 +147,14 @@
   int workers = 0;
   struct player *pplayer = city_owner(pcity);
 
-  assert(pcity->size >= 1);
-  assert(is_normal_map_pos(pcity->x, pcity->y));
-  assert(!terrain_has_flag(map_get_terrain(pcity->x, pcity->y),
+  fc_assert(pcity->size >= 1);
+  fc_assert(is_normal_map_pos(pcity->x, pcity->y));
+  fc_assert(!terrain_has_flag(map_get_terrain(pcity->x, pcity->y),
                           TER_NO_CITIES));
 
   unit_list_iterate(pcity->units_supported, punit) {
-    assert(punit->homecity == pcity->id);
-    assert(unit_owner(punit) == pplayer);
+    fc_assert(punit->homecity == pcity->id);
+    fc_assert(unit_owner(punit) == pplayer);
   } unit_list_iterate_end;
 
   /* Note that cities may be found on land or water. */
@@ -224,7 +225,7 @@
        break;
       }
     } else {
-      assert(get_worker_city(pcity, x, y) == C_TILE_UNAVAILABLE);
+      fc_assert(get_worker_city(pcity, x, y) == C_TILE_UNAVAILABLE);
     }
   } city_map_iterate_end;
 
@@ -248,7 +249,7 @@
 {
   players_iterate(pplayer) {
     city_list_iterate(pplayer->cities, pcity) {
-      assert(city_owner(pcity) == pplayer);
+      fc_assert(city_owner(pcity) == pplayer);
 
       sanity_check_city(pcity);
     } city_list_iterate_end;
@@ -262,7 +263,7 @@
       bool is_valid;
 
       is_valid = map_to_city_map(&city_x, &city_y, pcity, x, y);
-      assert(is_valid);
+      fc_assert(is_valid);
 
       if (pcity->city_map[city_x][city_y] != C_TILE_WORKER) {
        freelog(LOG_ERROR, "%d,%d is listed as being worked by %s "
@@ -285,13 +286,13 @@
       int y = punit->y;
       struct city *pcity;
 
-      assert(unit_owner(punit) == pplayer);
+      fc_assert(unit_owner(punit) == pplayer);
       CHECK_MAP_POS(x, y);
 
       if (punit->homecity != 0) {
        pcity = player_find_city_by_id(pplayer, punit->homecity);
-       assert(pcity != NULL);
-       assert(city_owner(pcity) == pplayer);
+       fc_assert(pcity != NULL);
+       fc_assert(city_owner(pcity) == pplayer);
       }
 
       if (!can_unit_continue_current_activity(punit)) {
@@ -304,24 +305,24 @@
 
       pcity = map_get_city(x, y);
       if (pcity) {
-       assert(pplayers_allied(city_owner(pcity), pplayer));
+       fc_assert(pplayers_allied(city_owner(pcity), pplayer));
       } else if (is_ocean(map_get_terrain(x, y))) {
-       assert(ground_unit_transporter_capacity(x, y, pplayer) >= 0);
+       fc_assert(ground_unit_transporter_capacity(x, y, pplayer) >= 0);
       }
 
-      assert(punit->moves_left >= 0);
-      assert(punit->hp > 0);
+      fc_assert(punit->moves_left >= 0);
+      fc_assert(punit->hp > 0);
 
       /* Check for ground units in the ocean. */
       if (!pcity
          && is_ocean(map_get_terrain(punit->x, punit->y))
          && is_ground_unit(punit)) {
-       assert(punit->transported_by != -1);
-       assert(!is_ground_unit(find_unit_by_id(punit->transported_by)));
+       fc_assert(punit->transported_by != -1);
+       fc_assert(!is_ground_unit(find_unit_by_id(punit->transported_by)));
       }
 
       /* Check for over-full transports. */
-      assert(get_transporter_occupancy(punit)
+      fc_assert(get_transporter_occupancy(punit)
             <= get_transporter_capacity(punit));
     } unit_list_iterate_end;
   } players_iterate_end;
@@ -341,14 +342,14 @@
       if (is_capital(pcity)) {
        found_palace++;
       }
-      assert(found_palace <= 1);
+      fc_assert(found_palace <= 1);
     } city_list_iterate_end;
 
     players_iterate(pplayer2) {
-      assert(pplayer->diplstates[pplayer2->player_no].type
+      fc_assert(pplayer->diplstates[pplayer2->player_no].type
             == pplayer2->diplstates[pplayer->player_no].type);
       if (pplayer->diplstates[pplayer2->player_no].type == DS_CEASEFIRE)
-       assert(pplayer->diplstates[pplayer2->player_no].turns_left
+       fc_assert(pplayer->diplstates[pplayer2->player_no].turns_left
               == pplayer2->diplstates[pplayer->player_no].turns_left);
     } players_iterate_end;
 
@@ -357,9 +358,9 @@
         freelog(LOG_FATAL, "%s's government is anarchy but does not finish",
                 pplayer->name);
       }
-      assert(pplayer->government != game.government_when_anarchy);
+      fc_assert(pplayer->government != game.government_when_anarchy);
     } else if (pplayer->revolution_finishes > game.turn) {
-      assert(pplayer->government == game.government_when_anarchy);
+      fc_assert(pplayer->government == game.government_when_anarchy);
     } else {
       /* Things may vary in this case depending on when the sanity_check
        * call is made.  No better check is possible. */
@@ -372,12 +373,12 @@
 
     if (!pplayer->is_alive) {
       /* Dead players' units and cities are disbanded in kill_player(). */
-      assert(unit_list_size(&pplayer->units) == 0);
-      assert(city_list_size(&pplayer->cities) == 0);
+      fc_assert(unit_list_size(&pplayer->units) == 0);
+      fc_assert(city_list_size(&pplayer->cities) == 0);
     }
 
     /* Dying players shouldn't be left around.  But they are. */
-    assert(!pplayer->is_dying);
+    fc_assert(!pplayer->is_dying);
   }
 }
 

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#10318) [Patch] fc_assert(), Marko Lindqvist <=