From 80b14048be75d72a6f88042fbf97d0db5f428801 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Apr 2016 19:45:27 +0200 Subject: [PATCH] remove some unused code from alliances start writing test coverage --- src/kernel/CMakeLists.txt | 22 ++++---- src/kernel/alliance.c | 113 ++++++------------------------------- src/kernel/alliance.h | 1 + src/kernel/alliance.test.c | 28 +++++++++ src/kernel/command.c | 10 ++++ src/kernel/command.h | 1 + src/test_eressea.c | 1 + src/tests.c | 4 ++ 8 files changed, 72 insertions(+), 108 deletions(-) diff --git a/src/kernel/CMakeLists.txt b/src/kernel/CMakeLists.txt index fd8496f89..f40d2acbb 100644 --- a/src/kernel/CMakeLists.txt +++ b/src/kernel/CMakeLists.txt @@ -2,26 +2,26 @@ project(kernel C) SET(_TEST_FILES alliance.test.c -build.test.c -config.test.c -group.test.c -faction.test.c -unit.test.c -save.test.c -ship.test.c -spell.test.c ally.test.c +build.test.c building.test.c -equipment.test.c +command.test.c +config.test.c curse.test.c +equipment.test.c +faction.test.c +group.test.c item.test.c +messages.test.c order.test.c pool.test.c race.test.c +save.test.c +ship.test.c spellbook.test.c -curse.test.c +spell.test.c +unit.test.c jsonconf.test.c -messages.test.c ) SET(_FILES diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index 56754c491..732305dd3 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -314,6 +314,15 @@ static void execute(const struct syntaxtree *syntax, keyword_t kwd) } } +const char* alliance_kwd[ALLIANCE_MAX] = { + "kick", + "leave", + "command", + "new", + "invite", + "join" +}; + void alliance_cmd(void) { static syntaxtree *stree = NULL; @@ -321,35 +330,21 @@ void alliance_cmd(void) syntaxtree *slang = stree = stree_create(); while (slang) { void *leaf = 0; - add_command(&leaf, NULL, LOC(slang->lang, "new"), &cmd_new); - add_command(&leaf, NULL, LOC(slang->lang, "invite"), &cmd_invite); - add_command(&leaf, NULL, LOC(slang->lang, "join"), &cmd_join); - add_command(&leaf, NULL, LOC(slang->lang, "kick"), &cmd_kick); - add_command(&leaf, NULL, LOC(slang->lang, "leave"), &cmd_leave); - add_command(&leaf, NULL, LOC(slang->lang, "command"), &cmd_transfer); + add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_KICK]), &cmd_kick); + add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_LEAVE]), &cmd_leave); + add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_TRANSFER]), &cmd_transfer); + add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_NEW]), &cmd_new); + add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_INVITE]), &cmd_invite); + add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_JOIN]), &cmd_join); slang->root = leaf; slang = slang->next; } } execute(stree, K_ALLIANCE); + stree_free(stree); /* some may have been kicked, must remove f->alliance==NULL */ } -void alliancejoin(void) -{ - static syntaxtree *stree = NULL; - if (stree == NULL) { - syntaxtree *slang = stree = stree_create(); - while (slang) { - void *leaf = 0; - add_command(&leaf, NULL, LOC(slang->lang, "join"), &cmd_join); - add_command(&slang->root, leaf, LOC(slang->lang, "alliance"), NULL); - slang = slang->next; - } - } - execute(stree, K_ALLIANCE); -} - void setalliance(faction * f, alliance * al) { if (f->alliance == al) @@ -442,82 +437,6 @@ void alliancevictory(void) } } -int victorycondition(const alliance * al, const char *name) -{ - const char *gems[] = - { "opal", "diamond", "zaphire", "topaz", "beryl", "agate", "garnet", - "emerald", NULL }; - if (strcmp(name, "gems") == 0) { - const char **igem; - - for (igem = gems; *igem; ++igem) { - const struct resource_type *rtype = rt_find(*igem); - quicklist *flist = al->members; - int qi; - bool found = false; - - assert(rtype); - for (qi = 0; flist && !found; ql_advance(&flist, &qi, 1)) { - faction *f = (faction *)ql_get(flist, 0); - unit *u; - - for (u = f->units; u; u = u->nextF) { - if (i_get(u->items, rtype->itype) > 0) { - found = true; - break; - } - } - } - if (!found) - return 0; - } - return 1; - - } - else if (strcmp(name, "phoenix") == 0) { - quicklist *flist = al->members; - int qi; - - for (qi = 0; flist; ql_advance(&flist, &qi, 1)) { - faction *f = (faction *)ql_get(flist, qi); - if (key_get(f->attribs, atoi36("phnx"))) { - return 1; - } - } - return 0; - - } - else if (strcmp(name, "pyramid") == 0) { - - /* Logik: - * - if (pyr > last_passed_size && pyr > all_others) { - * pyr->passed->counter++; - * for(all_other_pyrs) { - * pyr->passed->counter=0; - * } - * - * if(pyr->passed->counter >= 3) { - * set(pyr, passed); - * pyr->owner->set_attrib(pyra); - * } - * last_passed_size = pyr->size; - * } - */ - - quicklist *flist = al->members; - int qi; - - for (qi = 0; flist; ql_advance(&flist, &qi, 1)) { - faction *f = (faction *)ql_get(flist, qi); - if (key_get(f->attribs, atoi36("pyra"))) { - return 1; - } - } - return 0; - } - return -1; -} - void alliance_setname(alliance * self, const char *name) { free(self->name); diff --git a/src/kernel/alliance.h b/src/kernel/alliance.h index b96342397..2c5b0d677 100644 --- a/src/kernel/alliance.h +++ b/src/kernel/alliance.h @@ -38,6 +38,7 @@ extern "C" { ALLIANCE_MAX }; + extern const char* alliance_kwd[ALLIANCE_MAX]; #define ALF_NON_ALLIED (1<<0) /* this alliance is just a default for a non-allied faction */ #define ALLY_ENEMY (1<<0) diff --git a/src/kernel/alliance.test.c b/src/kernel/alliance.test.c index 1b708b017..99a8e3384 100644 --- a/src/kernel/alliance.test.c +++ b/src/kernel/alliance.test.c @@ -1,6 +1,9 @@ #include #include #include +#include +#include +#include #include "alliance.h" #include #include @@ -83,12 +86,37 @@ static void test_alliance_dead_faction(CuTest *tc) { test_cleanup(); } +static void test_alliance_cmd(CuTest *tc) { + unit *u1, *u2; + struct region *r; + + test_cleanup(); + r = test_create_region(0, 0, 0); + u1 = test_create_unit(test_create_faction(0), r); + u2 = test_create_unit(test_create_faction(0), r); + u1->orders = create_order(K_ALLIANCE, u1->faction->locale, "%s %s", alliance_kwd[ALLIANCE_NEW], itoa36(42)); + // TODO: INVITE + u2->orders = create_order(K_ALLIANCE, u1->faction->locale, "%s %s", alliance_kwd[ALLIANCE_JOIN], itoa36(42)); + CuAssertTrue(tc, is_allied(u1->faction, u1->faction)); + CuAssertTrue(tc, !is_allied(u1->faction, u2->faction)); + alliance_cmd(); + CuAssertPtrNotNull(tc, u1->faction->alliance); + CuAssertIntEquals(tc, 42, u1->faction->alliance->id); + CuAssertPtrNotNull(tc, u1->faction->alliance->members); + CuAssertPtrEquals(tc, u1->faction, alliance_get_leader(u1->faction->alliance)); + CuAssertPtrEquals(tc, u1->faction->alliance, findalliance(42)); +// CuAssertTrue(tc, is_allied(u1->faction, u1->faction)); +// CuAssertPtrNotNull(tc, u2->faction->alliance); + test_cleanup(); +} + CuSuite *get_alliance_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_alliance_dead_faction); SUITE_ADD_TEST(suite, test_alliance_make); SUITE_ADD_TEST(suite, test_alliance_join); + SUITE_ADD_TEST(suite, test_alliance_cmd); return suite; } diff --git a/src/kernel/command.c b/src/kernel/command.c index ac359a4cf..0642be060 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -43,6 +43,15 @@ void *stree_find(const syntaxtree * stree, const struct locale *lang) return NULL; } +void stree_free(syntaxtree *stree) { + while (stree) { + syntaxtree *snext = stree->next; + freetokens(stree->root); + free(stree); + stree = snext; + } +} + syntaxtree *stree_create(void) { syntaxtree *sroot = NULL; @@ -65,6 +74,7 @@ const char *str, parser fun) command *cmd = (command *)malloc(sizeof(command)); variant var; + assert(str); cmd->fun = fun; cmd->nodes = tnext; var.v = cmd; diff --git a/src/kernel/command.h b/src/kernel/command.h index 54d8fb25c..70b03a3a6 100644 --- a/src/kernel/command.h +++ b/src/kernel/command.h @@ -32,6 +32,7 @@ extern "C" { void do_command(const void *troot, struct unit *u, struct order *); struct syntaxtree *stree_create(void); + void stree_free(struct syntaxtree *); void *stree_find(const struct syntaxtree *stree, const struct locale *lang); diff --git a/src/test_eressea.c b/src/test_eressea.c index 658f20b9b..23cfdb203 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -86,6 +86,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(xerewards); /* kernel */ ADD_SUITE(alliance); + ADD_SUITE(command); ADD_SUITE(unit); ADD_SUITE(faction); ADD_SUITE(group); diff --git a/src/tests.c b/src/tests.c index a0d54a4cc..e150d2089 100644 --- a/src/tests.c +++ b/src/tests.c @@ -6,6 +6,7 @@ #include "reports.h" #include +#include #include #include #include @@ -78,6 +79,9 @@ struct locale * test_create_locale(void) { if (!locale_getstring(loc, mkname("skill", skillnames[i]))) locale_setstring(loc, mkname("skill", skillnames[i]), skillnames[i]); } + for (i = 0; i != ALLIANCE_MAX; ++i) { + locale_setstring(loc, alliance_kwd[i], alliance_kwd[i]); + } for (i = 0; i != MAXDIRECTIONS; ++i) { locale_setstring(loc, directions[i], directions[i]); init_direction(loc, i, directions[i]);