create a test that crashes for bug 2194

This commit is contained in:
Enno Rehling 2016-03-04 20:50:58 +01:00
parent 2b62747718
commit 13f51ab796
2 changed files with 50 additions and 6 deletions

View File

@ -205,7 +205,7 @@ teach_unit(unit * teacher, unit * student, int nteaching, skill_t sk,
n = _min(n, nteaching); n = _min(n, nteaching);
if (n != 0) { if (n != 0 && teacher->building && student->building) {
const struct building_type *btype = bt_find("academy"); const struct building_type *btype = bt_find("academy");
int index = 0; int index = 0;

View File

@ -5,8 +5,11 @@
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/order.h> #include <kernel/order.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/building.h>
#include <util/rand.h>
#include <util/message.h> #include <util/message.h>
#include <util/language.h> #include <util/language.h>
#include <util/base36.h> #include <util/base36.h>
@ -21,6 +24,15 @@ typedef struct {
unit *teachers[2]; unit *teachers[2];
} study_fixture; } study_fixture;
static void setup_locale(struct locale *lang) {
int i;
for (i = 0; i < MAXSKILLS; ++i) {
if (!locale_getstring(lang, mkname("skill", skillnames[i])))
locale_setstring(lang, mkname("skill", skillnames[i]), skillnames[i]);
}
init_skills(lang);
}
static void setup_study(study_fixture *fix, skill_t sk) { static void setup_study(study_fixture *fix, skill_t sk) {
struct region * r; struct region * r;
struct faction *f; struct faction *f;
@ -33,8 +45,7 @@ static void setup_study(study_fixture *fix, skill_t sk) {
r = findregion(0, 0); r = findregion(0, 0);
f = test_create_faction(0); f = test_create_faction(0);
lang = get_or_create_locale(locale_name(f->locale)); lang = get_or_create_locale(locale_name(f->locale));
locale_setstring(lang, mkname("skill", skillnames[sk]), skillnames[sk]); setup_locale(lang);
init_skills(lang);
fix->u = test_create_unit(f, r); fix->u = test_create_unit(f, r);
assert(fix->u); assert(fix->u);
fix->u->thisorder = create_order(K_STUDY, f->locale, "%s", skillnames[sk]); fix->u->thisorder = create_order(K_STUDY, f->locale, "%s", skillnames[sk]);
@ -80,12 +91,10 @@ static void test_study_with_teacher(CuTest *tc) {
static void test_study_with_bad_teacher(CuTest *tc) { static void test_study_with_bad_teacher(CuTest *tc) {
study_fixture fix; study_fixture fix;
skill *sv; skill *sv;
message *msg;
setup_study(&fix, SK_CROSSBOW); setup_study(&fix, SK_CROSSBOW);
teach_cmd(fix.teachers[0], fix.teachers[0]->thisorder); teach_cmd(fix.teachers[0], fix.teachers[0]->thisorder);
CuAssertPtrNotNull(tc, msg = test_get_last_message(fix.u->faction->msgs)); CuAssertPtrNotNull(tc, test_find_messagetype(fix.u->faction->msgs, "teach_asgood"));
CuAssertStrEquals(tc, "teach_asgood", test_get_messagetype(msg));
study_cmd(fix.u, fix.u->thisorder); study_cmd(fix.u, fix.u->thisorder);
CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW)); CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW));
CuAssertIntEquals(tc, 1, sv->level); CuAssertIntEquals(tc, 1, sv->level);
@ -93,11 +102,46 @@ static void test_study_with_bad_teacher(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_study_bug_2194(CuTest *tc) {
unit *u, *u1, *u2;
struct locale * loc;
building * b;
test_cleanup();
random_source_inject_constant(0.0);
init_resources();
loc = get_or_create_locale("de");
setup_locale(loc);
u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0));
scale_number(u, 2);
set_level(u, SK_CROSSBOW, TEACHDIFFERENCE);
u->faction->locale = loc;
u1 = test_create_unit(u->faction, u->region);
scale_number(u1, 17);
u1->thisorder = create_order(K_STUDY, loc, skillnames[SK_CROSSBOW]);
u2 = test_create_unit(u->faction, u->region);
scale_number(u2, 3);
u2->thisorder = create_order(K_STUDY, loc, skillnames[SK_MAGIC]);
u->thisorder = create_order(K_TEACH, loc, "%s %s", itoa36(u1->no), itoa36(u2->no));
b = test_create_building(u->region, test_create_buildingtype("academy"));
b->size = 22;
u_set_building(u, b);
u_set_building(u1, b);
u_set_building(u2, b);
i_change(&u1->items, get_resourcetype(R_SILVER)->itype, 50);
i_change(&u2->items, get_resourcetype(R_SILVER)->itype, 50);
b->flags = BLD_WORKING;
teach_cmd(u, u->thisorder);
CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "teach_asgood"));
test_cleanup();
}
CuSuite *get_study_suite(void) CuSuite *get_study_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_study_no_teacher); SUITE_ADD_TEST(suite, test_study_no_teacher);
SUITE_ADD_TEST(suite, test_study_with_teacher); SUITE_ADD_TEST(suite, test_study_with_teacher);
SUITE_ADD_TEST(suite, test_study_with_bad_teacher); SUITE_ADD_TEST(suite, test_study_with_bad_teacher);
SUITE_ADD_TEST(suite, test_study_bug_2194);
return suite; return suite;
} }