From ec787743f00fecef6dc12b8d1531dfc00eedb555 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 12 Nov 2016 00:47:25 +0100 Subject: [PATCH] extract read_faction and test that it repairs bad names --- src/kernel/save.c | 16 +++++++++------- src/kernel/save.h | 3 +++ src/kernel/save.test.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/kernel/save.c b/src/kernel/save.c index d063fc8f7..96263feed 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -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,9 +1363,10 @@ 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); @@ -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); } diff --git a/src/kernel/save.h b/src/kernel/save.h index d74017f62..7c28d6c43 100644 --- a/src/kernel/save.h +++ b/src/kernel/save.h @@ -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); diff --git a/src/kernel/save.test.c b/src/kernel/save.test.c index 4f13851cb..298b9e21f 100644 --- a/src/kernel/save.test.c +++ b/src/kernel/save.test.c @@ -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);