bug 2041: group alliances are lost.

added test coverage, too.
This commit is contained in:
Enno Rehling 2014-11-03 18:10:05 +01:00
parent d14a40b7e7
commit fecf3b04f8
4 changed files with 47 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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

View File

@ -1,14 +1,53 @@
#include <platform.h>
#include "types.h"
#include "ally.h"
#include "group.h"
#include "faction.h"
#include "unit.h"
#include "region.h"
#include <storage.h>
#include <binarystore.h>
#include <CuTest.h>
#include <tests.h>
#include <assert.h>
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;
}

View File

@ -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);
}