refactoring and tests for wormhole module.

This commit is contained in:
Enno Rehling 2014-09-29 23:19:59 +02:00
parent 139ff04108
commit 4527ffdda2
12 changed files with 119 additions and 46 deletions

View File

@ -92,6 +92,7 @@ set (ERESSEA_SRC
study.c
summary.c
monsters.c
wormhole.c
${SPELLS_SRC}
${RACES_SRC}
${ITEMS_SRC}
@ -153,6 +154,7 @@ target_link_libraries(eressea
)
set(TESTS_SRC
wormhole.test.c
test_eressea.c
tests.c
battle.test.c

View File

@ -16,7 +16,6 @@
#include <kernel/xmlreader.h>
#include <kernel/item.h>
#include <kernel/building.h>
#include <modules/wormhole.h>
#include <modules/gmcmd.h>
#include <modules/xmas.h>
#include <items/itemtypes.h>
@ -25,6 +24,7 @@
#include "items.h"
#include "creport.h"
#include "names.h"
#include "wormhole.h"
void game_done(void)
{

View File

@ -17,6 +17,7 @@
#include "gmtool_structs.h"
#include "console.h"
#include "listbox.h"
#include "wormhole.h"
#include <modules/xmas.h>
#include <modules/gmcmd.h>
@ -26,7 +27,6 @@
#if ARENA_MODULE
#include <modules/arena.h>
#endif
#include <modules/wormhole.h>
#include <modules/autoseed.h>
#if DUNGEON_MODULE
#include <modules/dungeon.h>

View File

@ -22,18 +22,18 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "laws.h"
#include <modules/gmcmd.h>
#include <modules/wormhole.h>
#include "alchemy.h"
#include "battle.h"
#include "economy.h"
#include "keyword.h"
#include "market.h"
#include "monster.h"
#include "move.h"
#include "randenc.h"
#include "spy.h"
#include "study.h"
#include "market.h"
#include "keyword.h"
#include "move.h"
#include "battle.h"
#include "alchemy.h"
#include "wormhole.h"
/* kernel includes */
#include <kernel/alliance.h>

View File

@ -7,7 +7,6 @@ gmcmd.c
museum.c
score.c
weather.c
wormhole.c
xmas.c
)
FOREACH(_FILE ${_FILES})

View File

@ -60,6 +60,7 @@ int RunAllTests(void)
ADD_TESTS(suite, stealth);
ADD_TESTS(suite, upkeep);
ADD_TESTS(suite, vortex);
ADD_TESTS(suite, wormhole);
CuSuiteRun(suite);
CuSuiteSummary(suite, output);

View File

@ -115,7 +115,9 @@ building_type * test_create_buildingtype(const char * name)
btype->construction->materials[1].number = 0;
btype->construction->materials[0].number = 1;
btype->construction->materials[0].rtype = get_resourcetype(R_STONE);
locale_setstring(default_locale, name, name);
if (default_locale) {
locale_setstring(default_locale, name, name);
}
bt_register(btype);
return btype;
}

View File

@ -116,22 +116,8 @@ void addtoken(void ** root, const char *str, variant id)
const char str[3];
} replace[] = {
/* match lower-case (!) umlauts and others to transcriptions */
{
228, "AE" }, /* auml */
{
246, "OE" }, /* ouml */
{
252, "UE" }, /* uuml */
{
223, "SS" }, /* szlig */
{
230, "AE" }, /* norsk */
{
248, "OE" }, /* norsk */
{
229, "AA" }, /* norsk */
{
0, "" }
{ 228, "AE" }, { 246, "OE" }, { 252, "UE" }, { 223, "SS" },
{ 230, "AE" }, { 248, "OE" }, { 229, "AA" }, { 0, "" }
};
assert(root && str);

View File

@ -35,13 +35,9 @@ static void test_move_to_vortex(CuTest *tc) {
CuAssertPtrEquals(tc, r2, r);
}
static void test_vortex(CuTest *tc) {
}
CuSuite *get_vortex_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_vortex);
SUITE_ADD_TEST(suite, test_move_to_vortex);
return suite;
}

View File

