adding more tests.

test for build()
test for scale_number()
update submodules
This commit is contained in:
Enno Rehling 2014-08-26 20:06:28 +02:00
parent 2ceeb0b819
commit d435c13bfc
9 changed files with 1265 additions and 1069 deletions

@ -1 +1 @@
Subproject commit 2a7af5e2347217ea4efcf7fb3f0bc9c2681d1a17
Subproject commit 4ab2cfe0a6dd6d57ebb460f9b824f68dba50b7de

@ -1 +1 @@
Subproject commit f84066fb7d3254bdd9e89694acc4c1c20d001eed
Subproject commit fe53382982d0f417b6f7ad0fe08e32393cb07c21

View File

@ -4,6 +4,7 @@ SET(_TEST_FILES
build.test.c
config.test.c
faction.test.c
unit.test.c
save.test.c
ship.test.c
spell.test.c

View File

@ -521,9 +521,7 @@ int build(unit * u, const construction * ctype, int completed, int want)
}
}
if (want > 0) {
n = _min(want, n);
}
if (want < n) n = want;
if (type->maxsize > 0) {
n = _min(type->maxsize - completed, n);

View File

@ -1,5 +1,6 @@
#include <platform.h>
#include <kernel/config.h>
#include <kernel/alchemy.h>
#include "types.h"
#include "build.h"
#include "order.h"
@ -21,6 +22,7 @@ typedef struct build_fixture {
unit *u;
region *r;
race *rc;
construction cons;
} build_fixture;
static unit * setup_build(build_fixture *bf) {
@ -32,37 +34,159 @@ static unit * setup_build(build_fixture *bf) {
assert(bf->rc && bf->f && bf->r);
bf->u = test_create_unit(bf->f, bf->r);
assert(bf->u);
bf->cons.materials = calloc(2, sizeof(requirement));
bf->cons.materials[0].number = 1;
bf->cons.materials[0].rtype = get_resourcetype(R_SILVER);
bf->cons.skill = SK_ARMORER;
bf->cons.minskill = 2;
bf->cons.maxsize = -1;
bf->cons.reqsize = 1;
return bf->u;
}
static void test_build(CuTest *tc) {
build_fixture bf;
static void test_build_requires_materials(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct item_type *itype;
u = setup_build(&bf);
set_level(u, SK_ARMORER, 2);
CuAssertIntEquals(tc, ENOMATERIALS, build(u, &bf.cons, 0, 1));
itype = bf.cons.materials[0].rtype->itype;
i_change(&u->items, itype, 2);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1));
CuAssertIntEquals(tc, 1, i_get(u->items, itype));
}
static void test_build_requires_building(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
building_type *btype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, 2);
bf.cons.btype = btype = bt_get_or_create("hodor");
btype->maxcapacity = 1;
btype->capacity = 1;
CuAssertIntEquals_Msg(tc, "must be inside a production building", EBUILDINGREQ, build(u, &bf.cons, 0, 1));
u->building = test_create_building(u->region, btype);
fset(u->building, BLD_WORKING);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 1));
btype->maxcapacity = 0;
CuAssertIntEquals_Msg(tc, "cannot build when production building capacity exceeded", EBUILDINGREQ, build(u, &bf.cons, 0, 1));
}
static void test_build_failure_missing_skill(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
construction cons = { 0 };
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = get_resourcetype(R_SILVER);
assert(rtype);
cons.materials = calloc(2, sizeof(requirement));
cons.materials[0].number = 1;
cons.materials[0].rtype = rtype;
cons.skill = SK_ARMORER;
cons.minskill = 2;
cons.reqsize = 1;
CuAssertIntEquals(tc, ENEEDSKILL, build(u, &cons, 1, 1));
set_level(u, SK_ARMORER, 1);
CuAssertIntEquals(tc, ELOWSKILL, build(u, &cons, 1, 1));
set_level(u, SK_ARMORER, 2);
CuAssertIntEquals(tc, ENOMATERIALS, build(u, &cons, 1, 1));
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
CuAssertIntEquals(tc, 1, build(u, &cons, 1, 1));
CuAssertIntEquals(tc, ENEEDSKILL, build(u, &bf.cons, 1, 1));
}
static void test_build_failure_low_skill(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill-1);
CuAssertIntEquals(tc, ELOWSKILL, build(u, &bf.cons, 0, 10));
}
static void test_build_failure_completed(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill);
bf.cons.maxsize = 1;
CuAssertIntEquals(tc, ECOMPLETE, build(u, &bf.cons, bf.cons.maxsize, 10));
CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype));
}
static void test_build_limits(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
i_change(&u->items, rtype->itype, 1);
set_level(u, SK_ARMORER, bf.cons.minskill);
CuAssertIntEquals(tc, 1, build(u, &bf.cons, 0, 10));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
scale_number(u, 2);
set_level(u, SK_ARMORER, bf.cons.minskill);
i_change(&u->items, rtype->itype, 2);
CuAssertIntEquals(tc, 2, build(u, &cons, 2, 2));
CuAssertIntEquals(tc, 2, build(u, &bf.cons, 0, 10));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
scale_number(u, 2);
set_level(u, SK_ARMORER, bf.cons.minskill * 2);
i_change(&u->items, rtype->itype, 4);
CuAssertIntEquals(tc, 4, build(u, &bf.cons, 0, 10));
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
test_cleanup();
}
static void test_build_with_ring(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
item_type *ring;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
ring = it_get_or_create(rt_get_or_create("roqf"));
assert(rtype && ring);
set_level(u, SK_ARMORER, bf.cons.minskill);
i_change(&u->items, rtype->itype, 20);
i_change(&u->items, ring, 1);
CuAssertIntEquals(tc, 10, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 10, i_get(u->items, rtype->itype));
test_cleanup();
}
static void test_build_with_potion(CuTest *tc) {
build_fixture bf = { 0 };
unit *u;
const potion_type *ptype;
const struct resource_type *rtype;
u = setup_build(&bf);
rtype = bf.cons.materials[0].rtype;
oldpotiontype[P_DOMORE] = ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1);
assert(rtype && ptype);
i_change(&u->items, rtype->itype, 20);
change_effect(u, ptype, 4);
set_level(u, SK_ARMORER, bf.cons.minskill);
CuAssertIntEquals(tc, 2, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 18, i_get(u->items, rtype->itype));
CuAssertIntEquals(tc, 3, get_effect(u, ptype));
set_level(u, SK_ARMORER, bf.cons.minskill*2);
CuAssertIntEquals(tc, 4, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
set_level(u, SK_ARMORER, bf.cons.minskill);
scale_number(u, 2); // OBS: this scales the effects, too:
CuAssertIntEquals(tc, 4, get_effect(u, ptype));
CuAssertIntEquals(tc, 4, build(u, &bf.cons, 0, 20));
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
test_cleanup();
}
@ -82,7 +206,7 @@ static void test_build_building_no_materials(CuTest *tc) {
static void test_build_building_with_golem(CuTest *tc) {
unit *u;
build_fixture bf;
build_fixture bf = { 0 };
const building_type *btype;
u = setup_build(&bf);
@ -92,7 +216,7 @@ static void test_build_building_with_golem(CuTest *tc) {
assert(btype->construction);
set_level(bf.u, SK_BUILDING, 1);
CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 4, 0));
CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 1, 0));
CuAssertPtrNotNull(tc, u->region->buildings);
CuAssertIntEquals(tc, 1, u->region->buildings->size);
CuAssertIntEquals(tc, 0, u->number);
@ -124,7 +248,14 @@ static void test_build_building_success(CuTest *tc) {
CuSuite *get_build_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_build);
SUITE_ADD_TEST(suite, test_build_limits);
SUITE_ADD_TEST(suite, test_build_failure_low_skill);
SUITE_ADD_TEST(suite, test_build_failure_missing_skill);
SUITE_ADD_TEST(suite, test_build_requires_materials);
SUITE_ADD_TEST(suite, test_build_requires_building);
SUITE_ADD_TEST(suite, test_build_failure_completed);
SUITE_ADD_TEST(suite, test_build_with_ring);
SUITE_ADD_TEST(suite, test_build_with_potion);
SUITE_ADD_TEST(suite, test_build_building_success);
SUITE_ADD_TEST(suite, test_build_building_with_golem);
SUITE_ADD_TEST(suite, test_build_building_no_materials);

