From 23a91be9c1815899a39e8c55f66dfe6f04f48861 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 29 Aug 2016 08:04:43 +0100 Subject: [PATCH] basic test for skilldiff. --- src/battle.test.c | 48 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/battle.test.c b/src/battle.test.c index f5c4979d7..0d7d73e19 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -203,10 +203,14 @@ static void test_building_defence_bonus(CuTest * tc) } static fighter *setup_fighter(battle **bp, unit *u) { - battle *b; + battle *b = *bp; + side *s; - *bp = b = make_battle(u->region); - return make_fighter(b, u, make_side(b, u->faction, 0, 0, 0), false); + if (!b) { + *bp = b = make_battle(u->region); + } + s = make_side(b, u->faction, 0, 0, 0); + return make_fighter(b, u, s, false); } static void test_natural_armor(CuTest * tc) @@ -229,7 +233,7 @@ static void test_natural_armor(CuTest * tc) static void test_calculate_armor(CuTest * tc) { troop dt; - battle *b; + battle *b = NULL; region *r; unit *du; weapon_type *wtype; @@ -255,6 +259,7 @@ static void test_calculate_armor(CuTest * tc) CuAssertDblEquals_Msg(tc, "magres unmodified", 1.0, magres, 0.01); free_battle(b); + b = NULL; i_change(&du->items, ibelt, 1); dt.fighter = setup_fighter(&b, du); CuAssertIntEquals_Msg(tc, "magical armor", 1, calculate_armor(dt, 0, 0, 0)); @@ -263,6 +268,7 @@ static void test_calculate_armor(CuTest * tc) rc->armor = 0; free_battle(b); + b = NULL; i_change(&du->items, ishield, 1); i_change(&du->items, ichain, 1); dt.fighter = setup_fighter(&b, du); @@ -270,6 +276,7 @@ static void test_calculate_armor(CuTest * tc) CuAssertIntEquals_Msg(tc, "require BF_EQUIPMENT", 1, calculate_armor(dt, 0, 0, 0)); free_battle(b); + b = NULL; rc->battle_flags |= BF_EQUIPMENT; dt.fighter = setup_fighter(&b, du); CuAssertIntEquals_Msg(tc, "stack equipment rc", 5, calculate_armor(dt, 0, 0, 0)); @@ -296,7 +303,7 @@ static void test_calculate_armor(CuTest * tc) static void test_magic_resistance(CuTest *tc) { troop dt; - battle *b; + battle *b = NULL; region *r; unit *du; armor_type *ashield, *achain; @@ -324,6 +331,7 @@ static void test_magic_resistance(CuTest *tc) calculate_armor(dt, 0, 0, &magres); free_battle(b); + b = NULL; i_change(&du->items, ishield, 1); i_change(&du->items, ichain, 1); achain->flags |= ATF_LAEN; @@ -335,6 +343,7 @@ static void test_magic_resistance(CuTest *tc) CuAssertDblEquals_Msg(tc, "laen reduction", 0.81, magres, 0.01); free_battle(b); + b = NULL; i_change(&du->items, ishield, -1); i_change(&du->items, ichain, -1); set_level(du, SK_MAGIC, 2); @@ -359,7 +368,7 @@ static void test_magic_resistance(CuTest *tc) static void test_projectile_armor(CuTest * tc) { troop dt; - battle *b; + battle *b = NULL; region *r; unit *du; weapon_type *wtype; @@ -394,10 +403,37 @@ static void test_projectile_armor(CuTest * tc) test_cleanup(); } +static void test_battle_skilldiff(CuTest *tc) +{ + troop ta, td; + region *r; + unit *ua, *ud; + battle *b = NULL; + + test_cleanup(); + r = test_create_region(0, 0, 0); + ud = test_create_unit(test_create_faction(0), r); + ua = test_create_unit(test_create_faction(0), r); + td.fighter = setup_fighter(&b, ud); + td.index = 0; + ta.fighter = setup_fighter(&b, ua); + ta.index = 0; + ua = test_create_unit(test_create_faction(0), r); + CuAssertIntEquals(tc, 0, skilldiff(ta, td, 0)); + + ta.fighter->person[0].attack = 2; + td.fighter->person[0].defence = 1; + CuAssertIntEquals(tc, 1, skilldiff(ta, td, 0)); + + free_battle(b); + test_cleanup(); +} + CuSuite *get_battle_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_make_fighter); + SUITE_ADD_TEST(suite, test_battle_skilldiff); 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);