From 6911af02b11a782e3ef363c0f341fd983e0b3d45 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 30 Jun 2012 11:07:28 -0700 Subject: [PATCH] moving the ally struct into a separate file, with interface and test coverage. --- src/CMakeLists.txt | 2 ++ src/gamecode/creport.c | 1 + src/gamecode/laws.c | 1 + src/gamecode/report.c | 1 + src/gamecode/xmlreport.c | 1 + src/kernel/ally.c | 39 +++++++++++++++++++++++++++++++++++++++ src/kernel/ally.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/kernel/ally_test.c | 27 +++++++++++++++++++++++++++ src/kernel/config.c | 1 + src/kernel/config.h | 8 -------- src/kernel/faction.c | 1 + src/kernel/group.c | 3 ++- src/kernel/save.c | 1 + src/kernel/types.h | 1 + src/tests.c | 1 + src/tests.h | 1 + 16 files changed, 120 insertions(+), 9 deletions(-) create mode 100644 src/kernel/ally.c create mode 100644 src/kernel/ally.h create mode 100644 src/kernel/ally_test.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e9eb8d6c..8421e4692 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/gamecode/creport.c b/src/gamecode/creport.c index 0a4768097..90374cb4b 100644 --- a/src/gamecode/creport.c +++ b/src/gamecode/creport.c @@ -33,6 +33,7 @@ without prior permission by the authors of Eressea. /* kernel includes */ #include #include +#include #include #include #include diff --git a/src/gamecode/laws.c b/src/gamecode/laws.c index 3e7ca3595..80a819f5c 100755 --- a/src/gamecode/laws.c +++ b/src/gamecode/laws.c @@ -37,6 +37,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* kernel includes */ #include #include +#include #include #include #include diff --git a/src/gamecode/report.c b/src/gamecode/report.c index c01ea5089..898b06c20 100644 --- a/src/gamecode/report.c +++ b/src/gamecode/report.c @@ -38,6 +38,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* kernel includes */ #include +#include #include #include #include diff --git a/src/gamecode/xmlreport.c b/src/gamecode/xmlreport.c index 9ccf72a31..f4e4f9b6d 100644 --- a/src/gamecode/xmlreport.c +++ b/src/gamecode/xmlreport.c @@ -31,6 +31,7 @@ without prior permission by the authors of Eressea. /* kernel includes */ #include #include +#include #include #include #include diff --git a/src/kernel/ally.c b/src/kernel/ally.c new file mode 100644 index 000000000..cf5772fd6 --- /dev/null +++ b/src/kernel/ally.c @@ -0,0 +1,39 @@ +#include "types.h" +#include "ally.h" + +#include + +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; + } +} diff --git a/src/kernel/ally.h b/src/kernel/ally.h new file mode 100644 index 000000000..5e09b721c --- /dev/null +++ b/src/kernel/ally.h @@ -0,0 +1,40 @@ +/* +Copyright (c) 1998-2010, Enno Rehling + Katja Zedel + +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 diff --git a/src/kernel/ally_test.c b/src/kernel/ally_test.c new file mode 100644 index 000000000..c7b8500ad --- /dev/null +++ b/src/kernel/ally_test.c @@ -0,0 +1,27 @@ +#include +#include "types.h" +#include "ally.h" + +#include +#include + +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; +} diff --git a/src/kernel/config.c b/src/kernel/config.c index 7d84f396a..224923bf3 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -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" diff --git a/src/kernel/config.h b/src/kernel/config.h index c9c43f89b..23542caf5 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -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); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 8bf86e30a..ea02a62a1 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -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" diff --git a/src/kernel/group.c b/src/kernel/group.c index 8b6144d82..0e49fd4aa 100755 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -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 */ diff --git a/src/kernel/save.c b/src/kernel/save.c index c8c39b50f..1ba845cc0 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -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" diff --git a/src/kernel/types.h b/src/kernel/types.h index 130d576a0..f6f1e4099 100644 --- a/src/kernel/types.h +++ b/src/kernel/types.h @@ -35,6 +35,7 @@ typedef short item_t; struct attrib; struct attrib_type; +struct ally; struct building; struct building_type; struct curse; diff --git a/src/tests.c b/src/tests.c index b269663dc..a0c5dcd94 100644 --- a/src/tests.c +++ b/src/tests.c @@ -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()); diff --git a/src/tests.h b/src/tests.h index 2b6b91cce..2ea13633e 100644 --- a/src/tests.h +++ b/src/tests.h @@ -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);