From d78098886200473941a0fe0de991080adef16c72 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:04:11 +0100 Subject: [PATCH 1/7] valgrind the unit tests for good measure --- tests/write-reports.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/write-reports.sh b/tests/write-reports.sh index 775fdb622..c87059854 100755 --- a/tests/write-reports.sh +++ b/tests/write-reports.sh @@ -21,13 +21,16 @@ cd $ROOT/tests setup cleanup VALGRIND=`which valgrind` +TESTS=../Debug/eressea/test_eressea SERVER=../Debug/eressea/eressea if [ -n "$VALGRIND" ]; then SUPP=../share/ubuntu-12_04.supp -SERVER="$VALGRIND --suppressions=$SUPP --error-exitcode=1 --leak-check=no $SERVER" +VALGRIND="$VALGRIND --suppressions=$SUPP --error-exitcode=1 --leak-check=no" fi +echo "running $TESTS" +$VALGRIND $TESTS echo "running $SERVER" -$SERVER -t 184 ../scripts/reports.lua +$VALGRIND $SERVER -t 184 ../scripts/reports.lua [ -d reports ] || quit 4 "no reports directory created" CRFILE=184-zvto.cr grep -q PARTEI reports/$CRFILE || quit 1 "CR did not contain any factions" From d30154f3a528774efd54a8a676803d0898ead09c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:14:38 +0100 Subject: [PATCH 2/7] stick some assertions in natural_armor refactor it for readability eliminate a static memory leak reset num_races to zero at end of test --- src/battle.c | 33 +++++++++++++++++++++------------ src/kernel/race.c | 1 + 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/battle.c b/src/battle.c index 97afb3de4..f57e7b377 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1022,23 +1022,32 @@ static void vampirism(troop at, int damage) } } +#define MAXRACES 128 + static int natural_armor(unit * du) { - static int *bonus = 0; - int an = u_race(du)->armor; - if (bonus == 0) { - assert(num_races > 0); - bonus = calloc((size_t)num_races, sizeof(int)); + static int cookie = -1; + static int bonus[MAXRACES]; + const race *rc = u_race(du); + int index, an = rc->armor; + + assert(rc); + if (cookie!=global.cookie) { + cookie = global.cookie; + memset(bonus, 0, sizeof(bonus)); } - if (bonus[u_race(du)->index] == 0) { - bonus[u_race(du)->index] = - get_param_int(u_race(du)->parameters, "armor.stamina", -1); - if (bonus[u_race(du)->index] == 0) - bonus[u_race(du)->index] = -1; + assert(num_races < MAXRACES); + index = rc->index; + assert(index >= 0 && index < num_races); + if (bonus[index] == 0) { + bonus[index] = + get_param_int(rc->parameters, "armor.stamina", -1); + if (bonus[index] == 0) + bonus[index] = -1; } - if (bonus[u_race(du)->index] > 0) { + if (bonus[index] > 0) { int sk = effskill(du, SK_STAMINA, 0); - sk /= bonus[u_race(du)->index]; + sk /= bonus[index]; an += sk; } return an; diff --git a/src/kernel/race.c b/src/kernel/race.c index 191c4f27d..7d881fe7f 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -146,6 +146,7 @@ void free_races(void) { free(races); races = rc; } + num_races = 0; } static race *rc_find_i(const char *name) From bb0fd4d810432fde83a856f4b5119500f4c75550 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:20:37 +0100 Subject: [PATCH 3/7] Fehlende Initializierung, github #375 --- src/spells/flyingship.test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/spells/flyingship.test.c b/src/spells/flyingship.test.c index 00199e971..05d28ed8d 100644 --- a/src/spells/flyingship.test.c +++ b/src/spells/flyingship.test.c @@ -28,6 +28,7 @@ static void test_flyingship(CuTest * tc) par.param = &par_data_ptr; par_data.typ = SPP_SHIP; + par_data.flag = 0; test_cleanup(); test_create_world(); From 26b6ae4e3023db3b29b66d538eef36e562c940ed Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:14:38 +0100 Subject: [PATCH 4/7] github issue #374 stick some assertions in natural_armor refactor it for readability eliminate a static memory leak reset num_races to zero at end of test --- src/battle.c | 33 +++++++++++++++++++++------------ src/kernel/race.c | 1 + 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/battle.c b/src/battle.c index 97afb3de4..f57e7b377 100644 --- a/src/battle.c +++ b/src/battle.c @@ -1022,23 +1022,32 @@ static void vampirism(troop at, int damage) } } +#define MAXRACES 128 + static int natural_armor(unit * du) { - static int *bonus = 0; - int an = u_race(du)->armor; - if (bonus == 0) { - assert(num_races > 0); - bonus = calloc((size_t)num_races, sizeof(int)); + static int cookie = -1; + static int bonus[MAXRACES]; + const race *rc = u_race(du); + int index, an = rc->armor; + + assert(rc); + if (cookie!=global.cookie) { + cookie = global.cookie; + memset(bonus, 0, sizeof(bonus)); } - if (bonus[u_race(du)->index] == 0) { - bonus[u_race(du)->index] = - get_param_int(u_race(du)->parameters, "armor.stamina", -1); - if (bonus[u_race(du)->index] == 0) - bonus[u_race(du)->index] = -1; + assert(num_races < MAXRACES); + index = rc->index; + assert(index >= 0 && index < num_races); + if (bonus[index] == 0) { + bonus[index] = + get_param_int(rc->parameters, "armor.stamina", -1); + if (bonus[index] == 0) + bonus[index] = -1; } - if (bonus[u_race(du)->index] > 0) { + if (bonus[index] > 0) { int sk = effskill(du, SK_STAMINA, 0); - sk /= bonus[u_race(du)->index]; + sk /= bonus[index]; an += sk; } return an; diff --git a/src/kernel/race.c b/src/kernel/race.c index 191c4f27d..7d881fe7f 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -146,6 +146,7 @@ void free_races(void) { free(races); races = rc; } + num_races = 0; } static race *rc_find_i(const char *name) From 242efafd61efd2dc0ada33db021821b263adb4ed Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:20:37 +0100 Subject: [PATCH 5/7] Fehlende Initializierung, github #375 --- src/spells/flyingship.test.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/spells/flyingship.test.c b/src/spells/flyingship.test.c index 00199e971..05d28ed8d 100644 --- a/src/spells/flyingship.test.c +++ b/src/spells/flyingship.test.c @@ -28,6 +28,7 @@ static void test_flyingship(CuTest * tc) par.param = &par_data_ptr; par_data.typ = SPP_SHIP; + par_data.flag = 0; test_cleanup(); test_create_world(); From 7ef18b15337a73b234c5a018bc22e7d7d838ae6b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:45:41 +0100 Subject: [PATCH 6/7] call cleanup --- src/spells/flyingship.test.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/spells/flyingship.test.c b/src/spells/flyingship.test.c index 05d28ed8d..00894ab0e 100644 --- a/src/spells/flyingship.test.c +++ b/src/spells/flyingship.test.c @@ -26,13 +26,13 @@ static void test_flyingship(CuTest * tc) ship_type *shipType1, *shipType2; ship *sh1, *sh2; + test_cleanup(); + test_create_world(); + par.param = &par_data_ptr; par_data.typ = SPP_SHIP; par_data.flag = 0; - test_cleanup(); - test_create_world(); - r = findregion(0, 0); f = test_create_faction(test_create_race("human")); u = test_create_unit(f, r); @@ -58,6 +58,7 @@ static void test_flyingship(CuTest * tc) CuAssertTrue(tc, !flying_ship(sh2)); CuAssertIntEquals(tc, 0, sp_flying_ship(&co)); CuAssertTrue(tc, !flying_ship(sh2)); + test_cleanup(); } CuSuite *get_flyingship_suite(void) From e8a42b20dd312de28d42fb91b9183b2acd1d0d32 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 7 Nov 2015 18:47:55 +0100 Subject: [PATCH 7/7] don't use test_create_world --- src/spells/flyingship.test.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/spells/flyingship.test.c b/src/spells/flyingship.test.c index 00894ab0e..b3d03f01b 100644 --- a/src/spells/flyingship.test.c +++ b/src/spells/flyingship.test.c @@ -27,13 +27,12 @@ static void test_flyingship(CuTest * tc) ship *sh1, *sh2; test_cleanup(); - test_create_world(); par.param = &par_data_ptr; par_data.typ = SPP_SHIP; par_data.flag = 0; - r = findregion(0, 0); + r = test_create_region(0, 0, 0); f = test_create_faction(test_create_race("human")); u = test_create_unit(f, r);