refactor prepare_report to extract prepare_seen, because we assume the bug is in there.

add a failing test for it to prove that bug is in here.
This commit is contained in:
Enno Rehling 2015-09-09 09:04:59 +02:00
parent aefc74c2fb
commit 3eabf082ac
5 changed files with 105 additions and 38 deletions

View File

@ -1371,6 +1371,9 @@ static void cb_add_seen(region *r, unit *u, void *cbdata) {
unused_arg(cbdata);
if (u->faction) {
add_seen(u->faction->seen, r, see_travel, false);
#ifdef SMART_INTERVALS
update_interval(u->faction, r);
#endif
}
}
@ -1403,7 +1406,7 @@ static void prepare_reports(void)
}
/* Region owner get always the Lighthouse report */
if (check_param(global.parameters, "rules.region_owner_pay_building", bt_lighthouse->_name)) {
if (bt_lighthouse && check_param(global.parameters, "rules.region_owner_pay_building", bt_lighthouse->_name)) {
for (b = rbuildings(r); b; b = b->next) {
if (b && b->type == bt_lighthouse) {
u = building_owner(b);
@ -1510,37 +1513,45 @@ static region *firstregion(faction * f)
#endif
}
static void prepare_report(struct report_context *ctx, faction *f)
static void view_region(region *r, faction *f) {
plane *p = rplane(r);
void(*view) (struct seen_region **, region *, faction *) = view_default;
if (p && fval(p, PFL_SEESPECIAL)) {
/* TODO: this is not very customizable */
view = (strcmp(p->name, "Regatta") == 0) ? view_regatta : view_neighbours;
}
view(f->seen, r, f);
}
void prepare_seen(faction *f)
{
region *r;
struct seen_region *sr;
ctx->f = f;
ctx->report_time = time(NULL);
ctx->first = firstregion(f);
ctx->last = lastregion(f);
ctx->addresses = NULL;
ctx->userdata = NULL;
for (r = ctx->first, sr = NULL; sr == NULL && r != ctx->last; r = r->next) {
for (r = f->first, sr = NULL; sr == NULL && r != f->last; r = r->next) {
sr = find_seen(f->seen, r);
}
for (; sr != NULL; sr = sr->next) {
if (sr->mode > see_neighbour) {
region *r = sr->r;
plane *p = rplane(r);
void(*view) (struct seen_region **, region *, faction *) = view_default;
if (p && fval(p, PFL_SEESPECIAL)) {
/* TODO: this is not very customizable */
view = (strcmp(p->name, "Regatta") == 0) ? view_regatta : view_neighbours;
}
view(f->seen, r, f);
view_region(r, f);
}
}
get_seen_interval(ctx->f->seen, &ctx->first, &ctx->last);
link_seen(ctx->f->seen, ctx->first, ctx->last);
get_seen_interval(f->seen, &f->first, &f->last);
link_seen(f->seen, f->first, f->last);
}
static void prepare_report(struct report_context *ctx, faction *f)
{
prepare_seen(f);
ctx->f = f;
ctx->report_time = time(NULL);
ctx->addresses = NULL;
ctx->userdata = NULL;
ctx->first = firstregion(f);
ctx->last = lastregion(f);
}
int write_reports(faction * f, time_t ltime)

View File

@ -56,6 +56,7 @@ extern "C" {
void spunit(struct strlist **SP, const struct faction *f,
const struct unit *u, unsigned int indent, int mode);
void prepare_seen(struct faction *f);
int reports(void);
int write_reports(struct faction *f, time_t ltime);
int init_reports(void);

View File

@ -122,7 +122,7 @@ void get_seen_interval(struct seen_region *seen[], struct region **firstp, struc
*lastp = last;
}
bool add_seen(struct seen_region *seehash[], struct region *r, int mode, bool dis)
bool add_seen(struct seen_region *seehash[], struct region *r, unsigned char mode, bool dis)
{
seen_region *find = find_seen(seehash, r);
if (find == NULL) {

View File

@ -51,7 +51,7 @@ void free_seen(void);
void link_seen(struct seen_region *seehash[], const struct region * first, const struct region * last);
struct seen_region *find_seen(struct seen_region *seehash[], const struct region * r);
void get_seen_interval(struct seen_region *seen[], struct region **firstp, struct region **lastp);
bool add_seen(struct seen_region *seehash[], struct region *r, int mode, bool dis);
bool add_seen(struct seen_region *seehash[], struct region *r, unsigned char mode, bool dis);
void link_seen(struct seen_region *seehash[], const struct region *first, const struct region *last);
#ifdef __cplusplus
}

View File

@ -2,24 +2,83 @@
#include <config.h>
#include "seen.h"
#include "reports.h"
#include "travelthru.h"
#include <kernel/region.h>
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <CuTest.h>
#include <tests.h>
static void setup_seen(int x, int y) {
int dir;
for (dir = 0; dir != MAXDIRECTIONS; ++dir) {
test_create_region(x+delta_x[dir], y+delta_y[dir], 0);
}
}
static void test_prepare_seen(CuTest *tc) {
region *r;
faction *f;
unit *u;
test_cleanup();
f = test_create_faction(0);
r = test_create_region(0, 0, 0);
u = test_create_unit(f, r);
f->seen = seen_init();
add_seen(f->seen, r, see_unit, false);
setup_seen(0, 0);
r = test_create_region(2, 2, 0);
setup_seen(2, 2);
travelthru_add(r, u);
init_reports();
prepare_seen(f);
CuAssertPtrEquals(tc, regions, f->first);
CuAssertPtrEquals(tc, 0, f->last);
seen_done(f->seen);
test_cleanup();
}
static void test_seen_travelthru(CuTest *tc) {
seen_region *sr;
region *r;
faction *f;
unit *u;
test_cleanup();
setup_seen(0, 0);
r = test_create_region(0, 0, 0);
f = test_create_faction(0);
u = test_create_unit(f, 0);
travelthru_add(r, u);
init_reports();
view_default(f->seen, r, f);
get_seen_interval(f->seen, &f->first, &f->last);
link_seen(f->seen, f->first, f->last);
CuAssertPtrEquals(tc, regions, f->first);
CuAssertPtrEquals(tc, 0, f->last);
sr = find_seen(f->seen, regions);
CuAssertPtrEquals(tc, regions, sr->r);
CuAssertIntEquals(tc, see_neighbour, sr->mode);
sr = find_seen(f->seen, r);
CuAssertPtrEquals(tc, r, sr->r);
CuAssertIntEquals(tc, see_travel, sr->mode);
test_cleanup();
}
static void test_seen_region(CuTest *tc) {
seen_region **seen, *sr;
region *r;
int dir;
test_cleanup();
for (dir=0;dir!=MAXDIRECTIONS;++dir) {
region *rn = test_create_region(delta_x[dir], delta_y[dir], 0);
}
setup_seen(0, 0);
r = test_create_region(0, 0, 0);
seen = seen_init();
add_seen(seen, r, see_unit, true);
add_seen(seen, r, see_unit, false);
sr = find_seen(seen, r);
CuAssertPtrEquals(tc, r, sr->r);
seen_done(seen);
@ -28,16 +87,13 @@ static void test_seen_region(CuTest *tc) {
static void test_seen_interval_backward(CuTest *tc) {
region *r, *first, *last;
seen_region **seen, *sr;
int dir;
seen_region **seen;
test_cleanup();
r = test_create_region(0, 0, 0);
for (dir=0;dir!=MAXDIRECTIONS;++dir) {
region *rn = test_create_region(delta_x[dir], delta_y[dir], 0);
}
setup_seen(0, 0);
seen = seen_init();
add_seen(seen, r, see_unit, true);
add_seen(seen, r, see_unit, false);
view_default(seen, r, 0);
first = r;
last = 0;
@ -49,13 +105,10 @@ static void test_seen_interval_backward(CuTest *tc) {
static void test_seen_interval_forward(CuTest *tc) {
region *r, *first, *last;
seen_region **seen, *sr;
int dir;
seen_region **seen;
test_cleanup();
for (dir=0;dir!=MAXDIRECTIONS;++dir) {
region *rn = test_create_region(delta_x[dir], delta_y[dir], 0);
}
setup_seen(0, 0);
r = test_create_region(0, 0, 0);
seen = seen_init();
add_seen(seen, r, see_unit, true);
@ -71,6 +124,8 @@ static void test_seen_interval_forward(CuTest *tc) {
CuSuite *get_seen_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_prepare_seen);
SUITE_ADD_TEST(suite, test_seen_travelthru);
SUITE_ADD_TEST(suite, test_seen_region);
SUITE_ADD_TEST(suite, test_seen_interval_backward);
SUITE_ADD_TEST(suite, test_seen_interval_forward);