add the test coverage for getunit.

fix a bug where NULL was not returned when unit is in the wrong region.
This commit is contained in:
Enno Rehling 2014-12-12 21:55:44 +01:00
parent 98994f233d
commit 63f69ec1cf
2 changed files with 76 additions and 18 deletions

View File

@ -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 --------------------------------------------- */

View File

@ -2,10 +2,68 @@
#include <stdlib.h>
#include <kernel/config.h>
#include <kernel/terrain.h>
#include <kernel/unit.h>
#include <kernel/order.h>
#include <util/language.h>
#include <util/base36.h>
#include <util/attrib.h>
#include <CuTest.h>
#include <tests.h>
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);