Merge pull request #39 from badgerman/master

Additional tests and elimination of dead code
This commit is contained in:
Enno Rehling 2014-10-16 10:25:39 +02:00
commit 34c80713ad
12 changed files with 243 additions and 132 deletions

View File

@ -4,6 +4,7 @@
#include "json.h" #include "json.h"
#include <kernel/faction.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/save.h> #include <kernel/save.h>

View File

@ -57,9 +57,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/bsdstring.h> #include <util/bsdstring.h>
#include <util/event.h> #include <util/event.h>
#include <util/goodies.h> #include <util/goodies.h>
#include <util/lists.h>
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
#include <util/log.h>
#include <util/parser.h> #include <util/parser.h>
#include <util/rng.h> #include <util/rng.h>

View File

@ -102,7 +102,6 @@ struct settings global = {
bool lomem = false; bool lomem = false;
FILE *logfile; FILE *logfile;
FILE *updatelog;
bool battledebug = false; bool battledebug = false;
int turn = -1; int turn = -1;
@ -122,17 +121,6 @@ bool IsImmune(const faction * f)
return !fval(f, FFL_NPC) && f->age < NewbieImmunity(); return !fval(f, FFL_NPC) && f->age < NewbieImmunity();
} }
static int MaxAge(void)
{
static int value = -1;
static int gamecookie = -1;
if (value < 0 || gamecookie != global.cookie) {
gamecookie = global.cookie;
value = get_param_int(global.parameters, "MaxAge", 0);
}
return value;
}
static int ally_flag(const char *s, int help_mask) static int ally_flag(const char *s, int help_mask)
{ {
if ((help_mask & HELP_MONEY) && strcmp(s, "money") == 0) if ((help_mask & HELP_MONEY) && strcmp(s, "money") == 0)
@ -2069,100 +2057,6 @@ char *_strdup(const char *s)
} }
#endif #endif
void remove_empty_factions(void)
{
faction **fp, *f3;
for (fp = &factions; *fp;) {
faction *f = *fp;
/* monster (0) werden nicht entfernt. alive kann beim readgame
* () auf 0 gesetzt werden, wenn monsters keine einheiten mehr
* haben. */
if ((f->units == NULL || f->alive == 0) && !is_monsters(f)) {
ursprung *ur = f->ursprung;
while (ur && ur->id != 0)
ur = ur->next;
if (verbosity >= 2)
log_printf(stdout, "\t%s\n", factionname(f));
/* Einfach in eine Datei schreiben und später vermailen */
if (updatelog)
fprintf(updatelog, "dropout %s\n", itoa36(f->no));
for (f3 = factions; f3; f3 = f3->next) {
ally *sf;
group *g;
ally **sfp = &f3->allies;
while (*sfp) {
sf = *sfp;
if (sf->faction == f || sf->faction == NULL) {
*sfp = sf->next;
free(sf);
}
else
sfp = &(*sfp)->next;
}
for (g = f3->groups; g; g = g->next) {
sfp = &g->allies;
while (*sfp) {
sf = *sfp;
if (sf->faction == f || sf->faction == NULL) {
*sfp = sf->next;
free(sf);
}
else
sfp = &(*sfp)->next;
}
}
}
*fp = f->next;
funhash(f);
free_faction(f);
free(f);
}
else
fp = &(*fp)->next;
}
}
void remove_empty_units_in_region(region * r)
{
unit **up = &r->units;
while (*up) {
unit *u = *up;
if (u->number) {
faction *f = u->faction;
if (f == NULL || !f->alive) {
set_number(u, 0);
}
if (MaxAge() > 0) {
if ((!fval(f, FFL_NOTIMEOUT) && f->age > MaxAge())) {
set_number(u, 0);
}
}
}
if ((u->number == 0 && u_race(u) != get_race(RC_SPELL)) || (u->age <= 0
&& u_race(u) == get_race(RC_SPELL))) {
remove_unit(up, u);
}
if (*up == u)
up = &u->next;
}
}
void remove_empty_units(void)
{
region *r;
for (r = regions; r; r = r->next) {
remove_empty_units_in_region(r);
}
}
bool faction_id_is_unused(int id) bool faction_id_is_unused(int id)
{ {
return findfaction(id) == NULL; return findfaction(id) == NULL;
@ -2715,20 +2609,12 @@ int entertainmoney(const region * r)
int rule_give(void) int rule_give(void)
{ {
static int value = -1; return get_param_int(global.parameters, "rules.give", GIVE_DEFAULT);
if (value < 0) {
value = get_param_int(global.parameters, "rules.give", GIVE_DEFAULT);
}
return value;
} }
int markets_module(void) int markets_module(void)
{ {
static int value = -1; return get_param_int(global.parameters, "modules.markets", 0);
if (value < 0) {
value = get_param_int(global.parameters, "modules.markets", 0);
}
return value;
} }
/** releases all memory associated with the game state. /** releases all memory associated with the game state.

View File

@ -107,10 +107,6 @@ extern "C" {
#define i2b(i) ((bool)((i)?(true):(false))) #define i2b(i) ((bool)((i)?(true):(false)))
void remove_empty_units_in_region(struct region *r);
void remove_empty_units(void);
void remove_empty_factions(void);
typedef struct strlist { typedef struct strlist {
struct strlist *next; struct strlist *next;
char *s; char *s;

View File

@ -601,3 +601,57 @@ int skill_limit(faction * f, skill_t sk)
return m; return m;
} }
void remove_empty_factions(void)
{
faction **fp, *f3;
for (fp = &factions; *fp;) {
faction *f = *fp;
/* monster (0) werden nicht entfernt. alive kann beim readgame
* () auf 0 gesetzt werden, wenn monsters keine einheiten mehr
* haben. */
if ((f->units == NULL || f->alive == 0) && !is_monsters(f)) {
ursprung *ur = f->ursprung;
while (ur && ur->id != 0)
ur = ur->next;
if (verbosity >= 2)
log_printf(stdout, "\t%s\n", factionname(f));
/* Einfach in eine Datei schreiben und später vermailen */
for (f3 = factions; f3; f3 = f3->next) {
ally *sf;
group *g;
ally **sfp = &f3->allies;
while (*sfp) {
sf = *sfp;
if (sf->faction == f || sf->faction == NULL) {
*sfp = sf->next;
free(sf);
}
else
sfp = &(*sfp)->next;
}
for (g = f3->groups; g; g = g->next) {
sfp = &g->allies;
while (*sfp) {
sf = *sfp;
if (sf->faction == f || sf->faction == NULL) {
*sfp = sf->next;
free(sf);
}
else
sfp = &(*sfp)->next;
}
}
}
*fp = f->next;
funhash(f);
free_faction(f);
free(f);
}
else
fp = &(*fp)->next;
}
}

