fixed memory leaks in tests, thanks to valgrind

This commit is contained in:
Enno Rehling 2014-12-30 23:34:24 +01:00
parent a55cb13924
commit 9f09b9b915
9 changed files with 34 additions and 5 deletions

View File

@ -45,6 +45,7 @@ static void test_export_no_regions(CuTest * tc) {
buf[len] = '\0'; buf[len] = '\0';
CuAssertStrEquals(tc, "{}", strip(buf)); CuAssertStrEquals(tc, "{}", strip(buf));
mstream_done(&out); mstream_done(&out);
test_cleanup();
} }
static cJSON *export_a_region(CuTest * tc, const struct terrain_type *terrain, region **_r) { static cJSON *export_a_region(CuTest * tc, const struct terrain_type *terrain, region **_r) {
@ -93,6 +94,7 @@ static void test_export_land_region(CuTest * tc) {
CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(json, "name")); CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(json, "name"));
CuAssertStrEquals(tc, r->land->name, attr->valuestring); CuAssertStrEquals(tc, r->land->name, attr->valuestring);
cJSON_Delete(json); cJSON_Delete(json);
test_cleanup();
} }
static void test_export_ocean_region(CuTest * tc) { static void test_export_ocean_region(CuTest * tc) {
@ -103,6 +105,7 @@ static void test_export_ocean_region(CuTest * tc) {
json = export_a_region(tc, terrain, 0); json = export_a_region(tc, terrain, 0);
CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "name")); CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "name"));
cJSON_Delete(json); cJSON_Delete(json);
test_cleanup();
} }
static void test_export_no_factions(CuTest * tc) { static void test_export_no_factions(CuTest * tc) {
@ -119,6 +122,7 @@ static void test_export_no_factions(CuTest * tc) {
buf[len]=0; buf[len]=0;
CuAssertStrEquals(tc, "{}", strip(buf)); CuAssertStrEquals(tc, "{}", strip(buf));
mstream_done(&out); mstream_done(&out);
test_cleanup();
} }
CuSuite *get_json_suite(void) { CuSuite *get_json_suite(void) {

View File

@ -57,6 +57,7 @@ static void test_build_requires_materials(CuTest *tc) {
i_change(&u->items, itype, 2); i_change(&u->items, itype, 2);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1)); CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1));
CuAssertIntEquals(tc, 1, i_get(u->items, itype)); CuAssertIntEquals(tc, 1, i_get(u->items, itype));
test_cleanup();
} }
static void test_build_requires_building(CuTest *tc) { static void test_build_requires_building(CuTest *tc) {
@ -78,6 +79,7 @@ static void test_build_requires_building(CuTest *tc) {
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1)); CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1));
btype->maxcapacity = 0; btype->maxcapacity = 0;
CuAssertIntEquals_Msg(tc, "cannot build when production building capacity exceeded", EBUILDINGREQ, build(u, &bf.cons, 0, 1)); CuAssertIntEquals_Msg(tc, "cannot build when production building capacity exceeded", EBUILDINGREQ, build(u, &bf.cons, 0, 1));
test_cleanup();
} }
static void test_build_failure_missing_skill(CuTest *tc) { static void test_build_failure_missing_skill(CuTest *tc) {
@ -89,6 +91,7 @@ static void test_build_failure_missing_skill(CuTest *tc) {
rtype = bf.cons.materials[0].rtype; rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1); i_change(&u->items, rtype->itype, 1);
CuAssertIntEquals(tc, ENEEDSKILL, build(u, &bf.cons, 1, 1)); CuAssertIntEquals(tc, ENEEDSKILL, build(u, &bf.cons, 1, 1));
test_cleanup();
} }
static void test_build_failure_low_skill(CuTest *tc) { static void test_build_failure_low_skill(CuTest *tc) {
@ -101,6 +104,7 @@ static void test_build_failure_low_skill(CuTest *tc) {
i_change(&u->items, rtype->itype, 1); i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill-1); set_level(u, SK_ARMORER, bf.cons.minskill-1);
CuAssertIntEquals(tc, ELOWSKILL, build(u, &bf.cons, 0, 10)); CuAssertIntEquals(tc, ELOWSKILL, build(u, &bf.cons, 0, 10));
test_cleanup();
} }
static void test_build_failure_completed(CuTest *tc) { static void test_build_failure_completed(CuTest *tc) {
@ -115,6 +119,7 @@ static void test_build_failure_completed(CuTest *tc) {
bf.cons.maxsize = 1; bf.cons.maxsize = 1;
CuAssertIntEquals(tc, ECOMPLETE, build(u, &bf.cons, bf.cons.maxsize, 10)); CuAssertIntEquals(tc, ECOMPLETE, build(u, &bf.cons, bf.cons.maxsize, 10));
CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype)); CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype));
test_cleanup();
} }
static void test_build_limits(CuTest *tc) { static void test_build_limits(CuTest *tc) {
@ -202,6 +207,7 @@ static void test_build_building_no_materials(CuTest *tc) {
CuAssertIntEquals(tc, ENOMATERIALS, build_building(u, btype, 0, 4, 0)); CuAssertIntEquals(tc, ENOMATERIALS, build_building(u, btype, 0, 4, 0));
CuAssertPtrEquals(tc, 0, u->region->buildings); CuAssertPtrEquals(tc, 0, u->region->buildings);
CuAssertPtrEquals(tc, 0, u->building); CuAssertPtrEquals(tc, 0, u->building);
test_cleanup();
} }
static void test_build_building_with_golem(CuTest *tc) { static void test_build_building_with_golem(CuTest *tc) {
@ -220,6 +226,7 @@ static void test_build_building_with_golem(CuTest *tc) {
CuAssertPtrNotNull(tc, u->region->buildings); CuAssertPtrNotNull(tc, u->region->buildings);
CuAssertIntEquals(tc, 1, u->region->buildings->size); CuAssertIntEquals(tc, 1, u->region->buildings->size);
CuAssertIntEquals(tc, 0, u->number); CuAssertIntEquals(tc, 0, u->number);
test_cleanup();
} }
static void test_build_building_success(CuTest *tc) { static void test_build_building_success(CuTest *tc) {
@ -243,6 +250,7 @@ static void test_build_building_success(CuTest *tc) {
CuAssertPtrEquals(tc, u->region->buildings, u->building); CuAssertPtrEquals(tc, u->region->buildings, u->building);
CuAssertIntEquals(tc, 1, u->building->size); CuAssertIntEquals(tc, 1, u->building->size);
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype)); CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
test_cleanup();
} }
CuSuite *get_build_suite(void) CuSuite *get_build_suite(void)

View File

@ -92,8 +92,14 @@ void bt_register(building_type * type)
ql_push(&buildingtypes, (void *)type); ql_push(&buildingtypes, (void *)type);
} }
void free_buildingtype(void *ptr) {
building_type *btype =(building_type *)ptr;
free(btype->_name);
free(btype);
}
void free_buildingtypes(void) { void free_buildingtypes(void) {
ql_foreach(buildingtypes, free); ql_foreach(buildingtypes, free_buildingtype);
ql_free(buildingtypes); ql_free(buildingtypes);
buildingtypes = 0; buildingtypes = 0;
} }

