moving the ally struct into a separate file, with interface and test coverage.

This commit is contained in:
Enno Rehling 2012-06-30 11:07:28 -07:00
parent d98742e4b5
commit 6911af02b1
16 changed files with 120 additions and 9 deletions

View File

@ -50,6 +50,7 @@ set (TEST_SRC
gamecode/economy_test.c
gamecode/laws_test.c
gamecode/market_test.c
kernel/ally_test.c
kernel/battle_test.c
kernel/building_test.c
kernel/curse_test.c
@ -116,6 +117,7 @@ set (LIB_SRC
items/xerewards.c
kernel/alchemy.c
kernel/alliance.c
kernel/ally.c
kernel/battle.c
kernel/binarystore.c
kernel/build.c

View File

@ -33,6 +33,7 @@ without prior permission by the authors of Eressea.
/* kernel includes */
#include <kernel/alchemy.h>
#include <kernel/alliance.h>
#include <kernel/ally.h>
#include <kernel/connection.h>
#include <kernel/building.h>
#include <kernel/curse.h>

View File

@ -37,6 +37,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* kernel includes */
#include <kernel/alchemy.h>
#include <kernel/alliance.h>
#include <kernel/ally.h>
#include <kernel/battle.h>
#include <kernel/connection.h>
#include <kernel/curse.h>

View File

@ -38,6 +38,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* kernel includes */
#include <kernel/alchemy.h>
#include <kernel/ally.h>
#include <kernel/connection.h>
#include <kernel/build.h>
#include <kernel/building.h>

View File

@ -31,6 +31,7 @@ without prior permission by the authors of Eressea.
/* kernel includes */
#include <kernel/alchemy.h>
#include <kernel/alliance.h>
#include <kernel/ally.h>
#include <kernel/connection.h>
#include <kernel/curse.h>
#include <kernel/building.h>

39
src/kernel/ally.c Normal file
View File

@ -0,0 +1,39 @@
#include "types.h"
#include "ally.h"
#include <stdlib.h>
ally * ally_find(ally *al, const struct faction *f) {
for (;al;al=al->next) {
if (al->faction==f) return al;
}
return 0;
}
ally * ally_add(ally **al_p, struct faction *f) {
ally * al;
while (*al_p) {
al = *al_p;
if (al->faction==f) return al;
al_p = &al->next;
}
al = (ally *)malloc(sizeof(ally));
al->faction = f;
al->status = 0;
al->next = 0;
*al_p = al;
return al;
}
void ally_remove(ally **al_p, struct faction *f) {
ally * al;
while (*al_p) {
al = *al_p;
if (al->faction==f) {
*al_p = al->next;
free(al);
break;
}
al_p = &al->next;
}
}

40
src/kernel/ally.h Normal file
View File

@ -0,0 +1,40 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#ifndef ALLY_H
#define ALLY_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ally {
struct ally *next;
struct faction *faction;
int status;
} ally;
ally * ally_find(ally *al, const struct faction *f);
ally * ally_add(ally **al_p, struct faction *f);
void ally_remove(ally **al_p, struct faction *f);
#ifdef __cplusplus
}
#endif
#endif

27
src/kernel/ally_test.c Normal file
View File

@ -0,0 +1,27 @@
#include <platform.h>
#include "types.h"
#include "ally.h"
#include <CuTest.h>
#include <tests.h>
static void test_ally(CuTest * tc)
{
ally * al = 0;
struct faction * f1 = test_create_faction(0);
ally_add(&al, f1);
CuAssertPtrNotNull(tc, al);
CuAssertPtrEquals(tc, f1, ally_find(al, f1)->faction);
ally_remove(&al, f1);
CuAssertPtrEquals(tc, 0, al);
CuAssertPtrEquals(tc, 0, ally_find(al, f1));
}
CuSuite *get_ally_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_ally);
return suite;
}

View File

@ -25,6 +25,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* kernel includes */
#include "alliance.h"
#include "ally.h"
#include "alchemy.h"
#include "battle.h"
#include "connection.h"

View File

@ -113,14 +113,6 @@ extern "C" {
#define i2b(i) ((bool)((i)?(true):(false)))
typedef struct ally {
struct ally *next;
struct faction *faction;
int status;
} ally;
ally * ally_find(const ally *al, const struct faction *f);
void remove_empty_units_in_region(struct region *r);
void remove_empty_units(void);
void remove_empty_factions(void);

View File

@ -21,6 +21,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "faction.h"
#include "alliance.h"
#include "ally.h"
#include "equipment.h"
#include "group.h"
#include "item.h"

View File

@ -21,9 +21,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "group.h"
/* kernel includes */
#include "unit.h"
#include "ally.h"
#include "faction.h"
#include "save.h"
#include "unit.h"
#include "version.h"
/* attrib includes */

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "alchemy.h"
#include "alliance.h"
#include "ally.h"
#include "connection.h"
#include "building.h"
#include "faction.h"

View File

@ -35,6 +35,7 @@ typedef short item_t;
struct attrib;
struct attrib_type;
struct ally;
struct building;
struct building_type;
struct curse;

View File

@ -51,6 +51,7 @@ int RunAllTests(void)
CuSuiteAddSuite(suite, get_building_suite());
CuSuiteAddSuite(suite, get_spell_suite());
CuSuiteAddSuite(suite, get_battle_suite());
CuSuiteAddSuite(suite, get_ally_suite());
/* gamecode */
CuSuiteAddSuite(suite, get_market_suite());
CuSuiteAddSuite(suite, get_laws_suite());

View File

@ -25,6 +25,7 @@ extern "C" {
CuSuite *get_bsdstring_suite(void);
CuSuite *get_functions_suite(void);
CuSuite *get_umlaut_suite(void);
CuSuite *get_ally_suite(void);
void test_cleanup(void);