View File

@ -20,6 +20,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#define H_KRNL_FACTION #define H_KRNL_FACTION
#include "skill.h" #include "skill.h"
#include "types.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -48,10 +49,10 @@ extern "C" {
#define FFL_NOIDLEOUT (1<<24) /* Partei stirbt nicht an NMRs */ #define FFL_NOIDLEOUT (1<<24) /* Partei stirbt nicht an NMRs */
#define FFL_NPC (1<<25) /* eine Partei mit Monstern */ #define FFL_NPC (1<<25) /* eine Partei mit Monstern */
#define FFL_DBENTRY (1<<28) /* Partei ist in Datenbank eingetragen */ #define FFL_DBENTRY (1<<28) /* Partei ist in Datenbank eingetragen */
#define FFL_NOTIMEOUT (1<<29) /* ignore MaxAge() */
#define FFL_GM (1<<30) /* eine Partei mit Sonderrechten */ #define FFL_GM (1<<30) /* eine Partei mit Sonderrechten */
#define FFL_SAVEMASK (FFL_DEFENDER|FFL_NEWID|FFL_GM|FFL_NPC|FFL_NOTIMEOUT|FFL_DBENTRY|FFL_NOIDLEOUT) #define FFL_SAVEMASK (FFL_DEFENDER|FFL_NEWID|FFL_GM|FFL_NPC|FFL_DBENTRY|FFL_NOIDLEOUT)
#define is_monsters(f) ((f)->flags&FFL_NPC) #define is_monsters(f) ((f)->flags&FFL_NPC)
@ -136,6 +137,7 @@ void destroyfaction(faction * f);
extern void renumber_faction(faction * f, int no); extern void renumber_faction(faction * f, int no);
void free_faction(struct faction *f); void free_faction(struct faction *f);
void remove_empty_factions(void);
#ifdef SMART_INTERVALS #ifdef SMART_INTERVALS
extern void update_interval(struct faction *f, struct region *r); extern void update_interval(struct faction *f, struct region *r);

View File

