From 5902a6922f06eae2d42591a71ff76b670cd146de Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 15 Feb 2017 20:50:45 +0100 Subject: [PATCH] BUG 2279: add a failing unit test. --- src/economy.test.c | 36 +++++++++++++++++++++++++++++++++++- src/kernel/resources.h | 16 +++++++--------- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/economy.test.c b/src/economy.test.c index bee074714..ddb0cafbc 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -11,10 +11,12 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -345,11 +347,17 @@ static void test_make_item(CuTest *tc) { unit *u; struct item_type *itype; const struct resource_type *rt_silver; + resource_type *rtype; + attrib *a; + resource_limit *rdata; test_setup(); init_resources(); + + /* make items from other items (turn silver to stone) */ rt_silver = get_resourcetype(R_SILVER); - itype = test_create_itemtype("log"); + itype = test_create_itemtype("stone"); + rtype = itype->rtype; u = test_create_unit(test_create_faction(0), test_create_region(0,0,0)); make_item(u, itype, 1); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error_cannotmake")); @@ -368,6 +376,32 @@ static void test_make_item(CuTest *tc) { make_item(u, itype, 1); CuAssertIntEquals(tc, 1, get_item(u, itype)); CuAssertIntEquals(tc, 0, get_item(u, rt_silver->itype)); + + /* make level-based raw materials, no materials used in construction */ + free(itype->construction->materials); + itype->construction->materials = 0; + rtype->flags |= RTF_LIMITED; + a = a_add(&rtype->attribs, a_new(&at_resourcelimit)); + rdata = (resource_limit *)a->data.v; + rdata->value = 0; + rmt_create(rtype, "stone"); + add_resource(u->region, 1, 300, 150, rtype); + u->region->resources->amount = 300; /* there are 300 stones at level 1 */ + set_level(u, SK_ALCHEMY, 10); + + make_item(u, itype, 10); + split_allocations(u->region); + CuAssertIntEquals(tc, 11, get_item(u, itype)); + CuAssertIntEquals(tc, 290, u->region->resources->amount); /* used 10 stones to make 10 stones */ + + rdata->modifiers = calloc(2, sizeof(resource_mod)); + rdata->modifiers[0].flags = RMF_SAVEMATERIAL; + rdata->modifiers[0].race = u->_race; + rdata->modifiers[0].value.f = (float)0.6; + make_item(u, itype, 10); + split_allocations(u->region); + CuAssertIntEquals(tc, 21, get_item(u, itype)); + CuAssertIntEquals(tc, 284, u->region->resources->amount); /* 60% saving = 6 stones make 10 stones */ test_cleanup(); } diff --git a/src/kernel/resources.h b/src/kernel/resources.h index 32c69036c..a1a7abd40 100644 --- a/src/kernel/resources.h +++ b/src/kernel/resources.h @@ -55,18 +55,16 @@ extern "C" { extern struct rawmaterial_type *rawmaterialtypes; - extern void update_resources(struct region *r); - extern void terraform_resources(struct region *r); - extern void read_resources(struct region *r); - extern void write_resources(struct region *r); - extern struct rawmaterial *rm_get(struct region *, + void update_resources(struct region *r); + void terraform_resources(struct region *r); + struct rawmaterial *rm_get(struct region *, const struct resource_type *); - extern struct rawmaterial_type *rmt_find(const char *str); - extern struct rawmaterial_type *rmt_get(const struct resource_type *); + struct rawmaterial_type *rmt_find(const char *str); + struct rawmaterial_type *rmt_get(const struct resource_type *); - extern void add_resource(struct region *r, int level, int base, int divisor, + void add_resource(struct region *r, int level, int base, int divisor, const struct resource_type *rtype); - extern struct rawmaterial_type *rmt_create(const struct resource_type *rtype, + struct rawmaterial_type *rmt_create(const struct resource_type *rtype, const char *name); #ifdef __cplusplus