diff --git a/src/kernel/item.c b/src/kernel/item.c index fd35f0776..2fd76ded3 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -1226,6 +1226,10 @@ void test_clear_resources(void) void register_resources(void) { + static bool registered = false; + if (registered) return; + registered = true; + register_function((pf_generic) mod_elves_only, "mod_elves_only"); register_function((pf_generic) mod_dwarves_only, "mod_dwarves_only"); register_function((pf_generic) res_changeitem, "changeitem"); diff --git a/src/kernel/item_test.c b/src/kernel/item_test.c index c59e47d33..6279bc0af 100644 --- a/src/kernel/item_test.c +++ b/src/kernel/item_test.c @@ -2,11 +2,35 @@ #include #include +#include #include +#include #include #include +void test_change_item(CuTest * tc) +{ + rtype_uchange res_changeitem; + const resource_type * rtype; + unit * u; + + register_resources(); + res_changeitem = (rtype_uchange)get_function("changeitem"); + CuAssertPtrNotNull(tc, res_changeitem); + + test_cleanup(); + test_create_world(); + rtype = olditemtype[I_IRON]->rtype; + + u = test_create_unit(0, 0); + CuAssertIntEquals(tc, 0, res_changeitem(u, rtype, 0)); + i_change(&u->items, rtype->itype, 4); + CuAssertIntEquals(tc, 4, res_changeitem(u, rtype, 0)); + CuAssertIntEquals(tc, 1, res_changeitem(u, rtype, -3)); + CuAssertIntEquals(tc, 1, i_get(u->items, rtype->itype)); +} + void test_resource_type(CuTest * tc) { struct item_type *itype; @@ -68,6 +92,7 @@ void test_findresourcetype(CuTest * tc) CuSuite *get_item_suite(void) { CuSuite *suite = CuSuiteNew(); + 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); diff --git a/src/tests.c b/src/tests.c index dd05c7d93..568e65ac4 100644 --- a/src/tests.c +++ b/src/tests.c @@ -180,15 +180,15 @@ void test_create_world(void) terrain_type *t_plain, *t_ocean; region *island[2]; int i; - item_type * itype; - const char * names[] = { "horse", "horse_p", "boat", "boat_p" }; + const char * names[] = { "horse", "horse_p", "boat", "boat_p", "iron", "iron_p", "stone", "stone_p" }; make_locale("de"); init_resources(); assert(!olditemtype[I_HORSE]); - itype = test_create_itemtype(names); - olditemtype[I_HORSE] = itype; + olditemtype[I_HORSE] = test_create_itemtype(names+0); + olditemtype[I_IRON] = test_create_itemtype(names+4); + olditemtype[I_STONE] = test_create_itemtype(names+6); t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION); t_plain->size = 1000;