From 4452f93009a68a2ed5d7217f11e2e1bf16ac1a41 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Oct 2017 22:13:39 +0200 Subject: [PATCH] BUG 2367: add a test for set_familiar. --- src/kernel/unit.test.c | 25 +---------------------- src/magic.c | 8 +++++--- src/magic.test.c | 46 ++++++++++++++++++++++++++++++++++++++++++ src/test_eressea.c | 1 + 4 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/kernel/unit.test.c b/src/kernel/unit.test.c index 2e4ce3568..6ca15a7f0 100644 --- a/src/kernel/unit.test.c +++ b/src/kernel/unit.test.c @@ -287,33 +287,11 @@ static void test_skill_familiar(CuTest *tc) { test_cleanup(); } -static void test_age_familiar(CuTest *tc) { - unit *mag, *fam; - - test_cleanup(); - - mag = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); - fam = test_create_unit(mag->faction, test_create_region(0, 0, 0)); - CuAssertPtrEquals(tc, 0, get_familiar(mag)); - CuAssertPtrEquals(tc, 0, get_familiar_mage(fam)); - CuAssertIntEquals(tc, true, create_newfamiliar(mag, fam)); - CuAssertPtrEquals(tc, fam, get_familiar(mag)); - CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); - a_age(&fam->attribs, fam); - a_age(&mag->attribs, mag); - CuAssertPtrEquals(tc, fam, get_familiar(mag)); - CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); - set_number(fam, 0); - a_age(&mag->attribs, mag); - CuAssertPtrEquals(tc, 0, get_familiar(mag)); - test_cleanup(); -} - static void test_inside_building(CuTest *tc) { unit *u; building *b; - test_cleanup(); + test_setup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); b = test_create_building(u->region, 0); @@ -631,7 +609,6 @@ CuSuite *get_unit_suite(void) SUITE_ADD_TEST(suite, test_skillmod); SUITE_ADD_TEST(suite, test_skill_hunger); SUITE_ADD_TEST(suite, test_skill_familiar); - SUITE_ADD_TEST(suite, test_age_familiar); SUITE_ADD_TEST(suite, test_inside_building); SUITE_ADD_TEST(suite, test_skills); SUITE_ADD_TEST(suite, test_limited_skills); diff --git a/src/magic.c b/src/magic.c index d41ce71e4..210781e9e 100644 --- a/src/magic.c +++ b/src/magic.c @@ -2192,17 +2192,19 @@ void set_familiar(unit * mage, unit * familiar) a = a_add(&mage->attribs, a_new(&at_familiar)); a->data.v = familiar; } - else + else { assert(!a->data.v || a->data.v == familiar); - /* TODO: Diese Attribute beim Tod des Familiars entfernen: */ + } + /* TODO: Diese Attribute beim Tod des Familiars entfernen: */ a = a_find(familiar->attribs, &at_familiarmage); if (a == NULL) { a = a_add(&familiar->attribs, a_new(&at_familiarmage)); a->data.v = mage; } - else + else { assert(!a->data.v || a->data.v == mage); + } } void remove_familiar(unit * mage) diff --git a/src/magic.test.c b/src/magic.test.c index c6866ce47..0ce454af1 100644 --- a/src/magic.test.c +++ b/src/magic.test.c @@ -477,6 +477,52 @@ static void test_illusioncastle(CuTest *tc) test_cleanup(); } +static void test_familiar_set(CuTest *tc) { + unit *mag, *fam; + + test_setup(); + + mag = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + fam = test_create_unit(mag->faction, test_create_region(0, 0, 0)); + CuAssertPtrEquals(tc, NULL, get_familiar(mag)); + CuAssertPtrEquals(tc, NULL, get_familiar_mage(fam)); + CuAssertPtrEquals(tc, NULL, a_find(mag->attribs, &at_skillmod)); + set_familiar(mag, fam); + CuAssertPtrEquals(tc, fam, get_familiar(mag)); + CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); + CuAssertPtrNotNull(tc, a_find(mag->attribs, &at_skillmod)); + test_cleanup(); +} + +static void test_familiar_age(CuTest *tc) { + unit *mag, *fam; + + test_setup(); + + mag = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + fam = test_create_unit(mag->faction, test_create_region(0, 0, 0)); + set_familiar(mag, fam); + CuAssertPtrEquals(tc, fam, get_familiar(mag)); + CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); + a_age(&fam->attribs, fam); + a_age(&mag->attribs, mag); + CuAssertPtrEquals(tc, fam, get_familiar(mag)); + CuAssertPtrEquals(tc, mag, get_familiar_mage(fam)); + set_number(fam, 0); + a_age(&mag->attribs, mag); + CuAssertPtrEquals(tc, NULL, get_familiar(mag)); + test_cleanup(); +} + + +CuSuite *get_familiar_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_familiar_set); + SUITE_ADD_TEST(suite, test_familiar_age); + return suite; +} + CuSuite *get_magic_suite(void) { CuSuite *suite = CuSuiteNew(); diff --git a/src/test_eressea.c b/src/test_eressea.c index 5361ddf5e..215bdd2c5 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -101,6 +101,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(pool); ADD_SUITE(curse); ADD_SUITE(equipment); + ADD_SUITE(familiar); ADD_SUITE(item); ADD_SUITE(magic); ADD_SUITE(alchemy);