diff --git a/src/kernel/group.c b/src/kernel/group.c index 510c327ac..402ebe9a3 100755 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -48,7 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static group *ghash[GMAXHASH]; static int maxgid; -static group *new_group(faction * f, const char *name, int gid) +group *new_group(faction * f, const char *name, int gid) { group **gp = &f->groups; int index = gid % GMAXHASH; @@ -195,9 +195,10 @@ bool join_group(unit * u, const char *name) return true; } -void write_groups(struct storage *store, group * g) +void write_groups(struct storage *store, const faction * f) { - while (g) { + group *g; + for (g = f->groups; g; g=g->next) { ally *a; WRITE_INT(store, g->gid); WRITE_STR(store, g->name); @@ -210,7 +211,6 @@ void write_groups(struct storage *store, group * g) WRITE_INT(store, 0); a_write(store, g->attribs, g); WRITE_SECTION(store); - g = g->next; } WRITE_INT(store, 0); } @@ -243,7 +243,6 @@ void read_groups(struct storage *store, faction * f) if (!a->faction) ur_add(fid, &a->faction, resolve_faction); } - *pa = 0; a_read(store, &g->attribs, g); } } diff --git a/src/kernel/group.h b/src/kernel/group.h index 076761d98..e4492d4ac 100755 --- a/src/kernel/group.h +++ b/src/kernel/group.h @@ -40,8 +40,9 @@ extern "C" { extern void set_group(struct unit *u, struct group *g); extern struct group * get_group(const struct unit *u); extern void free_group(struct group *g); + struct group *new_group(struct faction * f, const char *name, int gid); - extern void write_groups(struct storage *data, struct group *g); + extern void write_groups(struct storage *data, const struct faction *f); extern void read_groups(struct storage *data, struct faction *f); #ifdef __cplusplus diff --git a/src/kernel/group.test.c b/src/kernel/group.test.c index c1bd01d59..d37a3ed41 100644 --- a/src/kernel/group.test.c +++ b/src/kernel/group.test.c @@ -1,14 +1,53 @@ #include #include "types.h" +#include "ally.h" #include "group.h" #include "faction.h" #include "unit.h" #include "region.h" +#include +#include #include #include #include +static void test_group_readwrite(CuTest * tc) +{ + faction * f; + group *g; + ally *al; + storage store; + FILE *F; + + F = fopen("test.dat", "wb"); + binstore_init(&store, F); + test_cleanup(); + test_create_world(); + f = test_create_faction(0); + g = new_group(f, "test", 42); + al = ally_add(&g->allies, f); + al->status = HELP_COMMAND; + write_groups(&store, f); + binstore_done(&store); + fclose(F); + + F = fopen("test.dat", "rb"); + binstore_init(&store, F); + f->groups = 0; + free_group(g); + read_groups(&store, f); + binstore_done(&store); + fclose(F); + + CuAssertPtrNotNull(tc, f->groups); + CuAssertPtrNotNull(tc, f->groups->allies); + CuAssertPtrEquals(tc, 0, f->groups->allies->next); + CuAssertPtrEquals(tc, f, f->groups->allies->faction); + CuAssertIntEquals(tc, HELP_COMMAND, f->groups->allies->status); + test_cleanup(); +} + static void test_group(CuTest * tc) { unit *u; @@ -40,6 +79,7 @@ CuSuite *get_group_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_group); + SUITE_ADD_TEST(suite, test_group_readwrite); return suite; } diff --git a/src/kernel/save.c b/src/kernel/save.c index 6b23bb544..4133f0971 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1439,7 +1439,7 @@ void writefaction(struct gamedata *data, const faction * f) } WRITE_INT(data->store, 0); WRITE_SECTION(data->store); - write_groups(data->store, f->groups); + write_groups(data->store, f); write_spellbook(f->spellbook, data->store); }