diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index a0b82f5b9..08c2df891 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -1,12 +1,55 @@ #include + +#include #include #include +#include #include #include -#include "faction.h" + #include +#include + +#include #include +static void test_remove_empty_factions(CuTest *tc) { + faction *f, *fm; + int fno; + + test_cleanup(); + fm = get_or_create_monsters(); + assert(fm); + f = test_create_faction(0); + fno = f->no; + remove_empty_factions(); + CuAssertPtrEquals(tc, 0, findfaction(fno)); + CuAssertPtrEquals(tc, fm, get_monsters()); + test_cleanup(); +} + +static void test_remove_dead_factions(CuTest *tc) { + faction *f, *fm; + region *r; + + test_cleanup(); + r = test_create_region(0, 0, 0); + fm = get_or_create_monsters(); + f = test_create_faction(0); + assert(fm && r && f); + test_create_unit(f, r); + test_create_unit(fm, r); + remove_empty_factions(); + CuAssertPtrEquals(tc, f, findfaction(f->no)); + CuAssertPtrNotNull(tc, get_monsters()); + fm->alive = 0; + f->alive = 0; + remove_empty_factions(); + CuAssertPtrEquals(tc, 0, findfaction(f->no)); + CuAssertPtrEquals(tc, fm, get_monsters()); + test_cleanup(); +} + static void test_addfaction(CuTest *tc) { faction *f = 0; const struct race *rc = rc_get_or_create("human"); @@ -51,6 +94,8 @@ CuSuite *get_faction_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_addfaction); + SUITE_ADD_TEST(suite, test_remove_empty_factions); + SUITE_ADD_TEST(suite, test_remove_dead_factions); SUITE_ADD_TEST(suite, test_get_monsters); return suite; } diff --git a/src/tests.c b/src/tests.c index 066b93262..ebd8c7bf4 100644 --- a/src/tests.c +++ b/src/tests.c @@ -34,25 +34,26 @@ struct race *test_create_race(const char *name) struct region *test_create_region(int x, int y, const terrain_type *terrain) { region *r = new_region(x, y, NULL, 0); - terraform_region(r, terrain); + terraform_region(r, terrain ? terrain : get_or_create_terrain("plain")); rsettrees(r, 0, 0); rsettrees(r, 1, 0); rsettrees(r, 2, 0); rsethorses(r, 0); - rsetpeasants(r, terrain->size); + rsetpeasants(r, r->terrain->size); return r; } struct faction *test_create_faction(const struct race *rc) { - faction *f = addfaction("nobody@eressea.de", NULL, rc?rc:rc_find("human"), default_locale, 0); + faction *f = addfaction("nobody@eressea.de", NULL, rc?rc:rc_get_or_create("human"), default_locale, 0); return f; } struct unit *test_create_unit(struct faction *f, struct region *r) { + const struct race * rc = f ? f->race : 0; assert(f || !r); - return create_unit(r, f, 1, f ? f->race : rc_find("human"), 0, 0, 0); + return create_unit(r, f, 1, rc ? rc : rc_get_or_create("human"), 0, 0, 0); } void test_cleanup(void)