From 490782e75096ffb50ab2cb4326b79ab768748290 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 15 Jan 2015 16:40:19 +0100 Subject: [PATCH] add some simple tests for messages, prior to any refactorings. --- src/kernel/CMakeLists.txt | 1 + src/kernel/messages.c | 11 +++++----- src/kernel/messages.h | 1 + src/kernel/messages.test.c | 44 ++++++++++++++++++++++++++++++++++++++ src/test_eressea.c | 1 + src/util/message.c | 2 +- 6 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/kernel/messages.test.c diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index 9dbb73b49..bc2c20418 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -21,6 +21,7 @@ race.test.c spellbook.test.c curse.test.c jsonconf.test.c +messages.test.c ) SET(_FILES diff --git a/src/kernel/messages.c b/src/kernel/messages.c index f7297f832..46329d66b 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -17,7 +17,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. **/ #include -#include #include "messages.h" /* kernel includes */ @@ -140,7 +139,7 @@ struct message *msg_feedback(const struct unit *u, struct order *ord, message *msg_message(const char *name, const char *sig, ...) /* msg_message("oops_error", "unit region command", u, r, cmd) */ { - va_list marker; + va_list vargs; const message_type *mtype = mt_find(name); char paramname[64]; const char *ic = sig; @@ -155,7 +154,7 @@ message *msg_message(const char *name, const char *sig, ...) return NULL; } - va_start(marker, sig); + va_start(vargs, sig); while (*ic && !isalnum(*ic)) ic++; while (*ic) { @@ -172,9 +171,9 @@ message *msg_message(const char *name, const char *sig, ...) } if (i != mtype->nparameters) { if (mtype->types[i]->vtype == VAR_VOIDPTR) { - args[i].v = va_arg(marker, void *); + args[i].v = va_arg(vargs, void *); } else if (mtype->types[i]->vtype == VAR_INT) { - args[i].i = va_arg(marker, int); + args[i].i = va_arg(vargs, int); } else { assert(!"unknown variant type"); } @@ -185,7 +184,7 @@ message *msg_message(const char *name, const char *sig, ...) while (*ic && !isalnum(*ic)) ic++; } - va_end(marker); + va_end(vargs); return msg_create(mtype, args); } diff --git a/src/kernel/messages.h b/src/kernel/messages.h index 83c0f2613..b9c07b0c9 100644 --- a/src/kernel/messages.h +++ b/src/kernel/messages.h @@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif +#include #include struct faction; diff --git a/src/kernel/messages.test.c b/src/kernel/messages.test.c new file mode 100644 index 000000000..e237ab8de --- /dev/null +++ b/src/kernel/messages.test.c @@ -0,0 +1,44 @@ +#include +#include "messages.h" + +#include +#include + +void test_missing_message(CuTest *tc) { + message *msg; + msg = msg_message("unknown", "unit", NULL); + CuAssertPtrNotNull(tc, msg); + CuAssertPtrNotNull(tc, msg->type); + CuAssertStrEquals(tc, msg->type->name, "missing_message"); + msg_release(msg); +} + +void test_message(CuTest *tc) { + message *msg; +// const char * args[] = { } + message_type *mtype = mt_new("custom", NULL); + mt_register(mtype); + CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom")); + CuAssertIntEquals(tc, 0, mtype->nparameters); + CuAssertPtrEquals(tc, NULL, (void *)mtype->pnames); + CuAssertPtrEquals(tc, NULL, (void *)mtype->types); + msg = msg_message("custom", ""); + CuAssertPtrNotNull(tc, msg); + CuAssertIntEquals(tc, 1, msg->refcount); + CuAssertPtrEquals(tc, NULL, msg->parameters); + CuAssertPtrEquals(tc, mtype, (void *)msg->type); + + CuAssertPtrEquals(tc, msg, msg_addref(msg)); + CuAssertIntEquals(tc, 2, msg->refcount); + msg_release(msg); + CuAssertIntEquals(tc, 1, msg->refcount); + msg_release(msg); + test_cleanup(); +} + +CuSuite *get_messages_suite(void) { + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_missing_message); + SUITE_ADD_TEST(suite, test_message); + return suite; +} \ No newline at end of file diff --git a/src/test_eressea.c b/src/test_eressea.c index b8ff259b6..ca6ebf604 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -71,6 +71,7 @@ int RunAllTests(void) RUN_TESTS(suite, building); RUN_TESTS(suite, spell); RUN_TESTS(suite, ally); + RUN_TESTS(suite, messages); /* gamecode */ RUN_TESTS(suite, battle); RUN_TESTS(suite, economy); diff --git a/src/util/message.c b/src/util/message.c index 7e9c1a9f0..b557e4832 100644 --- a/src/util/message.c +++ b/src/util/message.c @@ -146,7 +146,7 @@ message *msg_create(const struct message_type *mtype, variant args[]) return NULL; } msg->type = mtype; - msg->parameters = (variant *) calloc(mtype->nparameters, sizeof(variant)); + msg->parameters = (variant *)mtype->nparameters ? calloc(mtype->nparameters, sizeof(variant)) : NULL; msg->refcount = 1; for (i = 0; i != mtype->nparameters; ++i) { msg->parameters[i] = copy_arg(mtype->types[i], args[i]);