add a happy test for mt_new.

refactor to not use strncpy.
This commit is contained in:
Enno Rehling 2017-01-06 20:54:57 +01:00
parent ce17d7f2d7
commit f9fbe60766
4 changed files with 42 additions and 10 deletions

View File

@ -65,6 +65,7 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(direction); ADD_SUITE(direction);
ADD_SUITE(skill); ADD_SUITE(skill);
ADD_SUITE(keyword); ADD_SUITE(keyword);
ADD_SUITE(message);
ADD_SUITE(order); ADD_SUITE(order);
ADD_SUITE(race); ADD_SUITE(race);
/* util */ /* util */

View File

@ -14,7 +14,7 @@ gamedata.test.c
language.test.c language.test.c
# lists.test.c # lists.test.c
# log.test.c # log.test.c
# message.test.c message.test.c
# nrmessage.test.c # nrmessage.test.c
parser.test.c parser.test.c
password.test.c password.test.c

View File

@ -87,19 +87,21 @@ message_type *mt_new(const char *name, const char *args[])
for (i = 0; args[i]; ++i) { for (i = 0; args[i]; ++i) {
const char *x = args[i]; const char *x = args[i];
const char *spos = strchr(x, ':'); const char *spos = strchr(x, ':');
if (spos == NULL) { struct arg_type *atype = NULL;
mtype->pnames[i] = _strdup(x); if (spos != NULL) {
mtype->types[i] = 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 { 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'; cp[spos - x] = '\0';
mtype->pnames[i] = cp; mtype->pnames[i] = cp;
mtype->types[i] = find_argtype(spos + 1); mtype->types[i] = atype;
if (mtype->types[i] == NULL) {
log_error("unknown argument type %s for message type %s\n", spos + 1, mtype->name);
}
assert(mtype->types[i]);
} }
} }
} }

29
src/util/message.test.c Normal file
View File

@ -0,0 +1,29 @@
#include <platform.h>
#include "message.h"
#include <CuTest.h>
#include <tests.h>
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;
}