server/src/tests.c

220 lines
5.8 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
#include <cutest/CuTest.h>
#include <stdio.h>
#include <platform.h>
#include "tests.h"
#include "tests_test.c"
#include <util/base36_test.c>
#include <util/functions_test.c>
#include <util/quicklist_test.c>
#include <util/umlaut_test.c>
#include <kernel/magic_test.c>
#include <kernel/move_test.c>
#include <kernel/item_test.c>
#include <kernel/ship_test.c>
#include <kernel/building_test.c>
#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>
#include <gamecode/economy_test.c>
2011-03-09 06:16:47 +01:00
#include <gamecode/laws_test.c>
#include <gamecode/market_test.c>
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>
#include <kernel/terrain.h>
#include <util/functions.h>
2010-08-08 10:06:34 +02:00
#include <util/language.h>
2011-03-07 08:02:35 +01:00
int RunAllTests(void)
{
2010-08-08 10:06:34 +02:00
CuString *output = CuStringNew();
2011-03-07 08:02:35 +01:00
CuSuite *suite = CuSuiteNew();
int flags = log_flags;
2010-08-08 10:06:34 +02:00
2011-03-07 08:02:35 +01:00
log_flags = LOG_FLUSH | LOG_CPERROR;
2010-08-08 10:06:34 +02:00
/* self-test */
CuSuiteAddSuite(suite, get_tests_suite());
/* util */
2010-08-08 10:06:34 +02:00
CuSuiteAddSuite(suite, get_base36_suite());
CuSuiteAddSuite(suite, get_quicklist_suite());
CuSuiteAddSuite(suite, get_functions_suite());
CuSuiteAddSuite(suite, get_umlaut_suite());
/* kernel */
CuSuiteAddSuite(suite, get_pool_suite());
2010-08-08 10:06:34 +02:00
CuSuiteAddSuite(suite, get_curse_suite());
CuSuiteAddSuite(suite, get_equipment_suite());
CuSuiteAddSuite(suite, get_item_suite());
CuSuiteAddSuite(suite, get_magic_suite());
CuSuiteAddSuite(suite, get_move_suite());
CuSuiteAddSuite(suite, get_reports_suite());
CuSuiteAddSuite(suite, get_ship_suite());
CuSuiteAddSuite(suite, get_spellbook_suite());
CuSuiteAddSuite(suite, get_building_suite());
CuSuiteAddSuite(suite, get_spell_suite());
CuSuiteAddSuite(suite, get_battle_suite());
/* gamecode */
CuSuiteAddSuite(suite, get_market_suite());
CuSuiteAddSuite(suite, get_laws_suite());
CuSuiteAddSuite(suite, get_economy_suite());
2010-08-08 10:06:34 +02:00
CuSuiteRun(suite);
CuSuiteSummary(suite, output);
CuSuiteDetails(suite, output);
printf("%s\n", output->buffer);
log_flags = flags;
2010-08-08 10:06:34 +02:00
return suite->failCount;
}
2011-03-07 08:02:35 +01:00
struct race *test_create_race(const char *name)
2010-08-08 10:06:34 +02:00
{
race *rc = rc_add(rc_new(name));
rc->flags |= RCF_PLAYERRACE;
rc->maintenance = 10;
2010-08-08 10:06:34 +02:00
return rc;
}
struct region *test_create_region(int x, int y, const terrain_type *terrain)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
region *r = new_region(x, y, NULL, 0);
2010-08-08 10:06:34 +02:00
terraform_region(r, terrain);
rsettrees(r, 0, 0);
rsettrees(r, 1, 0);
rsettrees(r, 2, 0);
rsethorses(r, 0);
rsetpeasants(r, terrain->size);
return r;
}
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
{
faction *f = addfaction("nobody@eressea.de", NULL, rc?rc:rc_find("human"), default_locale, 0);
2010-08-08 10:06:34 +02:00
return f;
}
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
{
unit *u = create_unit(r, f, 1, f?f->race:rc_find("human"), 0, 0, 0);
2010-08-08 10:06:34 +02:00
return u;
}
2011-03-07 08:02:35 +01:00
void test_cleanup(void)
{
test_clear_terrains();
test_clear_resources();
2010-08-08 10:06:34 +02:00
global.functions.maintenance = NULL;
global.functions.wage = NULL;
default_locale = 0;
free_locales(); /* TODO: this is evil and leaky */
free_spells();
free_spellbooks();
2010-08-08 10:06:34 +02:00
free_gamedata();
}
terrain_type *
test_create_terrain(const char * name, unsigned int flags)
{
terrain_type * t;
assert(!get_terrain(name));
t = (terrain_type*)calloc(1, sizeof(terrain_type));
t->_name = strdup(name);
t->flags = flags;
register_terrain(t);
return t;
}
building * test_create_building(region * r, const building_type * btype)
{
building * b = new_building(btype?btype:bt_find("castle"), r, default_locale);
b->size = b->type->maxsize>0?b->type->maxsize:1;
return b;
}
ship * test_create_ship(region * r, const ship_type * stype)
{
ship * s = new_ship(stype?stype:st_find("boat"), r, default_locale);
s->size = s->type->construction?s->type->construction->maxsize:1;
return s;
}
item_type * test_create_itemtype(const char ** names) {
resource_type * rtype;
item_type * itype;
rtype = new_resourcetype(names, NULL, RTF_ITEM);
itype = new_itemtype(rtype, ITF_ANIMAL|ITF_BIG, 5000, 7000);
return itype;
}
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)
{
2011-03-07 08:02:35 +01:00
terrain_type *t_plain, *t_ocean;
region *island[2];
2010-08-08 10:06:34 +02:00
int i;
2011-03-07 08:02:35 +01:00
building_type *btype;
ship_type *stype;
item_type * itype;
const char * horses[2] = { "horse", "horse_p" };
make_locale("de");
init_resources();
assert(!olditemtype[I_HORSE]);
itype = test_create_itemtype(horses);
olditemtype[I_HORSE] = itype;
2010-08-08 10:06:34 +02:00
t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION);
t_plain->size = 1000;
t_ocean = test_create_terrain("ocean", SEA_REGION | SAIL_INTO | SWIM_INTO);
t_ocean->size = 0;
2010-08-08 10:06:34 +02:00
island[0] = test_create_region(0, 0, t_plain);
island[1] = test_create_region(1, 0, t_plain);
2011-03-07 08:02:35 +01:00
for (i = 0; i != 2; ++i) {
2011-03-09 06:16:47 +01:00
int j;
2011-03-07 08:02:35 +01:00
region *r = island[i];
for (j = 0; j != MAXDIRECTIONS; ++j) {
2011-03-09 06:16:47 +01:00
region *rn = r_connect(r, (direction_t)j);
2010-08-08 10:06:34 +02:00
if (!rn) {
2011-03-07 08:02:35 +01:00
rn = test_create_region(r->x + delta_x[j], r->y + delta_y[j], t_ocean);
2010-08-08 10:06:34 +02:00
}
}
}
test_create_race("human");
2010-08-08 10:06:34 +02:00
btype = (building_type*)calloc(sizeof(building_type), 1);
btype->flags = BTF_NAMECHANGE;
2010-08-08 10:06:34 +02:00
btype->_name = strdup("castle");
locale_setstring(default_locale, "castle", "castle");
2010-08-08 10:06:34 +02:00
bt_register(btype);
stype = (ship_type*)calloc(sizeof(ship_type), 1);
2010-08-08 10:06:34 +02:00
stype->name[0] = strdup("boat");
stype->name[1] = strdup("boat_p");
locale_setstring(default_locale, "boat", "boat");
2010-08-08 10:06:34 +02:00
st_register(stype);
}