extract read_faction and test that it repairs bad names

This commit is contained in:
Enno Rehling 2016-11-12 00:47:25 +01:00
parent 710811131f
commit ec787743f0
3 changed files with 42 additions and 7 deletions

View File

@ -1348,7 +1348,7 @@ void _test_write_password(gamedata *data, const faction *f) {
* This function requires no context, can be called in any state. The
* faction may not already exist, however.
*/
faction *readfaction(struct gamedata * data)
faction *read_faction(struct gamedata * data)
{
ally **sfp;
int planes, n;
@ -1363,10 +1363,11 @@ faction *readfaction(struct gamedata * data)
f->no = n;
}
else {
f->allies = NULL; /* mem leak */
while (f->attribs)
f->allies = NULL; /* FIXME: mem leak */
while (f->attribs) {
a_remove(&f->attribs, f->attribs);
}
}
READ_INT(data->store, &f->subscription);
if (data->version >= SPELL_LEVEL_VERSION) {
@ -1499,7 +1500,7 @@ faction *readfaction(struct gamedata * data)
return f;
}
void writefaction(struct gamedata *data, const faction * f)
void write_faction(struct gamedata *data, const faction * f)
{
ally *sf;
ursprung *ur;
@ -1781,11 +1782,12 @@ int read_game(gamedata *data) {
READ_INT(store, &nread);
log_debug(" - Einzulesende Parteien: %d\n", nread);
fp = &factions;
while (*fp)
while (*fp) {
fp = &(*fp)->next;
}
while (--nread >= 0) {
faction *f = readfaction(data);
faction *f = read_faction(data);
*fp = f;
fp = &f->next;
@ -2010,7 +2012,7 @@ int write_game(gamedata *data) {
if (fval(f, FFL_NPC)) {
clear_npc_orders(f);
}
writefaction(data, f);
write_faction(data, f);
WRITE_SECTION(store);
}

View File

@ -60,6 +60,9 @@ extern "C" {
void write_unit(struct gamedata *data, const struct unit *u);
struct unit *read_unit(struct gamedata *data);
void write_faction(struct gamedata *data, const struct faction *f);
struct faction *read_faction(struct gamedata *data);
void write_building(struct gamedata *data, const struct building *b);
struct building *read_building(struct gamedata *data);

View File

@ -82,6 +82,35 @@ static void test_readwrite_unit(CuTest * tc)
test_cleanup();
}
static void test_readwrite_faction(CuTest * tc)
{
gamedata data;
storage store;
faction *f;
test_setup();
f = test_create_faction(0);
free(f->name);
f->name = _strdup(" Hodor ");
CuAssertStrEquals(tc, " Hodor ", f->name);
mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION);
write_faction(&data, f);
data.strm.api->rewind(data.strm.handle);
free_gamedata();
gamedata_init(&data, &store, RELEASE_VERSION);
f = read_faction(&data);
CuAssertPtrNotNull(tc, f);
CuAssertStrEquals(tc, "Hodor", f->name);
CuAssertPtrEquals(tc, 0, f->units);
factions = f;
mstream_done(&data.strm);
gamedata_done(&data);
test_cleanup();
}
static void test_readwrite_building(CuTest * tc)
{
gamedata data;
@ -397,6 +426,7 @@ CuSuite *get_save_suite(void)
SUITE_ADD_TEST(suite, test_readwrite_attrib);
SUITE_ADD_TEST(suite, test_readwrite_data);
SUITE_ADD_TEST(suite, test_readwrite_unit);
SUITE_ADD_TEST(suite, test_readwrite_faction);
SUITE_ADD_TEST(suite, test_readwrite_building);
SUITE_ADD_TEST(suite, test_readwrite_ship);
SUITE_ADD_TEST(suite, test_readwrite_dead_faction_createunit);