2011-03-09 06:06:38 +01:00
|
|
|
#include <platform.h>
|
|
|
|
|
2015-05-12 18:07:47 +02:00
|
|
|
#include <kernel/config.h>
|
2015-01-12 08:18:41 +01:00
|
|
|
#include <kernel/region.h>
|
2016-02-26 17:54:25 +01:00
|
|
|
#include <kernel/save.h>
|
2015-01-12 08:18:41 +01:00
|
|
|
#include <kernel/unit.h>
|
2018-09-29 11:37:17 +02:00
|
|
|
#include <kernel/attrib.h>
|
2016-10-23 13:02:53 +02:00
|
|
|
#include <util/rng.h>
|
2018-09-29 13:21:46 +02:00
|
|
|
#include <kernel/gamedata.h>
|
2015-01-12 08:18:41 +01:00
|
|
|
#include <util/message.h>
|
2015-05-12 18:07:47 +02:00
|
|
|
#include <binarystore.h>
|
|
|
|
#include <filestream.h>
|
|
|
|
#include <memstream.h>
|
|
|
|
#include <storage.h>
|
|
|
|
#include <stream.h>
|
2015-01-12 08:18:41 +01:00
|
|
|
#include <tests.h>
|
2011-03-09 06:06:38 +01:00
|
|
|
|
2015-05-12 18:07:47 +02:00
|
|
|
#include "curse.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-05-31 04:17:08 +02:00
|
|
|
#include <CuTest.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-05-12 18:07:47 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static void test_curse(CuTest * tc)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
attrib *attrs = NULL;
|
|
|
|
curse *c, *result;
|
|
|
|
int cid;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL };
|
2017-12-27 19:58:39 +01:00
|
|
|
|
|
|
|
test_setup();
|
2015-01-30 20:37:14 +01:00
|
|
|
c = create_curse(NULL, &attrs, &ct_dummy, 1.0, 1, 1, 1);
|
|
|
|
cid = c->no;
|
|
|
|
result = findcurse(cid);
|
|
|
|
CuAssertPtrEquals(tc, c, result);
|
2015-10-14 12:51:45 +02:00
|
|
|
a_remove(&attrs, attrs);
|
2015-01-30 20:37:14 +01:00
|
|
|
result = findcurse(cid);
|
|
|
|
CuAssertPtrEquals(tc, NULL, result);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2015-01-12 08:18:41 +01:00
|
|
|
typedef struct {
|
|
|
|
curse *c;
|
|
|
|
region *r;
|
|
|
|
unit *u;
|
|
|
|
} curse_fixture;
|
|
|
|
|
|
|
|
static void setup_curse(curse_fixture *fix, const char *name) {
|
2017-12-27 19:58:39 +01:00
|
|
|
test_setup();
|
2015-01-12 08:18:41 +01:00
|
|
|
fix->r = test_create_region(0, 0, NULL);
|
|
|
|
fix->u = test_create_unit(test_create_faction(NULL), fix->r);
|
|
|
|
fix->c = create_curse(fix->u, &fix->r->attribs, ct_find(name), 1.0, 1, 1.0, 0);
|
|
|
|
}
|
|
|
|
|
2017-12-27 19:58:39 +01:00
|
|
|
static void teardown_curse(curse_fixture *fix) {
|
|
|
|
test_teardown();
|
2016-02-26 17:54:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 08:18:41 +01:00
|
|
|
static void test_magicstreet(CuTest *tc) {
|
|
|
|
curse_fixture fix;
|
|
|
|
message *msg;
|
|
|
|
setup_curse(&fix, "magicstreet");
|
2015-01-12 18:22:59 +01:00
|
|
|
fix.c->duration = 2;
|
2015-01-12 08:18:41 +01:00
|
|
|
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
|
2015-01-12 17:50:10 +01:00
|
|
|
CuAssertStrEquals(tc, "curseinfo::magicstreet", test_get_messagetype(msg));
|
2015-01-12 08:18:41 +01:00
|
|
|
msg_release(msg);
|
2017-12-27 19:58:39 +01:00
|
|
|
teardown_curse(&fix);
|
2015-01-12 08:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_magicstreet_warning(CuTest *tc) {
|
|
|
|
curse_fixture fix;
|
|
|
|
message *msg;
|
|
|
|
setup_curse(&fix, "magicstreet");
|
2015-01-12 18:22:59 +01:00
|
|
|
fix.c->duration = 1;
|
2015-01-12 08:18:41 +01:00
|
|
|
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
|
2015-01-12 17:50:10 +01:00
|
|
|
CuAssertStrEquals(tc, "curseinfo::magicstreetwarn", test_get_messagetype(msg));
|
2015-01-12 08:18:41 +01:00
|
|
|
msg_release(msg);
|
2017-12-27 19:58:39 +01:00
|
|
|
teardown_curse(&fix);
|
2015-01-12 08:18:41 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 18:22:59 +01:00
|
|
|
static void test_good_dreams(CuTest *tc) {
|
|
|
|
curse_fixture fix;
|
|
|
|
message *msg;
|
|
|
|
setup_curse(&fix, "gbdream");
|
|
|
|
fix.c->effect = 1;
|
|
|
|
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
|
|
|
|
CuAssertStrEquals(tc, "curseinfo::gooddream", test_get_messagetype(msg));
|
|
|
|
msg_release(msg);
|
2017-12-27 19:58:39 +01:00
|
|
|
teardown_curse(&fix);
|
2015-01-12 18:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bad_dreams(CuTest *tc) {
|
|
|
|
curse_fixture fix;
|
|
|
|
message *msg;
|
|
|
|
setup_curse(&fix, "gbdream");
|
|
|
|
fix.c->effect = -1;
|
|
|
|
msg = fix.c->type->curseinfo(fix.r, TYP_REGION, fix.c, 0);
|
|
|
|
CuAssertStrEquals(tc, "curseinfo::baddream", test_get_messagetype(msg));
|
|
|
|
msg_release(msg);
|
2017-12-27 19:58:39 +01:00
|
|
|
teardown_curse(&fix);
|
2015-01-12 18:22:59 +01:00
|
|
|
}
|
|
|
|
|
2015-05-12 18:07:47 +02:00
|
|
|
static void test_memstream(CuTest *tc) {
|
|
|
|
storage store;
|
|
|
|
stream out = { 0 };
|
|
|
|
char buf[1024];
|
2017-08-18 19:36:17 +02:00
|
|
|
int val = 0;
|
2015-05-12 18:07:47 +02:00
|
|
|
|
|
|
|
mstream_init(&out);
|
|
|
|
binstore_init(&store, &out);
|
|
|
|
store.handle.data = &out;
|
|
|
|
|
|
|
|
WRITE_INT(&store, 999999);
|
|
|
|
WRITE_TOK(&store, "fortytwo");
|
|
|
|
WRITE_INT(&store, 42);
|
|
|
|
|
|
|
|
out.api->rewind(out.handle);
|
|
|
|
READ_INT(&store, &val);
|
|
|
|
READ_TOK(&store, buf, 1024);
|
|
|
|
CuAssertIntEquals(tc, 999999, val);
|
|
|
|
CuAssertStrEquals(tc, "fortytwo", buf);
|
|
|
|
READ_INT(&store, &val);
|
|
|
|
CuAssertIntEquals(tc, 42, val);
|
2015-05-31 11:03:44 +02:00
|
|
|
mstream_done(&out);
|
2015-05-12 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_write_flag(CuTest *tc) {
|
|
|
|
curse_fixture fix;
|
2016-02-13 13:42:02 +01:00
|
|
|
gamedata data;
|
2015-05-12 18:07:47 +02:00
|
|
|
storage store;
|
2016-02-26 17:54:25 +01:00
|
|
|
region * r;
|
|
|
|
curse * c;
|
|
|
|
int uid;
|
2015-05-31 11:03:44 +02:00
|
|
|
|
2016-02-13 13:42:02 +01:00
|
|
|
mstream_init(&data.strm);
|
|
|
|
gamedata_init(&data, &store, RELEASE_VERSION);
|
2015-05-12 18:07:47 +02:00
|
|
|
|
2017-08-18 19:36:17 +02:00
|
|
|
setup_curse(&fix, "magicwalls");
|
2016-02-26 17:54:25 +01:00
|
|
|
c = fix.c;
|
|
|
|
r = fix.r;
|
|
|
|
uid = r->uid;
|
2017-08-18 19:36:17 +02:00
|
|
|
c_setflag(c, CURSE_ISNEW);
|
|
|
|
c_setflag(c, CURSE_IMMUNE);
|
|
|
|
CuAssertIntEquals(tc, c->type->flags | CURSE_IMMUNE | CURSE_ISNEW, c_flags(c));
|
2016-02-26 17:54:25 +01:00
|
|
|
write_game(&data);
|
|
|
|
free_gamedata();
|
2016-02-13 13:42:02 +01:00
|
|
|
data.strm.api->rewind(data.strm.handle);
|
2016-02-26 17:54:25 +01:00
|
|
|
read_game(&data);
|
|
|
|
r = findregionbyid(uid);
|
|
|
|
CuAssertPtrNotNull(tc, r);
|
|
|
|
CuAssertPtrNotNull(tc, r->attribs);
|
|
|
|
c = (curse *)r->attribs->data.v;
|
2017-08-18 19:36:17 +02:00
|
|
|
CuAssertIntEquals(tc, c->type->flags | CURSE_IMMUNE | CURSE_ISNEW, c_flags(c));
|
2015-05-12 18:07:47 +02:00
|
|
|
|
2016-02-13 13:42:02 +01:00
|
|
|
mstream_done(&data.strm);
|
|
|
|
gamedata_done(&data);
|
2017-12-27 19:58:39 +01:00
|
|
|
teardown_curse(&fix);
|
2015-05-12 18:07:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 13:02:53 +02:00
|
|
|
static void test_curse_ids(CuTest *tc) {
|
|
|
|
const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL };
|
|
|
|
curse *c1, *c2;
|
|
|
|
attrib *a1 = 0, *a2 = 0;
|
|
|
|
|
|
|
|
test_setup();
|
|
|
|
rng_init(0);
|
|
|
|
c1 = create_curse(NULL, &a1, &ct_dummy, 1, 1, 1, 1);
|
|
|
|
rng_init(0);
|
|
|
|
c2 = create_curse(NULL, &a2, &ct_dummy, 1, 1, 1, 1);
|
|
|
|
CuAssertTrue(tc, c1->no != c2->no);
|
|
|
|
a_remove(&a1, a1);
|
|
|
|
a_remove(&a2, a2);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2016-10-23 13:02:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_curse_flags(CuTest *tc) {
|
|
|
|
const curse_type ct_dummy = { "dummy", CURSETYP_NORM, 0, M_SUMEFFECT, NULL };
|
|
|
|
curse *c1, *c2;
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
test_setup();
|
2018-01-14 09:38:26 +01:00
|
|
|
u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL));
|
2016-10-23 13:02:53 +02:00
|
|
|
c1 = create_curse(u, &u->attribs, &ct_dummy, 1, 1, 1, 0);
|
|
|
|
CuAssertPtrEquals(tc, u, c1->magician);
|
|
|
|
CuAssertIntEquals(tc, 1, (int)c1->effect);
|
|
|
|
CuAssertIntEquals(tc, 1, (int)c1->vigour);
|
|
|
|
CuAssertIntEquals(tc, 1, c1->duration);
|
|
|
|
c2 = create_curse(u, &u->attribs, &ct_dummy, 1, 1, 1, 0);
|
|
|
|
CuAssertPtrEquals(tc, c1, c2);
|
|
|
|
CuAssertPtrEquals(tc, u, c1->magician);
|
|
|
|
CuAssertIntEquals(tc, 2, (int)c1->effect);
|
|
|
|
CuAssertIntEquals(tc, 1, (int)c1->vigour);
|
|
|
|
CuAssertIntEquals(tc, 1, c1->duration);
|
2017-12-27 19:58:39 +01:00
|
|
|
test_teardown();
|
2016-10-23 13:02:53 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
CuSuite *get_curse_suite(void)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_curse);
|
|
|
|
SUITE_ADD_TEST(suite, test_magicstreet);
|
|
|
|
SUITE_ADD_TEST(suite, test_magicstreet_warning);
|
|
|
|
SUITE_ADD_TEST(suite, test_good_dreams);
|
|
|
|
SUITE_ADD_TEST(suite, test_bad_dreams);
|
2015-05-12 18:07:47 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_memstream);
|
2015-10-15 08:29:20 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_write_flag);
|
2016-10-23 13:02:53 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_curse_flags);
|
|
|
|
SUITE_ADD_TEST(suite, test_curse_ids);
|
2015-01-30 20:37:14 +01:00
|
|
|
return suite;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|