@ -173,20 +173,13 @@ make_wormhole(const building_type * bt_wormhole, region * r1, region * r2)
ADDMSG(&r2->msgs, msg_message("wormhole_appear", "region", r2));
}
void create_wormholes(void)
{
#define WORMHOLE_CHANCE 10000
const building_type *bt_wormhole = bt_find("wormhole");
quicklist *ql, *rlist = 0;
region *r = regions;
int qi, i = 0, count = 0;
region **match;
if (bt_wormhole == NULL)
return;
/*
* select a list of regions. we'll sort them by age later.
*/
void select_wormhole_regions(quicklist **rlistp, int *countp) {
quicklist *rlist = 0;
region *r = regions;
int count = 0;
while (r != NULL) {
int next = rng_int() % (2 * WORMHOLE_CHANCE);
while (r != NULL && (next != 0 || !good_region(r))) {
@ -202,21 +195,47 @@ void create_wormholes(void)
r = r->next;
}
if (count < 2)
return;
*countp = count;
*rlistp = rlist;
}
match = (region **)malloc(sizeof(region *) * count);
void sort_wormhole_regions(quicklist *rlist, region **match, int count) {
quicklist *ql;
int qi, i = 0;
for (ql = rlist, qi = 0; i != count; ql_advance(&ql, &qi, 1)) {
match[i++] = (region *)ql_get(ql, qi);
}
qsort(match, count, sizeof(region *), cmp_age);
ql_free(rlist);
}
void make_wormholes(region **match, int count, const building_type *bt_wormhole) {
int i;
count /= 2;
for (i = 0; i != count; ++i) {
make_wormhole(bt_wormhole, match[i], match[i + count]);
}
}
void create_wormholes(void)
{
const building_type *bt_wormhole = bt_find("wormhole");
quicklist *rlist = 0;
int count = 0;
region **match;
if (bt_wormhole == NULL) {
return;
}
select_wormhole_regions(&rlist, &count);
if (count < 2) {
return;
}
match = (region **)malloc(sizeof(region *) * count);
sort_wormhole_regions(rlist, match, count);
ql_free(rlist);
make_wormholes(match, count, bt_wormhole);
free(match);
}

68
src/wormhole.test.c Normal file
View File

@ -0,0 +1,68 @@
#include <platform.h>
#include <kernel/types.h>
#include "wormhole.h"
#include "tests.h"
#include <kernel/building.h>
#include <kernel/region.h>
#include <kernel/terrain.h>
#include <util/attrib.h>
#include <quicklist.h>
#include <CuTest.h>
void sort_wormhole_regions(quicklist *rlist, region **match, int count);
void make_wormholes(region **match, int count, const building_type *bt_wormhole);
static void test_make_wormholes(CuTest *tc) {
region *r1, *r2, *match[2];
terrain_type *t_plain;
building_type *btype;
test_cleanup();
t_plain = test_create_terrain("plain", LAND_REGION);
btype = test_create_buildingtype("wormhole");
match[0] = r1 = test_create_region(0, 0, t_plain);
match[1] = r2 = test_create_region(1, 0, t_plain);
make_wormholes(match, 2, btype);
CuAssertPtrNotNull(tc, r1->buildings);
CuAssertPtrNotNull(tc, r1->buildings->attribs);
CuAssertPtrEquals(tc, (void *)r1->buildings->type, (void *)btype);
CuAssertPtrNotNull(tc, r2->buildings);
CuAssertPtrNotNull(tc, r2->buildings->attribs);
CuAssertPtrEquals(tc, (void *)r2->buildings->type, (void *)btype);
CuAssertPtrEquals(tc, (void *)r1->buildings->attribs->type, (void *)r2->buildings->attribs->type);
CuAssertStrEquals(tc, r1->buildings->attribs->type->name, "wormhole");
test_cleanup();
}
static void test_sort_wormhole_regions(CuTest *tc) {
region *r1, *r2, *match[2];
terrain_type *t_plain;
quicklist *rlist = 0;
test_cleanup();
t_plain = test_create_terrain("plain", LAND_REGION);
r1 = test_create_region(0, 0, t_plain);
r2 = test_create_region(1, 0, t_plain);
r1->age = 4;
r2->age = 2;
ql_push(&rlist, r1);
ql_push(&rlist, r2);
sort_wormhole_regions(rlist, match, 2);
CuAssertPtrEquals(tc, r2, match[0]);
CuAssertPtrEquals(tc, r1, match[1]);
ql_free(rlist);
test_cleanup();
}
CuSuite *get_wormhole_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_sort_wormhole_regions);
SUITE_ADD_TEST(suite, test_make_wormholes);
return suite;
}