From 1050daac19d90bcc5ac5d288418e96cccf663f69 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Dec 2014 17:40:36 +0100 Subject: [PATCH 1/3] remove strlist from config.c, move it to reports.c (where it is used primarily). this is not the best data structure there is, should replace it some day. --- src/kernel/config.c | 20 -------------------- src/kernel/config.h | 8 -------- src/reports.c | 20 ++++++++++++++++++++ src/reports.h | 9 +++++++++ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/kernel/config.c b/src/kernel/config.c index 30f49a186..5063a4427 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -990,26 +990,6 @@ int getunit(const region * r, const faction * f, unit **uresult) return result; } -/* - String Listen --------------------------------------------- */ -void addstrlist(strlist ** SP, const char *s) -{ - strlist *slist = malloc(sizeof(strlist)); - slist->next = NULL; - slist->s = _strdup(s); - addlist(SP, slist); -} - -void freestrlist(strlist * s) -{ - strlist *q, *p = s; - while (p) { - q = p->next; - free(p->s); - free(p); - p = q; - } -} - /* - Namen der Strukturen -------------------------------------- */ char *untilde(char *ibuf) { diff --git a/src/kernel/config.h b/src/kernel/config.h index 9a44e7540..908f9b993 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -108,14 +108,6 @@ extern "C" { /* special units */ void make_undead_unit(struct unit *); - typedef struct strlist { - struct strlist *next; - char *s; - } strlist; - - void addstrlist(strlist ** SP, const char *s); - void freestrlist(strlist * s); - unsigned int atoip(const char *s); unsigned int getuint(void); int getint(void); diff --git a/src/reports.c b/src/reports.c index c01a49bf2..e478a0200 100644 --- a/src/reports.c +++ b/src/reports.c @@ -2406,6 +2406,26 @@ static void eval_int36(struct opstack **stack, const void *userdata) /*** END MESSAGE RENDERING ***/ +/* - String Listen --------------------------------------------- */ +void addstrlist(strlist ** SP, const char *s) +{ + strlist *slist = malloc(sizeof(strlist)); + slist->next = NULL; + slist->s = _strdup(s); + addlist(SP, slist); +} + +void freestrlist(strlist * s) +{ + strlist *q, *p = s; + while (p) { + q = p->next; + free(p->s); + free(p); + p = q; + } +} + #include static void log_orders(const struct message *msg) diff --git a/src/reports.h b/src/reports.h index cb4ab54f3..60019ac2f 100644 --- a/src/reports.h +++ b/src/reports.h @@ -142,6 +142,15 @@ extern "C" { size_t f_regionid(const struct region *r, const struct faction *f, char *buffer, size_t size); + typedef struct strlist { + struct strlist *next; + char *s; + } strlist; + + void addstrlist(strlist ** SP, const char *s); + void freestrlist(strlist * s); + + #define GR_PLURAL 0x01 /* grammar: plural */ #define MAX_INVENTORY 128 /* maimum number of different items in an inventory */ #define MAX_RAWMATERIALS 8 /* maximum kinds of raw materials in a regions */ From 916271ef3ac2c90fd040479291372ceb2cbd8df9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Dec 2014 18:47:01 +0100 Subject: [PATCH 2/3] gcc/clang want structs to be defined before used in an argument list --- src/lighthouse.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lighthouse.h b/src/lighthouse.h index 874e0fe8c..6000248ba 100644 --- a/src/lighthouse.h +++ b/src/lighthouse.h @@ -23,6 +23,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. extern "C" { #endif + + struct faction; + struct region; + struct building; /* leuchtturm */ bool check_leuchtturm(struct region *r, struct faction *f); void update_lighthouse(struct building *lh); From bd6b9761b00ddef906e283ebfab07d398ac95199 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 17 Dec 2014 19:53:19 +0100 Subject: [PATCH 3/3] add simple tests for pay_cmd. --- src/kernel/config.test.c | 4 +-- src/laws.test.c | 69 +++++++++++++++++++++++++++++++++++++--- src/tests.c | 8 +++++ src/tests.h | 2 ++ 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index 79643106c..b5d7957be 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -21,12 +21,10 @@ static void test_getunit(CuTest *tc) { struct region *r; struct locale *lang; struct terrain_type *t_plain; - struct critbit_tree ** cb; test_cleanup(); lang = get_or_create_locale("de"); - cb = (struct critbit_tree **)get_translations(lang, UT_PARAMS); - add_translation(cb, "TEMP", P_TEMP); + test_translate_param(lang, P_TEMP, "TEMP"); /* note that the english order is FIGHT, not COMBAT, so this is a poor example */ t_plain = test_create_terrain("plain", LAND_REGION); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, t_plain)); diff --git a/src/laws.test.c b/src/laws.test.c index bc8fa7247..5275d459e 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -329,7 +329,6 @@ static void test_reserve_cmd(CuTest *tc) { region *r; order *ord; const resource_type *rtype; - const struct locale *loc; test_cleanup(); test_create_world(); @@ -342,9 +341,7 @@ static void test_reserve_cmd(CuTest *tc) { u1 = test_create_unit(f, r); u2 = test_create_unit(f, r); assert(u1 && u2); - loc = get_locale("de"); - assert(loc); - ord = create_order(K_RESERVE, loc, "200 SILBER"); + ord = create_order(K_RESERVE, f->locale, "200 SILBER"); assert(ord); i_change(&u1->items, rtype->itype, 100); i_change(&u2->items, rtype->itype, 100); @@ -354,6 +351,68 @@ static void test_reserve_cmd(CuTest *tc) { test_cleanup(); } +struct pay_fixture { + unit *u1; + unit *u2; +}; + +static void setup_pay_cmd(struct pay_fixture *fix) { + faction *f; + region *r; + building *b; + + test_create_world(); + f = test_create_faction(NULL); + r = findregion(0, 0); + assert(r && f); + b = test_create_building(r, bt_get_or_create("lighthouse")); + assert(b); + fix->u1 = test_create_unit(f, r); + fix->u2 = test_create_unit(f, r); + assert(fix->u1 && fix->u2); + u_set_building(fix->u1, b); + u_set_building(fix->u2, b); + assert(building_owner(b) == fix->u1); + test_translate_param(f->locale, P_NOT, "NOT"); +} + +static void test_pay_cmd(CuTest *tc) { + struct pay_fixture fix; + order *ord; + faction *f; + building *b; + + test_cleanup(); + setup_pay_cmd(&fix); + b = fix.u1->building; + f = fix.u1->faction; + + ord = create_order(K_PAY, f->locale, "NOT"); + assert(ord); + CuAssertIntEquals(tc, 0, pay_cmd(fix.u1, ord)); + CuAssertIntEquals(tc, BLD_DONTPAY, b->flags&BLD_DONTPAY); + test_cleanup(); +} + +static void test_pay_cmd_must_be_owner(CuTest *tc) { + struct pay_fixture fix; + order *ord; + faction *f; + building *b; + + test_cleanup(); + setup_pay_cmd(&fix); + b = fix.u1->building; + f = fix.u1->faction; + + ord = create_order(K_PAY, f->locale, "NOT"); + assert(ord); + CuAssertIntEquals(tc, 0, pay_cmd(fix.u2, ord)); + CuAssertIntEquals(tc, 0, b->flags&BLD_DONTPAY); + test_cleanup(); +} + + static void test_new_units(CuTest *tc) { unit *u; faction *f; @@ -521,6 +580,8 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_low_skill_cannot_guard); SUITE_ADD_TEST(suite, test_reserve_self); SUITE_ADD_TEST(suite, test_reserve_cmd); + SUITE_ADD_TEST(suite, test_pay_cmd); + SUITE_ADD_TEST(suite, test_pay_cmd_must_be_owner); SUITE_ADD_TEST(suite, test_new_units); SUITE_ADD_TEST(suite, test_cannot_create_unit_above_limit); SUITE_ADD_TEST(suite, test_contact); diff --git a/src/tests.c b/src/tests.c index db9bcbc69..7f7b73d75 100644 --- a/src/tests.c +++ b/src/tests.c @@ -132,6 +132,14 @@ item_type * test_create_itemtype(const char * name) { return itype; } +void test_translate_param(const struct locale *lang, param_t param, const char *text) { + struct critbit_tree **cb; + + assert(lang && text); + cb = (struct critbit_tree **)get_translations(lang, UT_PARAMS); + add_translation(cb, text, param); +} + /** creates a small world and some stuff in it. * two terrains: 'plain' and 'ocean' * one race: 'human' diff --git a/src/tests.h b/src/tests.h index a35b5acf3..5a565076f 100644 --- a/src/tests.h +++ b/src/tests.h @@ -1,6 +1,7 @@ #ifndef ERESSEA_TESTS_H #define ERESSEA_TESTS_H +#include #include #ifdef __cplusplus @@ -33,6 +34,7 @@ extern "C" { struct building_type *test_create_buildingtype(const char *name); int RunAllTests(void); + void test_translate_param(const struct locale *lang, param_t param, const char *text); #ifdef __cplusplus }