add some tests for pools, because I found a bug here and we don't want to have that happen again.

This commit is contained in:
Enno Rehling 2012-05-25 16:15:21 -07:00
parent 3b05e2e09e
commit 58cbc3ccbc
9 changed files with 62 additions and 5 deletions

View File

@ -29,6 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/curse.h>
#include <kernel/message.h>
#include <kernel/magic.h>
#include <kernel/pool.h>
/* util includes */
#include <util/functions.h>
@ -59,7 +60,7 @@ use_skillpotion(struct unit *u, const struct item_type *itype, int amount,
}
ADDMSG(&u->faction->msgs, msg_message("skillpotion_use", "unit", u));
res_changeitem(u, itype->rtype, -amount);
change_resource(u, itype->rtype, -amount);
return 0;
}
@ -81,7 +82,7 @@ use_manacrystal(struct unit *u, const struct item_type *itype, int amount,
ADDMSG(&u->faction->msgs, msg_message("manacrystal_use", "unit aura", u, sp));
res_changeitem(u, itype->rtype, -amount);
change_resource(u, itype->rtype, -amount);
return 0;
}

View File

@ -117,6 +117,7 @@
<ClCompile Include="kernel\plane.c" />
<ClCompile Include="kernel\player.c" />
<ClCompile Include="kernel\pool.c" />
<ClCompile Include="kernel\pool_test.c" />
<ClCompile Include="kernel\race.c" />
<ClCompile Include="kernel\region.c" />
<ClCompile Include="kernel\reports.c" />

View File

@ -322,6 +322,9 @@
<ClCompile Include="kernel\equipment_test.c">
<Filter>kernel</Filter>
</ClCompile>
<ClCompile Include="kernel\pool_test.c">
<Filter>kernel</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="kernel\alchemy.h">

View File

@ -96,7 +96,7 @@ static int res_changepeasants(unit * u, const resource_type * rtype, int delta)
return u->region->land->peasants;
}
int res_changeitem(unit * u, const resource_type * rtype, int delta)
static int res_changeitem(unit * u, const resource_type * rtype, int delta)
{
int num;
if (rtype == oldresourcetype[R_STONE] && u->race == new_race[RC_STONEGOLEM]

View File

@ -343,7 +343,6 @@ extern "C" {
int get_money(const struct unit *);
int set_money(struct unit *, int);
int change_money(struct unit *, int);
int res_changeitem(struct unit *u, const resource_type * rtype, int delta);
extern struct attrib_type at_showitem; /* show this potion's description */

View File

@ -693,8 +693,9 @@ int change_spellpoints(unit * u, int mp)
int sp;
m = get_mage(u);
if (!m)
if (!m) {
return 0;
}
/* verhindere negative Magiepunkte */
sp = MAX(m->spellpoints + mp, 0);

View File

@ -41,12 +41,19 @@ int get_resource(const unit * u, const resource_type * rtype)
{
const item_type *itype = resource2item(rtype);
assert(rtype);
if (rtype->uget) {
/* this resource is probably special */
int i = rtype->uget(u, rtype);
if (i >= 0)
return i;
}
else if (rtype->uchange) {
/* this resource is probably special */
int i = rtype->uchange((unit *)u, rtype, 0);
if (i >= 0)
return i;
}
if (itype != NULL) {
if (itype == olditemtype[R_STONE] && (u->race->flags & RCF_STONEGOLEM)) {
return u->number * GOLEM_STONE;

43
src/kernel/pool_test.c Normal file
View File

@ -0,0 +1,43 @@
#include <platform.h>
#include "pool.h"
#include "unit.h"
#include "item.h"
#include "region.h"
#include <cutest/CuTest.h>
#include <tests.h>
void test_change_resource(CuTest * tc)
{
struct unit * u;
struct faction * f;
struct region * r;
const char * names[] = { "money", "aura", "permaura", "horse", "hp", 0 };
int i;
test_cleanup();
test_create_world();
skill_enabled[SK_MAGIC] = 1;
r = findregion(0, 0);
f = test_create_faction(0);
u = test_create_unit(f, r);
CuAssertPtrNotNull(tc, u);
set_level(u, SK_MAGIC, 5);
create_mage(u, M_DRAIG);
for (i=0;names[i];++i) {
const struct resource_type *rtype = rt_find(names[i]);
int have = get_resource(u, rtype);
CuAssertIntEquals(tc, have+1, change_resource(u, rtype, 1));
CuAssertIntEquals(tc, have+1, get_resource(u, rtype));
}
}
CuSuite *get_pool_suite(void)
{
CuSuite *suite = CuSuiteNew();
/* SUITE_ADD_TEST(suite, test_pool); */
SUITE_ADD_TEST(suite, test_change_resource);
return suite;
}

View File

@ -17,6 +17,7 @@
#include <kernel/spell_test.c>
#include <kernel/curse_test.c>
#include <kernel/battle_test.c>
#include <kernel/pool_test.c>
#include <kernel/equipment_test.c>
#include <kernel/reports_test.c>
#include <kernel/spellbook_test.c>
@ -51,6 +52,7 @@ int RunAllTests(void)
CuSuiteAddSuite(suite, get_functions_suite());
CuSuiteAddSuite(suite, get_umlaut_suite());
/* kernel */
CuSuiteAddSuite(suite, get_pool_suite());
CuSuiteAddSuite(suite, get_curse_suite());
CuSuiteAddSuite(suite, get_equipment_suite());
CuSuiteAddSuite(suite, get_item_suite());