2014-06-29 01:19:46 +02:00
|
|
|
#include <platform.h>
|
2014-06-29 08:58:00 +02:00
|
|
|
#include <kernel/config.h>
|
2014-06-29 01:19:46 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "build.h"
|
|
|
|
#include "order.h"
|
|
|
|
#include "unit.h"
|
|
|
|
#include "building.h"
|
|
|
|
#include "faction.h"
|
|
|
|
#include "region.h"
|
|
|
|
#include "race.h"
|
|
|
|
#include "item.h"
|
|
|
|
#include <util/language.h>
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
2014-08-24 17:09:32 +02:00
|
|
|
#include <stdlib.h>
|
2014-06-29 01:19:46 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-08-24 17:09:32 +02:00
|
|
|
typedef struct build_fixture {
|
|
|
|
faction *f;
|
2014-06-29 01:19:46 +02:00
|
|
|
unit *u;
|
|
|
|
region *r;
|
|
|
|
race *rc;
|
2014-08-24 17:09:32 +02:00
|
|
|
} build_fixture;
|
2014-06-29 01:19:46 +02:00
|
|
|
|
2014-08-24 17:09:32 +02:00
|
|
|
static unit * setup_build(build_fixture *bf) {
|
2014-06-29 01:19:46 +02:00
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
2014-08-24 17:09:32 +02:00
|
|
|
bf->rc = test_create_race("human");
|
|
|
|
bf->r = findregion(0, 0);
|
|
|
|
bf->f = test_create_faction(bf->rc);
|
|
|
|
assert(bf->rc && bf->f && bf->r);
|
|
|
|
bf->u = test_create_unit(bf->f, bf->r);
|
|
|
|
assert(bf->u);
|
|
|
|
return bf->u;
|
|
|
|
}
|
2014-06-29 01:19:46 +02:00
|
|
|
|
2014-08-24 17:09:32 +02:00
|
|
|
static void test_build(CuTest *tc) {
|
|
|
|
build_fixture bf;
|
|
|
|
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));
|
|
|
|
i_change(&u->items, rtype->itype, 1);
|
|
|
|
CuAssertIntEquals(tc, 1, build(u, &cons, 1, 1));
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
|
|
|
|
scale_number(u, 2);
|
|
|
|
i_change(&u->items, rtype->itype, 2);
|
|
|
|
CuAssertIntEquals(tc, 2, build(u, &cons, 2, 2));
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_build_building_no_materials(CuTest *tc) {
|
|
|
|
const building_type *btype;
|
|
|
|
build_fixture bf = { 0 };
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
u = setup_build(&bf);
|
2014-06-29 01:19:46 +02:00
|
|
|
btype = bt_find("castle");
|
2014-08-24 17:09:32 +02:00
|
|
|
assert(btype);
|
|
|
|
set_level(bf.u, SK_BUILDING, 1);
|
|
|
|
CuAssertIntEquals(tc, ENOMATERIALS, build_building(bf.u, btype, 0, 4, 0));
|
|
|
|
CuAssertPtrEquals(tc, 0, bf.r->buildings);
|
|
|
|
CuAssertPtrEquals(tc, 0, bf.u->building);
|
2014-06-29 01:19:46 +02:00
|
|
|
}
|
|
|
|
|
2014-06-29 08:58:00 +02:00
|
|
|
static void test_build_building_with_golem(CuTest *tc) {
|
|
|
|
unit *u;
|
2014-08-24 17:09:32 +02:00
|
|
|
build_fixture bf;
|
2014-06-29 08:58:00 +02:00
|
|
|
const building_type *btype;
|
|
|
|
|
2014-08-24 17:09:32 +02:00
|
|
|
u = setup_build(&bf);
|
|
|
|
bf.rc->flags |= RCF_STONEGOLEM;
|
2014-06-29 08:58:00 +02:00
|
|
|
btype = bt_find("castle");
|
2014-08-24 17:09:32 +02:00
|
|
|
assert(btype);
|
2014-06-29 08:58:00 +02:00
|
|
|
assert(btype->construction);
|
2014-08-24 17:09:32 +02:00
|
|
|
|
|
|
|
set_level(bf.u, SK_BUILDING, 1);
|
2014-06-29 08:58:00 +02:00
|
|
|
CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 4, 0));
|
2014-08-24 17:09:32 +02:00
|
|
|
CuAssertPtrNotNull(tc, u->region->buildings);
|
|
|
|
CuAssertIntEquals(tc, 1, u->region->buildings->size);
|
2014-06-29 08:58:00 +02:00
|
|
|
CuAssertIntEquals(tc, 0, u->number);
|
|
|
|
}
|
|
|
|
|
2014-06-29 01:19:46 +02:00
|
|
|
static void test_build_building_success(CuTest *tc) {
|
|
|
|
unit *u;
|
2014-08-24 17:09:32 +02:00
|
|
|
build_fixture bf = { 0 };
|
2014-06-29 01:19:46 +02:00
|
|
|
const building_type *btype;
|
|
|
|
const resource_type *rtype;
|
|
|
|
|
2014-08-24 17:09:32 +02:00
|
|
|
u = setup_build(&bf);
|
2014-06-29 01:19:46 +02:00
|
|
|
|
|
|
|
rtype = get_resourcetype(R_STONE);
|
|
|
|
btype = bt_find("castle");
|
2014-08-24 17:09:32 +02:00
|
|
|
assert(btype && rtype && rtype->itype);
|
2014-06-29 08:58:00 +02:00
|
|
|
assert(btype->construction);
|
2014-08-24 17:09:32 +02:00
|
|
|
assert(!u->region->buildings);
|
|
|
|
|
|
|
|
i_change(&bf.u->items, rtype->itype, 1);
|
2014-06-29 01:19:46 +02:00
|
|
|
set_level(u, SK_BUILDING, 1);
|
|
|
|
CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 4, 0));
|
2014-08-24 17:09:32 +02:00
|
|
|
CuAssertPtrNotNull(tc, u->region->buildings);
|
|
|
|
CuAssertPtrEquals(tc, u->region->buildings, u->building);
|
2014-06-29 01:19:46 +02:00
|
|
|
CuAssertIntEquals(tc, 1, u->building->size);
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u->items, rtype->itype));
|
|
|
|
}
|
|
|
|
|
|
|
|
CuSuite *get_build_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
2014-08-24 17:09:32 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_build);
|
2014-06-29 01:19:46 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_build_building_success);
|
2014-06-29 08:58:00 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_build_building_with_golem);
|
2014-06-29 01:19:46 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_build_building_no_materials);
|
|
|
|
return suite;
|
|
|
|
}
|
|
|
|
|