From cf7971bcfc68a1cb7823d7c48bda25f9273af46e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 25 Aug 2019 17:43:18 +0200 Subject: [PATCH] Tests for GIVE UNIT and groups. --- scripts/tests/common.lua | 29 +++++++++++++++++++++++++++++ src/kernel/group.c | 13 +++++++++---- src/kernel/group.h | 2 +- src/kernel/group.test.c | 6 ++++-- src/kernel/unit.c | 2 +- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 70e1b61e4..41f551772 100644 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -37,6 +37,35 @@ function setup() eressea.settings.set("study.random_progress", "0") end +function test_give_unit() + local r = region.create(0, 0, "plain") + local f1 = create_faction('elf') + local f2 = create_faction('elf') + local u1, u2 = two_units(r, f1, f2) + assert_equal(f1, u1.faction) + assert_equal(f2, u2.faction) + u2.name = 'Xolgrim' + u2.group = 'Experten' + assert_equal('Experten', u2.group) + u1:add_order("HELFE " .. itoa36(f2.id) .. " GIB") + u2:add_order("GIB " .. itoa36(u1.id) .. " EINHEIT") + process_orders() + assert_equal(f1, u2.faction) + assert_nil(u2.group) +end + +function test_set_faction() + local r = region.create(0, 0, "plain") + local f1 = create_faction('elf') + local f2 = create_faction('elf') + local u = one_unit(r, f1) + u.group = 'Experten' + assert_equal('Experten', u.group) + u.faction = f2 + assert_equal(f2, u.faction) + assert_nil(u.group) +end + function test_locales() assert_equal(2, eressea.locale.direction("de", "Ost")) assert_equal(5, eressea.locale.direction("de", "westen")) diff --git a/src/kernel/group.c b/src/kernel/group.c index 482eb26f8..1515f8ea1 100755 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -49,7 +49,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. static group *ghash[GMAXHASH]; static int maxgid; -group *new_group(faction * f, const char *name, int gid) +group *create_group(faction * f, const char *name, int gid) { group **gp = &f->groups; int index = gid % GMAXHASH; @@ -63,6 +63,7 @@ group *new_group(faction * f, const char *name, int gid) if (gid > maxgid) maxgid = gid; g->name = str_strdup(name); g->gid = gid; + g->f = f; g->nexthash = ghash[index]; return ghash[index] = g; @@ -93,11 +94,15 @@ static int read_group(variant *var, void *owner, gamedata *data) { struct storage *store = data->store; group *g; + unit * u = (unit *)owner; int gid; READ_INT(store, &gid); var->v = g = find_group(gid); - if (g != 0) { + if (g != NULL) { + if (g->f != u->faction) { + return AT_READ_FAIL; + } g->members++; return AT_READ_OK; } @@ -184,7 +189,7 @@ group *join_group(unit * u, const char *name) if (name && name[0]) { g = find_groupbyname(u->faction->groups, name); if (g == NULL) { - g = new_group(u->faction, name, ++maxgid); + g = create_group(u->faction, name, ++maxgid); init_group(u->faction, g); } } @@ -219,7 +224,7 @@ void read_groups(gamedata *data, faction * f) if (gid == 0) break; READ_STR(store, buf, sizeof(buf)); - g = new_group(f, buf, gid); + g = create_group(f, buf, gid); read_allies(data, &g->allies); read_attribs(data, &g->attribs, g); } diff --git a/src/kernel/group.h b/src/kernel/group.h index 9b6d97cf9..8bb764603 100755 --- a/src/kernel/group.h +++ b/src/kernel/group.h @@ -40,7 +40,7 @@ extern "C" { extern void set_group(struct unit *u, struct group *g); extern struct group * get_group(const struct unit *u); extern void free_group(struct group *g); - struct group *new_group(struct faction * f, const char *name, int gid); + struct group *create_group(struct faction * f, const char *name, int gid); extern void write_groups(struct gamedata *data, const struct faction *f); extern void read_groups(struct gamedata *data, struct faction *f); diff --git a/src/kernel/group.test.c b/src/kernel/group.test.c index 7d14380d6..8c3bcb16e 100644 --- a/src/kernel/group.test.c +++ b/src/kernel/group.test.c @@ -80,8 +80,10 @@ static void test_group_readwrite(CuTest * tc) mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); f = test_create_faction(NULL); - new_group(f, "NW", 42); - g = new_group(f, "Egoisten", 43); + create_group(f, "NW", 42); + g = create_group(f, "Egoisten", 43); + CuAssertPtrEquals(tc, f, g->f); + CuAssertStrEquals(tc, "Egoisten", g->name); key_set(&g->attribs, 44, 44); ally_set(&g->allies, f, HELP_GIVE); write_groups(&data, f); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 2a9a3b5da..e92ea4ca0 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -923,7 +923,7 @@ void u_setfaction(unit * u, faction * f) --u->faction->num_units; u->faction->num_people -= u->number; } - join_group(u, NULL); + set_group(u, NULL); free_orders(&u->orders); set_order(&u->thisorder, NULL);