Merge pull request #823 from ennorehling/master

bugfixes for 3.18.2
This commit is contained in:
Enno Rehling 2018-12-02 21:02:25 +01:00 committed by GitHub
commit 069a94f830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 132 additions and 27 deletions

View File

@ -3339,7 +3339,6 @@ int pay_cmd(unit * u, struct order *ord)
cmistake(u, ord, 6, MSG_EVENT);
}
else {
building *b = NULL;
param_t p;
int id;
@ -3355,13 +3354,12 @@ int pay_cmd(unit * u, struct order *ord)
}
else {
/* If no building id is given or it is the id of our building, just set the do-not-pay flag */
if (id == 0 || id == u->building->no)
{
if (id == 0 || id == u->building->no) {
u->building->flags |= BLD_DONTPAY;
}
else {
/* Find the building that matches to the given id*/
b = findbuilding(id);
building *b = findbuilding(id);
/* If there is a building and it is in the same region as the unit continue, else: error */
if (b && b->region == u->region)
{

View File

@ -22,7 +22,7 @@
#include <string.h>
static void begin_orders(unit *u) {
if (u->flags & UFL_ORDERS) {
if ((u->flags & UFL_ORDERS) == 0) {
order **ordp;
/* alle wiederholbaren, langen befehle werden gesichert: */
u->flags |= UFL_ORDERS;
@ -47,13 +47,12 @@ static void begin_orders(unit *u) {
u->orders = NULL;
}
static unit *unitorders(input *in, faction *f)
static void unitorders(input *in, faction *f)
{
int i;
unit *u;
if (!f)
return NULL;
assert(f);
i = getid();
u = findunit(i);
@ -120,10 +119,6 @@ static unit *unitorders(input *in, faction *f)
}
}
else {
return NULL;
}
return u;
}
static faction *factionorders(void)
@ -189,7 +184,8 @@ int read_orders(input *in)
* vermerkt. */
case P_UNIT:
if (!f || !unitorders(in, f)) {
if (f) {
unitorders(in, f);
do {
b = in->getbuf(in->data);
if (!b) {

View File

@ -3,9 +3,20 @@
#include "orderfile.h"
#include "direction.h"
#include <kernel/calendar.h>
#include <kernel/faction.h>
#include <kernel/order.h>
#include <kernel/unit.h>
#include <util/base36.h>
#include <util/keyword.h>
#include <util/language.h>
#include <util/lists.h>
#include <util/message.h>
#include <util/param.h>
#include <util/password.h>
#include <CuTest.h>
#include <tests.h>
@ -24,6 +35,52 @@ static void test_read_orders(CuTest *tc) {
test_teardown();
}
static const char *getbuf_strings(void *data)
{
strlist **listp = (strlist **)data;
if (listp && *listp) {
strlist *list = *listp;
*listp = list->next;
return list->s;
}
return NULL;
}
static void test_unit_orders(CuTest *tc) {
input in;
unit *u;
faction *f;
strlist *orders = NULL, *backup;
char buf[64];
test_setup();
u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0));
f->locale = test_create_locale();
u->orders = create_order(K_ENTERTAIN, f->locale, NULL);
faction_setpassword(f, password_hash("password", PASSWORD_DEFAULT));
snprintf(buf, sizeof(buf), "%s %s %s",
LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password");
addstrlist(&orders, buf);
snprintf(buf, sizeof(buf), "%s %s",
LOC(f->locale, parameters[P_UNIT]), itoa36(u->no));
addstrlist(&orders, buf);
snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale),
LOC(f->locale, shortdirections[D_WEST]));
backup = orders;
addstrlist(&orders, buf);
in.data = &orders;
in.getbuf = getbuf_strings;
CuAssertIntEquals(tc, 0, read_orders(&in));
CuAssertPtrNotNull(tc, u->old_orders);
CuAssertPtrNotNull(tc, u->orders);
CuAssertPtrEquals(tc, NULL, orders);
CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders));
CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders));
CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS);
freestrlist(backup);
test_teardown();
}
typedef struct order_list {
const char **orders;
int next;
@ -85,6 +142,7 @@ CuSuite *get_orderfile_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_read_orders);
SUITE_ADD_TEST(suite, test_unit_orders);
SUITE_ADD_TEST(suite, test_faction_password_okay);
SUITE_ADD_TEST(suite, test_faction_password_bad);

View File

@ -2156,7 +2156,7 @@ static void eval_resources(struct opstack **stack, const void *userdata)
const char *rname =
resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0);
sbs_strcat(&sbs, str_itoa(res->number));
sbs_strcat(&sbs, "");
sbs_strcat(&sbs, " ");
sbs_strcat(&sbs, LOC(lang, rname));
res = res->next;

View File

