From a463a8a351344f7df912ac9c7bb01a5d11cff11c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 3 Mar 2024 12:58:58 +0100 Subject: [PATCH] refactor tests that use castles --- src/battle.test.c | 14 +++++-------- src/kernel/build.test.c | 37 +++----------------------------- src/kernel/building.test.c | 43 +++++++++++++++----------------------- src/kernel/region.test.c | 4 ++-- src/laws.test.c | 16 +++++++------- src/tests.c | 6 ++---- 6 files changed, 37 insertions(+), 83 deletions(-) diff --git a/src/battle.test.c b/src/battle.test.c index d949ec728..98e2fd8fa 100644 --- a/src/battle.test.c +++ b/src/battle.test.c @@ -219,10 +219,6 @@ static void test_select_armor(CuTest *tc) { test_teardown(); } -static building_type * setup_castle(void) { - return test_create_castle(); -} - static void test_defenders_get_building_bonus(CuTest * tc) { unit *du, *au; @@ -235,7 +231,7 @@ static void test_defenders_get_building_bonus(CuTest * tc) building_type * btype; test_setup(); - btype = setup_castle(); + btype = test_create_castle(); r = test_create_plain(0, 0); bld = test_create_building(r, btype); @@ -288,7 +284,7 @@ static void test_attackers_get_no_building_bonus(CuTest * tc) test_setup(); r = test_create_plain(0, 0); - btype = setup_castle(); + btype = test_create_castle(); btype->flags |= BTF_FORTIFICATION; bld = test_create_building(r, btype); bld->size = 10; @@ -317,7 +313,7 @@ static void test_building_bonus_respects_size(CuTest * tc) faction * f; test_setup(); - btype = setup_castle(); + btype = test_create_castle(); r = test_create_plain(0, 0); btype->flags |= BTF_FORTIFICATION; bld = test_create_building(r, btype); @@ -347,7 +343,7 @@ static void test_building_defense_bonus(CuTest * tc) building_type * btype; test_setup(); - btype = setup_castle(); + btype = test_create_castle(); btype->maxsize = -1; /* unlimited buildings get the castle bonus */ CuAssertIntEquals(tc, 0, bt_protection(btype, 0)); @@ -988,7 +984,7 @@ static void test_battle_skilldiff_building(CuTest *tc) building_type *btype; test_setup(); - btype = setup_castle(); + btype = test_create_castle(); r = test_create_plain(0, 0); ud = test_create_unit(test_create_faction(), r); diff --git a/src/kernel/build.test.c b/src/kernel/build.test.c index a59087f30..6b0da949d 100644 --- a/src/kernel/build.test.c +++ b/src/kernel/build.test.c @@ -41,7 +41,7 @@ static unit * setup_build(build_fixture *bf) { test_create_itemtype("stone"); test_create_itemtype("log"); - bf->btype = test_create_buildingtype("castle"); + bf->btype = test_create_castle(); bf->stype = test_create_shiptype("boat"); bf->rc = test_create_race("human"); bf->r = test_create_plain(0, 0); @@ -67,37 +67,6 @@ static unit * setup_build(build_fixture *bf) { return bf->u; } -static building_type *setup_castle(item_type *it_stone) { - building_type *btype; - building_stage *stage; - construction * cons; - - btype = test_create_buildingtype("castle"); - stage = btype->stages = calloc(1, sizeof(building_stage)); - if (!stage) abort(); - cons = &stage->construction; - cons->materials = calloc(2, sizeof(requirement)); - if (!cons->materials) abort(); - cons->materials[0].number = 1; - cons->materials[0].rtype = it_stone->rtype; - cons->minskill = 1; - cons->maxsize = 2; - cons->reqsize = 1; - cons->skill = SK_BUILDING; - stage = stage->next = calloc(1, sizeof(building_stage)); - if (!stage) abort(); - cons = &stage->construction; - cons->materials = calloc(2, sizeof(requirement)); - if (!cons->materials) abort(); - cons->materials[0].number = 1; - cons->materials[0].rtype = it_stone->rtype; - cons->minskill = 1; - cons->maxsize = 8; - cons->reqsize = 1; - cons->skill = SK_BUILDING; - return btype; -} - static void test_build_building_stages(CuTest *tc) { building_type *btype; item_type *it_stone; @@ -106,7 +75,7 @@ static void test_build_building_stages(CuTest *tc) { test_setup(); init_resources(); it_stone = test_create_itemtype("stone"); - btype = setup_castle(it_stone); + btype = test_create_castle(); u = test_create_unit(test_create_faction(), test_create_plain(0, 0)); u->building = test_create_building(u->region, btype); u->building->size = 1; @@ -128,7 +97,7 @@ static void test_build_building_stage_continue(CuTest *tc) { test_setup(); init_resources(); it_stone = test_create_itemtype("stone"); - btype = setup_castle(it_stone); + btype = test_create_castle(); u = test_create_unit(test_create_faction(), test_create_plain(0, 0)); set_level(u, SK_BUILDING, 2); i_change(&u->items, it_stone, 4); diff --git a/src/kernel/building.test.c b/src/kernel/building.test.c index 70f0ace89..19f148ec4 100644 --- a/src/kernel/building.test.c +++ b/src/kernel/building.test.c @@ -319,7 +319,7 @@ static void test_buildingtype_exists(CuTest * tc) test_setup(); - btype2 = test_create_buildingtype("castle"); + btype2 = test_create_castle(); assert(btype2); btype = test_create_buildingtype("lighhouse"); btype->maxsize = 10; @@ -356,7 +356,7 @@ static void test_active_building(CuTest *tc) { test_setup(); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); assert(btype && btype->maxsize == -1); b = test_create_building(r = test_create_plain(0, 0), btype); u = test_create_unit(test_create_faction(), r); @@ -384,20 +384,20 @@ static void test_safe_building(CuTest *tc) { unit *u1, *u2; test_setup(); - btype = test_create_buildingtype("castle"); + btype = test_create_buildingtype("house"); u1 = test_create_unit(test_create_faction(), test_create_plain(0, 0)); u2 = test_create_unit(test_create_faction(), test_create_plain(0, 0)); - CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + CuAssertTrue(tc, !in_safe_building(u1, u2)); u1->building = test_create_building(u1->region, btype); - CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + CuAssertTrue(tc, !in_safe_building(u1, u2)); btype->flags |= BTF_FORTIFICATION; - CuAssertIntEquals(tc, true, in_safe_building(u1, u2)); + CuAssertTrue(tc, in_safe_building(u1, u2)); u2->building = u1->building; - CuAssertIntEquals(tc, true, in_safe_building(u1, u2)); + CuAssertTrue(tc, in_safe_building(u1, u2)); u1->number = 2; - CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + CuAssertTrue(tc, !in_safe_building(u1, u2)); u1->building->size = 3; - CuAssertIntEquals(tc, false, in_safe_building(u1, u2)); + CuAssertTrue(tc, !in_safe_building(u1, u2)); test_teardown(); } @@ -407,7 +407,7 @@ static void test_visible_building(CuTest *tc) { test_setup(); bt_light = test_create_buildingtype("lighthouse"); - bt_castle = test_create_buildingtype("castle"); + bt_castle = test_create_castle(); b = test_create_building(test_create_plain(0, 0), bt_light); CuAssertPtrEquals(tc, bt_light, (void *)visible_building(b)); @@ -433,8 +433,8 @@ static void test_cmp_castle_size(CuTest *tc) { test_setup(); r = test_create_plain(0, 0); - b1 = test_create_building(r, NULL); - b2 = test_create_building(r, NULL); + b1 = test_create_building(r, test_create_castle()); + b2 = test_create_building(r, test_create_castle()); u1 = test_create_unit(test_create_faction(), r); u_set_building(u1, b1); u2 = test_create_unit(test_create_faction(), r); @@ -451,21 +451,12 @@ static void test_wage(CuTest *tc) { region *r; building *b; building_type *btype; - struct building_stage *stage; race *rc_orc, *rc_elf; test_setup(); rc_orc = test_create_race("orc"); rc_elf = test_create_race("elf"); rc_elf->maintenance = 13; - btype = test_create_buildingtype("castle"); - stage = btype->stages; - stage->construction.maxsize = 2; /* site */ - stage = stage->next = calloc(1, sizeof(struct building_stage)); - stage->construction.maxsize = 8; /* tradepost */ - stage = stage->next = calloc(1, sizeof(struct building_stage)); - stage->construction.maxsize = 40; /* fortification */ - stage = stage->next = calloc(1, sizeof(struct building_stage)); - stage->construction.maxsize = 200; /* tower */ + btype = test_create_castle(); r = test_create_plain(0, 0); CuAssertIntEquals(tc, 10, wage(r, rc_elf)); CuAssertIntEquals(tc, 10, wage(r, rc_orc)); @@ -524,7 +515,7 @@ static void test_cmp_wage(CuTest *tc) { building_type *btype; test_setup(); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); btype->taxes = 100; r = test_create_plain(0, 0); b1 = test_create_building(r, btype); @@ -545,7 +536,7 @@ static void test_cmp_taxes(CuTest *tc) { unit *u1, *u2; test_setup(); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); btype->taxes = 100; r = test_create_plain(0, 0); b1 = test_create_building(r, btype); @@ -576,7 +567,7 @@ static void test_cmp_current_owner(CuTest *tc) { btype->stages->construction.maxsize = 1; btype->taxes = 200; b1 = test_create_building(r, btype); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); btype->stages->construction.maxsize = 1; btype->taxes = 100; b2 = test_create_building(r, btype); @@ -603,7 +594,7 @@ static void test_building_effsize(CuTest *tc) { construction *cons; test_setup(); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); stage = btype->stages; assert(stage); cons = &stage->construction; diff --git a/src/kernel/region.test.c b/src/kernel/region.test.c index 2166e1ace..e4710f7a3 100644 --- a/src/kernel/region.test.c +++ b/src/kernel/region.test.c @@ -58,8 +58,8 @@ static void test_region_get_owner(CuTest *tc) { test_setup(); r = test_create_plain(0, 0); - b1 = test_create_building(r, NULL); - b2 = test_create_building(r, NULL); + b1 = test_create_building(r, test_create_castle()); + b2 = test_create_building(r, test_create_castle()); b1->size = 5; b2->size = 10; u1 = test_create_unit(test_create_faction(), r); diff --git a/src/laws.test.c b/src/laws.test.c index 3beb17a35..628335e1d 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -75,7 +75,7 @@ static void test_rename_building(CuTest * tc) test_setup(); test_create_locale(); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); r = test_create_plain(0, 0); b = new_building(btype, r, default_locale, 1); f = test_create_faction(); @@ -98,7 +98,7 @@ static void test_rename_building_twice(CuTest * tc) test_setup(); test_create_locale(); - btype = test_create_buildingtype("castle"); + btype = test_create_castle(); r = test_create_plain(0, 0); b = new_building(btype, r, default_locale, 1); f = test_create_faction(); @@ -125,7 +125,7 @@ static void test_enter_building(CuTest * tc) r = test_create_plain(0, 0); rc = test_create_race("human"); u = test_create_unit(test_create_faction_ex(rc, NULL), r); - b = test_create_building(r, test_create_buildingtype("castle")); + b = test_create_building(r, test_create_castle()); rc->flags = RCF_WALK; u->building = 0; @@ -700,7 +700,7 @@ static void setup_pay_cmd(struct pay_fixture *fix) { test_setup(); f = test_create_faction(); r = test_create_plain(0, 0); - btcastle = test_create_buildingtype("castle"); + btcastle = test_create_castle(); btcastle->taxes = 100; b = test_create_building(r, btcastle); assert(b); @@ -1154,7 +1154,7 @@ static void test_name_region(CuTest *tc) { order *ord; u = setup_name_cmd(); - u_set_building(u, test_create_building(u->region, NULL)); + u_set_building(u, test_create_building(u->region, test_create_castle())); f = u->faction; ord = create_order(K_NAME, f->locale, "%s ' Hodor Hodor '", param_name(P_REGION, f->locale)); @@ -1399,7 +1399,7 @@ static void test_name_cmd(CuTest *tc) { free_order(ord); ord = create_order(K_NAME, f->locale, "%s ' Ho\tdor '", param_name(P_BUILDING, f->locale)); - u_set_building(u, test_create_building(u->region, NULL)); + u_set_building(u, test_create_building(u->region, test_create_castle())); name_cmd(u, ord); CuAssertStrEquals(tc, "Hodor", u->building->name); free_order(ord); @@ -1445,9 +1445,9 @@ static void test_name_cmd_2274(CuTest *tc) { u1 = test_create_unit(test_create_faction(), r); u2 = test_create_unit(test_create_faction(), r); u3 = test_create_unit(u2->faction, r); - u_set_building(u1, test_create_building(r, NULL)); + u_set_building(u1, test_create_building(r, test_create_castle())); u1->building->size = 10; - u_set_building(u2, test_create_building(r, NULL)); + u_set_building(u2, test_create_building(r, test_create_castle())); u2->building->size = 20; f = u2->faction; diff --git a/src/tests.c b/src/tests.c index 41d1e8c66..f0d8ccb8e 100644 --- a/src/tests.c +++ b/src/tests.c @@ -352,9 +352,7 @@ building * test_create_building(region * r, const building_type * btype) building * b; assert(r); if (!btype) { - building_type *bt_castle = test_create_buildingtype("castle"); - bt_castle->flags |= BTF_FORTIFICATION; - btype = bt_castle; + btype = test_create_buildingtype("test"); } b = new_building(btype, r, default_locale, (btype->maxsize > 0) ? btype->maxsize : 1); return b; @@ -362,7 +360,7 @@ building * test_create_building(region * r, const building_type * btype) ship * test_create_ship(region * r, const ship_type * stype) { - ship * s = new_ship(stype ? stype : test_create_shiptype("boat"), r, default_locale); + ship * s = new_ship(stype ? stype : test_create_shiptype("test"), r, default_locale); s->size = s->type->construction ? s->type->construction->maxsize : 1; return s; }