From c23e7bf9b9080ba68b844f03b626797ec0895686 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 29 Dec 2017 13:37:17 +0100 Subject: [PATCH] add a unit test for academy_can_teach. --- src/CMakeLists.txt | 1 + src/academy.c | 4 ++-- src/academy.test.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ src/study.c | 5 +++++ src/test_eressea.c | 1 + 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/academy.test.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b3b842a3a..70ef60b87 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -201,6 +201,7 @@ target_link_libraries(convert set(TESTS_SRC test_eressea.c tests.c + academy.test.c alchemy.test.c battle.test.c calendar.test.c diff --git a/src/academy.c b/src/academy.c index 245c89207..5df13efa4 100644 --- a/src/academy.c +++ b/src/academy.c @@ -36,8 +36,8 @@ void academy_teaching_bonus(struct unit *u, skill_t sk, int students) { bool academy_can_teach(unit *teacher, unit *student, skill_t sk) { const struct building_type *btype = bt_find("academy"); if (active_building(teacher, btype) && active_building(student, btype)) { - int j = study_cost(student, sk); - j = MAX(50, j * 2); + int j = study_cost(student, sk) * 2; + if (j < 50) j = 50; /* kann Einheit das zahlen? */ return get_pooled(student, get_resourcetype(R_SILVER), GET_DEFAULT, j) >= j; /* sonst nehmen sie nicht am Unterricht teil */ diff --git a/src/academy.test.c b/src/academy.test.c new file mode 100644 index 000000000..6e971ee60 --- /dev/null +++ b/src/academy.test.c @@ -0,0 +1,56 @@ +#include + +#include "academy.h" +#include "skill.h" + +#include +#include +#include +#include +#include +#include + +#include +#include "tests.h" + +static void test_academy(CuTest * tc) +{ + faction *f; + unit *u, *u2; + region *r; + building *b; + const item_type *it_silver; + + test_setup(); + config_set_int("skills.cost.alchemy", 100); + r = test_create_region(0, 0, NULL); + f = test_create_faction(NULL); + u = test_create_unit(f, r); + b = test_create_building(r, test_create_buildingtype("academy")); + u2 = test_create_unit(f, r); + it_silver = test_create_silver(); + + CuAssert(tc, "teacher must be in academy", !academy_can_teach(u, u2, SK_CROSSBOW)); + u_set_building(u, b); + CuAssert(tc, "student must be in academy", !academy_can_teach(u, u2, SK_CROSSBOW)); + u_set_building(u2, b); + CuAssert(tc, "student must have 50 silver", !academy_can_teach(u, u2, SK_CROSSBOW)); + i_change(&u2->items, it_silver, 50); + CuAssert(tc, "building must be maintained", !academy_can_teach(u, u2, SK_CROSSBOW)); + b->flags |= BLD_MAINTAINED; + CuAssert(tc, "building must have capacity", !academy_can_teach(u, u2, SK_CROSSBOW)); + b->size = 2; + CuAssertTrue(tc, academy_can_teach(u, u2, SK_CROSSBOW)); + + CuAssert(tc, "student must pay skillcost", !academy_can_teach(u, u2, SK_ALCHEMY)); + i_change(&u2->items, it_silver, 150); + CuAssertTrue(tc, academy_can_teach(u, u2, SK_ALCHEMY)); + test_teardown(); +} + +CuSuite *get_academy_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_academy); + return suite; +} diff --git a/src/study.c b/src/study.c index bcb83a723..b7d5dc519 100644 --- a/src/study.c +++ b/src/study.c @@ -130,9 +130,14 @@ bool magic_lowskill(unit * u) int study_cost(unit * u, skill_t sk) { + static int config; static int cost[MAXSKILLS]; int stufe, k = 50; + if (config_changed(&config)) { + memset(cost, 0, sizeof(cost)); + } + if (cost[sk] == 0) { char buffer[256]; sprintf(buffer, "skills.cost.%s", skillnames[sk]); diff --git a/src/test_eressea.c b/src/test_eressea.c index 8f23b8f6b..08ceefc45 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -107,6 +107,7 @@ int RunAllTests(int argc, char *argv[]) ADD_SUITE(familiar); ADD_SUITE(item); ADD_SUITE(magic); + ADD_SUITE(academy); ADD_SUITE(alchemy); ADD_SUITE(reports); ADD_SUITE(region);