From 0197fce9a10b64c6f1af12e3873476ea522ae65f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Dec 2016 21:21:50 +0100 Subject: [PATCH 1/6] add a test for teach-messages. vheck that they are generated for students and teachers. --- src/study.test.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/study.test.c b/src/study.test.c index 47976930d..54e32bf28 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -2,6 +2,7 @@ #include "study.h" +#include #include #include #include @@ -10,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -483,6 +485,7 @@ static void test_teach_one_to_many(CuTest *tc) { static void test_teach_many_to_one(CuTest *tc) { unit *u, *u1, *u2; + test_setup(); init_resources(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); @@ -505,6 +508,47 @@ static void test_teach_many_to_one(CuTest *tc) { test_cleanup(); } +static void test_teach_message(CuTest *tc) { + unit *u, *u1, *u2; + attrib *a; + ally *al; + teaching_info *teach; + + test_setup(); + init_resources(); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); + scale_number(u, 20); + u->thisorder = create_order(K_STUDY, u->faction->locale, "CROSSBOW"); + u1 = test_create_unit(test_create_faction(0), u->region); + set_level(u1, SK_CROSSBOW, TEACHDIFFERENCE); + u1->thisorder = create_order(K_TEACH, u->faction->locale, itoa36(u->no)); + u2 = test_create_unit(test_create_faction(0), u->region); + al = ally_add(&u->faction->allies, u2->faction); + al->status = HELP_GUARD; + set_level(u2, SK_CROSSBOW, TEACHDIFFERENCE); + u2->thisorder = create_order(K_TEACH, u->faction->locale, itoa36(u->no)); + CuAssertTrue(tc, !alliedunit(u, u1->faction, HELP_GUARD)); + CuAssertTrue(tc, alliedunit(u, u2->faction, HELP_GUARD)); + teach_cmd(u1, u1->thisorder); + teach_cmd(u2, u2->thisorder); + a = a_find(u->attribs, &at_learning); + CuAssertPtrNotNull(tc, a); + CuAssertPtrNotNull(tc, a->data.v); + teach = (teaching_info *)a->data.v; + CuAssertPtrNotNull(tc, teach->teachers); + CuAssertIntEquals(tc, 600, teach->value); + CuAssertPtrEquals(tc, u1, teach->teachers[0]); + CuAssertPtrEquals(tc, u2, teach->teachers[1]); + CuAssertPtrEquals(tc, NULL, teach->teachers[2]); + study_cmd(u, u->thisorder); + CuAssertPtrEquals(tc, NULL, test_find_messagetype(u1->faction->msgs, "teach_teacher")); + CuAssertPtrNotNull(tc, test_find_messagetype(u2->faction->msgs, "teach_teacher")); + CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "teach_student")); + a = a_find(u->attribs, &at_learning); + CuAssertPtrEquals(tc, NULL, a); + test_cleanup(); +} + static void test_teach_many_to_many(CuTest *tc) { unit *s1, *s2, *t1, *t2; region *r; @@ -554,6 +598,7 @@ CuSuite *get_study_suite(void) SUITE_ADD_TEST(suite, test_teach_one_to_many); SUITE_ADD_TEST(suite, test_teach_many_to_one); SUITE_ADD_TEST(suite, test_teach_many_to_many); + SUITE_ADD_TEST(suite, test_teach_message); SUITE_ADD_TEST(suite, test_teach_two_skills); SUITE_ADD_TEST(suite, test_learn_skill_single); SUITE_ADD_TEST(suite, test_learn_skill_multi); From ccb179972605496c54ab02a4b96a99571d45c16c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Dec 2016 21:29:07 +0100 Subject: [PATCH 2/6] unlimited teachers. quicklist replaces fixed array. --- src/study.c | 25 +++++++------------------ src/study.h | 4 ++-- src/study.test.c | 10 ++++++---- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/study.c b/src/study.c index 1dd4da8b8..f3372334a 100644 --- a/src/study.c +++ b/src/study.c @@ -53,6 +53,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include +#include + /* libc includes */ #include #include @@ -216,24 +218,11 @@ teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk, n = _min(n, nteaching); if (n != 0) { - int index = 0; - if (teach == NULL) { a = a_add(&student->attribs, a_new(&at_learning)); teach = (teaching_info *)a->data.v; } - else { - while (teach->teachers[index] && index != MAXTEACHERS) - ++index; - } - if (index < MAXTEACHERS) - teach->teachers[index++] = teacher; - if (index < MAXTEACHERS) { - teach->teachers[index] = NULL; - } - else { - log_error("MAXTEACHERS=%d is too low for student %s, teacher %s", MAXTEACHERS, unitname(student), unitname(teacher)); - } + ql_push(&teach->teachers, teacher); teach->value += n; if (student->building && teacher->building == student->building) { @@ -717,7 +706,7 @@ int study_cmd(unit * u, order * ord) a = a_add(&u->attribs, a_new(&at_learning)); teach = (teaching_info *)a->data.v; assert(teach); - teach->teachers[0] = 0; + teach->teachers = NULL; } if (money > 0) { use_pooled(u, get_resourcetype(R_SILVER), GET_DEFAULT, money); @@ -766,9 +755,9 @@ int study_cmd(unit * u, order * ord) learn_skill(u, sk, days); if (a != NULL) { - int index = 0; - while (teach->teachers[index] && index != MAXTEACHERS) { - unit *teacher = teach->teachers[index++]; + ql_iter qli = qli_init(&teach->teachers); + while (qli_more(qli)) { + unit *teacher = (unit *)qli_next(&qli); if (teacher->faction != u->faction) { bool feedback = alliedunit(u, teacher->faction, HELP_GUARD); if (feedback) { diff --git a/src/study.h b/src/study.h index 20903583a..8067bc9bd 100644 --- a/src/study.h +++ b/src/study.h @@ -27,6 +27,7 @@ extern "C" { #endif struct unit; + struct quicklist; int teach_cmd(struct unit *u, struct order *ord); int study_cmd(struct unit *u, struct order *ord); @@ -45,10 +46,9 @@ extern "C" { void demon_skillchange(struct unit *u); -#define MAXTEACHERS 32 #define TEACHNUMBER 10 typedef struct teaching_info { - struct unit *teachers[MAXTEACHERS]; + struct quicklist *teachers; int value; } teaching_info; diff --git a/src/study.test.c b/src/study.test.c index 54e32bf28..4c1e5f171 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -18,9 +18,11 @@ #include #include +#include +#include + #include -#include #define MAXLOG 4 typedef struct log_entry { @@ -537,9 +539,9 @@ static void test_teach_message(CuTest *tc) { teach = (teaching_info *)a->data.v; CuAssertPtrNotNull(tc, teach->teachers); CuAssertIntEquals(tc, 600, teach->value); - CuAssertPtrEquals(tc, u1, teach->teachers[0]); - CuAssertPtrEquals(tc, u2, teach->teachers[1]); - CuAssertPtrEquals(tc, NULL, teach->teachers[2]); + CuAssertIntEquals(tc, 2, ql_length(teach->teachers)); + CuAssertPtrEquals(tc, u1, ql_get(teach->teachers, 0)); + CuAssertPtrEquals(tc, u2, ql_get(teach->teachers, 1)); study_cmd(u, u->thisorder); CuAssertPtrEquals(tc, NULL, test_find_messagetype(u1->faction->msgs, "teach_teacher")); CuAssertPtrNotNull(tc, test_find_messagetype(u2->faction->msgs, "teach_teacher")); From a72b29610cc157535679043603a07a4936175b8f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 11 Dec 2016 22:40:06 +0100 Subject: [PATCH 3/6] small edit (int->bool) --- src/guard.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/guard.c b/src/guard.c index ee26e467b..4b0c63e22 100644 --- a/src/guard.c +++ b/src/guard.c @@ -128,7 +128,7 @@ bool is_guard(const struct unit * u) unit *is_guarded(region * r, unit * u) { unit *u2; - int noguards = 1; + bool noguards = true; if (!fval(r, RF_GUARDED)) { return NULL; @@ -140,7 +140,7 @@ unit *is_guarded(region * r, unit * u) for (u2 = r->units; u2; u2 = u2->next) { if (is_guardian_r(u2)) { - noguards = 0; + noguards = false; if (is_guardian_u(u2, u)) { /* u2 is our guard. stop processing (we might have to go further next time) */ return u2; From 95954fb386f0e25595882bc77306f703541e0895 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 16 Dec 2016 07:29:43 +0100 Subject: [PATCH 4/6] start writing a test (WIP). --- src/move.c | 2 +- src/move.h | 2 ++ src/move.test.c | 26 +++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/move.c b/src/move.c index b6a908837..e6469cc4a 100644 --- a/src/move.c +++ b/src/move.c @@ -571,7 +571,7 @@ direction_t reldirection(const region * from, const region * to) return NODIRECTION; } -static void leave_trail(ship * sh, region * from, region_list * route) +void leave_trail(ship * sh, region * from, region_list * route) { region *r = from; diff --git a/src/move.h b/src/move.h index 6363fe2f9..14c040493 100644 --- a/src/move.h +++ b/src/move.h @@ -63,6 +63,8 @@ extern "C" { int enoughsailors(const struct ship *sh, int sumskill); bool canswim(struct unit *u); bool canfly(struct unit *u); + void leave_trail(struct ship *sh, struct region *from, + struct region_list *route); struct ship *move_ship(struct ship *sh, struct region *from, struct region *to, struct region_list *route); int walkingcapacity(const struct unit *u); diff --git a/src/move.test.c b/src/move.test.c index bc93ec124..bdd4a62a8 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -478,7 +478,7 @@ static void test_drifting_ships(CuTest *tc) { region *r1, *r2, *r3; terrain_type *t_ocean, *t_plain; ship_type *st_boat; - test_cleanup(); + test_setup(); t_ocean = test_create_terrain("ocean", SEA_REGION); t_plain = test_create_terrain("plain", LAND_REGION); r1 = test_create_region(0, 0, t_ocean); @@ -491,11 +491,35 @@ static void test_drifting_ships(CuTest *tc) { test_cleanup(); } +static void test_ship_leave_trail(CuTest *tc) { + ship *s1, *s2; + region *r1, *r2; + terrain_type *t_ocean; + ship_type *st_boat; + region_list *route = NULL; + + test_setup(); + t_ocean = test_create_terrain("ocean", SEA_REGION); + r1 = test_create_region(0, 0, t_ocean); + add_regionlist(&route, r2 = test_create_region(1, 0, t_ocean)); + add_regionlist(&route, test_create_region(2, 0, t_ocean)); + st_boat = test_create_shiptype("boat"); + s1 = test_create_ship(r1, st_boat); + s2 = test_create_ship(r1, st_boat); + leave_trail(s1, r1, route); + leave_trail(s2, r1, route); +// CuAssertPtrNotNull(tc, r1->attribs); + CuAssertPtrNotNull(tc, r2->attribs); + free_regionlist(route); + test_cleanup(); +} + CuSuite *get_move_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_walkingcapacity); SUITE_ADD_TEST(suite, test_ship_not_allowed_in_coast); + SUITE_ADD_TEST(suite, test_ship_leave_trail); SUITE_ADD_TEST(suite, test_ship_allowed_without_harbormaster); SUITE_ADD_TEST(suite, test_ship_blocked_by_harbormaster); SUITE_ADD_TEST(suite, test_ship_has_harbormaster_contact); From ddc7707cdece29b4b509d9e8173379ef0c3d2a3b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 16 Dec 2016 17:16:10 +0100 Subject: [PATCH 5/6] add a failing test for bug 2266 --- src/lighthouse.c | 2 +- src/lighthouse.h | 2 +- src/move.test.c | 13 ++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/lighthouse.c b/src/lighthouse.c index d962b9b76..09c9b7d11 100644 --- a/src/lighthouse.c +++ b/src/lighthouse.c @@ -14,7 +14,7 @@ #include #include -const attrib_type at_lighthouse = { +attrib_type at_lighthouse = { "lighthouse" /* Rest ist NULL; tempor�res, nicht alterndes Attribut */ }; diff --git a/src/lighthouse.h b/src/lighthouse.h index 518b05b5a..3bf970bf1 100644 --- a/src/lighthouse.h +++ b/src/lighthouse.h @@ -29,7 +29,7 @@ extern "C" { struct building; struct attrib; - extern const struct attrib_type at_lighthouse; + extern struct attrib_type at_lighthouse; /* leuchtturm */ bool check_leuchtturm(struct region *r, struct faction *f); void update_lighthouse(struct building *lh); diff --git a/src/move.test.c b/src/move.test.c index bdd4a62a8..6a2a0e7db 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -3,6 +3,7 @@ #include "move.h" #include "keyword.h" +#include "lighthouse.h" #include #include @@ -501,15 +502,21 @@ static void test_ship_leave_trail(CuTest *tc) { test_setup(); t_ocean = test_create_terrain("ocean", SEA_REGION); r1 = test_create_region(0, 0, t_ocean); - add_regionlist(&route, r2 = test_create_region(1, 0, t_ocean)); add_regionlist(&route, test_create_region(2, 0, t_ocean)); + add_regionlist(&route, r2 = test_create_region(1, 0, t_ocean)); st_boat = test_create_shiptype("boat"); s1 = test_create_ship(r1, st_boat); s2 = test_create_ship(r1, st_boat); leave_trail(s1, r1, route); + a_add(&r1->attribs, a_new(&at_lighthouse)); leave_trail(s2, r1, route); -// CuAssertPtrNotNull(tc, r1->attribs); - CuAssertPtrNotNull(tc, r2->attribs); + a_add(&r2->attribs, a_new(&at_lighthouse)); + CuAssertPtrEquals(tc, &at_shiptrail, (void *)r1->attribs->type); + CuAssertPtrEquals(tc, &at_shiptrail, (void *)r1->attribs->next->type); + CuAssertPtrEquals(tc, &at_lighthouse, (void *)r1->attribs->next->next->type); + CuAssertPtrEquals(tc, &at_shiptrail, (void *)r2->attribs->type); + CuAssertPtrEquals(tc, &at_shiptrail, (void *)r2->attribs->next->type); + CuAssertPtrEquals(tc, &at_lighthouse, (void *)r2->attribs->next->next->type); free_regionlist(route); test_cleanup(); } From 72ac8017346afd74676c2cff2aa15586dd32fe1c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 16 Dec 2016 17:17:04 +0100 Subject: [PATCH 6/6] fix bug 2266 --- src/move.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/move.c b/src/move.c index e6469cc4a..f1b2311f1 100644 --- a/src/move.c +++ b/src/move.c @@ -595,7 +595,7 @@ void leave_trail(ship * sh, region * from, region_list * route) a = a->next; } - if (a == NULL) { + if (a == NULL || a->type != &at_shiptrail) { a = a_add(&(r->attribs), a_new(&at_shiptrail)); td = (traveldir *)a->data.v; td->no = sh->no;