server/src/kernel/item.test.c

180 lines
5.4 KiB
C

#include <platform.h>
#include <kernel/item.h>
#include <kernel/pool.h>
#include <kernel/region.h>
#include <kernel/terrain.h>
#include <kernel/unit.h>
#include <util/language.h>
#include <util/functions.h>
#include <CuTest.h>
#include <tests.h>
#include <assert.h>
static void test_resourcename_no_appearance(CuTest *tc) {
const resource_type *rtype;
test_cleanup();
init_resources(); // creates R_SILVER
rtype = get_resourcetype(R_SILVER);
assert(rtype && rtype->itype);
assert(rtype->itype->_appearance[0] == 0);
assert(rtype->itype->_appearance[1] == 0);
CuAssertStrEquals(tc, "money", resourcename(rtype, 0));
CuAssertStrEquals(tc, "money_p", resourcename(rtype, NMF_PLURAL));
CuAssertStrEquals(tc, "money", resourcename(rtype, NMF_APPEARANCE));
CuAssertStrEquals(tc, "money_p", resourcename(rtype, NMF_APPEARANCE | NMF_PLURAL));
test_cleanup();
}
static void test_resourcename_with_appearance(CuTest *tc) {
item_type *itype;
test_cleanup();
itype = it_get_or_create(rt_get_or_create("foo"));
assert(itype && itype->rtype);
it_set_appearance(itype, "bar");
CuAssertStrEquals(tc, "foo", resourcename(itype->rtype, 0));
CuAssertStrEquals(tc, "foo_p", resourcename(itype->rtype, NMF_PLURAL));
CuAssertStrEquals(tc, "bar", resourcename(itype->rtype, NMF_APPEARANCE));
CuAssertStrEquals(tc, "bar_p", resourcename(itype->rtype, NMF_APPEARANCE | NMF_PLURAL));
test_cleanup();
}
static void test_uchange(CuTest * tc, unit * u, const resource_type * rtype) {
int n;
change_resource(u, rtype, 4);
n = get_resource(u, rtype);
CuAssertPtrNotNull(tc, rtype->uchange);
CuAssertIntEquals(tc, n, rtype->uchange(u, rtype, 0));
CuAssertIntEquals(tc, n - 3, rtype->uchange(u, rtype, -3));
CuAssertIntEquals(tc, n - 3, get_resource(u, rtype));
CuAssertIntEquals(tc, 0, rtype->uchange(u, rtype, -n));
}
void test_change_item(CuTest * tc)
{
unit * u;
test_cleanup();
init_resources();
test_create_world();
u = test_create_unit(0, 0);
test_uchange(tc, u, get_resourcetype(R_IRON));
}
void test_resource_type(CuTest * tc)
{
struct item_type *itype;
test_cleanup();
CuAssertPtrEquals(tc, 0, rt_find("herpderp"));
test_create_itemtype("herpderp");
test_create_itemtype("herpes");
itype = test_create_itemtype("herp");
CuAssertPtrEquals(tc, itype->rtype, rt_find("herp"));
}
void test_finditemtype(CuTest * tc)
{
const item_type *itype;
const resource_type *rtype;
struct locale * lang;
test_cleanup();
test_create_world();
lang = get_locale("de");
locale_setstring(lang, "horse", "Pferd");
rtype = get_resourcetype(R_HORSE);
itype = finditemtype("Pferd", lang);
CuAssertPtrNotNull(tc, itype);
CuAssertPtrEquals(tc, (void*)rtype->itype, (void*)itype);
}
void test_findresourcetype(CuTest * tc)
{
const resource_type *rtype, *rresult;
struct locale * lang;
test_cleanup();
test_create_world();
lang = get_locale("de");
locale_setstring(lang, "horse", "Pferd");
locale_setstring(lang, "peasant", "Bauer");
rtype = get_resourcetype(R_HORSE);
rresult = findresourcetype("Pferd", lang);
CuAssertPtrNotNull(tc, rresult);
CuAssertPtrEquals(tc, (void*)rtype, (void*)rresult);
CuAssertPtrNotNull(tc, findresourcetype("Bauer", lang));
}
#include <modules/autoseed.h>
static void test_fix_demand(CuTest *tc) {
region *r;
terrain_type *tplain;
item_type *ltype;
test_cleanup();
ltype = test_create_itemtype("balm");
ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED);
new_luxurytype(ltype, 0);
ltype = test_create_itemtype("oint");
ltype->rtype->flags |= (RTF_ITEM | RTF_POOLED);
new_luxurytype(ltype, 0);
tplain = test_create_terrain("plain", LAND_REGION);
r = new_region(0, 0, NULL, 0);
CuAssertPtrNotNull(tc, r);
terraform_region(r, tplain);
CuAssertPtrNotNull(tc, r->land);
CuAssertIntEquals(tc, 0, fix_demand(r));
CuAssertPtrNotNull(tc, r->land->demands);
CuAssertPtrNotNull(tc, r->land->demands->next);
CuAssertPtrNotNull(tc, r_luxury(r));
test_cleanup();
}
static void test_core_resources(CuTest *tc) {
resource_type * rtype;
test_cleanup();
init_resources();
CuAssertPtrNotNull(tc, rtype = rt_find("money"));
CuAssertPtrNotNull(tc, rtype->itype);
CuAssertPtrNotNull(tc, rtype->uchange);
CuAssertPtrNotNull(tc, rtype->itype->give);
CuAssertPtrNotNull(tc, rtype = rt_find("peasant"));
CuAssertPtrEquals(tc, 0, rtype->itype);
CuAssertPtrNotNull(tc, rtype = rt_find("person"));
CuAssertPtrEquals(tc, 0, rtype->itype);
CuAssertPtrNotNull(tc, rtype = rt_find("permaura"));
CuAssertPtrEquals(tc, 0, rtype->itype);
CuAssertPtrNotNull(tc, rtype = rt_find("hp"));
CuAssertPtrEquals(tc, 0, rtype->itype);
CuAssertPtrNotNull(tc, rtype = rt_find("aura"));
CuAssertPtrEquals(tc, 0, rtype->itype);
test_cleanup();
}
CuSuite *get_item_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_resourcename_no_appearance);
SUITE_ADD_TEST(suite, test_resourcename_with_appearance);
SUITE_ADD_TEST(suite, test_change_item);
SUITE_ADD_TEST(suite, test_resource_type);
SUITE_ADD_TEST(suite, test_finditemtype);
SUITE_ADD_TEST(suite, test_findresourcetype);
SUITE_ADD_TEST(suite, test_fix_demand);
SUITE_ADD_TEST(suite, test_core_resources);
return suite;
}