2012-05-26 01:15:21 +02:00
|
|
|
#include <platform.h>
|
|
|
|
|
2014-10-31 07:51:57 +01:00
|
|
|
#include "ally.h"
|
2012-05-26 01:15:21 +02:00
|
|
|
#include "pool.h"
|
2012-06-04 08:36:40 +02:00
|
|
|
#include "magic.h"
|
2012-05-26 01:15:21 +02:00
|
|
|
#include "unit.h"
|
|
|
|
#include "item.h"
|
2014-10-31 07:51:57 +01:00
|
|
|
#include "faction.h"
|
2018-12-09 14:41:14 +01:00
|
|
|
#include "race.h"
|
2012-05-26 01:15:21 +02:00
|
|
|
#include "region.h"
|
2012-06-03 21:51:35 +02:00
|
|
|
#include "skill.h"
|
2012-06-04 08:36:40 +02:00
|
|
|
|
2012-05-31 04:17:08 +02:00
|
|
|
#include <CuTest.h>
|
2012-05-26 01:15:21 +02:00
|
|
|
#include <tests.h>
|
2014-10-31 07:51:57 +01:00
|
|
|
#include <assert.h>
|
2014-10-31 15:54:25 +01:00
|
|
|
#include <limits.h>
|
2014-10-31 07:51:57 +01:00
|
|
|
|
2014-10-31 19:01:13 +01:00
|
|
|
void test_reservation(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
struct resource_type *rtype;
|
2017-12-27 19:58:39 +01:00
|
|
|
test_setup();
|
2014-10-31 19:01:13 +01:00
|
|
|
test_create_world();
|
|
|
|
rtype = rt_get_or_create("money");
|
|
|
|
it_get_or_create(rtype);
|
2018-01-14 09:38:26 +01:00
|
|
|
f = test_create_faction(NULL);
|
2014-10-31 19:01:13 +01:00
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f && rtype && rtype->itype);
|
|
|
|
u = test_create_unit(f, r);
|
|
|
|
assert(u);
|
|
|
|
i_change(&u->items, rtype->itype, 100);
|
|
|
|
CuAssertIntEquals(tc, 100, get_resource(u, rtype));
|
2014-12-12 11:28:37 +01:00
|
|
|
CuAssertIntEquals(tc, 0, get_reservation(u, rtype->itype));
|
|
|
|
CuAssertIntEquals(tc, 50, change_reservation(u, rtype->itype, 50));
|
|
|
|
CuAssertIntEquals(tc, 100, change_reservation(u, rtype->itype, 50));
|
|
|
|
CuAssertIntEquals(tc, 50, set_resvalue(u, rtype->itype, 50));
|
2014-10-31 19:01:13 +01:00
|
|
|
CuAssertIntEquals(tc, 100, get_resource(u, rtype));
|
|
|
|
CuAssertIntEquals(tc, 200, change_resource(u, rtype, 100));
|
|
|
|
CuAssertIntEquals(tc, 200, get_resource(u, rtype));
|
|
|
|
CuAssertIntEquals(tc, 200, i_get(u->items, rtype->itype));
|
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2014-10-31 19:01:13 +01:00
|
|
|
}
|
|
|
|
|
2018-12-09 14:41:14 +01:00
|
|
|
void test_pool_get_item(CuTest *tc) {
|
|
|
|
unit *u1, *u2;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
race *rc;
|
|
|
|
struct resource_type *rtype;
|
|
|
|
|
|
|
|
test_setup();
|
|
|
|
rtype = rt_get_or_create("money");
|
|
|
|
rtype->flags |= RTF_POOLED;
|
|
|
|
it_get_or_create(rtype);
|
|
|
|
f = test_create_faction(NULL);
|
|
|
|
r = test_create_plain(0, 0);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u_setrace(u1, rc = test_create_race("undead"));
|
|
|
|
rc->ec_flags &= ~ECF_GETITEM;
|
|
|
|
u2 = test_create_unit(f, r);
|
|
|
|
i_change(&u2->items, rtype->itype, 2);
|
|
|
|
CuAssertIntEquals(tc, 0, get_pooled(u1, rtype, GET_DEFAULT, 1));
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u1->items, rtype->itype));
|
|
|
|
CuAssertIntEquals(tc, 2, i_get(u2->items, rtype->itype));
|
|
|
|
test_teardown();
|
|
|
|
}
|
|
|
|
|
2014-10-31 07:51:57 +01:00
|
|
|
void test_pool(CuTest *tc) {
|
|
|
|
unit *u1, *u2, *u3;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
struct resource_type *rtype;
|
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_setup();
|
2014-10-31 07:51:57 +01:00
|
|
|
rtype = rt_get_or_create("money");
|
2018-12-09 14:41:14 +01:00
|
|
|
rtype->flags |= RTF_POOLED;
|
2014-10-31 07:51:57 +01:00
|
|
|
it_get_or_create(rtype);
|
2018-01-14 09:38:26 +01:00
|
|
|
f = test_create_faction(NULL);
|
2018-12-09 14:41:14 +01:00
|
|
|
r = test_create_plain(0, 0);
|
2014-10-31 07:51:57 +01:00
|
|
|
assert(r && f && rtype && rtype->itype);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u2 = test_create_unit(f, r);
|
2018-01-14 09:38:26 +01:00
|
|
|
u3 = test_create_unit(test_create_faction(NULL), r);
|
2014-10-31 07:51:57 +01:00
|
|
|
assert(u1 && u2);
|
|
|
|
i_change(&u1->items, rtype->itype, 100);
|
2014-12-12 11:28:37 +01:00
|
|
|
set_resvalue(u1, rtype->itype, 50);
|
2014-10-31 07:51:57 +01:00
|
|
|
i_change(&u2->items, rtype->itype, 200);
|
2014-12-12 11:28:37 +01:00
|
|
|
set_resvalue(u2, rtype->itype, 100);
|
2014-10-31 07:51:57 +01:00
|
|
|
i_change(&u3->items, rtype->itype, 400);
|
2014-12-12 11:28:37 +01:00
|
|
|
set_resvalue(u3, rtype->itype, 200);
|
2014-10-31 07:51:57 +01:00
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 50, get_pooled(u1, rtype, GET_SLACK, 40));
|
|
|
|
CuAssertIntEquals(tc, 50, get_pooled(u1, rtype, GET_SLACK, INT_MAX));
|
|
|
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_SLACK | GET_RESERVE, INT_MAX));
|
|
|
|
CuAssertIntEquals(tc, 150, get_pooled(u1, rtype, GET_SLACK | GET_POOLED_SLACK, INT_MAX));
|
|
|
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_POOLED_SLACK, INT_MAX));
|
|
|
|
CuAssertIntEquals(tc, 200, get_pooled(u1, rtype, GET_POOLED_SLACK | GET_POOLED_RESERVE, INT_MAX));
|
|
|
|
|
2018-10-26 19:47:50 +02:00
|
|
|
ally_set(&u3->faction->allies, f, HELP_GUARD);
|
2014-10-31 07:51:57 +01:00
|
|
|
CuAssertIntEquals(tc, 0, get_pooled(u1, rtype, GET_ALLIED_SLACK | GET_ALLIED_RESERVE, INT_MAX));
|
2018-10-26 19:47:50 +02:00
|
|
|
ally_set(&u3->faction->allies, f, HELP_MONEY);
|
2014-10-31 07:51:57 +01:00
|
|
|
CuAssertIntEquals(tc, 200, get_pooled(u1, rtype, GET_ALLIED_SLACK, INT_MAX));
|
|
|
|
CuAssertIntEquals(tc, 200, get_pooled(u1, rtype, GET_ALLIED_RESERVE, INT_MAX));
|
|
|
|
CuAssertIntEquals(tc, 400, get_pooled(u1, rtype, GET_ALLIED_SLACK | GET_ALLIED_RESERVE, INT_MAX));
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_ALL, 50));
|
|
|
|
CuAssertIntEquals(tc, 300, get_pooled(u1, rtype, GET_ALL, 150));
|
|
|
|
CuAssertIntEquals(tc, 300, get_pooled(u1, rtype, GET_ALL, INT_MAX));
|
2017-12-27 19:58:39 +01:00
|
|
|
|
|
|
|
test_teardown();
|
2014-10-31 07:51:57 +01:00
|
|
|
}
|
2012-05-26 01:15:21 +02:00
|
|
|
|
2014-11-03 07:52:00 +01:00
|
|
|
void test_pool_bug_2042(CuTest *tc) {
|
|
|
|
unit *u1, *u2;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
|
|
|
struct resource_type *rtype;
|
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_setup();
|
2014-11-03 07:52:00 +01:00
|
|
|
test_create_world();
|
|
|
|
rtype = rt_get_or_create("money");
|
|
|
|
it_get_or_create(rtype);
|
2018-01-14 09:38:26 +01:00
|
|
|
f = test_create_faction(NULL);
|
2014-11-03 07:52:00 +01:00
|
|
|
r = findregion(0, 0);
|
|
|
|
assert(r && f && rtype && rtype->itype);
|
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u2 = test_create_unit(f, r);
|
|
|
|
assert(u1 && u2);
|
|
|
|
i_change(&u2->items, rtype->itype, 100);
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 100, get_pooled(u1, rtype, GET_SLACK | GET_POOLED_SLACK, 100));
|
2017-12-27 19:58:39 +01:00
|
|
|
|
|
|
|
test_teardown();
|
2014-11-03 07:52:00 +01:00
|
|
|
}
|
|
|
|
|
2014-10-31 19:01:13 +01:00
|
|
|
void test_pool_use(CuTest *tc) {
|
|
|
|
unit *u1, *u2, *u3;
|
|
|
|
faction *f;
|
|
|
|
region *r;
|
2014-12-12 11:28:37 +01:00
|
|
|
struct item_type *itype;
|
2014-10-31 19:01:13 +01:00
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_setup();
|
2014-10-31 19:01:13 +01:00
|
|
|
test_create_world();
|
2014-12-12 11:28:37 +01:00
|
|
|
itype = it_get_or_create(rt_get_or_create("money"));
|
2018-01-14 09:38:26 +01:00
|
|
|
f = test_create_faction(NULL);
|
2014-10-31 19:01:13 +01:00
|
|
|
r = findregion(0, 0);
|
2014-12-12 11:28:37 +01:00
|
|
|
assert(r && f && itype);
|
2014-10-31 19:01:13 +01:00
|
|
|
u1 = test_create_unit(f, r);
|
|
|
|
u2 = test_create_unit(f, r);
|
2018-01-14 09:38:26 +01:00
|
|
|
u3 = test_create_unit(test_create_faction(NULL), r);
|
2014-10-31 19:01:13 +01:00
|
|
|
assert(u1 && u2);
|
2014-12-12 11:28:37 +01:00
|
|
|
i_change(&u1->items, itype, 100);
|
|
|
|
set_resvalue(u1, itype, 50);
|
|
|
|
i_change(&u2->items, itype, 200);
|
|
|
|
set_resvalue(u2, itype, 100);
|
|
|
|
i_change(&u3->items, itype, 400);
|
|
|
|
set_resvalue(u3, itype, 200);
|
2018-10-26 19:47:50 +02:00
|
|
|
ally_set(&u3->faction->allies, f, HELP_MONEY);
|
2014-10-31 19:01:13 +01:00
|
|
|
|
2014-12-12 11:28:37 +01:00
|
|
|
CuAssertIntEquals(tc, 10, use_pooled(u1, itype->rtype, GET_SLACK, 10));
|
|
|
|
CuAssertIntEquals(tc, 40, use_pooled(u1, itype->rtype, GET_SLACK, 50));
|
|
|
|
CuAssertIntEquals(tc, 50, i_get(u1->items, itype));
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 50, get_reservation(u1, itype));
|
|
|
|
CuAssertIntEquals(tc, 10, use_pooled(u1, itype->rtype, GET_RESERVE, 10));
|
|
|
|
CuAssertIntEquals(tc, 40, i_get(u1->items, itype));
|
|
|
|
CuAssertIntEquals(tc, 40, get_reservation(u1, itype));
|
|
|
|
CuAssertIntEquals(tc, 40, use_pooled(u1, itype->rtype, GET_RESERVE, 50));
|
|
|
|
|
|
|
|
CuAssertIntEquals(tc, 10, use_pooled(u1, itype->rtype, GET_POOLED_SLACK, 10));
|
|
|
|
CuAssertIntEquals(tc, 90, use_pooled(u1, itype->rtype, GET_POOLED_SLACK, 100));
|
|
|
|
CuAssertIntEquals(tc, 100, i_get(u2->items, itype));
|
|
|
|
CuAssertIntEquals(tc, 10, use_pooled(u1, itype->rtype, GET_POOLED_RESERVE, 10));
|
|
|
|
CuAssertIntEquals(tc, 90, get_reservation(u2, itype));
|
|
|
|
CuAssertIntEquals(tc, 90, use_pooled(u1, itype->rtype, GET_POOLED_RESERVE, 100));
|
|
|
|
CuAssertIntEquals(tc, 0, i_get(u2->items, itype));
|
2017-12-27 19:58:39 +01:00
|
|
|
|
|
|
|
test_teardown();
|
2014-10-31 19:01:13 +01:00
|
|
|
}
|
|
|
|
|
2012-05-26 01:15:21 +02:00
|
|
|
void test_change_resource(CuTest * tc)
|
|
|
|
{
|
2014-10-31 07:51:57 +01:00
|
|
|
struct unit * u;
|
|
|
|
struct faction * f;
|
|
|
|
struct region * r;
|
|
|
|
const char * names[] = { "money", "aura", "permaura", "horse", "hp", 0 };
|
|
|
|
int i;
|
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
test_setup();
|
2014-10-31 07:51:57 +01:00
|
|
|
test_create_world();
|
|
|
|
enable_skill(SK_MAGIC, true);
|
|
|
|
|
|
|
|
r = findregion(0, 0);
|
2018-01-14 09:38:26 +01:00
|
|
|
f = test_create_faction(NULL);
|
2014-10-31 07:51:57 +01:00
|
|
|
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));
|
|
|
|
}
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2012-05-26 01:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CuSuite *get_pool_suite(void)
|
|
|
|
{
|
2014-10-31 07:51:57 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
2014-10-31 19:01:13 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_reservation);
|
2014-10-31 07:51:57 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pool);
|
2018-12-09 14:41:14 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pool_get_item);
|
2014-11-03 07:52:00 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pool_bug_2042);
|
2014-10-31 19:01:13 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_pool_use);
|
2014-10-31 07:51:57 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_change_resource);
|
|
|
|
return suite;
|
2012-05-26 01:15:21 +02:00
|
|
|
}
|