From 0f10b58167791b22890d590e0bd1ec073edc77c0 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 1 Oct 2017 15:08:26 +0200 Subject: [PATCH] add tests and rewrite MAKE TEMP --- src/laws.c | 156 +++++++++++++++++++++++++++--------------------- src/laws.test.c | 65 ++++++++++++++++++++ 2 files changed, 154 insertions(+), 67 deletions(-) diff --git a/src/laws.c b/src/laws.c index d7f73fa6c..c1a30cac5 100644 --- a/src/laws.c +++ b/src/laws.c @@ -3007,10 +3007,92 @@ int checkunitnumber(const faction * f, int add) return 0; } +void maketemp_cmd(unit *u, order **olist) +{ + order *makeord; + int err = checkunitnumber(u->faction, 1); + + makeord = *olist; + if (err) { + if (err == 1) { + ADDMSG(&u->faction->msgs, + msg_feedback(u, makeord, + "too_many_units_in_alliance", + "allowed", maxunits(u->faction))); + } + else { + ADDMSG(&u->faction->msgs, + msg_feedback(u, makeord, + "too_many_units_in_faction", + "allowed", maxunits(u->faction))); + } + *olist = makeord->next; + makeord->next = NULL; + free_order(makeord); + while (*olist) { + keyword_t kwd; + order * ord = *olist; + *olist = ord->next; + ord->next = NULL; + kwd = getkeyword(ord); + free_order(ord); + if (kwd == K_END) { + break; + } + } + } + else { + char token[128]; + const char *s; + int alias; + ship *sh; + unit *u2; + order **ordp, **oinsert; +#ifndef NDEBUG + keyword_t kwd = init_order(makeord); + assert(kwd == K_MAKETEMP); +#endif + alias = getid(); + s = gettoken(token, sizeof(token)); + if (s && s[0] == '\0') { + /* empty name? => generate one */ + s = NULL; + } + u2 = create_unit(u->region, u->faction, 0, u->faction->race, alias, s, u); + fset(u2, UFL_ISNEW); + a_add(&u2->attribs, a_new(&at_alias))->data.i = alias; + sh = leftship(u); + if (sh) { + set_leftship(u2, sh); + } + setstatus(u2, u->status); + + /* copy orders until K_END from u to u2 */ + ordp = &makeord->next; + oinsert = &u2->orders; + + while (*ordp) { + order *ord = *ordp; + *ordp = ord->next; + if (getkeyword(ord) == K_END) { + ord->next = NULL; + free_order(ord); + break; + } + *oinsert = ord; + oinsert = &ord->next; + *oinsert = NULL; + } + *olist = *ordp; + makeord->next = NULL; + free_order(makeord); + } +} + void new_units(void) { region *r; - unit *u, *u2; + unit *u; /* neue einheiten werden gemacht und ihre befehle (bis zum "ende" zu * ihnen rueberkopiert, damit diese einheiten genauso wie die alten @@ -3028,73 +3110,13 @@ void new_units(void) } while (*ordp) { - order *makeord = *ordp; - if (getkeyword(makeord) == K_MAKETEMP) { - char token[128], *name = NULL; - const char *s; - int alias; - ship *sh; - order **newordersp; - int err = checkunitnumber(u->faction, 1); - - if (err) { - if (err == 1) { - ADDMSG(&u->faction->msgs, - msg_feedback(u, makeord, - "too_many_units_in_alliance", - "allowed", maxunits(u->faction))); - } - else { - ADDMSG(&u->faction->msgs, - msg_feedback(u, makeord, - "too_many_units_in_faction", - "allowed", maxunits(u->faction))); - } - ordp = &makeord->next; - - while (*ordp) { - order *ord = *ordp; - if (getkeyword(ord) == K_END) - break; - *ordp = ord->next; - ord->next = NULL; - free_order(ord); - } - continue; - } - init_order(makeord); - alias = getid(); - - s = gettoken(token, sizeof(token)); - if (s && s[0]) { - name = strdup(s); - } - u2 = create_unit(r, u->faction, 0, u->faction->race, alias, name, u); - if (name != NULL) - free(name); /* TODO: use a buffer on the stack instead? */ - fset(u2, UFL_ISNEW); - - a_add(&u2->attribs, a_new(&at_alias))->data.i = alias; - sh = leftship(u); - if (sh) { - set_leftship(u2, sh); - } - setstatus(u2, u->status); - - ordp = &makeord->next; - newordersp = &u2->orders; - while (*ordp) { - order *ord = *ordp; - if (getkeyword(ord) == K_END) - break; - *ordp = ord->next; - ord->next = NULL; - *newordersp = ord; - newordersp = &ord->next; - } + order *ord = *ordp; + if (getkeyword(ord) == K_MAKETEMP) { + maketemp_cmd(u, ordp); + } + else { + ordp = &ord->next; } - if (*ordp == makeord) - ordp = &makeord->next; } } } diff --git a/src/laws.test.c b/src/laws.test.c index 9f7884fc5..ee5c55f19 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -428,6 +428,63 @@ static void test_unit_limit(CuTest * tc) test_cleanup(); } +static void test_maketemp(CuTest * tc) +{ + faction *f; + unit *u, *u2; + + test_setup(); + f = test_create_faction(NULL); + u = test_create_unit(f, test_create_region(0, 0, NULL)); + + u->orders = create_order(K_MAKETEMP, f->locale, "1"); + u->orders->next = create_order(K_ENTERTAIN, f->locale, NULL); + u->orders->next->next = create_order(K_END, f->locale, NULL); + u->orders->next->next->next = create_order(K_TAX, f->locale, NULL); + + new_units(); + CuAssertIntEquals(tc, 2, f->num_units); + CuAssertPtrNotNull(tc, u2 = u->next); + CuAssertPtrNotNull(tc, u2->orders); + CuAssertPtrEquals(tc, NULL, u2->orders->next); + CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u2->orders)); + + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, NULL, u->orders->next); + CuAssertIntEquals(tc, K_TAX, getkeyword(u->orders)); + test_cleanup(); +} + +static void test_maketemp_default_order(CuTest * tc) +{ + faction *f; + unit *u, *u2; + + test_setup(); + config_set("orders.default", "work"); + f = test_create_faction(NULL); + u = test_create_unit(f, test_create_region(0, 0, NULL)); + + new_units(); + CuAssertIntEquals(tc, 1, f->num_units); + + u->orders = create_order(K_MAKETEMP, f->locale, "1"); + u->orders->next = create_order(K_END, f->locale, NULL); + u->orders->next->next = create_order(K_TAX, f->locale, NULL); + + new_units(); + CuAssertIntEquals(tc, 2, f->num_units); + CuAssertPtrNotNull(tc, u2 = u->next); + CuAssertPtrNotNull(tc, u2->orders); + CuAssertPtrEquals(tc, NULL, u2->orders->next); + CuAssertIntEquals(tc, K_WORK, getkeyword(u2->orders)); + + CuAssertPtrNotNull(tc, u->orders); + CuAssertPtrEquals(tc, NULL, u->orders->next); + CuAssertIntEquals(tc, K_TAX, getkeyword(u->orders)); + test_cleanup(); +} + static void test_limit_new_units(CuTest * tc) { faction *f; @@ -449,6 +506,8 @@ static void test_limit_new_units(CuTest * tc) CuAssertPtrNotNull(tc, u->next); CuAssertIntEquals(tc, 2, f->num_units); + CuAssertPtrEquals(tc, NULL, u->orders); + u->orders = create_order(K_MAKETEMP, f->locale, "1"); new_units(); CuAssertIntEquals(tc, 2, f->num_units); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_faction")); @@ -458,6 +517,8 @@ static void test_limit_new_units(CuTest * tc) config_set("rules.limit.faction", "3"); config_set("rules.limit.alliance", "2"); + CuAssertPtrEquals(tc, NULL, u->orders); + u->orders = create_order(K_MAKETEMP, f->locale, "1"); new_units(); CuAssertIntEquals(tc, 2, f->num_units); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_alliance")); @@ -466,6 +527,8 @@ static void test_limit_new_units(CuTest * tc) u = test_create_unit(test_create_faction(NULL), u->region); setalliance(u->faction, al); + CuAssertPtrEquals(tc, NULL, u->orders); + u->orders = create_order(K_MAKETEMP, f->locale, "1"); new_units(); CuAssertIntEquals(tc, 2, f->num_units); CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_alliance")); @@ -1555,6 +1618,8 @@ static void test_armedmen(CuTest *tc) { CuSuite *get_laws_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_maketemp_default_order); + SUITE_ADD_TEST(suite, test_maketemp); SUITE_ADD_TEST(suite, test_nmr_warnings); SUITE_ADD_TEST(suite, test_ally_cmd); SUITE_ADD_TEST(suite, test_name_cmd);