From 36b58cca6e0ec798731c44fa0b032b74f6cfe447 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 15 Mar 2011 21:04:38 -0700 Subject: [PATCH] test that fighters get created properly. better test initialization code. --- src/kernel/battle_test.c | 80 ++++++++++++++++++++++++++++++++++++++++ src/kernel/item.c | 51 ++++++++++++++++++------- src/kernel/item.h | 5 ++- src/kernel/terrain.h | 2 +- src/tests.c | 21 ++++++++++- 5 files changed, 143 insertions(+), 16 deletions(-) diff --git a/src/kernel/battle_test.c b/src/kernel/battle_test.c index 89f2f15b4..ceac668c7 100644 --- a/src/kernel/battle_test.c +++ b/src/kernel/battle_test.c @@ -3,12 +3,54 @@ #include "battle.h" #include "building.h" #include "faction.h" +#include "item.h" #include "race.h" #include "region.h" +#include "skill.h" #include "unit.h" #include "tests.h" #include +static void test_make_fighter(CuTest * tc) +{ + unit *au; + region *r; + fighter *af; + battle *b; + side *as; + faction * f; + + test_cleanup(); + test_create_world(); + r = findregion(0, 0); + f = test_create_faction(rc_find("human")); + au = test_create_unit(f, r); + skill_enabled[SK_MAGIC] = 1; + skill_enabled[SK_RIDING] = 1; + set_level(au, SK_MAGIC, 3); + set_level(au, SK_RIDING, 3); + au->status = ST_BEHIND; + set_item(au, I_HORSE, 1); + + b = make_battle(r); + as = make_side(b, au->faction, 0, 0, 0); + af = make_fighter(b, au, as, false); + + CuAssertIntEquals(tc, 1, b->nfighters); + CuAssertPtrEquals(tc, 0, af->building); + CuAssertPtrEquals(tc, as, af->side); + CuAssertIntEquals(tc, 0, af->run.hp); + CuAssertIntEquals(tc, ST_BEHIND, af->status); + CuAssertIntEquals(tc, 0, af->run.number); + CuAssertIntEquals(tc, au->hp, af->person[0].hp); + CuAssertIntEquals(tc, 1, af->person[0].speed); + CuAssertIntEquals(tc, au->number, af->alive); + CuAssertIntEquals(tc, 0, af->removed); + CuAssertIntEquals(tc, 3, af->magic); + CuAssertIntEquals(tc, 1, af->horses); + CuAssertIntEquals(tc, 0, af->elvenhorses); +} + static int add_two(building * b, unit * u) { return 2; } @@ -87,10 +129,48 @@ static void test_attackers_get_no_building_bonus(CuTest * tc) CuAssertPtrEquals(tc, 0, af->building); } +static void test_building_bonus_respects_size(CuTest * tc) +{ + unit *au, *du; + region *r; + building * bld; + fighter *af, *df; + battle *b; + side *as; + building_type * btype; + faction * f; + + test_cleanup(); + test_create_world(); + r = findregion(0, 0); + btype = bt_find("castle"); + btype->protection = &add_two; + bld = test_create_building(r, btype); + bld->size = 10; + + f = test_create_faction(rc_find("human")); + au = test_create_unit(f, r); + scale_number(au, 9); + au->building = bld; + du = test_create_unit(f, r); + du->building = bld; + scale_number(du, 2); + + b = make_battle(r); + as = make_side(b, au->faction, 0, 0, 0); + af = make_fighter(b, au, as, false); + df = make_fighter(b, du, as, false); + + CuAssertPtrEquals(tc, bld, af->building); + CuAssertPtrEquals(tc, 0, df->building); +} + CuSuite *get_battle_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_make_fighter); SUITE_ADD_TEST(suite, test_defenders_get_building_bonus); SUITE_ADD_TEST(suite, test_attackers_get_no_building_bonus); + SUITE_ADD_TEST(suite, test_building_bonus_respects_size); return suite; } diff --git a/src/kernel/item.c b/src/kernel/item.c index 72f21cac8..08d9b2ca4 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -151,7 +151,7 @@ resource_type *new_resourcetype(const char **names, const char **appearances, if (rtype == NULL) { int i; - rtype = calloc(sizeof(resource_type), 1); + rtype = (resource_type *)calloc(sizeof(resource_type), 1); for (i = 0; i != 2; ++i) { rtype->_name[i] = strdup(names[i]); @@ -284,7 +284,7 @@ potion_type *new_potiontype(item_type * itype, int level) assert(resource2potion(itype->rtype) == NULL); - ptype = calloc(sizeof(potion_type), 1); + ptype = (potion_type *)calloc(sizeof(potion_type), 1); ptype->itype = itype; ptype->level = level; pt_register(ptype); @@ -628,7 +628,10 @@ int get_item(const unit * u, item_t it) int set_item(unit * u, item_t it, int value) { const item_type *type = olditemtype[it]; - item *i = *i_find(&u->items, type); + item *i; + + assert(type); + i = *i_find(&u->items, type); if (!i) { i = i_add(&u->items, i_new(type, value)); } else { @@ -740,11 +743,6 @@ mod_dwarves_only(const unit * u, const region * r, skill_t sk, int value) static void init_olditems(void) { item_t i; -#if 0 - resource_type *rtype; - const struct locale *lang = find_locale("de"); - assert(lang); -#endif for (i = 0; i != MAXITEMS; ++i) { /* item is defined in XML file, but IT_XYZ enum still in use */ @@ -969,9 +967,8 @@ resource_type *r_silver; resource_type *r_aura; resource_type *r_permaura; resource_type *r_unit; -resource_type *r_hp; +static resource_type *r_hp; -resource_type *r_silver; item_type *i_silver; static const char *names[] = { @@ -986,10 +983,9 @@ static const char *names[] = { void init_resources(void) { - static boolean initialized = false; - if (initialized) + if (r_hp) { return; - initialized = true; + } /* silver was never an item: */ r_silver = new_resourcetype(&names[0], NULL, RTF_ITEM | RTF_POOLED); @@ -1199,6 +1195,35 @@ static item *default_spoil(const struct race *rc, int size) return itm; } +#ifndef DISABLE_TESTS +void test_clear_resources(void) +{ + int i; + + for (i=0;i!=IMAXHASH;++i) { + item_type * itype = itemtypes[i]; + if (itype) { + itemtypes[i] = 0; + free(itype->construction); + free(itype); + } + } + + while (resourcetypes) { + resource_type * rtype = resourcetypes; + resourcetypes = rtype->next; + free(rtype->_name[0]); + free(rtype->_name[1]); + free(rtype->_appearance[0]); + free(rtype->_appearance[1]); + free(rtype); + } + resourcetypes = 0; + r_hp = 0; + init_resources(); +} +#endif + void register_resources(void) { register_function((pf_generic) mod_elves_only, "mod_elves_only"); diff --git a/src/kernel/item.h b/src/kernel/item.h index d4ce242f2..b08ff2b35 100644 --- a/src/kernel/item.h +++ b/src/kernel/item.h @@ -273,7 +273,6 @@ extern "C" { extern resource_type *r_aura; extern resource_type *r_permaura; extern resource_type *r_unit; - extern resource_type *r_hp; enum { I_IRON, /* 0 */ @@ -363,6 +362,10 @@ extern "C" { extern struct item_type *i_silver; +#ifndef DISABLE_TESTS + void test_clear_resources(void); +#endif + #ifdef __cplusplus } #endif diff --git a/src/kernel/terrain.h b/src/kernel/terrain.h index fca223e01..355c66fd2 100644 --- a/src/kernel/terrain.h +++ b/src/kernel/terrain.h @@ -28,7 +28,7 @@ extern "C" { #define SEA_REGION (1<<1) /* hier braucht man ein Boot */ #define FOREST_REGION (1<<2) /* Elfen- und Kampfvorteil durch Bäume */ #define ARCTIC_REGION (1<<3) /* Gletscher & co = Keine Insekten! */ -#define CAVALRY_REGION (1<<4) /* Gletscher & co = Keine Insekten! */ +#define CAVALRY_REGION (1<<4) /* riding in combat is possible */ /* Achtung: SEA_REGION ist nicht das Gegenteil von LAND_REGION. Die zwei schliessen sich nichtmal aus! */ #define FORBIDDEN_REGION (1<<5) /* unpassierbare Blockade-struct region */ #define SAIL_INTO (1<<6) /* man darf hierhin segeln */ diff --git a/src/tests.c b/src/tests.c index fa5b6e315..118910c05 100644 --- a/src/tests.c +++ b/src/tests.c @@ -85,6 +85,7 @@ struct unit *test_create_unit(struct faction *f, struct region *r) void test_cleanup(void) { test_clear_terrains(); + test_clear_resources(); global.functions.maintenance = NULL; global.functions.wage = NULL; free_gamedata(); @@ -109,6 +110,17 @@ building * test_create_building(region * r, const building_type * btype) b->size = btype->maxsize>0?btype->maxsize:1; return b; } + +item_type * test_create_itemtype(const char ** names) { + resource_type * rtype; + item_type * itype; + + rtype = new_resourcetype(names, NULL, RTF_ITEM); + itype = new_itemtype(rtype, ITF_ANIMAL|ITF_BIG, 5000, 7000); + + return itype; +} + /** creates a small world and some stuff in it. * two terrains: 'plain' and 'ocean' * one race: 'human' @@ -124,9 +136,16 @@ void test_create_world(void) int i; building_type *btype; ship_type *stype; + item_type * itype; + const char * names[2] = { "horse", "horse_p" }; - t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO); + itype = test_create_itemtype(names); + olditemtype[I_HORSE] = itype; + + t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION); + t_plain->size = 1000; t_ocean = test_create_terrain("ocean", SEA_REGION | SAIL_INTO | SWIM_INTO); + t_ocean->size = 0; island[0] = test_create_region(0, 0, t_plain); island[1] = test_create_region(1, 0, t_plain);