From f9130fcb38b9c22835fa369a2f97f6f59c4bba9c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 7 Jul 2015 13:38:14 +0200 Subject: [PATCH] some more easy tests for sabotage. --- src/kernel/ship.c | 15 +++++++++++ src/kernel/ship.h | 5 ++-- src/move.c | 14 ---------- src/move.h | 1 - src/spy.test.c | 65 +++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 78 insertions(+), 22 deletions(-) diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 1ae81b436..c5aded3cd 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -445,3 +445,18 @@ const char *ship_getname(const ship * self) { return self->name; } + +unit *get_captain(const ship * sh) +{ + const region *r = sh->region; + unit *u; + + for (u = r->units; u; u = u->next) { + if (u->ship == sh && u->number + && eff_skill(u, SK_SAILING, r) >= sh->type->cptskill) + return u; + } + + return NULL; +} + diff --git a/src/kernel/ship.h b/src/kernel/ship.h index 809e678a4..f123242df 100644 --- a/src/kernel/ship.h +++ b/src/kernel/ship.h @@ -121,9 +121,10 @@ extern "C" { extern void free_ship(struct ship *s); extern void free_ships(void); - extern const char *ship_getname(const struct ship *self); - extern void ship_setname(struct ship *self, const char *name); + const char *ship_getname(const struct ship *self); + void ship_setname(struct ship *self, const char *name); int shipspeed(const struct ship *sh, const struct unit *u); + struct unit *get_captain(const struct ship *sh); #ifdef __cplusplus } diff --git a/src/move.c b/src/move.c index b14dfb68d..f29dd08ce 100644 --- a/src/move.c +++ b/src/move.c @@ -2130,20 +2130,6 @@ sail(unit * u, order * ord, bool move_on_land, region_list ** routep) } } -unit *get_captain(const ship * sh) -{ - const region *r = sh->region; - unit *u; - - for (u = r->units; u; u = u->next) { - if (u->ship == sh && u->number - && eff_skill(u, SK_SAILING, r) >= sh->type->cptskill) - return u; - } - - return NULL; -} - /* Segeln, Wandern, Reiten * when this routine returns a non-zero value, movement for the region needs * to be done again because of followers that got new MOVE orders. diff --git a/src/move.h b/src/move.h index f1c20d622..f35012b07 100644 --- a/src/move.h +++ b/src/move.h @@ -64,7 +64,6 @@ extern "C" { int enoughsailors(const struct ship *sh, const struct region *r); bool canswim(struct unit *u); bool canfly(struct unit *u); - struct unit *get_captain(const struct ship *sh); void travelthru(const struct unit *u, struct region *r); struct ship *move_ship(struct ship *sh, struct region *from, struct region *to, struct region_list *route); diff --git a/src/spy.test.c b/src/spy.test.c index 46b52e32d..7f97eb171 100644 --- a/src/spy.test.c +++ b/src/spy.test.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -101,10 +102,7 @@ static void test_all_spy_message(CuTest *tc) { test_cleanup(); } -static void test_sabotage_self(CuTest *tc) { - unit *u; - region *r; - order *ord; +static void setup_sabotage(void) { struct locale *lang; test_cleanup(); @@ -112,25 +110,82 @@ static void test_sabotage_self(CuTest *tc) { locale_setstring(lang, parameters[P_SHIP], "SCHIFF"); test_create_world(); init_locales(); +} +static void test_sabotage_self(CuTest *tc) { + unit *u; + region *r; + order *ord; + + setup_sabotage(); r = test_create_region(0, 0, NULL); assert(r); u = test_create_unit(test_create_faction(NULL), r); assert(u && u->faction && u->region == r); u->ship = test_create_ship(r, test_create_shiptype("boat")); assert(u->ship); - ord = create_order(K_SABOTAGE, lang, "SCHIFF"); + ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF"); assert(ord); CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord)); CuAssertPtrEquals(tc, 0, r->ships); test_cleanup(); } + +static void test_sabotage_other_fail(CuTest *tc) { + unit *u, *u2; + region *r; + order *ord; + + setup_sabotage(); + r = test_create_region(0, 0, NULL); + assert(r); + u = test_create_unit(test_create_faction(NULL), r); + u2 = test_create_unit(test_create_faction(NULL), r); + assert(u && u2); + u2->ship = test_create_ship(r, test_create_shiptype("boat")); + assert(u2->ship); + u->ship = u2->ship; + ship_update_owner(u->ship); + assert(ship_owner(u->ship) == u); + ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF"); + assert(ord); + CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord)); + CuAssertPtrNotNull(tc, r->ships); + test_cleanup(); +} + + +static void test_sabotage_other_success(CuTest *tc) { + unit *u, *u2; + region *r; + order *ord; + + setup_sabotage(); + r = test_create_region(0, 0, NULL); + assert(r); + u = test_create_unit(test_create_faction(NULL), r); + u2 = test_create_unit(test_create_faction(NULL), r); + assert(u && u2); + u2->ship = test_create_ship(r, test_create_shiptype("boat")); + assert(u2->ship); + u->ship = u2->ship; + ship_update_owner(u->ship); + assert(ship_owner(u->ship) == u); + ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF"); + assert(ord); + set_level(u2, SK_SPY, 1); + CuAssertIntEquals(tc, 0, sabotage_cmd(u2, ord)); + CuAssertPtrEquals(tc, 0, r->ships); + test_cleanup(); +} CuSuite *get_spy_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_simple_spy_message); SUITE_ADD_TEST(suite, test_all_spy_message); SUITE_ADD_TEST(suite, test_sabotage_self); + SUITE_ADD_TEST(suite, test_sabotage_other_fail); + SUITE_ADD_TEST(suite, test_sabotage_other_success); return suite; }