add tests and rewrite MAKE TEMP

This commit is contained in:
Enno Rehling 2017-10-01 15:08:26 +02:00
parent 03f46e35c4
commit 0f10b58167
2 changed files with 154 additions and 67 deletions

View File

@ -3007,10 +3007,92 @@ int checkunitnumber(const faction * f, int add)
return 0; 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) void new_units(void)
{ {
region *r; region *r;
unit *u, *u2; unit *u;
/* neue einheiten werden gemacht und ihre befehle (bis zum "ende" zu /* neue einheiten werden gemacht und ihre befehle (bis zum "ende" zu
* ihnen rueberkopiert, damit diese einheiten genauso wie die alten * ihnen rueberkopiert, damit diese einheiten genauso wie die alten
@ -3028,73 +3110,13 @@ void new_units(void)
} }
while (*ordp) { while (*ordp) {
order *makeord = *ordp; order *ord = *ordp;
if (getkeyword(makeord) == K_MAKETEMP) { if (getkeyword(ord) == K_MAKETEMP) {
char token[128], *name = NULL; maketemp_cmd(u, ordp);
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 { else {
ADDMSG(&u->faction->msgs, ordp = &ord->next;
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;
}
}
if (*ordp == makeord)
ordp = &makeord->next;
} }
} }
} }

View File

@ -428,6 +428,63 @@ static void test_unit_limit(CuTest * tc)
test_cleanup(); 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) static void test_limit_new_units(CuTest * tc)
{ {
faction *f; faction *f;
@ -449,6 +506,8 @@ static void test_limit_new_units(CuTest * tc)
CuAssertPtrNotNull(tc, u->next); CuAssertPtrNotNull(tc, u->next);
CuAssertIntEquals(tc, 2, f->num_units); CuAssertIntEquals(tc, 2, f->num_units);
CuAssertPtrEquals(tc, NULL, u->orders);
u->orders = create_order(K_MAKETEMP, f->locale, "1");
new_units(); new_units();
CuAssertIntEquals(tc, 2, f->num_units); CuAssertIntEquals(tc, 2, f->num_units);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_faction")); 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.faction", "3");
config_set("rules.limit.alliance", "2"); config_set("rules.limit.alliance", "2");
CuAssertPtrEquals(tc, NULL, u->orders);
u->orders = create_order(K_MAKETEMP, f->locale, "1");
new_units(); new_units();
CuAssertIntEquals(tc, 2, f->num_units); CuAssertIntEquals(tc, 2, f->num_units);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_alliance")); 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); u = test_create_unit(test_create_faction(NULL), u->region);
setalliance(u->faction, al); setalliance(u->faction, al);
CuAssertPtrEquals(tc, NULL, u->orders);
u->orders = create_order(K_MAKETEMP, f->locale, "1");
new_units(); new_units();
CuAssertIntEquals(tc, 2, f->num_units); CuAssertIntEquals(tc, 2, f->num_units);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "too_many_units_in_alliance")); 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 *get_laws_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); 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_nmr_warnings);
SUITE_ADD_TEST(suite, test_ally_cmd); SUITE_ADD_TEST(suite, test_ally_cmd);
SUITE_ADD_TEST(suite, test_name_cmd); SUITE_ADD_TEST(suite, test_name_cmd);