fix reading and writing curses from test (crash)

fix read_game/write_game incomaptibility.
This commit is contained in:
Enno Rehling 2016-02-26 17:54:25 +01:00
parent ce867af34f
commit 3c272fd53f
6 changed files with 47 additions and 19 deletions

View File

@ -49,7 +49,7 @@ void game_done(void)
calendar_cleanup(); calendar_cleanup();
#endif #endif
free_functions(); free_functions();
free_curses(); curses_done();
kernel_done(); kernel_done();
} }

View File

@ -74,17 +74,19 @@ void c_clearflag(curse * c, unsigned int flags)
void chash(curse * c) void chash(curse * c)
{ {
curse *old = cursehash[c->no % MAXENTITYHASH]; int i = c->no % MAXENTITYHASH;
cursehash[c->no % MAXENTITYHASH] = c; c->nexthash = cursehash[i];
c->nexthash = old; cursehash[i] = c;
assert(c->nexthash != c);
} }
static void cunhash(curse * c) static void cunhash(curse * c)
{ {
curse **show; curse **show;
int i = c->no % MAXENTITYHASH;
for (show = &cursehash[c->no % MAXENTITYHASH]; *show; for (show = &cursehash[i]; *show;
show = &(*show)->nexthash) { show = &(*show)->nexthash) {
if ((*show)->no == c->no) if ((*show)->no == c->no)
break; break;
@ -195,6 +197,7 @@ int curse_read(attrib * a, void *owner, gamedata *data)
int flags; int flags;
float flt; float flt;
assert(!c->no);
READ_INT(store, &c->no); READ_INT(store, &c->no);
chash(c); chash(c);
READ_TOK(store, cursename, sizeof(cursename)); READ_TOK(store, cursename, sizeof(cursename));
@ -824,7 +827,7 @@ double destr_curse(curse * c, int cast_level, double force)
return force; return force;
} }
void free_curses(void) { void curses_done(void) {
int i; int i;
for (i = 0; i != MAXCTHASH; ++i) { for (i = 0; i != MAXCTHASH; ++i) {
ql_free(cursetypes[i]); ql_free(cursetypes[i]);

View File

@ -216,7 +216,7 @@ extern "C" {
int duration; /* Dauer der Verzauberung. Wird jede Runde vermindert */ int duration; /* Dauer der Verzauberung. Wird jede Runde vermindert */
} curse; } curse;
void free_curses(void); /* de-register all curse-types */ void curses_done(void); /* de-register all curse-types */
void curse_write(const struct attrib *a, const void *owner, void curse_write(const struct attrib *a, const void *owner,
struct storage *store); struct storage *store);

View File

@ -2,6 +2,7 @@
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/save.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/version.h> #include <kernel/version.h>
#include <util/attrib.h> #include <util/attrib.h>
@ -53,6 +54,11 @@ static void setup_curse(curse_fixture *fix, const char *name) {
fix->c = create_curse(fix->u, &fix->r->attribs, ct_find(name), 1.0, 1, 1.0, 0); fix->c = create_curse(fix->u, &fix->r->attribs, ct_find(name), 1.0, 1, 1.0, 0);
} }
static void cleanup_curse(curse_fixture *fix) {
// destroy_curse(fix->c);
test_cleanup();
}
static void test_magicstreet(CuTest *tc) { static void test_magicstreet(CuTest *tc) {
curse_fixture fix; curse_fixture fix;
message *msg; message *msg;
@ -61,7 +67,7 @@ static void test_magicstreet(CuTest *tc) {
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0); msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
CuAssertStrEquals(tc, "curseinfo::magicstreet", test_get_messagetype(msg)); CuAssertStrEquals(tc, "curseinfo::magicstreet", test_get_messagetype(msg));
msg_release(msg); msg_release(msg);
test_cleanup(); cleanup_curse(&fix);
} }
static void test_magicstreet_warning(CuTest *tc) { static void test_magicstreet_warning(CuTest *tc) {
@ -72,7 +78,7 @@ static void test_magicstreet_warning(CuTest *tc) {
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0); msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
CuAssertStrEquals(tc, "curseinfo::magicstreetwarn", test_get_messagetype(msg)); CuAssertStrEquals(tc, "curseinfo::magicstreetwarn", test_get_messagetype(msg));
msg_release(msg); msg_release(msg);
test_cleanup(); cleanup_curse(&fix);
} }
static void test_good_dreams(CuTest *tc) { static void test_good_dreams(CuTest *tc) {
@ -83,7 +89,7 @@ static void test_good_dreams(CuTest *tc) {
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0); msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
CuAssertStrEquals(tc, "curseinfo::gooddream", test_get_messagetype(msg)); CuAssertStrEquals(tc, "curseinfo::gooddream", test_get_messagetype(msg));
msg_release(msg); msg_release(msg);
test_cleanup(); cleanup_curse(&fix);
} }
static void test_bad_dreams(CuTest *tc) { static void test_bad_dreams(CuTest *tc) {
@ -94,7 +100,7 @@ static void test_bad_dreams(CuTest *tc) {
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0); msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
CuAssertStrEquals(tc, "curseinfo::baddream", test_get_messagetype(msg)); CuAssertStrEquals(tc, "curseinfo::baddream", test_get_messagetype(msg));
msg_release(msg); msg_release(msg);
test_cleanup(); cleanup_curse(&fix);
} }
static void test_memstream(CuTest *tc) { static void test_memstream(CuTest *tc) {
@ -125,20 +131,31 @@ static void test_write_flag(CuTest *tc) {
curse_fixture fix; curse_fixture fix;
gamedata data; gamedata data;
storage store; storage store;
region * r;
curse * c;
int uid;
mstream_init(&data.strm); mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION); gamedata_init(&data, &store, RELEASE_VERSION);
setup_curse(&fix, "gbdream"); setup_curse(&fix, "gbdream");
fix.c->flags = 42 | CURSE_ISNEW; c = fix.c;
curse_write(fix.r->attribs, fix.r, &store); r = fix.r;
uid = r->uid;
c->flags = CURSE_ISNEW;
write_game(&data);
free_gamedata();
data.strm.api->rewind(data.strm.handle); data.strm.api->rewind(data.strm.handle);
curse_read(fix.r->attribs, fix.r, &data); read_game(&data);
CuAssertIntEquals(tc, 42 | CURSE_ISNEW, ((curse *) fix.r->attribs->data.v)->flags); r = findregionbyid(uid);
CuAssertPtrNotNull(tc, r);
CuAssertPtrNotNull(tc, r->attribs);
c = (curse *)r->attribs->data.v;
CuAssertIntEquals(tc, CURSE_ISNEW, c->flags);
mstream_done(&data.strm); mstream_done(&data.strm);
gamedata_done(&data); gamedata_done(&data);
test_cleanup(); cleanup_curse(&fix);
} }
CuSuite *get_curse_suite(void) CuSuite *get_curse_suite(void)

View File

@ -1842,6 +1842,7 @@ int writegame(const char *filename)
fstream_init(&strm, F); fstream_init(&strm, F);
binstore_init(&store, &strm); binstore_init(&store, &strm);
WRITE_INT(&store, VERSION_BUILD);
n = write_game(&gdata); n = write_game(&gdata);
binstore_done(&store); binstore_done(&store);
fstream_done(&strm); fstream_done(&strm);
@ -1875,7 +1876,6 @@ int write_game(gamedata *data) {
/* globale Variablen */ /* globale Variablen */
assert(data->version <= MAX_VERSION && data->version >= MIN_VERSION); assert(data->version <= MAX_VERSION && data->version >= MIN_VERSION);
WRITE_INT(store, VERSION_BUILD);
WRITE_INT(store, game_id()); WRITE_INT(store, game_id());
WRITE_SECTION(store); WRITE_SECTION(store);

View File

@ -107,6 +107,11 @@ static void test_readwrite_dead_faction_group(CuTest *tc) {
group *g; group *g;
ally *al; ally *al;
int fno; int fno;
gamedata data;
storage store;
mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION);
test_cleanup(); test_cleanup();
f = test_create_faction(0); f = test_create_faction(0);
@ -126,10 +131,11 @@ static void test_readwrite_dead_faction_group(CuTest *tc) {
destroyfaction(&factions); destroyfaction(&factions);
CuAssertTrue(tc, !f->_alive); CuAssertTrue(tc, !f->_alive);
CuAssertPtrEquals(tc, f2, factions); CuAssertPtrEquals(tc, f2, factions);
writegame("test.dat"); write_game(&data);
free_gamedata(); free_gamedata();
f = f2 = NULL; f = f2 = NULL;
readgame("test.dat", false); data.strm.api->rewind(data.strm.handle);
read_game(&data);
CuAssertPtrEquals(tc, 0, findfaction(fno)); CuAssertPtrEquals(tc, 0, findfaction(fno));
f2 = factions; f2 = factions;
CuAssertPtrNotNull(tc, f2); CuAssertPtrNotNull(tc, f2);
@ -138,6 +144,8 @@ static void test_readwrite_dead_faction_group(CuTest *tc) {
g = get_group(u); g = get_group(u);
CuAssertPtrNotNull(tc, g); CuAssertPtrNotNull(tc, g);
CuAssertPtrEquals(tc, 0, g->allies); CuAssertPtrEquals(tc, 0, g->allies);
mstream_done(&data.strm);
gamedata_done(&data);
test_cleanup(); test_cleanup();
} }