@ -16,6 +16,7 @@
#include "kernel/building.h"
#include "kernel/faction.h"
#include "kernel/item.h"
#include "kernel/messages.h"
#include "kernel/race.h"
#include "kernel/region.h"
#include "kernel/ship.h"
@ -29,6 +30,7 @@
#include "util/language.h"
#include "util/lists.h"
#include "util/message.h"
#include "util/nrmessage.h"
#include "attributes/attributes.h"
#include "attributes/key.h"
@ -906,6 +908,46 @@ static void test_visible_unit(CuTest *tc) {
test_teardown();
}
static void test_eval_functions(CuTest *tc)
{
message *msg;
message_type *mtype;
item *items = NULL;
char buf[1024];
struct locale * lang;
test_setup();
init_resources();
test_create_itemtype("stone");
test_create_itemtype("iron");
lang = test_create_locale();
locale_setstring(lang, "nr_claims", "$resources($items)");
register_reports();
mtype = mt_create_va(mt_new("nr_claims", NULL), "items:items", MT_NEW_END);
nrt_register(mtype);
msg = msg_message("nr_claims", "items", items);
nr_render(msg, lang, buf, sizeof(buf), NULL);
CuAssertStrEquals(tc, "", buf);
msg_release(msg);
i_change(&items, get_resourcetype(R_IRON)->itype, 1);
msg = msg_message("nr_claims", "items", items);
nr_render(msg, lang, buf, sizeof(buf), NULL);
CuAssertStrEquals(tc, "1 Eisen", buf);
msg_release(msg);
i_change(&items, get_resourcetype(R_STONE)->itype, 2);
msg = msg_message("nr_claims", "items", items);
nr_render(msg, lang, buf, sizeof(buf), NULL);
CuAssertStrEquals(tc, "1 Eisen, 2 Steine", buf);
msg_release(msg);
i_freeall(&items);
test_teardown();
}
CuSuite *get_reports_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -936,5 +978,6 @@ CuSuite *get_reports_suite(void)
SUITE_ADD_TEST(suite, test_insect_warnings);
SUITE_ADD_TEST(suite, test_newbie_warning);
SUITE_ADD_TEST(suite, test_visible_unit);
SUITE_ADD_TEST(suite, test_eval_functions);
return suite;
}

View File

@ -88,6 +88,11 @@ bool keyword_disabled(keyword_t kwd) {
return disabled_kwd[kwd];
}
const char *keyword_name(keyword_t kwd, const struct locale *lang)
{
return LOC(lang, mkname("keyword", keywords[kwd]));
}
const char *keywords[MAXKEYWORDS] = {
"//",
"banner",

View File

@ -83,6 +83,7 @@ extern "C"
void init_keywords(const struct locale *lang);
void init_keyword(const struct locale *lang, keyword_t kwd, const char *str);
bool keyword_disabled(keyword_t kwd);
const char *keyword_name(keyword_t kwd, const struct locale *lang);
void enable_keyword(keyword_t kwd, bool enabled);
const char *keyword(keyword_t kwd);

View File

@ -18,7 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifdef _MSC_VER
#include <platform.h>
#define HAVE__ITOA
#undef HAVE__ITOA
#endif
#include "strings.h"
@ -35,27 +35,23 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <string.h>
#endif
const char* str_itoab(int val, int base)
const char* str_itoa_r(int val, char *buf)
{
static char buf[32] = { 0 };
#ifdef HAVE__ITOAB
return _itoa(val, buf, base);
#ifdef HAVE__ITOA
return _itoa(val, buf, 10);
#else
int i = 30;
for (; val && i; --i, val /= base) {
buf[i] = "0123456789abcdefghijklmnopqrstuvwxyz"[val % base];
}
return &buf[i + 1];
snprintf(buf, 12, "%d", val);
return buf;
#endif
}
const char *str_itoa(int n)
{
static char buf[12];
#ifdef HAVE__ITOA
static char buf[32] = { 0 };
return _itoa(n, buf, 10);
#else
return str_itoab(n, 10);
return str_itoa_r(n, buf);
#endif
}

View File

@ -29,7 +29,6 @@ extern "C" {
void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value);
int str_hash(const char *s);
const char *str_itoa(int i);
const char *str_itoab(int i, int base);
size_t str_slprintf(char * dst, size_t size, const char * format, ...);
size_t str_strlcpy(char *dst, const char *src, size_t len);
size_t str_strlcat(char *dst, const char *src, size_t len);

View File

@ -128,6 +128,14 @@ static void test_str_strlcpy(CuTest * tc)
errno = 0;
}
static void test_str_itoa(CuTest * tc)
{
CuAssertStrEquals(tc, "1234", str_itoa(1234));
CuAssertStrEquals(tc, "0", str_itoa(0));
CuAssertStrEquals(tc, "1234567890", str_itoa(1234567890));
CuAssertStrEquals(tc, "-1234567890", str_itoa(-1234567890));
}
static void test_sbstring(CuTest * tc)
{
char buffer[16];
@ -165,6 +173,7 @@ CuSuite *get_strings_suite(void)
SUITE_ADD_TEST(suite, test_str_slprintf);
SUITE_ADD_TEST(suite, test_str_strlcat);
SUITE_ADD_TEST(suite, test_str_strlcpy);
SUITE_ADD_TEST(suite, test_str_itoa);
SUITE_ADD_TEST(suite, test_sbstring);
return suite;
}