additional testing.

This commit is contained in:
Enno Rehling 2014-10-18 14:16:26 +02:00
parent a86c2d88ab
commit 74a6bd72c2
7 changed files with 345 additions and 298 deletions

View file

@ -113,8 +113,6 @@ char *rns(FILE * f, char *c, size_t size)
return c;
}
extern unsigned int __at_hashkey(const char *s);
static unit *unitorders(FILE * F, int enc, struct faction *f)
{

View file

@ -30,6 +30,7 @@ int RunAllTests(void)
ADD_TESTS(suite, race);
/* util */
ADD_TESTS(suite, config);
ADD_TESTS(suite, attrib);
ADD_TESTS(suite, base36);
ADD_TESTS(suite, bsdstring);
ADD_TESTS(suite, functions);

View file

@ -1,5 +1,8 @@
#ifndef ERESSEA_TESTS_H
#define ERESSEA_TESTS_H
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif

View file

@ -2,6 +2,7 @@ project(util C)
SET(_TEST_FILES
base36.test.c
attrib.test.c
strings.test.c
bsdstring.test.c
functions.test.c

View file

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -29,7 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdlib.h>
#define MAXATHASH 61
attrib_type *at_hash[MAXATHASH];
static attrib_type *at_hash[MAXATHASH];
static unsigned int __at_hashkey(const char *s)
{
@ -51,12 +51,14 @@ void at_register(attrib_type * at)
}
at->hashkey = __at_hashkey(at->name);
find = at_hash[at->hashkey % MAXATHASH];
while (find && at->hashkey != find->hashkey)
while (find && at->hashkey != find->hashkey) {
find = find->nexthash;
}
if (find && find == at) {
log_warning("attribute '%s' was registered more than once\n", at->name);
return;
} else {
}
else {
assert(!find || !"hashkey is already in use");
}
at->nexthash = at_hash[at->hashkey % MAXATHASH];
@ -66,9 +68,9 @@ void at_register(attrib_type * at)
static attrib_type *at_find(unsigned int hk)
{
const char *translate[3][2] = {
{"zielregion", "targetregion"}, /* remapping: from 'zielregion, heute targetregion */
{"verzaubert", "curse"}, /* remapping: früher verzaubert, jetzt curse */
{NULL, NULL}
{ "zielregion", "targetregion" }, /* remapping: from 'zielregion, heute targetregion */
{ "verzaubert", "curse" }, /* remapping: früher verzaubert, jetzt curse */
{ NULL, NULL }
};
attrib_type *find = at_hash[hk % MAXATHASH];
while (find && hk != find->hashkey)
@ -180,7 +182,8 @@ static int a_unlink(attrib ** pa, attrib * a)
return 1;
while (*pnext && (*pnext)->type != a->type)
pnext = &(*pnext)->next;
} else {
}
else {
pnext = &(*pnexttype)->next;
}
while (*pnext && (*pnext)->type == a->type) {
@ -235,7 +238,7 @@ void a_removeall(attrib ** pa, const attrib_type * at)
attrib *a_new(const attrib_type * at)
{
attrib *a = (attrib *) calloc(1, sizeof(attrib));
attrib *a = (attrib *)calloc(1, sizeof(attrib));
assert(at != NULL);
a->type = at;
if (at->initialize)
@ -265,7 +268,7 @@ int a_age(attrib ** p)
static critbit_tree cb_deprecated = { 0 };
void at_deprecate(const char * name, int (*reader)(attrib *, void *, struct storage *))
void at_deprecate(const char * name, int(*reader)(attrib *, void *, struct storage *))
{
char buffer[64];
size_t len = strlen(name);
@ -287,19 +290,21 @@ int a_read(struct storage *store, attrib ** attribs, void *owner)
key = __at_hashkey(zText);
while (key != -1) {
int (*reader)(attrib *, void *, struct storage *) = 0;
int(*reader)(attrib *, void *, struct storage *) = 0;
attrib_type *at = at_find(key);
attrib * na = 0;
if (at) {
reader = at->read;
na = a_new(at);
} else {
}
else {
const void * kv;
cb_find_prefix(&cb_deprecated, zText, strlen(zText)+1, &kv, 1, 0);
cb_find_prefix(&cb_deprecated, zText, strlen(zText) + 1, &kv, 1, 0);
if (kv) {
cb_get_kv(kv, &reader, sizeof(reader));
} else {
}
else {
fprintf(stderr, "attribute hash: %d (%s)\n", key, zText);
assert(at || !"attribute not registered");
}
@ -320,7 +325,8 @@ int a_read(struct storage *store, attrib ** attribs, void *owner)
break;
}
}
} else {
}
else {
assert(!"error: no registered callback can read attribute");
}
@ -342,7 +348,8 @@ void a_write(struct storage *store, const attrib * attribs, const void *owner)
WRITE_TOK(store, na->type->name);
na->type->write(na, owner, store);
na = na->next;
} else {
}
else {
na = na->nexttype;
}
}

View file

@ -1,7 +1,7 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@ -18,13 +18,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef ATTRIB_H
#define ATTRIB_H
#ifdef __cplusplus
extern "C" {
#endif
struct gamedata;
struct storage;
typedef void (*afun) (void);
typedef void(*afun) (void);
typedef struct attrib {
const struct attrib_type *type;
@ -49,9 +50,9 @@ extern "C" {
typedef struct attrib_type {
const char *name;
void (*initialize) (struct attrib *);
void (*finalize) (struct attrib *);
int (*age) (struct attrib *);
void(*initialize) (struct attrib *);
void(*finalize) (struct attrib *);
int(*age) (struct attrib *);
/* age returns 0 if the attribute needs to be removed, !=0 otherwise */
void(*write) (const struct attrib *, const void *owner, struct storage *);
int(*read) (struct attrib *, void *owner, struct storage *); /* return AT_READ_OK on success, AT_READ_FAIL if attrib needs removal */
@ -62,7 +63,7 @@ extern "C" {
} attrib_type;
extern void at_register(attrib_type * at);
extern void at_deprecate(const char * name, int (*reader)(attrib *, void *, struct storage *));
extern void at_deprecate(const char * name, int(*reader)(attrib *, void *, struct storage *));
extern attrib *a_select(attrib * a, const void *data,
bool(*compare) (const attrib *, const void *));

36
src/util/attrib.test.c Normal file
View file

@ -0,0 +1,36 @@
#include <platform.h>
#include "attrib.h"
#include <CuTest.h>
#include <tests.h>
static void test_attrib_new(CuTest * tc)
{
attrib_type at_test = { "test" };
attrib * a;
CuAssertPtrNotNull(tc, (a = a_new(&at_test)));
CuAssertPtrEquals(tc, 0, a->next);
CuAssertPtrEquals(tc, 0, a->nexttype);
CuAssertPtrEquals(tc, (void *)a->type, (void *)&at_test);
}
static void test_attrib_add(CuTest * tc)
{
attrib_type at_test = { "test" };
attrib *a, *alist = 0;
CuAssertPtrNotNull(tc, (a = a_new(&at_test)));
CuAssertPtrEquals(tc, a, a_add(&alist, a));
CuAssertPtrEquals(tc, a, alist);
CuAssertPtrNotNull(tc, (a = a_add(&alist, a_new(&at_test))));
CuAssertPtrEquals(tc, alist->next, a);
CuAssertPtrEquals(tc, alist->nexttype, a);
}
CuSuite *get_attrib_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_attrib_new);
return suite;
}