write a test that proves the bug in turn 966

This commit is contained in:
Enno Rehling 2016-02-17 09:24:19 +01:00
parent 95092a8085
commit 325a0ccbf1
5 changed files with 50 additions and 7 deletions

View File

@ -153,8 +153,8 @@ static int tolua_unit_get_group(lua_State * L)
static int tolua_unit_set_group(lua_State * L) static int tolua_unit_set_group(lua_State * L)
{ {
unit *self = (unit *)tolua_tousertype(L, 1, 0); unit *self = (unit *)tolua_tousertype(L, 1, 0);
int result = join_group(self, tolua_tostring(L, 2, 0)); group *g = join_group(self, tolua_tostring(L, 2, 0));
lua_pushinteger(L, result); lua_pushboolean(L, g!=NULL);
return 1; return 1;
} }

View File

@ -179,7 +179,7 @@ void set_group(struct unit *u, struct group *g)
} }
} }
bool join_group(unit * u, const char *name) group *join_group(unit * u, const char *name)
{ {
group *g = NULL; group *g = NULL;
@ -192,7 +192,7 @@ bool join_group(unit * u, const char *name)
} }
set_group(u, g); set_group(u, g);
return true; return g;
} }
void write_groups(struct storage *store, const faction * f) void write_groups(struct storage *store, const faction * f)

View File

@ -36,7 +36,7 @@ extern "C" {
} group; } group;
extern struct attrib_type at_group; /* attribute for units assigned to a group */ extern struct attrib_type at_group; /* attribute for units assigned to a group */
extern bool join_group(struct unit *u, const char *name); extern struct group *join_group(struct unit *u, const char *name);
extern void set_group(struct unit *u, struct group *g); extern void set_group(struct unit *u, struct group *g);
extern struct group * get_group(const struct unit *u); extern struct group * get_group(const struct unit *u);
extern void free_group(struct group *g); extern void free_group(struct group *g);

View File

@ -68,8 +68,8 @@ static void test_group(CuTest * tc)
assert(r && f); assert(r && f);
u = test_create_unit(f, r); u = test_create_unit(f, r);
assert(u); assert(u);
CuAssertTrue(tc, join_group(u, "hodor")); CuAssertPtrNotNull(tc, (g = join_group(u, "hodor")));
CuAssertPtrNotNull(tc, (g = get_group(u))); CuAssertPtrEquals(tc, g, get_group(u));
CuAssertStrEquals(tc, "hodor", g->name); CuAssertStrEquals(tc, "hodor", g->name);
CuAssertIntEquals(tc, 1, g->members); CuAssertIntEquals(tc, 1, g->members);
set_group(u, 0); set_group(u, 0);

View File

@ -3,6 +3,8 @@
#include "save.h" #include "save.h"
#include "unit.h" #include "unit.h"
#include "group.h"
#include "ally.h"
#include "faction.h" #include "faction.h"
#include "version.h" #include "version.h"
#include <CuTest.h> #include <CuTest.h>
@ -61,10 +63,51 @@ static void test_readwrite_unit(CuTest * tc)
test_cleanup(); test_cleanup();
} }
static void test_readwrite_dead_faction(CuTest *tc) {
faction *f, *f2;
unit * u;
group *g;
ally *al;
int fno;
test_cleanup();
f = test_create_faction(0);
fno = f->no;
CuAssertPtrEquals(tc, f, factions);
CuAssertPtrEquals(tc, 0, f->next);
f2 = test_create_faction(0);
CuAssertPtrEquals(tc, f2, factions->next);
u = test_create_unit(f2, test_create_region(0, 0, 0));
CuAssertPtrNotNull(tc, u);
g = join_group(u, "group");
CuAssertPtrNotNull(tc, g);
al = ally_add(&g->allies, f);
CuAssertPtrNotNull(tc, al);
CuAssertPtrEquals(tc, f, factions);
destroyfaction(&factions);
CuAssertTrue(tc, !f->_alive);
CuAssertPtrEquals(tc, f2, factions);
writegame("test.dat");
free_gamedata();
f = f2 = NULL;
readgame("test.dat", false);
CuAssertPtrEquals(tc, 0, findfaction(fno));
f2 = factions;
CuAssertPtrNotNull(tc, f2);
u = f2->units;
CuAssertPtrNotNull(tc, u);
g = get_group(u);
CuAssertPtrNotNull(tc, g);
CuAssertPtrEquals(tc, 0, g->allies);
test_cleanup();
}
CuSuite *get_save_suite(void) CuSuite *get_save_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_readwrite_data); SUITE_ADD_TEST(suite, test_readwrite_data);
SUITE_ADD_TEST(suite, test_readwrite_unit); SUITE_ADD_TEST(suite, test_readwrite_unit);
SUITE_ADD_TEST(suite, test_readwrite_dead_faction);
return suite; return suite;
} }