View File

@ -49,7 +49,7 @@ extern "C" {
#define BTF_NAMECHANGE 0x100 /* name and description can be changed more than once */ #define BTF_NAMECHANGE 0x100 /* name and description can be changed more than once */
typedef struct building_type { typedef struct building_type {
const char *_name; char *_name;
int flags; /* flags */ int flags; /* flags */
int capacity; /* Kapazität pro Größenpunkt */ int capacity; /* Kapazität pro Größenpunkt */

View File

@ -22,6 +22,8 @@ static void test_register_building(CuTest * tc)
bt_register(btype); bt_register(btype);
CuAssertPtrNotNull(tc, bt_find("herp")); CuAssertPtrNotNull(tc, bt_find("herp"));
// free(btype->_name);
// free(btype);
test_cleanup(); test_cleanup();
} }

View File

@ -238,7 +238,8 @@ void free_ship(ship * s)
free(s); free(s);
} }
static void free_shiptype(ship_type *stype) { static void free_shiptype(void *ptr) {
ship_type *stype = (ship_type *)ptr;
free(stype->_name); free(stype->_name);
free(stype); free(stype);
} }

View File

@ -17,6 +17,7 @@
#include <tests.h> #include <tests.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
static void test_market_curse(CuTest * tc) static void test_market_curse(CuTest * tc)
{ {
@ -45,7 +46,7 @@ static void test_market_curse(CuTest * tc)
set_param(&global.parameters, "rules.region_owners", "1"); set_param(&global.parameters, "rules.region_owners", "1");
btype = (building_type *)calloc(1, sizeof(building_type)); btype = (building_type *)calloc(1, sizeof(building_type));
btype->_name = "market"; btype->_name = _strdup("market");
bt_register(btype); bt_register(btype);
terrain = get_terrain("plain"); terrain = get_terrain("plain");

View File

@ -3,6 +3,7 @@
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/terrain.h>
#include <util/language.h> #include <util/language.h>
@ -45,6 +46,8 @@ static void test_recreate_world(CuTest * tc)
CuAssertPtrEquals(tc, default_locale, get_locale("de")); CuAssertPtrEquals(tc, default_locale, get_locale("de"));
CuAssertPtrNotNull(tc, default_locale); CuAssertPtrNotNull(tc, default_locale);
CuAssertPtrNotNull(tc, findregion(0, 0)); CuAssertPtrNotNull(tc, findregion(0, 0));
CuAssertPtrNotNull(tc, get_terrain("plain"));
CuAssertPtrNotNull(tc, get_terrain("ocean"));
CuAssertPtrNotNull(tc, (void *)rt_find("horse")); CuAssertPtrNotNull(tc, (void *)rt_find("horse"));
CuAssertPtrNotNull(tc, get_resourcetype(R_HORSE)); CuAssertPtrNotNull(tc, get_resourcetype(R_HORSE));
CuAssertPtrNotNull(tc, (void *)rt_find("money")); CuAssertPtrNotNull(tc, (void *)rt_find("money"));
@ -56,6 +59,8 @@ static void test_recreate_world(CuTest * tc)
test_cleanup(); test_cleanup();
CuAssertPtrEquals(tc, 0, get_locale("de")); CuAssertPtrEquals(tc, 0, get_locale("de"));
CuAssertPtrEquals(tc, 0, (void*)get_terrain("plain"));
CuAssertPtrEquals(tc, 0, (void*)get_terrain("ocean"));
CuAssertPtrEquals(tc, 0, (void*)rt_find("horse")); CuAssertPtrEquals(tc, 0, (void*)rt_find("horse"));
CuAssertPtrEquals(tc, 0, (void*)get_resourcetype(R_HORSE)); CuAssertPtrEquals(tc, 0, (void*)get_resourcetype(R_HORSE));
CuAssertPtrNotNull(tc, (void *)rt_find("money")); CuAssertPtrNotNull(tc, (void *)rt_find("money"));

View File

@ -61,7 +61,9 @@ static void a_initdirection(attrib * a)
static void a_freedirection(attrib * a) static void a_freedirection(attrib * a)
{ {
free(a->data.v); spec_direction *d = (spec_direction *)(a->data.v);
free(d->desc);
free(d);
} }
static int a_agedirection(attrib * a) static int a_agedirection(attrib * a)