@ -1,12 +1,55 @@
#include <platform.h> #include <platform.h>
#include <kernel/faction.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/race.h> #include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <util/language.h> #include <util/language.h>
#include "faction.h"
#include <CuTest.h> #include <CuTest.h>
#include <tests.h>
#include <assert.h>
#include <stdio.h> #include <stdio.h>
static void test_remove_empty_factions(CuTest *tc) {
faction *f, *fm;
int fno;
test_cleanup();
fm = get_or_create_monsters();
assert(fm);
f = test_create_faction(0);
fno = f->no;
remove_empty_factions();
CuAssertPtrEquals(tc, 0, findfaction(fno));
CuAssertPtrEquals(tc, fm, get_monsters());
test_cleanup();
}
static void test_remove_dead_factions(CuTest *tc) {
faction *f, *fm;
region *r;
test_cleanup();
r = test_create_region(0, 0, 0);
fm = get_or_create_monsters();
f = test_create_faction(0);
assert(fm && r && f);
test_create_unit(f, r);
test_create_unit(fm, r);
remove_empty_factions();
CuAssertPtrEquals(tc, f, findfaction(f->no));
CuAssertPtrNotNull(tc, get_monsters());
fm->alive = 0;
f->alive = 0;
remove_empty_factions();
CuAssertPtrEquals(tc, 0, findfaction(f->no));
CuAssertPtrEquals(tc, fm, get_monsters());
test_cleanup();
}
static void test_addfaction(CuTest *tc) { static void test_addfaction(CuTest *tc) {
faction *f = 0; faction *f = 0;
const struct race *rc = rc_get_or_create("human"); const struct race *rc = rc_get_or_create("human");
@ -51,6 +94,8 @@ CuSuite *get_faction_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_addfaction); SUITE_ADD_TEST(suite, test_addfaction);
SUITE_ADD_TEST(suite, test_remove_empty_factions);
SUITE_ADD_TEST(suite, test_remove_dead_factions);
SUITE_ADD_TEST(suite, test_get_monsters); SUITE_ADD_TEST(suite, test_get_monsters);
return suite; return suite;
} }

View File

@ -1312,7 +1312,7 @@ faction *readfaction(struct gamedata * data)
READ_INT(data->store, &f->flags); READ_INT(data->store, &f->flags);
if (data->version < INTFLAGS_VERSION) { if (data->version < INTFLAGS_VERSION) {
if (f->no == 0 || f->no == 666) { if (f->no == 0 || f->no == 666) {
f->flags = FFL_NPC | FFL_NOTIMEOUT | FFL_NOIDLEOUT; f->flags = FFL_NPC | FFL_NOIDLEOUT;
} }
} }
assert((f->flags&FFL_SAVEMASK) == f->flags); assert((f->flags&FFL_SAVEMASK) == f->flags);

View File

@ -232,9 +232,8 @@ int gift_items(unit * u, int flags)
int rule = rule_give(); int rule = rule_give();
assert(u->region); assert(u->region);
assert(u->faction);
if ((u->faction->flags & FFL_QUIT) == 0 || (rule & GIVE_ONDEATH) == 0) { if ((rule & GIVE_ONDEATH) == 0 || !u->faction || (u->faction->flags & FFL_QUIT) == 0) {
if ((rule & GIVE_ALLITEMS) == 0 && (flags & GIFT_FRIENDS)) if ((rule & GIVE_ALLITEMS) == 0 && (flags & GIFT_FRIENDS))
flags -= GIFT_FRIENDS; flags -= GIFT_FRIENDS;
if ((rule & GIVE_PEASANTS) == 0 && (flags & GIFT_PEASANTS)) if ((rule & GIVE_PEASANTS) == 0 && (flags & GIFT_PEASANTS))
@ -1786,3 +1785,33 @@ int effskill(const unit * u, skill_t sk)
return eff_skill(u, sk, u->region); return eff_skill(u, sk, u->region);
} }
void remove_empty_units_in_region(region * r)
{
unit **up = &r->units;
while (*up) {
unit *u = *up;
if (u->number) {
faction *f = u->faction;
if (f == NULL || !f->alive) {
set_number(u, 0);
}
}
if ((u->number == 0 && u_race(u) != get_race(RC_SPELL)) || (u->age <= 0
&& u_race(u) == get_race(RC_SPELL))) {
remove_unit(up, u);
}
if (*up == u)
up = &u->next;
}
}
void remove_empty_units(void)
{
region *r;
for (r = regions; r; r = r->next) {
remove_empty_units_in_region(r);
}
}

View File