View File

@ -177,7 +177,8 @@ static buddy *get_friends(const unit * u, int *numfriends)
int allied = 0;
if (get_param_int(global.parameters, "rules.alliances", 0) != 0) {
allied = (f->alliance && f->alliance == u2->faction->alliance);
} else if (alliedunit(u, u2->faction, HELP_MONEY)
}
else if (alliedunit(u, u2->faction, HELP_MONEY)
&& alliedunit(u2, f, HELP_GIVE)) {
allied = 1;
}
@ -196,7 +197,8 @@ static buddy *get_friends(const unit * u, int *numfriends)
nf->unit = u2;
nf->number = 0;
*fr = nf;
} else if (nf->faction == u2->faction
}
else if (nf->faction == u2->faction
&& (u_race(u2)->ec_flags & GIVEITEM)) {
/* we don't like to gift it to units that won't give it back */
if ((u_race(nf->unit)->ec_flags & GIVEITEM) == 0) {
@ -256,7 +258,8 @@ int gift_items(unit * u, int flags)
i_merge(&u2->items, &u->items);
u->items = NULL;
break;
} else {
}
else {
u3 = u2;
}
}
@ -318,7 +321,8 @@ int gift_items(unit * u, int flags)
if (itm->number > 0 && (itm->type->flags & ITF_NOTLOST)) {
itm_p = &itm->next;
retval = -1;
} else {
}
else {
i_remove(itm_p, itm);
i_free(itm);
}
@ -444,7 +448,8 @@ const char *u_description(const unit * u, const struct locale *lang)
{
if (u->display && u->display[0]) {
return u->display;
} else if (u_race(u)->describe) {
}
else if (u_race(u)->describe) {
return u_race(u)->describe(u, lang);
}
return NULL;
@ -534,7 +539,8 @@ void usettarget(unit * u, const unit * t)
if (!t) {
a_remove(&u->attribs, a);
freset(u, UFL_TARGET);
} else {
}
else {
a->data.v = (void *)t;
fset(u, UFL_TARGET);
}
@ -589,7 +595,8 @@ void usetsiege(unit * u, const struct building *t)
if (!t) {
a_remove(&u->attribs, a);
freset(u, UFL_SIEGE);
} else {
}
else {
a->data.v = (void *)t;
fset(u, UFL_SIEGE);
}
@ -855,7 +862,8 @@ bool leave(unit * u, bool force)
}
if (u->building) {
leave_building(u);
} else if (u->ship) {
}
else if (u->ship) {
leave_ship(u);
}
return true;
@ -908,7 +916,8 @@ void move_unit(unit * u, region * r, unit ** ulist)
#endif
}
translist(&u->region->units, ulist, u);
} else {
}
else {
addlist(ulist, u);
}
@ -951,7 +960,8 @@ void transfermen(unit * u, unit * u2, int n)
/* new unit, easy to solve */
level = sv->level;
weeks = sv->weeks;
} else {
}
else {
double dlevel = 0.0;
if (sv && sv->level) {
@ -972,7 +982,8 @@ void transfermen(unit * u, unit * u2, int n)
* next level */
level = (int)dlevel;
weeks = (level + 1) - (int)((dlevel - level) * (level + 1));
} else {
}
else {
/* make it harder to reach the next level.
* weeks+level is the max difficulty, 1 - the fraction between
* level and dlevel applied to the number of weeks between this
@ -989,7 +1000,8 @@ void transfermen(unit * u, unit * u2, int n)
assert(sn->weeks > 0 && sn->weeks <= sn->level * 2 + 1);
assert(u2->number != 0 || (sn->level == sv->level
&& sn->weeks == sv->weeks));
} else if (sn) {
}
else if (sn) {
remove_skill(u2, sk);
sn = NULL;
}
@ -1026,13 +1038,15 @@ void transfermen(unit * u, unit * u2, int n)
change_effect(u2, olde->type, -e);
a = an;
}
} else if (r->land) {
}
else if (r->land) {
if ((u_race(u)->ec_flags & ECF_REC_ETHEREAL) == 0) {
const race *rc = u_race(u);
if (rc->ec_flags & ECF_REC_HORSES) { /* Zentauren an die Pferde */
int h = rhorses(r) + n;
rsethorses(r, h);
} else {
}
else {
int p = rpeasants(r);
p += (int)(n * rc->recruit_multi);
rsetpeasants(r, p);
@ -1049,10 +1063,12 @@ struct building *inside_building(const struct unit *u)
if (!fval(u->building, BLD_WORKING)) {
/* Unterhalt nicht bezahlt */
return NULL;
} else if (u->building->size < u->building->type->maxsize) {
}
else if (u->building->size < u->building->type->maxsize) {
/* Gebaeude noch nicht fertig */
return NULL;
} else {
}
else {
int p = 0, cap = buildingcapacity(u->building);
const unit *u2;
for (u2 = u->region->units; u2; u2 = u2->next) {
@ -1143,7 +1159,8 @@ bool learn_skill(unit * u, skill_t sk, double chance)
if (sv->id == sk) {
if (sv->weeks <= 1) {
sk_set(sv, sv->level + 1);
} else {
}
else {
sv->weeks--;
}
return true;
@ -1284,7 +1301,8 @@ static int att_modification(const unit * u, skill_t sk)
|| alliedunit(mage, u->faction, HELP_GUARD)) {
bonus = mod;
}
} else if (mod < malus) {
}
else if (mod < malus) {
if (mage == NULL || !alliedunit(mage, u->faction, HELP_GUARD)) {
malus = mod;
}
@ -1424,10 +1442,12 @@ void name_unit(unit * u)
const char *gen_name = u_race(u)->generate_name(u);
if (gen_name) {
unit_setname(u, gen_name);
} else {
}
else {
unit_setname(u, racename(u->faction->locale, u, u_race(u)));
}
} else {
}
else {
char name[32];
const char * result;
const struct locale * lang = u->faction ? u->faction->locale : default_locale;
@ -1441,7 +1461,8 @@ void name_unit(unit * u)
}
}
result = prefix[i];
} else {
}
else {
result = parameters[P_UNIT];
}
strlcpy(name, result, sizeof(name));
@ -1495,7 +1516,8 @@ unit *create_unit(region * r, faction * f, int number, const struct race *urace,
if (!dname) {
name_unit(u);
} else {
}
else {
u->name = _strdup(dname);
}
@ -1699,7 +1721,6 @@ int unit_max_hp(const unit * u)
void scale_number(unit * u, int n)
{
skill_t sk;
const attrib *a;
int remain;
@ -1714,7 +1735,8 @@ void scale_number(unit * u, int n)
assert(u->hp >= 0);
if ((rng_int() % u->number) < remain)
++u->hp; /* Nachkommastellen */
} else {
}
else {
remain = 0;
u->hp = 0;
}
@ -1734,6 +1756,7 @@ void scale_number(unit * u, int n)
}
}
if (u->number == 0 || n == 0) {
skill_t sk;
for (sk = 0; sk < MAXSKILLS; sk++) {
remove_skill(u, sk);
}

41
src/kernel/unit.test.c Normal file
View File

@ -0,0 +1,41 @@
#include <platform.h>
#include <kernel/config.h>
#include "alchemy.h"
#include "unit.h"
#include "item.h"
#include "region.h"
#include <CuTest.h>
#include <tests.h>
#include <stdlib.h>
#include <assert.h>
static void test_scale_number(CuTest *tc) {
unit *u;
const struct potion_type *ptype;
test_cleanup();
test_create_world();
ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1);
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
change_effect(u, ptype, 1);
CuAssertIntEquals(tc, 1, u->number);
CuAssertIntEquals(tc, 1, u->hp);
CuAssertIntEquals(tc, 1, get_effect(u, ptype));
scale_number(u, 2);
CuAssertIntEquals(tc, 2, u->number);
CuAssertIntEquals(tc, 2, u->hp);
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
set_level(u, SK_ALCHEMY, 1);
scale_number(u, 0);
CuAssertIntEquals(tc, 0, get_level(u, SK_ALCHEMY));
test_cleanup();
}
CuSuite *get_unit_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_scale_number);
return suite;
}

View File

@ -515,6 +515,7 @@ int autoseed(newfaction ** players, int nsize, int max_agediff)
static const terrain_type **terrainarr = 0;
static int *distribution;
assert(players);
if (nterrains < 0) {
int n = 0;
const terrain_type *terrain = terrains();

View File

@ -36,6 +36,7 @@ int RunAllTests(void)
ADD_TESTS(suite, umlaut);
ADD_TESTS(suite, strings);
/* kernel */
ADD_TESTS(suite, unit);
ADD_TESTS(suite, faction);
ADD_TESTS(suite, build);
ADD_TESTS(suite, pool);