2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include "tests.h"
|
2014-07-20 06:19:21 +02:00
|
|
|
#include "keyword.h"
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2012-06-04 11:35:56 +02:00
|
|
|
#include <kernel/config.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/terrain.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/unit.h>
|
|
|
|
#include <kernel/race.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/building.h>
|
|
|
|
#include <kernel/ship.h>
|
2012-06-04 11:41:07 +02:00
|
|
|
#include <kernel/spell.h>
|
|
|
|
#include <kernel/spellbook.h>
|
2011-03-13 02:01:20 +01:00
|
|
|
#include <kernel/terrain.h>
|
2015-01-13 07:43:30 +01:00
|
|
|
#include <kernel/messages.h>
|
2015-02-09 10:57:03 +01:00
|
|
|
#include <util/bsdstring.h>
|
2011-03-13 02:01:20 +01:00
|
|
|
#include <util/functions.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/language.h>
|
2015-01-12 17:50:10 +01:00
|
|
|
#include <util/message.h>
|
2012-06-02 03:05:07 +02:00
|
|
|
#include <util/log.h>
|
|
|
|
|
2015-02-01 19:14:06 +01:00
|
|
|
#include <CuTest.h>
|
|
|
|
|
2012-06-02 03:05:07 +02:00
|
|
|
#include <assert.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
struct race *test_create_race(const char *name)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
race *rc = rc_get_or_create(name);
|
|
|
|
rc->maintenance = 10;
|
2015-04-19 12:49:39 +02:00
|
|
|
rc->ec_flags |= GETITEM;
|
2015-01-30 20:37:14 +01:00
|
|
|
return rc;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-13 02:01:20 +01:00
|
|
|
struct region *test_create_region(int x, int y, const terrain_type *terrain)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
region *r = new_region(x, y, NULL, 0);
|
2015-02-01 19:14:06 +01:00
|
|
|
if (!terrain) {
|
|
|
|
terrain_type *t = get_or_create_terrain("plain");
|
|
|
|
t->size = 1000;
|
|
|
|
fset(t, LAND_REGION);
|
|
|
|
terraform_region(r, t);
|
|
|
|
} else
|
|
|
|
terraform_region(r, terrain);
|
2015-01-30 20:37:14 +01:00
|
|
|
rsettrees(r, 0, 0);
|
|
|
|
rsettrees(r, 1, 0);
|
|
|
|
rsettrees(r, 2, 0);
|
|
|
|
rsethorses(r, 0);
|
|
|
|
rsetpeasants(r, r->terrain->size);
|
|
|
|
return r;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
struct faction *test_create_faction(const struct race *rc)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
faction *f = addfaction("nobody@eressea.de", NULL, rc ? rc : rc_get_or_create("human"), default_locale, 0);
|
|
|
|
return f;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
struct unit *test_create_unit(struct faction *f, struct region *r)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2014-10-16 08:13:56 +02:00
|
|
|
const struct race * rc = f ? f->race : 0;
|
2014-10-16 08:06:44 +02:00
|
|
|
assert(f || !r);
|
2014-10-16 08:13:56 +02:00
|
|
|
return create_unit(r, f, 1, rc ? rc : rc_get_or_create("human"), 0, 0, 0);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
void test_cleanup(void)
|
|
|
|
{
|
2014-12-30 02:02:10 +01:00
|
|
|
free_terrains();
|
2014-12-30 23:49:50 +01:00
|
|
|
free_resources();
|
2014-06-14 02:36:05 +02:00
|
|
|
global.functions.maintenance = NULL;
|
|
|
|
global.functions.wage = NULL;
|
2014-06-17 17:46:22 +02:00
|
|
|
default_locale = 0;
|
|
|
|
free_locales();
|
|
|
|
free_spells();
|
|
|
|
free_buildingtypes();
|
|
|
|
free_shiptypes();
|
|
|
|
free_races();
|
|
|
|
free_spellbooks();
|
2014-06-14 02:36:05 +02:00
|
|
|
free_gamedata();
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2011-03-13 02:01:20 +01:00
|
|
|
terrain_type *
|
|
|
|
test_create_terrain(const char * name, unsigned int flags)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
terrain_type * t = get_or_create_terrain(name);
|
|
|
|
t->flags = flags;
|
|
|
|
return t;
|
2011-03-13 02:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
building * test_create_building(region * r, const building_type * btype)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
building * b = new_building(btype ? btype : bt_get_or_create("castle"), r, default_locale);
|
|
|
|
b->size = b->type->maxsize > 0 ? b->type->maxsize : 1;
|
|
|
|
return b;
|
2011-03-13 02:01:20 +01:00
|
|
|
}
|
2011-03-16 05:04:38 +01:00
|
|
|
|
2012-05-17 09:13:30 +02:00
|
|
|
ship * test_create_ship(region * r, const ship_type * stype)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship * s = new_ship(stype ? stype : st_get_or_create("boat"), r, default_locale);
|
|
|
|
s->size = s->type->construction ? s->type->construction->maxsize : 1;
|
|
|
|
return s;
|
2012-05-17 09:13:30 +02:00
|
|
|
}
|
|
|
|
|
2014-06-30 02:02:45 +02:00
|
|
|
ship_type * test_create_shiptype(const char * name)
|
2012-07-02 03:00:31 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
ship_type * stype = st_get_or_create(name);
|
|
|
|
locale_setstring(default_locale, name, name);
|
|
|
|
return stype;
|
2012-07-02 03:00:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
building_type * test_create_buildingtype(const char * name)
|
|
|
|
{
|
2014-06-29 01:19:46 +02:00
|
|
|
building_type *btype = (building_type *)calloc(sizeof(building_type), 1);
|
|
|
|
btype->flags = BTF_NAMECHANGE;
|
|
|
|
btype->_name = _strdup(name);
|
|
|
|
btype->construction = (construction *)calloc(sizeof(construction), 1);
|
|
|
|
btype->construction->skill = SK_BUILDING;
|
|
|
|
btype->construction->maxsize = -1;
|
|
|
|
btype->construction->minskill = 1;
|
|
|
|
btype->construction->reqsize = 1;
|
2014-06-29 08:58:00 +02:00
|
|
|
btype->construction->materials = (requirement *)calloc(sizeof(requirement), 2);
|
|
|
|
btype->construction->materials[1].number = 0;
|
|
|
|
btype->construction->materials[0].number = 1;
|
|
|
|
btype->construction->materials[0].rtype = get_resourcetype(R_STONE);
|
2014-09-29 23:19:59 +02:00
|
|
|
if (default_locale) {
|
|
|
|
locale_setstring(default_locale, name, name);
|
|
|
|
}
|
2014-06-29 01:19:46 +02:00
|
|
|
bt_register(btype);
|
|
|
|
return btype;
|
2012-07-02 03:00:31 +02:00
|
|
|
}
|
|
|
|
|
2014-06-30 02:02:45 +02:00
|
|
|
item_type * test_create_itemtype(const char * name) {
|
2015-01-30 20:37:14 +01:00
|
|
|
resource_type * rtype;
|
|
|
|
item_type * itype;
|
2011-03-16 05:04:38 +01:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
rtype = rt_get_or_create(name);
|
|
|
|
itype = it_get_or_create(rtype);
|
2011-03-16 05:04:38 +01:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
return itype;
|
2011-03-16 05:04:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-17 19:53:19 +01:00
|
|
|
void test_translate_param(const struct locale *lang, param_t param, const char *text) {
|
|
|
|
struct critbit_tree **cb;
|
2015-01-30 20:37:14 +01:00
|
|
|
|
2014-12-17 19:53:19 +01:00
|
|
|
assert(lang && text);
|
|
|
|
cb = (struct critbit_tree **)get_translations(lang, UT_PARAMS);
|
|
|
|
add_translation(cb, text, param);
|
|
|
|
}
|
|
|
|
|
2010-08-08 10:06:34 +02:00
|
|
|
/** creates a small world and some stuff in it.
|
|
|
|
* two terrains: 'plain' and 'ocean'
|
|
|
|
* one race: 'human'
|
|
|
|
* one ship_type: 'boat'
|
|
|
|
* one building_type: 'castle'
|
|
|
|
* in 0.0 and 1.0 is an island of two plains, around it is ocean.
|
|
|
|
*/
|
|
|
|
void test_create_world(void)
|
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
terrain_type *t_plain, *t_ocean;
|
|
|
|
region *island[2];
|
|
|
|
int i;
|
|
|
|
item_type * itype;
|
|
|
|
struct locale * loc;
|
|
|
|
|
|
|
|
loc = get_or_create_locale("de");
|
|
|
|
locale_setstring(loc, keyword(K_RESERVE), "RESERVIEREN");
|
|
|
|
locale_setstring(loc, "money", "SILBER");
|
|
|
|
init_resources();
|
|
|
|
|
|
|
|
itype = test_create_itemtype("horse");
|
|
|
|
itype->flags |= ITF_BIG | ITF_ANIMAL;
|
|
|
|
itype->weight = 5000;
|
|
|
|
itype->capacity = 7000;
|
|
|
|
|
2015-04-14 02:32:01 +02:00
|
|
|
itype = test_create_itemtype("cart");
|
|
|
|
itype->flags |= ITF_BIG | ITF_VEHICLE;
|
|
|
|
itype->weight = 4000;
|
|
|
|
itype->capacity = 14000;
|
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
test_create_itemtype("iron");
|
|
|
|
test_create_itemtype("stone");
|
|
|
|
|
|
|
|
t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION);
|
|
|
|
t_plain->size = 1000;
|
|
|
|
t_plain->max_road = 100;
|
|
|
|
t_ocean = test_create_terrain("ocean", SEA_REGION | SAIL_INTO | SWIM_INTO);
|
|
|
|
t_ocean->size = 0;
|
|
|
|
|
|
|
|
island[0] = test_create_region(0, 0, t_plain);
|
|
|
|
island[1] = test_create_region(1, 0, t_plain);
|
|
|
|
for (i = 0; i != 2; ++i) {
|
|
|
|
int j;
|
|
|
|
region *r = island[i];
|
|
|
|
for (j = 0; j != MAXDIRECTIONS; ++j) {
|
|
|
|
region *rn = r_connect(r, (direction_t)j);
|
|
|
|
if (!rn) {
|
|
|
|
rn = test_create_region(r->x + delta_x[j], r->y + delta_y[j], t_ocean);
|
|
|
|
}
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
test_create_race("human");
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
test_create_buildingtype("castle");
|
|
|
|
test_create_shiptype("boat");
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2012-06-04 11:35:56 +02:00
|
|
|
|
2015-01-13 07:43:30 +01:00
|
|
|
message * test_get_last_message(message_list *msgs) {
|
|
|
|
struct mlist *iter = msgs->begin;
|
|
|
|
while (iter->next) {
|
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
return iter->msg;
|
|
|
|
}
|
|
|
|
|
2015-01-12 17:50:10 +01:00
|
|
|
const char * test_get_messagetype(const message *msg) {
|
|
|
|
const char * name = msg->type->name;
|
|
|
|
if (strcmp(name, "missing_message") == 0) {
|
|
|
|
name = (const char *)msg->parameters[0].v;
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "missing_feedback") == 0) {
|
|
|
|
name = (const char *)msg->parameters[3].v;
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:14:06 +01:00
|
|
|
const message_type *register_msg(const char *type, int n_param, ...) {
|
|
|
|
char **argv;
|
|
|
|
va_list args;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
va_start(args, n_param);
|
|
|
|
|
|
|
|
argv = malloc(sizeof(char *) * (n_param + 1));
|
|
|
|
for (i = 0; i < n_param; ++i) {
|
|
|
|
argv[i] = va_arg(args, char *);
|
|
|
|
}
|
|
|
|
argv[n_param] = 0;
|
|
|
|
va_end(args);
|
|
|
|
return mt_register(mt_new(type, (const char **)argv));
|
|
|
|
}
|
|
|
|
|
|
|
|
void assert_messages(struct CuTest * tc, struct mlist *msglist, const message_type **types,
|
|
|
|
int num_msgs, bool exact_match, ...) {
|
2015-02-09 10:57:03 +01:00
|
|
|
char buf[100];
|
2015-02-01 19:14:06 +01:00
|
|
|
va_list args;
|
2015-02-09 10:57:03 +01:00
|
|
|
int found = 0, argc = -1;
|
2015-02-01 19:14:06 +01:00
|
|
|
struct message *msg;
|
|
|
|
bool match = true;
|
|
|
|
|
|
|
|
va_start(args, exact_match);
|
|
|
|
|
|
|
|
while (msglist) {
|
2015-02-09 10:57:03 +01:00
|
|
|
msg = msglist->msg;
|
2015-02-01 19:14:06 +01:00
|
|
|
if (found >= num_msgs) {
|
|
|
|
if (exact_match) {
|
2015-02-09 10:57:03 +01:00
|
|
|
slprintf(buf, sizeof(buf), "too many messages: %s", msg->type->name);
|
|
|
|
CuFail(tc, buf);
|
2015-02-01 19:14:06 +01:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (exact_match || match)
|
|
|
|
argc = va_arg(args, int);
|
|
|
|
|
|
|
|
match = strcmp(types[argc]->name, msg->type->name) == 0;
|
|
|
|
if (match)
|
|
|
|
++found;
|
|
|
|
else if (exact_match)
|
|
|
|
CuAssertStrEquals(tc, types[argc]->name, msg->type->name);
|
|
|
|
|
|
|
|
msglist = msglist->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
CuAssertIntEquals_Msg(tc, "not enough messages", num_msgs, found);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
2015-02-08 13:37:44 +01:00
|
|
|
|
2015-02-08 13:42:04 +01:00
|
|
|
void disabled_test(void *suite, void (*test)(CuTest *), const char *name) {
|
|
|
|
(void)test;
|
2015-02-08 13:37:44 +01:00
|
|
|
fprintf(stderr, "%s: SKIP\n", name);
|
|
|
|
}
|