@ -238,6 +238,8 @@ extern "C" {
struct spellbook * unit_get_spellbook(const struct unit * u); struct spellbook * unit_get_spellbook(const struct unit * u);
void unit_add_spell(struct unit * u, struct sc_mage * m, struct spell * sp, int level); void unit_add_spell(struct unit * u, struct sc_mage * m, struct spell * sp, int level);
void remove_empty_units_in_region(struct region * r);
void remove_empty_units(void);
extern struct attrib_type at_creator; extern struct attrib_type at_creator;
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,8 +1,10 @@
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
#include "alchemy.h" #include "alchemy.h"
#include "faction.h"
#include "unit.h" #include "unit.h"
#include "item.h" #include "item.h"
#include "race.h"
#include "region.h" #include "region.h"
#include <CuTest.h> #include <CuTest.h>
@ -11,6 +13,94 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
static void test_remove_empty_units(CuTest *tc) {
unit *u;
int uid;
test_cleanup();
test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
uid = u->no;
remove_empty_units();
CuAssertPtrNotNull(tc, findunit(uid));
u->number = 0;
remove_empty_units();
CuAssertPtrEquals(tc, 0, findunit(uid));
test_cleanup();
}
static void test_remove_empty_units_in_region(CuTest *tc) {
unit *u;
int uid;
test_cleanup();
test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
uid = u->no;
remove_empty_units_in_region(u->region);
CuAssertPtrNotNull(tc, findunit(uid));
u->number = 0;
remove_empty_units_in_region(u->region);
CuAssertPtrEquals(tc, 0, findunit(uid));
CuAssertPtrEquals(tc, 0, u->region);
CuAssertPtrEquals(tc, 0, u->faction);
test_cleanup();
}
static void test_remove_units_without_faction(CuTest *tc) {
unit *u;
int uid;
test_cleanup();
test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
uid = u->no;
u_setfaction(u, 0);
remove_empty_units_in_region(u->region);
CuAssertPtrEquals(tc, 0, findunit(uid));
CuAssertIntEquals(tc, 0, u->number);
test_cleanup();
}
static void test_remove_units_with_dead_faction(CuTest *tc) {
unit *u;
int uid;
test_cleanup();
test_create_world();
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
uid = u->no;
u->faction->alive = false;
remove_empty_units_in_region(u->region);
CuAssertPtrEquals(tc, 0, findunit(uid));
CuAssertIntEquals(tc, 0, u->number);
test_cleanup();
}
static void test_remove_units_ignores_spells(CuTest *tc) {
unit *u;
int uid;
test_cleanup();
test_create_world();
u = create_unit(findregion(0, 0), test_create_faction(test_create_race("human")), 1, get_race(RC_SPELL), 0, 0, 0);
uid = u->no;
u->number = 0;
u->age = 1;
remove_empty_units_in_region(u->region);
CuAssertPtrNotNull(tc, findunit(uid));
CuAssertPtrNotNull(tc, u->region);
u->age = 0;
remove_empty_units_in_region(u->region);
CuAssertPtrEquals(tc, 0, findunit(uid));
test_cleanup();
}
static void test_scale_number(CuTest *tc) { static void test_scale_number(CuTest *tc) {
unit *u; unit *u;
const struct potion_type *ptype; const struct potion_type *ptype;
@ -37,5 +127,10 @@ CuSuite *get_unit_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_scale_number); SUITE_ADD_TEST(suite, test_scale_number);
SUITE_ADD_TEST(suite, test_remove_empty_units);
SUITE_ADD_TEST(suite, test_remove_units_ignores_spells);
SUITE_ADD_TEST(suite, test_remove_units_without_faction);
SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction);
SUITE_ADD_TEST(suite, test_remove_empty_units_in_region);
return suite; return suite;
} }

View File

@ -34,25 +34,26 @@ struct race *test_create_race(const char *name)
struct region *test_create_region(int x, int y, const terrain_type *terrain) struct region *test_create_region(int x, int y, const terrain_type *terrain)
{ {
region *r = new_region(x, y, NULL, 0); region *r = new_region(x, y, NULL, 0);
terraform_region(r, terrain); terraform_region(r, terrain ? terrain : get_or_create_terrain("plain"));
rsettrees(r, 0, 0); rsettrees(r, 0, 0);
rsettrees(r, 1, 0); rsettrees(r, 1, 0);
rsettrees(r, 2, 0); rsettrees(r, 2, 0);
rsethorses(r, 0); rsethorses(r, 0);
rsetpeasants(r, terrain->size); rsetpeasants(r, r->terrain->size);
return r; return r;
} }
struct faction *test_create_faction(const struct race *rc) struct faction *test_create_faction(const struct race *rc)
{ {
faction *f = addfaction("nobody@eressea.de", NULL, rc?rc:rc_find("human"), default_locale, 0); faction *f = addfaction("nobody@eressea.de", NULL, rc?rc:rc_get_or_create("human"), default_locale, 0);
return f; return f;
} }
struct unit *test_create_unit(struct faction *f, struct region *r) struct unit *test_create_unit(struct faction *f, struct region *r)
{ {
unit *u = create_unit(r, f, 1, f?f->race:rc_find("human"), 0, 0, 0); const struct race * rc = f ? f->race : 0;
return u; assert(f || !r);
return create_unit(r, f, 1, rc ? rc : rc_get_or_create("human"), 0, 0, 0);
} }
void test_cleanup(void) void test_cleanup(void)