rewrite intermittent test to use memory stream.

This commit is contained in:
Enno Rehling 2016-02-12 23:29:47 +01:00
parent 6ca81646eb
commit ec3839fb9f
3 changed files with 30 additions and 51 deletions

View File

@ -12,6 +12,7 @@
#include "plane.h" #include "plane.h"
#include "region.h" #include "region.h"
#include "version.h" #include "version.h"
#include <triggers/changefaction.h> #include <triggers/changefaction.h>
#include <triggers/createunit.h> #include <triggers/createunit.h>
#include <triggers/timeout.h> #include <triggers/timeout.h>
@ -20,6 +21,8 @@
#include <util/base36.h> #include <util/base36.h>
#include <util/password.h> #include <util/password.h>
#include <storage.h>
#include <memstream.h>
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
@ -78,29 +81,28 @@ static void test_readwrite_unit(CuTest * tc)
} }
static void test_readwrite_attrib(CuTest *tc) { static void test_readwrite_attrib(CuTest *tc) {
gamedata *data; gamedata data;
storage store;
attrib *a = NULL; attrib *a = NULL;
const char *path = "attrib.dat";
test_cleanup(); test_cleanup();
global.data_version = RELEASE_VERSION; // FIXME: hack!
data = gamedata_open(path, "wb", RELEASE_VERSION);
CuAssertPtrNotNull(tc, data);
key_set(&a, 41); key_set(&a, 41);
key_set(&a, 42); key_set(&a, 42);
write_attribs(data->store, a, NULL); mstream_init(&data.strm);
gamedata_close(data); gamedata_init(&data, &store, RELEASE_VERSION);
global.data_version = RELEASE_VERSION; // FIXME: hack!
write_attribs(data.store, a, NULL);
a_removeall(&a, NULL); a_removeall(&a, NULL);
CuAssertPtrEquals(tc, 0, a); CuAssertPtrEquals(tc, 0, a);
data = gamedata_open(path, "rb", RELEASE_VERSION); data.strm.api->rewind(data.strm.handle);
CuAssertPtrNotNull(tc, data); read_attribs(data.store, &a, NULL);
read_attribs(data->store, &a, NULL); gamedata_done(&data);
gamedata_close(data); mstream_done(&data.strm);
CuAssertTrue(tc, key_get(a, 41)); CuAssertTrue(tc, key_get(a, 41));
CuAssertTrue(tc, key_get(a, 42)); CuAssertTrue(tc, key_get(a, 42));
a_removeall(&a, NULL); a_removeall(&a, NULL);
CuAssertIntEquals(tc, 0, remove(path));
test_cleanup(); test_cleanup();
} }

View File

@ -12,9 +12,8 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
void gamedata_close(gamedata *data) { void gamedata_done(gamedata *data) {
binstore_done(data->store); binstore_done(data->store);
fstream_done(&data->strm);
} }
void gamedata_init(gamedata *data, storage *store, int version) { void gamedata_init(gamedata *data, storage *store, int version) {
@ -23,6 +22,11 @@ void gamedata_init(gamedata *data, storage *store, int version) {
binstore_init(data->store, &data->strm); binstore_init(data->store, &data->strm);
} }
void gamedata_close(gamedata *data) {
gamedata_done(data);
fstream_done(&data->strm);
}
int gamedata_openfile(gamedata *data, const char *filename, const char *mode, int version) { int gamedata_openfile(gamedata *data, const char *filename, const char *mode, int version) {
FILE *F = fopen(filename, mode); FILE *F = fopen(filename, mode);
if (F) { if (F) {
@ -44,6 +48,7 @@ int gamedata_openfile(gamedata *data, const char *filename, const char *mode, in
fwrite(&n, sizeof(int), 1, F); fwrite(&n, sizeof(int), 1, F);
} }
if (err) { if (err) {
log_error("could not open %s: %s", filename, strerror(errno));
fclose(F); fclose(F);
free(data); free(data);
} }
@ -58,41 +63,10 @@ int gamedata_openfile(gamedata *data, const char *filename, const char *mode, in
} }
gamedata *gamedata_open(const char *filename, const char *mode, int version) { gamedata *gamedata_open(const char *filename, const char *mode, int version) {
FILE *F = fopen(filename, mode);
if (F) {
gamedata *data = (gamedata *)calloc(1, sizeof(gamedata)); gamedata *data = (gamedata *)calloc(1, sizeof(gamedata));
storage *store = (storage *)calloc(1, sizeof(storage)); if (gamedata_openfile(data, filename, mode, version) != 0) {
int err = 0;
size_t sz;
data->store = store;
if (strchr(mode, 'r')) {
sz = fread(&data->version, 1, sizeof(int), F);
if (sz != sizeof(int)) {
err = ferror(F);
}
else {
err = fseek(F, sizeof(int), SEEK_CUR);
}
}
else if (strchr(mode, 'w')) {
int n = STREAM_VERSION;
data->version = version;
fwrite(&data->version, sizeof(int), 1, F);
fwrite(&n, sizeof(int), 1, F);
}
if (err) {
fclose(F);
free(data); free(data);
free(store); return NULL;
} }
else {
fstream_init(&data->strm, F);
binstore_init(store, &data->strm);
return data;
}
}
log_error("could not open %s: %s", filename, strerror(errno));
return 0; return 0;
} }

View File

@ -14,8 +14,11 @@ typedef struct gamedata {
int encoding; int encoding;
} gamedata; } gamedata;
void gamedata_close(struct gamedata *data); void gamedata_init(gamedata *data, struct storage *store, int version);
struct gamedata *gamedata_open(const char *filename, const char *mode, int version); void gamedata_done(gamedata *data);
void gamedata_close(gamedata *data);
gamedata *gamedata_open(const char *filename, const char *mode, int version);
int gamedata_openfile(gamedata *data, const char *filename, const char *mode, int version); int gamedata_openfile(gamedata *data, const char *filename, const char *mode, int version);
#define STREAM_VERSION 2 /* internal encoding of binary files */ #define STREAM_VERSION 2 /* internal encoding of binary files */