diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 22311b919..e6a529ee3 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -289,7 +289,6 @@ void getshipweight(const ship * sh, int *sweight, int *scabins) unit *shipowner(const ship * sh) { -#ifndef NDEBUG unit *u; unit *first = NULL; @@ -301,7 +300,7 @@ unit *shipowner(const ship * sh) if (!first && u->number > 0) first = u; if (fval(u, UFL_OWNER) && u->number > 0) - break; + return u; if (u->number == 0) freset(u, UFL_OWNER); } @@ -310,13 +309,9 @@ unit *shipowner(const ship * sh) /* Eigentümer tot oder kein Eigentümer vorhanden. Erste lebende Einheit * nehmen. */ - if (first && !u) { - u = first; - fset(u, UFL_OWNER); - } - assert(u==sh->owner); -#endif - return sh->owner; + if (first) + fset(first, UFL_OWNER); + return first; } void write_ship_reference(const struct ship *sh, struct storage *store) diff --git a/src/kernel/ship_test.c b/src/kernel/ship_test.c index eee901b05..33bbb3cd3 100644 --- a/src/kernel/ship_test.c +++ b/src/kernel/ship_test.c @@ -24,12 +24,46 @@ static void test_register_ship(CuTest * tc) CuAssertPtrNotNull(tc, st_find("herp")); } -static void test_shipowner(CuTest * tc) +static void test_shipowner_goes_to_next(CuTest * tc) { struct region *r; - struct ship * sh; - struct unit * u; - struct faction * f; + struct ship *sh; + struct unit *u, *u2; + struct faction *f; + const struct ship_type *stype; + const struct race *human; + + test_cleanup(); + test_create_world(); + + human = rc_find("human"); + CuAssertPtrNotNull(tc, human); + + stype = st_find("boat"); + CuAssertPtrNotNull(tc, stype); + + f = test_create_faction(human); + r = findregion(0, 0); + + sh = test_create_ship(r, stype); + CuAssertPtrNotNull(tc, sh); + + u = test_create_unit(f, r); + u2 = test_create_unit(f, r); + CuAssertPtrNotNull(tc, u); + u_set_ship(u, sh); + u_set_ship(u2, sh); + CuAssertPtrEquals(tc, u, shipowner(sh)); + u->number = 0; + CuAssertPtrEquals(tc, u2, shipowner(sh)); +} + +static void test_shipowner_resets_when_dead(CuTest * tc) +{ + struct region *r; + struct ship *sh; + struct unit *u; + struct faction *f; const struct ship_type *stype; const struct race *human; @@ -52,12 +86,15 @@ static void test_shipowner(CuTest * tc) CuAssertPtrNotNull(tc, u); u_set_ship(u, sh); CuAssertPtrEquals(tc, u, shipowner(sh)); + u->number = 0; + CuAssertPtrEquals(tc, 0, shipowner(sh)); } CuSuite *get_ship_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_register_ship); - SUITE_ADD_TEST(suite, test_shipowner); + SUITE_ADD_TEST(suite, test_shipowner_resets_when_dead); + SUITE_ADD_TEST(suite, test_shipowner_goes_to_next); return suite; }