forked from github/server
additional testing.
This commit is contained in:
parent
a86c2d88ab
commit
74a6bd72c2
7 changed files with 345 additions and 298 deletions
|
@ -113,8 +113,6 @@ char *rns(FILE * f, char *c, size_t size)
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern unsigned int __at_hashkey(const char *s);
|
|
||||||
|
|
||||||
|
|
||||||
static unit *unitorders(FILE * F, int enc, struct faction *f)
|
static unit *unitorders(FILE * F, int enc, struct faction *f)
|
||||||
{
|
{
|
||||||
|
|
|
@ -30,6 +30,7 @@ int RunAllTests(void)
|
||||||
ADD_TESTS(suite, race);
|
ADD_TESTS(suite, race);
|
||||||
/* util */
|
/* util */
|
||||||
ADD_TESTS(suite, config);
|
ADD_TESTS(suite, config);
|
||||||
|
ADD_TESTS(suite, attrib);
|
||||||
ADD_TESTS(suite, base36);
|
ADD_TESTS(suite, base36);
|
||||||
ADD_TESTS(suite, bsdstring);
|
ADD_TESTS(suite, bsdstring);
|
||||||
ADD_TESTS(suite, functions);
|
ADD_TESTS(suite, functions);
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
#ifndef ERESSEA_TESTS_H
|
#ifndef ERESSEA_TESTS_H
|
||||||
#define ERESSEA_TESTS_H
|
#define ERESSEA_TESTS_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,6 +2,7 @@ project(util C)
|
||||||
|
|
||||||
SET(_TEST_FILES
|
SET(_TEST_FILES
|
||||||
base36.test.c
|
base36.test.c
|
||||||
|
attrib.test.c
|
||||||
strings.test.c
|
strings.test.c
|
||||||
bsdstring.test.c
|
bsdstring.test.c
|
||||||
functions.test.c
|
functions.test.c
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
||||||
Katja Zedel <katze@felidae.kn-bremen.de
|
Katja Zedel <katze@felidae.kn-bremen.de
|
||||||
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
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>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define MAXATHASH 61
|
#define MAXATHASH 61
|
||||||
attrib_type *at_hash[MAXATHASH];
|
static attrib_type *at_hash[MAXATHASH];
|
||||||
|
|
||||||
static unsigned int __at_hashkey(const char *s)
|
static unsigned int __at_hashkey(const char *s)
|
||||||
{
|
{
|
||||||
|
@ -51,12 +51,14 @@ void at_register(attrib_type * at)
|
||||||
}
|
}
|
||||||
at->hashkey = __at_hashkey(at->name);
|
at->hashkey = __at_hashkey(at->name);
|
||||||
find = at_hash[at->hashkey % MAXATHASH];
|
find = at_hash[at->hashkey % MAXATHASH];
|
||||||
while (find && at->hashkey != find->hashkey)
|
while (find && at->hashkey != find->hashkey) {
|
||||||
find = find->nexthash;
|
find = find->nexthash;
|
||||||
|
}
|
||||||
if (find && find == at) {
|
if (find && find == at) {
|
||||||
log_warning("attribute '%s' was registered more than once\n", at->name);
|
log_warning("attribute '%s' was registered more than once\n", at->name);
|
||||||
return;
|
return;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
assert(!find || !"hashkey is already in use");
|
assert(!find || !"hashkey is already in use");
|
||||||
}
|
}
|
||||||
at->nexthash = at_hash[at->hashkey % MAXATHASH];
|
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)
|
static attrib_type *at_find(unsigned int hk)
|
||||||
{
|
{
|
||||||
const char *translate[3][2] = {
|
const char *translate[3][2] = {
|
||||||
{"zielregion", "targetregion"}, /* remapping: from 'zielregion, heute targetregion */
|
{ "zielregion", "targetregion" }, /* remapping: from 'zielregion, heute targetregion */
|
||||||
{"verzaubert", "curse"}, /* remapping: früher verzaubert, jetzt curse */
|
{ "verzaubert", "curse" }, /* remapping: früher verzaubert, jetzt curse */
|
||||||
{NULL, NULL}
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
attrib_type *find = at_hash[hk % MAXATHASH];
|
attrib_type *find = at_hash[hk % MAXATHASH];
|
||||||
while (find && hk != find->hashkey)
|
while (find && hk != find->hashkey)
|
||||||
|
@ -180,7 +182,8 @@ static int a_unlink(attrib ** pa, attrib * a)
|
||||||
return 1;
|
return 1;
|
||||||
while (*pnext && (*pnext)->type != a->type)
|
while (*pnext && (*pnext)->type != a->type)
|
||||||
pnext = &(*pnext)->next;
|
pnext = &(*pnext)->next;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
pnext = &(*pnexttype)->next;
|
pnext = &(*pnexttype)->next;
|
||||||
}
|
}
|
||||||
while (*pnext && (*pnext)->type == a->type) {
|
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_new(const attrib_type * at)
|
||||||
{
|
{
|
||||||
attrib *a = (attrib *) calloc(1, sizeof(attrib));
|
attrib *a = (attrib *)calloc(1, sizeof(attrib));
|
||||||
assert(at != NULL);
|
assert(at != NULL);
|
||||||
a->type = at;
|
a->type = at;
|
||||||
if (at->initialize)
|
if (at->initialize)
|
||||||
|
@ -265,7 +268,7 @@ int a_age(attrib ** p)
|
||||||
|
|
||||||
static critbit_tree cb_deprecated = { 0 };
|
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];
|
char buffer[64];
|
||||||
size_t len = strlen(name);
|
size_t len = strlen(name);
|
||||||
|
@ -287,19 +290,21 @@ int a_read(struct storage *store, attrib ** attribs, void *owner)
|
||||||
key = __at_hashkey(zText);
|
key = __at_hashkey(zText);
|
||||||
|
|
||||||
while (key != -1) {
|
while (key != -1) {
|
||||||
int (*reader)(attrib *, void *, struct storage *) = 0;
|
int(*reader)(attrib *, void *, struct storage *) = 0;
|
||||||
attrib_type *at = at_find(key);
|
attrib_type *at = at_find(key);
|
||||||
attrib * na = 0;
|
attrib * na = 0;
|
||||||
|
|
||||||
if (at) {
|
if (at) {
|
||||||
reader = at->read;
|
reader = at->read;
|
||||||
na = a_new(at);
|
na = a_new(at);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
const void * kv;
|
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) {
|
if (kv) {
|
||||||
cb_get_kv(kv, &reader, sizeof(reader));
|
cb_get_kv(kv, &reader, sizeof(reader));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
fprintf(stderr, "attribute hash: %d (%s)\n", key, zText);
|
fprintf(stderr, "attribute hash: %d (%s)\n", key, zText);
|
||||||
assert(at || !"attribute not registered");
|
assert(at || !"attribute not registered");
|
||||||
}
|
}
|
||||||
|
@ -320,7 +325,8 @@ int a_read(struct storage *store, attrib ** attribs, void *owner)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
assert(!"error: no registered callback can read attribute");
|
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);
|
WRITE_TOK(store, na->type->name);
|
||||||
na->type->write(na, owner, store);
|
na->type->write(na, owner, store);
|
||||||
na = na->next;
|
na = na->next;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
na = na->nexttype;
|
na = na->nexttype;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
|
||||||
Katja Zedel <katze@felidae.kn-bremen.de
|
Katja Zedel <katze@felidae.kn-bremen.de
|
||||||
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
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
|
#ifndef ATTRIB_H
|
||||||
#define ATTRIB_H
|
#define ATTRIB_H
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct gamedata;
|
struct gamedata;
|
||||||
struct storage;
|
struct storage;
|
||||||
typedef void (*afun) (void);
|
typedef void(*afun) (void);
|
||||||
|
|
||||||
typedef struct attrib {
|
typedef struct attrib {
|
||||||
const struct attrib_type *type;
|
const struct attrib_type *type;
|
||||||
|
@ -49,9 +50,9 @@ extern "C" {
|
||||||
|
|
||||||
typedef struct attrib_type {
|
typedef struct attrib_type {
|
||||||
const char *name;
|
const char *name;
|
||||||
void (*initialize) (struct attrib *);
|
void(*initialize) (struct attrib *);
|
||||||
void (*finalize) (struct attrib *);
|
void(*finalize) (struct attrib *);
|
||||||
int (*age) (struct attrib *);
|
int(*age) (struct attrib *);
|
||||||
/* age returns 0 if the attribute needs to be removed, !=0 otherwise */
|
/* age returns 0 if the attribute needs to be removed, !=0 otherwise */
|
||||||
void(*write) (const struct attrib *, const void *owner, struct storage *);
|
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 */
|
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;
|
} attrib_type;
|
||||||
|
|
||||||
extern void at_register(attrib_type * at);
|
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,
|
extern attrib *a_select(attrib * a, const void *data,
|
||||||
bool(*compare) (const attrib *, const void *));
|
bool(*compare) (const attrib *, const void *));
|
||||||
|
|
36
src/util/attrib.test.c
Normal file
36
src/util/attrib.test.c
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue