some more easy tests for sabotage.

This commit is contained in:
Enno Rehling 2015-07-07 13:38:14 +02:00
parent d7e5876c62
commit f9130fcb38
5 changed files with 78 additions and 22 deletions

View File

@ -445,3 +445,18 @@ const char *ship_getname(const ship * self)
{ {
return self->name; 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;
}

View File

@ -121,9 +121,10 @@ extern "C" {
extern void free_ship(struct ship *s); extern void free_ship(struct ship *s);
extern void free_ships(void); extern void free_ships(void);
extern const char *ship_getname(const struct ship *self); const char *ship_getname(const struct ship *self);
extern void ship_setname(struct ship *self, const char *name); void ship_setname(struct ship *self, const char *name);
int shipspeed(const struct ship *sh, const struct unit *u); int shipspeed(const struct ship *sh, const struct unit *u);
struct unit *get_captain(const struct ship *sh);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -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 /* Segeln, Wandern, Reiten
* when this routine returns a non-zero value, movement for the region needs * 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. * to be done again because of followers that got new MOVE orders.

View File

@ -64,7 +64,6 @@ extern "C" {
int enoughsailors(const struct ship *sh, const struct region *r); int enoughsailors(const struct ship *sh, const struct region *r);
bool canswim(struct unit *u); bool canswim(struct unit *u);
bool canfly(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); void travelthru(const struct unit *u, struct region *r);
struct ship *move_ship(struct ship *sh, struct region *from, struct ship *move_ship(struct ship *sh, struct region *from,
struct region *to, struct region_list *route); struct region *to, struct region_list *route);

View File

@ -5,6 +5,7 @@
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/ship.h>
#include <kernel/order.h> #include <kernel/order.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/messages.h> #include <kernel/messages.h>
@ -101,10 +102,7 @@ static void test_all_spy_message(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_sabotage_self(CuTest *tc) { static void setup_sabotage(void) {
unit *u;
region *r;
order *ord;
struct locale *lang; struct locale *lang;
test_cleanup(); test_cleanup();
@ -112,25 +110,82 @@ static void test_sabotage_self(CuTest *tc) {
locale_setstring(lang, parameters[P_SHIP], "SCHIFF"); locale_setstring(lang, parameters[P_SHIP], "SCHIFF");
test_create_world(); test_create_world();
init_locales(); init_locales();
}
static void test_sabotage_self(CuTest *tc) {
unit *u;
region *r;
order *ord;
setup_sabotage();
r = test_create_region(0, 0, NULL); r = test_create_region(0, 0, NULL);
assert(r); assert(r);
u = test_create_unit(test_create_faction(NULL), r); u = test_create_unit(test_create_faction(NULL), r);
assert(u && u->faction && u->region == r); assert(u && u->faction && u->region == r);
u->ship = test_create_ship(r, test_create_shiptype("boat")); u->ship = test_create_ship(r, test_create_shiptype("boat"));
assert(u->ship); assert(u->ship);
ord = create_order(K_SABOTAGE, lang, "SCHIFF"); ord = create_order(K_SABOTAGE, u->faction->locale, "SCHIFF");
assert(ord); assert(ord);
CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord)); CuAssertIntEquals(tc, 0, sabotage_cmd(u, ord));
CuAssertPtrEquals(tc, 0, r->ships); CuAssertPtrEquals(tc, 0, r->ships);
test_cleanup(); 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 *get_spy_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_simple_spy_message); SUITE_ADD_TEST(suite, test_simple_spy_message);
SUITE_ADD_TEST(suite, test_all_spy_message); SUITE_ADD_TEST(suite, test_all_spy_message);
SUITE_ADD_TEST(suite, test_sabotage_self); 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; return suite;
} }