diff --git a/src/kernel/unit.c b/src/kernel/unit.c index b956465e2..27dff7f0c 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -232,9 +232,8 @@ int gift_items(unit * u, int flags) int rule = rule_give(); assert(u->region); - assert(u->faction); - if ((u->faction->flags & FFL_QUIT) == 0 || (rule & GIVE_ONDEATH) == 0) { + if ((rule & GIVE_ONDEATH) == 0 || !u->faction || (u->faction->flags & FFL_QUIT) == 0) { if ((rule & GIVE_ALLITEMS) == 0 && (flags & GIFT_FRIENDS)) flags -= GIFT_FRIENDS; if ((rule & GIVE_PEASANTS) == 0 && (flags & GIFT_PEASANTS)) diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 3a341cce2..12a83d3ad 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -1,8 +1,10 @@ #include #include #include "alchemy.h" +#include "faction.h" #include "unit.h" #include "item.h" +#include "race.h" #include "region.h" #include @@ -47,6 +49,58 @@ static void test_remove_empty_units_in_region(CuTest *tc) { test_cleanup(); } +static void test_remove_units_without_faction(CuTest *tc) { + unit *u; + int uid; + + test_cleanup(); + test_create_world(); + + u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); + uid = u->no; + u_setfaction(u, 0); + remove_empty_units_in_region(u->region); + CuAssertPtrEquals(tc, 0, findunit(uid)); + CuAssertIntEquals(tc, 0, u->number); + test_cleanup(); +} + +static void test_remove_units_with_dead_faction(CuTest *tc) { + unit *u; + int uid; + + test_cleanup(); + test_create_world(); + + u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0)); + uid = u->no; + u->faction->alive = false; + remove_empty_units_in_region(u->region); + CuAssertPtrEquals(tc, 0, findunit(uid)); + CuAssertIntEquals(tc, 0, u->number); + test_cleanup(); +} + +static void test_remove_units_ignores_spells(CuTest *tc) { + unit *u; + int uid; + + test_cleanup(); + test_create_world(); + + u = create_unit(findregion(0, 0), test_create_faction(test_create_race("human")), 1, get_race(RC_SPELL), 0, 0, 0); + uid = u->no; + u->number = 0; + u->age = 1; + remove_empty_units_in_region(u->region); + CuAssertPtrNotNull(tc, findunit(uid)); + CuAssertPtrNotNull(tc, u->region); + u->age = 0; + remove_empty_units_in_region(u->region); + CuAssertPtrEquals(tc, 0, findunit(uid)); + test_cleanup(); +} + static void test_scale_number(CuTest *tc) { unit *u; const struct potion_type *ptype; @@ -74,6 +128,9 @@ CuSuite *get_unit_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_scale_number); SUITE_ADD_TEST(suite, test_remove_empty_units); + SUITE_ADD_TEST(suite, test_remove_units_ignores_spells); + SUITE_ADD_TEST(suite, test_remove_units_without_faction); + SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction); SUITE_ADD_TEST(suite, test_remove_empty_units_in_region); return suite; } diff --git a/src/tests.c b/src/tests.c index e5017b5ad..066b93262 100644 --- a/src/tests.c +++ b/src/tests.c @@ -51,8 +51,8 @@ struct faction *test_create_faction(const struct race *rc) struct unit *test_create_unit(struct faction *f, struct region *r) { - unit *u = create_unit(r, f, 1, f?f->race:rc_find("human"), 0, 0, 0); - return u; + assert(f || !r); + return create_unit(r, f, 1, f ? f->race : rc_find("human"), 0, 0, 0); } void test_cleanup(void)