Einfacher Test

Plus Bugfix für Gebäude ohne Improvement, damit diese auch die gleiche
Funktionalität aus der XML nutzen können.
This commit is contained in:
CTD 2015-01-12 16:57:05 +01:00
parent 34fc1b3d34
commit d403afc7fc
2 changed files with 118 additions and 89 deletions

View File

@ -63,125 +63,154 @@ static int add_two(building * b, unit * u, building_bonus bonus) {
static void test_defenders_get_building_bonus(CuTest * tc) static void test_defenders_get_building_bonus(CuTest * tc)
{ {
unit *du, *au; unit *du, *au;
region *r; region *r;
building * bld; building * bld;
fighter *df, *af; fighter *df, *af;
battle *b; battle *b;
side *ds, *as; side *ds, *as;
int diff; int diff;
troop dt, at; troop dt, at;
building_type * btype; building_type * btype;
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
r = findregion(0, 0); r = findregion(0, 0);
btype = bt_get_or_create("castle"); btype = bt_get_or_create("castle");
btype->protection = &add_two; btype->protection = &add_two;
bld = test_create_building(r, btype); bld = test_create_building(r, btype);
bld->size = 10; bld->size = 10;
du = test_create_unit(test_create_faction(rc_find("human")), r); du = test_create_unit(test_create_faction(rc_find("human")), r);
au = test_create_unit(test_create_faction(rc_find("human")), r); au = test_create_unit(test_create_faction(rc_find("human")), r);
u_set_building(du, bld); u_set_building(du, bld);
b = make_battle(r); b = make_battle(r);
ds = make_side(b, du->faction, 0, 0, 0); ds = make_side(b, du->faction, 0, 0, 0);
df = make_fighter(b, du, ds, false); df = make_fighter(b, du, ds, false);
as = make_side(b, au->faction, 0, 0, 0); as = make_side(b, au->faction, 0, 0, 0);
af = make_fighter(b, au, as, true); af = make_fighter(b, au, as, true);
CuAssertPtrEquals(tc, bld, df->building); CuAssertPtrEquals(tc, bld, df->building);
CuAssertPtrEquals(tc, 0, af->building); CuAssertPtrEquals(tc, 0, af->building);
dt.fighter = df; dt.fighter = df;
dt.index = 0; dt.index = 0;
at.fighter = af; at.fighter = af;
at.index = 0; at.index = 0;
diff = skilldiff(at, dt, 0); diff = skilldiff(at, dt, 0);
CuAssertIntEquals(tc, -2, diff); CuAssertIntEquals(tc, -2, diff);
diff = skilldiff(dt, at, 0); diff = skilldiff(dt, at, 0);
CuAssertIntEquals(tc, 0, diff); CuAssertIntEquals(tc, 0, diff);
free_battle(b); free_battle(b);
test_cleanup(); test_cleanup();
} }
static void test_attackers_get_no_building_bonus(CuTest * tc) static void test_attackers_get_no_building_bonus(CuTest * tc)
{ {
unit *au; unit *au;
region *r; region *r;
building * bld; building * bld;
fighter *af; fighter *af;
battle *b; battle *b;
side *as; side *as;
building_type * btype; building_type * btype;
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
r = findregion(0, 0); r = findregion(0, 0);
btype = bt_get_or_create("castle"); btype = bt_get_or_create("castle");
btype->protection = &add_two; btype->protection = &add_two;
bld = test_create_building(r, btype); bld = test_create_building(r, btype);
bld->size = 10; bld->size = 10;
au = test_create_unit(test_create_faction(rc_find("human")), r); au = test_create_unit(test_create_faction(rc_find("human")), r);
u_set_building(au, bld); u_set_building(au, bld);
b = make_battle(r); b = make_battle(r);
as = make_side(b, au->faction, 0, 0, 0); as = make_side(b, au->faction, 0, 0, 0);
af = make_fighter(b, au, as, true); af = make_fighter(b, au, as, true);
CuAssertPtrEquals(tc, 0, af->building); CuAssertPtrEquals(tc, 0, af->building);
free_battle(b); free_battle(b);
test_cleanup(); test_cleanup();
} }
static void test_building_bonus_respects_size(CuTest * tc) static void test_building_bonus_respects_size(CuTest * tc)
{ {
unit *au, *du; unit *au, *du;
region *r; region *r;
building * bld; building * bld;
fighter *af, *df; fighter *af, *df;
battle *b; battle *b;
side *as; side *as;
building_type * btype; building_type * btype;
faction * f; faction * f;
test_cleanup(); test_cleanup();
test_create_world(); test_create_world();
r = findregion(0, 0); r = findregion(0, 0);
btype = bt_get_or_create("castle"); btype = bt_get_or_create("castle");
btype->protection = &add_two; btype->protection = &add_two;
bld = test_create_building(r, btype); bld = test_create_building(r, btype);
bld->size = 10; bld->size = 10;
f = test_create_faction(rc_find("human")); f = test_create_faction(rc_find("human"));
au = test_create_unit(f, r); au = test_create_unit(f, r);
scale_number(au, 9); scale_number(au, 9);
u_set_building(au, bld); u_set_building(au, bld);
du = test_create_unit(f, r); du = test_create_unit(f, r);
u_set_building(du, bld); u_set_building(du, bld);
scale_number(du, 2); scale_number(du, 2);
b = make_battle(r); b = make_battle(r);
as = make_side(b, au->faction, 0, 0, 0); as = make_side(b, au->faction, 0, 0, 0);
af = make_fighter(b, au, as, false); af = make_fighter(b, au, as, false);
df = make_fighter(b, du, as, false); df = make_fighter(b, du, as, false);
CuAssertPtrEquals(tc, bld, af->building); CuAssertPtrEquals(tc, bld, af->building);
CuAssertPtrEquals(tc, 0, df->building); CuAssertPtrEquals(tc, 0, df->building);
free_battle(b); free_battle(b);
test_cleanup(); test_cleanup();
} }
static void test_building_defence_bonus(CuTest * tc)
{
unit *au;
region *r;
building * bld;
building_type * btype;
faction * f;
int def;
test_cleanup();
test_create_world();
r = findregion(0, 0);
register_buildings();
btype = bt_get_or_create("castle");
btype->protection = get_function("building_protection");
btype->construction->defense_bonus = 3;
bld = test_create_building(r, btype);
bld->size = 1;
f = test_create_faction(rc_find("human"));
au = test_create_unit(f, r);
scale_number(au, 1);
u_set_building(au, bld);
def = btype->protection(bld, au, DEFENSE_BONUS);
CuAssertIntEquals(tc, 3, def);
test_cleanup();
}
CuSuite *get_battle_suite(void) CuSuite *get_battle_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_make_fighter); SUITE_ADD_TEST(suite, test_make_fighter);
SUITE_ADD_TEST(suite, test_defenders_get_building_bonus); SUITE_ADD_TEST(suite, test_defenders_get_building_bonus);
SUITE_ADD_TEST(suite, test_attackers_get_no_building_bonus); SUITE_ADD_TEST(suite, test_attackers_get_no_building_bonus);
SUITE_ADD_TEST(suite, test_building_bonus_respects_size); SUITE_ADD_TEST(suite, test_building_bonus_respects_size);
return suite; SUITE_ADD_TEST(suite, test_building_defence_bonus);
return suite;
} }

View File

@ -322,7 +322,7 @@ static int building_protection(building * b, unit * u, building_bonus bonus)
int i = 0; int i = 0;
int bsize = buildingeffsize(b, false); int bsize = buildingeffsize(b, false);
const construction *cons = b->type->construction; const construction *cons = b->type->construction;
if (!cons || !cons->improvement) { if (!cons) {
return 0; return 0;
} }