From ab1376d21282b5803de32f26202bbc77845a0fdb Mon Sep 17 00:00:00 2001 From: Steffen Mecke Date: Tue, 12 May 2015 18:07:47 +0200 Subject: [PATCH 1/2] trying to test curse_write/read --- src/kernel/curse.test.c | 94 ++++++++++++++++++++++++++++++++++++++++- src/kernel/save.c | 1 + src/names.c | 3 +- 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/src/kernel/curse.test.c b/src/kernel/curse.test.c index 577d6551a..8737c2587 100644 --- a/src/kernel/curse.test.c +++ b/src/kernel/curse.test.c @@ -1,15 +1,27 @@ #include -#include "types.h" -#include "curse.h" +#include #include #include +#include #include #include +#include +#include +#include +#include +#include #include +#include "curse.h" + +#include +#include +#include + #include + static void test_curse(CuTest * tc) { attrib *attrs = NULL; @@ -83,6 +95,82 @@ static void test_bad_dreams(CuTest *tc) { test_cleanup(); } +static void test_memstream(CuTest *tc) { + storage store; + stream out = { 0 }; + char buf[1024]; + int val=0; + +#ifdef FILESTREAMTEST + FILE *F; + F = fopen("test.dat", "wb"); + fstream_init(&out, F); +#else + mstream_init(&out); +#endif + binstore_init(&store, &out); + store.handle.data = &out; + + WRITE_INT(&store, 999999); + WRITE_TOK(&store, "fortytwo"); + WRITE_INT(&store, 42); + +#ifdef FILESTREAMTEST + fstream_done(&out); + F = fopen("test.dat", "rb"); + fstream_init(&out, F); +#endif + out.api->rewind(out.handle); + READ_INT(&store, &val); + READ_TOK(&store, buf, 1024); + CuAssertIntEquals(tc, 999999, val); + CuAssertStrEquals(tc, "fortytwo", buf); + READ_INT(&store, &val); + CuAssertIntEquals(tc, 42, val); +} + +static void test_write_flag(CuTest *tc) { + curse_fixture fix; + storage store; + char buf[1024]; + stream out = { 0 }; + size_t len; +#ifdef FILESTREAMTEST + FILE *F; + F = fopen("test.dat", "wb"); + fstream_init(&out, F); +#else + mstream_init(&out); +#endif + binstore_init(&store, &out); + store.handle.data = &out; + + setup_curse(&fix, "gbdream"); + fix.c->flags = 42 | CURSE_ISNEW; + curse_write(fix.r->attribs, fix.r, &store); +#ifdef FILESTREAMTEST + fstream_done(&out); + F = fopen("test.dat", "rb"); + fstream_init(&out, F); +#endif + out.api->rewind(out.handle); + len = out.api->read(out.handle, buf, sizeof(buf)); + buf[len] = '\0'; + out.api->rewind(out.handle); + curse_read(fix.r->attribs, fix.r, &store); + CuAssertIntEquals(tc, 42 | CURSE_ISNEW, ((curse *) fix.r->attribs->data.v)->flags); + global.data_version = RELEASE_VERSION; + CuAssertIntEquals(tc, RELEASE_VERSION, global.data_version); + +#ifdef FILESTREAMTEST + fstream_done(&out); +#else + mstream_done(&out); +#endif + binstore_done(&store); + test_cleanup(); +} + CuSuite *get_curse_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -91,5 +179,7 @@ CuSuite *get_curse_suite(void) SUITE_ADD_TEST(suite, test_magicstreet_warning); SUITE_ADD_TEST(suite, test_good_dreams); SUITE_ADD_TEST(suite, test_bad_dreams); + SUITE_ADD_TEST(suite, test_memstream); + SUITE_ADD_TEST(suite, test_write_flag); return suite; } diff --git a/src/kernel/save.c b/src/kernel/save.c index d9a18f2b3..49a2325a4 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1724,6 +1724,7 @@ int writegame(const char *filename) gdata.store = &store; gdata.encoding = enc_gamedata; gdata.version = RELEASE_VERSION; + global.data_version = RELEASE_VERSION; n = STREAM_VERSION; fwrite(&gdata.version, sizeof(int), 1, F); fwrite(&n, sizeof(int), 1, F); diff --git a/src/names.c b/src/names.c index 6681877fd..df2e5e0a6 100644 --- a/src/names.c +++ b/src/names.c @@ -357,7 +357,8 @@ static const char *dracoid_name(const unit * u) static char name[NAMESIZE + 1]; // FIXME: static return value int mid_syllabels; - u = u; + /* ignore u */ + u = 0; /* Wieviele Mittelteile? */ mid_syllabels = rng_int() % 4; From 13a358005fe1f46e30194378f1cb287c1ec925b8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 31 May 2015 11:03:44 +0200 Subject: [PATCH 2/2] fix memstream-based tests, new storage submodule. --- src/kernel/curse.test.c | 28 ++-------------------------- src/util/bsdstring.c | 4 ---- storage | 2 +- 3 files changed, 3 insertions(+), 31 deletions(-) diff --git a/src/kernel/curse.test.c b/src/kernel/curse.test.c index 8737c2587..7f8bb4db2 100644 --- a/src/kernel/curse.test.c +++ b/src/kernel/curse.test.c @@ -101,13 +101,7 @@ static void test_memstream(CuTest *tc) { char buf[1024]; int val=0; -#ifdef FILESTREAMTEST - FILE *F; - F = fopen("test.dat", "wb"); - fstream_init(&out, F); -#else mstream_init(&out); -#endif binstore_init(&store, &out); store.handle.data = &out; @@ -115,11 +109,6 @@ static void test_memstream(CuTest *tc) { WRITE_TOK(&store, "fortytwo"); WRITE_INT(&store, 42); -#ifdef FILESTREAMTEST - fstream_done(&out); - F = fopen("test.dat", "rb"); - fstream_init(&out, F); -#endif out.api->rewind(out.handle); READ_INT(&store, &val); READ_TOK(&store, buf, 1024); @@ -127,6 +116,7 @@ static void test_memstream(CuTest *tc) { CuAssertStrEquals(tc, "fortytwo", buf); READ_INT(&store, &val); CuAssertIntEquals(tc, 42, val); + mstream_done(&out); } static void test_write_flag(CuTest *tc) { @@ -135,24 +125,14 @@ static void test_write_flag(CuTest *tc) { char buf[1024]; stream out = { 0 }; size_t len; -#ifdef FILESTREAMTEST - FILE *F; - F = fopen("test.dat", "wb"); - fstream_init(&out, F); -#else + mstream_init(&out); -#endif binstore_init(&store, &out); store.handle.data = &out; setup_curse(&fix, "gbdream"); fix.c->flags = 42 | CURSE_ISNEW; curse_write(fix.r->attribs, fix.r, &store); -#ifdef FILESTREAMTEST - fstream_done(&out); - F = fopen("test.dat", "rb"); - fstream_init(&out, F); -#endif out.api->rewind(out.handle); len = out.api->read(out.handle, buf, sizeof(buf)); buf[len] = '\0'; @@ -162,11 +142,7 @@ static void test_write_flag(CuTest *tc) { global.data_version = RELEASE_VERSION; CuAssertIntEquals(tc, RELEASE_VERSION, global.data_version); -#ifdef FILESTREAMTEST - fstream_done(&out); -#else mstream_done(&out); -#endif binstore_done(&store); test_cleanup(); } diff --git a/src/util/bsdstring.c b/src/util/bsdstring.c index c43847da1..652608483 100644 --- a/src/util/bsdstring.c +++ b/src/util/bsdstring.c @@ -12,10 +12,6 @@ int wrptr(char **ptr, size_t * size, size_t bytes) if (bytes == 0) { return 0; } - if (bytes < 0) { - *size = 0; - return EINVAL; - } if (bytes <= *size) { *ptr += bytes; *size -= bytes; diff --git a/storage b/storage index 48768e4be..2bcd3b1e6 160000 --- a/storage +++ b/storage @@ -1 +1 @@ -Subproject commit 48768e4bef7ff28365487e047d3b910127c716d0 +Subproject commit 2bcd3b1e64764321773672333bd133a61b35b840