void read_groups(struct storage *store, faction * f) { for (;;) { ally **pa; group *g; int gid; char buf[1024]; READ_INT(store, &gid); if (gid == 0) break; READ_STR(store, buf, sizeof(buf)); g = new_group(f, buf, gid); pa = &g->allies; for (;;) { ally *a; variant fid; READ_INT(store, &fid.i); if (fid.i <= 0) break; a = ally_add(pa, findfaction(fid.i)); READ_INT(store, &a->status); if (!a->faction) ur_add(fid, &a->faction, resolve_faction); } a_read(store, &g->attribs, g); } }
static void init_group(faction * f, group * g) { ally *a, **an; an = &g->allies; for (a = f->allies; a; a = a->next) if (a->faction) { ally *ga = ally_add(an, a->faction); ga->status = a->status; an = &ga->next; } }
static void test_group_readwrite(CuTest * tc) { faction * f; group *g; ally *al; int i; gamedata data; storage store; test_cleanup(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); f = test_create_faction(0); new_group(f, "NW", 42); g = new_group(f, "Egoisten", 43); key_set(&g->attribs, 44); al = ally_add(&g->allies, f); al->status = HELP_GIVE; write_groups(&store, f); WRITE_INT(&store, 47); free_group(f->groups); free_group(g); f->groups = 0; data.strm.api->rewind(data.strm.handle); read_groups(&data, f); READ_INT(&store, &i); mstream_done(&data.strm); gamedata_done(&data); CuAssertIntEquals(tc, 47, i); CuAssertPtrNotNull(tc, f->groups); CuAssertIntEquals(tc, 42, f->groups->gid); CuAssertStrEquals(tc, "NW", f->groups->name); CuAssertPtrNotNull(tc, f->groups->next); CuAssertIntEquals(tc, 43, f->groups->next->gid); CuAssertStrEquals(tc, "Egoisten", f->groups->next->name); CuAssertPtrEquals(tc, 0, f->groups->allies); g = f->groups->next; CuAssertTrue(tc, key_get(g->attribs, 44)); CuAssertPtrNotNull(tc, g->allies); CuAssertPtrEquals(tc, 0, g->allies->next); CuAssertPtrEquals(tc, f, g->allies->faction); CuAssertIntEquals(tc, HELP_GIVE, g->allies->status); test_cleanup(); }
static void test_group_readwrite_dead_faction(CuTest *tc) { gamedata data; storage store; 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); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_game(&data); free_gamedata(); f = f2 = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); mstream_done(&data.strm); gamedata_done(&data); 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(); }
static void test_destroyfaction_allies(CuTest *tc) { faction *f1, *f2; region *r; ally *al; test_cleanup(); r = test_create_region(0, 0, 0); f1 = test_create_faction(0); test_create_unit(f1, r); f2 = test_create_faction(0); al = ally_add(&f1->allies, f2); al->status = HELP_FIGHT; CuAssertIntEquals(tc, HELP_FIGHT, alliedgroup(0, f1, f2, f1->allies, HELP_ALL)); CuAssertPtrEquals(tc, f2, f1->next); destroyfaction(&f1->next); CuAssertIntEquals(tc, false, faction_alive(f2)); CuAssertIntEquals(tc, 0, alliedgroup(0, f1, f2, f1->allies, HELP_ALL)); test_cleanup(); }
static void test_group_readwrite(CuTest * tc) { faction * f; group *g; ally *al; storage store; FILE *F; stream strm; F = fopen("test.dat", "wb"); fstream_init(&strm, F); binstore_init(&store, &strm); 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_GIVE; write_groups(&store, f); binstore_done(&store); fstream_done(&strm); F = fopen("test.dat", "rb"); fstream_init(&strm, F); binstore_init(&store, &strm); f->groups = 0; free_group(g); read_groups(&store, f); binstore_done(&store); fstream_done(&strm); 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_GIVE, f->groups->allies->status); remove("test.dat"); test_cleanup(); }