2014-08-26 20:06:28 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
2015-05-06 17:36:20 +02:00
|
|
|
#include <kernel/curse.h>
|
|
|
|
#include <kernel/item.h>
|
|
|
|
#include <kernel/faction.h>
|
|
|
|
#include <kernel/order.h>
|
|
|
|
#include <kernel/race.h>
|
|
|
|
#include <kernel/region.h>
|
|
|
|
#include <kernel/spell.h>
|
2014-12-09 06:45:21 +01:00
|
|
|
#include <util/base36.h>
|
|
|
|
#include <util/language.h>
|
2015-05-06 17:36:20 +02:00
|
|
|
#include <util/attrib.h>
|
|
|
|
#include <spells/regioncurse.h>
|
|
|
|
#include <alchemy.h>
|
|
|
|
#include <laws.h>
|
|
|
|
#include <spells.h>
|
2014-08-26 20:06:28 +02:00
|
|
|
#include "unit.h"
|
|
|
|
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
2014-12-09 06:45:21 +01:00
|
|
|
#include <stdio.h>
|
2014-08-26 20:06:28 +02:00
|
|
|
#include <stdlib.h>
|
2014-12-09 06:45:21 +01:00
|
|
|
#include <string.h>
|
2014-08-26 20:06:28 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-10-16 07:34:09 +02:00
|
|
|
static void test_remove_empty_units(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
int uid;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
uid = u->no;
|
|
|
|
remove_empty_units();
|
|
|
|
CuAssertPtrNotNull(tc, findunit(uid));
|
|
|
|
u->number = 0;
|
|
|
|
remove_empty_units();
|
|
|
|
CuAssertPtrEquals(tc, 0, findunit(uid));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_remove_empty_units_in_region(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
int uid;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
uid = u->no;
|
|
|
|
remove_empty_units_in_region(u->region);
|
|
|
|
CuAssertPtrNotNull(tc, findunit(uid));
|
|
|
|
u->number = 0;
|
|
|
|
remove_empty_units_in_region(u->region);
|
|
|
|
CuAssertPtrEquals(tc, 0, findunit(uid));
|
|
|
|
CuAssertPtrEquals(tc, 0, u->region);
|
|
|
|
CuAssertPtrEquals(tc, 0, u->faction);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-10-16 08:06:44 +02:00
|
|
|
static void test_remove_units_without_faction(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
int uid;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
uid = u->no;
|
|
|
|
u_setfaction(u, 0);
|
|
|
|
remove_empty_units_in_region(u->region);
|
|
|
|
CuAssertPtrEquals(tc, 0, findunit(uid));
|
|
|
|
CuAssertIntEquals(tc, 0, u->number);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_remove_units_with_dead_faction(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
int uid;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
uid = u->no;
|
|
|
|
u->faction->alive = false;
|
|
|
|
remove_empty_units_in_region(u->region);
|
|
|
|
CuAssertPtrEquals(tc, 0, findunit(uid));
|
|
|
|
CuAssertIntEquals(tc, 0, u->number);
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_remove_units_ignores_spells(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
int uid;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
|
|
|
|
u = create_unit(findregion(0, 0), test_create_faction(test_create_race("human")), 1, get_race(RC_SPELL), 0, 0, 0);
|
|
|
|
uid = u->no;
|
|
|
|
u->number = 0;
|
|
|
|
u->age = 1;
|
|
|
|
remove_empty_units_in_region(u->region);
|
|
|
|
CuAssertPtrNotNull(tc, findunit(uid));
|
|
|
|
CuAssertPtrNotNull(tc, u->region);
|
|
|
|
u->age = 0;
|
|
|
|
remove_empty_units_in_region(u->region);
|
|
|
|
CuAssertPtrEquals(tc, 0, findunit(uid));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-08-26 20:06:28 +02:00
|
|
|
static void test_scale_number(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
const struct potion_type *ptype;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
ptype = new_potiontype(it_get_or_create(rt_get_or_create("hodor")), 1);
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
change_effect(u, ptype, 1);
|
|
|
|
CuAssertIntEquals(tc, 1, u->number);
|
|
|
|
CuAssertIntEquals(tc, 1, u->hp);
|
|
|
|
CuAssertIntEquals(tc, 1, get_effect(u, ptype));
|
|
|
|
scale_number(u, 2);
|
|
|
|
CuAssertIntEquals(tc, 2, u->number);
|
|
|
|
CuAssertIntEquals(tc, 2, u->hp);
|
|
|
|
CuAssertIntEquals(tc, 2, get_effect(u, ptype));
|
|
|
|
set_level(u, SK_ALCHEMY, 1);
|
|
|
|
scale_number(u, 0);
|
|
|
|
CuAssertIntEquals(tc, 0, get_level(u, SK_ALCHEMY));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-12-09 06:45:21 +01:00
|
|
|
static void test_unit_name(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
char name[32];
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
unit_setname(u, "Hodor");
|
|
|
|
_snprintf(name, sizeof(name), "Hodor (%s)", itoa36(u->no));
|
|
|
|
CuAssertStrEquals(tc, name, unitname(u));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_unit_name_from_race(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
char name[32];
|
|
|
|
struct locale *lang;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
unit_setname(u, NULL);
|
|
|
|
lang = get_or_create_locale("de");
|
2014-12-09 07:20:36 +01:00
|
|
|
locale_setstring(lang, rc_name_s(u->_race, NAME_SINGULAR), "Mensch");
|
|
|
|
locale_setstring(lang, rc_name_s(u->_race, NAME_PLURAL), "Menschen");
|
2014-12-09 06:45:21 +01:00
|
|
|
|
|
|
|
_snprintf(name, sizeof(name), "Mensch (%s)", itoa36(u->no));
|
|
|
|
CuAssertStrEquals(tc, name, unitname(u));
|
2015-02-11 07:55:35 +01:00
|
|
|
CuAssertStrEquals(tc, "Mensch", unit_getname(u));
|
2014-12-09 06:45:21 +01:00
|
|
|
|
|
|
|
u->number = 2;
|
|
|
|
_snprintf(name, sizeof(name), "Menschen (%s)", itoa36(u->no));
|
|
|
|
CuAssertStrEquals(tc, name, unitname(u));
|
2015-02-11 07:55:35 +01:00
|
|
|
CuAssertStrEquals(tc, "Menschen", unit_getname(u));
|
2014-12-09 06:45:21 +01:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2014-12-09 07:20:36 +01:00
|
|
|
static void test_update_monster_name(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
struct locale *lang;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
lang = get_or_create_locale("de");
|
|
|
|
locale_setstring(lang, rc_name_s(u->_race, NAME_SINGULAR), "Mensch");
|
|
|
|
locale_setstring(lang, rc_name_s(u->_race, NAME_PLURAL), "Menschen");
|
|
|
|
|
|
|
|
unit_setname(u, "Hodor");
|
2014-12-09 07:44:26 +01:00
|
|
|
CuAssertTrue(tc, !unit_name_equals_race(u));
|
|
|
|
|
|
|
|
unit_setname(u, "Menschling");
|
|
|
|
CuAssertTrue(tc, !unit_name_equals_race(u));
|
|
|
|
|
|
|
|
unit_setname(u, rc_name_s(u->_race, NAME_SINGULAR));
|
|
|
|
CuAssertTrue(tc, unit_name_equals_race(u));
|
|
|
|
|
|
|
|
unit_setname(u, rc_name_s(u->_race, NAME_PLURAL));
|
|
|
|
CuAssertTrue(tc, unit_name_equals_race(u));
|
2014-12-09 07:20:36 +01:00
|
|
|
|
|
|
|
unit_setname(u, "Mensch");
|
2014-12-09 07:44:26 +01:00
|
|
|
CuAssertTrue(tc, unit_name_equals_race(u));
|
2014-12-09 07:20:36 +01:00
|
|
|
|
|
|
|
unit_setname(u, "Menschen");
|
2014-12-09 07:44:26 +01:00
|
|
|
CuAssertTrue(tc, unit_name_equals_race(u));
|
2014-12-09 07:20:36 +01:00
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-02-11 22:41:10 +01:00
|
|
|
static void test_names(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
|
|
|
|
unit_setname(u, "Hodor");
|
|
|
|
unit_setid(u, 5);
|
|
|
|
CuAssertStrEquals(tc, "Hodor", unit_getname(u));
|
|
|
|
CuAssertStrEquals(tc, "Hodor (5)", unitname(u));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:47:26 +01:00
|
|
|
static void test_default_name(CuTest *tc) {
|
|
|
|
unit *u;
|
|
|
|
struct locale* lang;
|
|
|
|
char buf[32], compare[32];
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
test_create_world();
|
|
|
|
lang = get_or_create_locale("de");
|
|
|
|
/* FIXME this has no real effect: default_name uses a static buffer that is initialized in some other test. This sucks. */
|
|
|
|
locale_setstring(lang, "unitdefault", "Einheit");
|
|
|
|
|
|
|
|
u = test_create_unit(test_create_faction(test_create_race("human")), findregion(0, 0));
|
|
|
|
|
|
|
|
default_name(u, buf, sizeof(buf));
|
|
|
|
|
|
|
|
sprintf(compare, "Einheit %s", itoa36(u->no));
|
|
|
|
CuAssertStrEquals(tc, compare, buf);
|
|
|
|
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2015-05-06 17:36:20 +02:00
|
|
|
|
2014-08-26 20:06:28 +02:00
|
|
|
CuSuite *get_unit_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_scale_number);
|
2014-12-09 06:45:21 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_unit_name);
|
|
|
|
SUITE_ADD_TEST(suite, test_unit_name_from_race);
|
2014-12-09 07:20:36 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_update_monster_name);
|
2014-10-16 07:34:09 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_remove_empty_units);
|
2014-10-16 08:06:44 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_remove_units_ignores_spells);
|
|
|
|
SUITE_ADD_TEST(suite, test_remove_units_without_faction);
|
|
|
|
SUITE_ADD_TEST(suite, test_remove_units_with_dead_faction);
|
2014-10-16 07:34:09 +02:00
|
|
|
SUITE_ADD_TEST(suite, test_remove_empty_units_in_region);
|
2015-02-11 22:41:10 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_names);
|
2015-02-11 16:47:26 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_default_name);
|
2014-08-26 20:06:28 +02:00
|
|
|
return suite;
|
|
|
|
}
|