diff --git a/src/kernel/config.c b/src/kernel/config.c index 9cb6ee7ef..4d6f18059 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1018,30 +1018,29 @@ int read_unitid(const faction * f, const region * r) int getunit(const region * r, const faction * f, unit **uresult) { int n = read_unitid(f, r); - unit *u2; + int result = GET_NOTFOUND; + unit *u2 = NULL; if (n == 0) { - if (uresult) { - *uresult = 0; - } - return GET_PEASANTS; + result = GET_PEASANTS; + } + else if (n>0) { + u2 = findunit(n); + if (u2 != NULL && u2->region == r) { + /* there used to be a 'u2->flags & UFL_ISNEW || u2->number>0' condition + * here, but it got removed because of a bug that made units disappear: + * http://eressea.upb.de/mantis/bug_view_page.php?bug_id=0000172 + */ + result = GET_UNIT; + } + else { + u2 = NULL; + } } - if (n < 0) - return GET_NOTFOUND; - - u2 = findunit(n); if (uresult) { *uresult = u2; } - if (u2 != NULL && u2->region == r) { - /* there used to be a 'u2->flags & UFL_ISNEW || u2->number>0' condition - * here, but it got removed because of a bug that made units disappear: - * http://eressea.upb.de/mantis/bug_view_page.php?bug_id=0000172 - */ - return GET_UNIT; - } - - return GET_NOTFOUND; + return result; } /* - String Listen --------------------------------------------- */ diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index 78562f5dd..79643106c 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -2,10 +2,68 @@ #include #include +#include +#include +#include +#include +#include +#include #include #include +struct critbit_tree; + +static void test_getunit(CuTest *tc) { + unit *u, *u2; + order *ord; + attrib *a; + struct region *r; + struct locale *lang; + struct terrain_type *t_plain; + struct critbit_tree ** cb; + + test_cleanup(); + lang = get_or_create_locale("de"); + cb = (struct critbit_tree **)get_translations(lang, UT_PARAMS); + add_translation(cb, "TEMP", P_TEMP); + /* note that the english order is FIGHT, not COMBAT, so this is a poor example */ + t_plain = test_create_terrain("plain", LAND_REGION); + u = test_create_unit(test_create_faction(0), test_create_region(0, 0, t_plain)); + a = a_add(&u->attribs, a_new(&at_alias)); + a->data.i = atoi36("42"); /* this unit is also TEMP 42 */ + r = test_create_region(1, 0, t_plain); + + ord = create_order(K_GIVE, lang, itoa36(u->no)); + init_order(ord); + CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2)); + CuAssertPtrEquals(tc, u, u2); + init_order(ord); + CuAssertIntEquals(tc, GET_NOTFOUND, getunit(r, u->faction, &u2)); + CuAssertPtrEquals(tc, NULL, u2); + free_order(ord); + + ord = create_order(K_GIVE, lang, itoa36(u->no+1)); + init_order(ord); + CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); + CuAssertPtrEquals(tc, NULL, u2); + free_order(ord); + + ord = create_order(K_GIVE, lang, "0"); + init_order(ord); + CuAssertIntEquals(tc, GET_PEASANTS, getunit(u->region, u->faction, &u2)); + CuAssertPtrEquals(tc, NULL, u2); + free_order(ord); + + ord = create_order(K_GIVE, lang, "TEMP 42"); + init_order(ord); + CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2)); + CuAssertPtrEquals(tc, u, u2); + free_order(ord); + + test_cleanup(); +} + static void test_get_set_param(CuTest * tc) { struct param *par = 0; @@ -42,6 +100,7 @@ static void test_param_flt(CuTest * tc) CuSuite *get_config_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_getunit); SUITE_ADD_TEST(suite, test_get_set_param); SUITE_ADD_TEST(suite, test_param_int); SUITE_ADD_TEST(suite, test_param_flt);