From f9fbe607663052f47a2179b89f6c96ca716995c3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 6 Jan 2017 20:54:57 +0100 Subject: [PATCH] add a happy test for mt_new. refactor to not use strncpy. --- src/test_eressea.c | 1 + src/util/CMakeLists.txt | 2 +- src/util/message.c | 20 +++++++++++--------- src/util/message.test.c | 29 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/util/message.test.c diff --git a/src/test_eressea.c b/src/test_eressea.c index 71cf008f2..6d2dead3f 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -65,6 +65,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(direction); ADD_SUITE(skill); ADD_SUITE(keyword); + ADD_SUITE(message); ADD_SUITE(order); ADD_SUITE(race); /* util */ diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 7eda87132..842bcf58c 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -14,7 +14,7 @@ gamedata.test.c language.test.c # lists.test.c # log.test.c -# message.test.c +message.test.c # nrmessage.test.c parser.test.c password.test.c diff --git a/src/util/message.c b/src/util/message.c index f19fc3313..5b6f12eb8 100644 --- a/src/util/message.c +++ b/src/util/message.c @@ -87,19 +87,21 @@ message_type *mt_new(const char *name, const char *args[]) for (i = 0; args[i]; ++i) { const char *x = args[i]; const char *spos = strchr(x, ':'); - if (spos == NULL) { - mtype->pnames[i] = _strdup(x); - mtype->types[i] = NULL; + struct arg_type *atype = NULL; + if (spos != NULL) { + atype = find_argtype(spos + 1); + } + if (!atype) { + log_error("unknown argument type %s for message type %s\n", spos + 1, mtype->name); + assert(atype); } else { - char *cp = strncpy((char *)malloc(spos - x + 1), x, spos - x); + char *cp; + cp = malloc(spos - x + 1); + memcpy(cp, x, spos - x); cp[spos - x] = '\0'; mtype->pnames[i] = cp; - mtype->types[i] = find_argtype(spos + 1); - if (mtype->types[i] == NULL) { - log_error("unknown argument type %s for message type %s\n", spos + 1, mtype->name); - } - assert(mtype->types[i]); + mtype->types[i] = atype; } } } diff --git a/src/util/message.test.c b/src/util/message.test.c new file mode 100644 index 000000000..d114e33f2 --- /dev/null +++ b/src/util/message.test.c @@ -0,0 +1,29 @@ +#include +#include "message.h" + +#include +#include + +static void test_mt_new(CuTest *tc) +{ + message_type *mt; + test_setup(); + mt = mt_new_va("test", "name:string", "number:int", NULL); + CuAssertPtrNotNull(tc, mt); + CuAssertStrEquals(tc, "test", mt->name); + CuAssertIntEquals(tc, 2, mt->nparameters); + CuAssertPtrNotNull(tc, mt->pnames); + CuAssertStrEquals(tc, "name", mt->pnames[0]); + CuAssertStrEquals(tc, "number", mt->pnames[1]); + CuAssertPtrNotNull(tc, mt->types); + CuAssertStrEquals(tc, "string", mt->types[0]->name); + CuAssertStrEquals(tc, "int", mt->types[1]->name); + test_cleanup(); +} + +CuSuite *get_message_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_mt_new); + return suite; +}