diff --git a/src/laws.c b/src/laws.c index feacca8a1..7ed1cd75d 100644 --- a/src/laws.c +++ b/src/laws.c @@ -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) { diff --git a/src/orderfile.c b/src/orderfile.c index 90b44477b..410fa094a 100644 --- a/src/orderfile.c +++ b/src/orderfile.c @@ -22,7 +22,7 @@ #include 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) { diff --git a/src/orderfile.test.c b/src/orderfile.test.c index ca98fdeb0..204e20d5d 100644 --- a/src/orderfile.test.c +++ b/src/orderfile.test.c @@ -3,9 +3,20 @@ #include "orderfile.h" +#include "direction.h" + #include #include +#include +#include + +#include +#include +#include +#include #include +#include +#include #include #include @@ -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); diff --git a/src/reports.test.c b/src/reports.test.c index 86f69b0fd..db9f9634f 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -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; } diff --git a/src/util/keyword.c b/src/util/keyword.c index e2b5b4775..0d9a11dc4 100644 --- a/src/util/keyword.c +++ b/src/util/keyword.c @@ -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", diff --git a/src/util/keyword.h b/src/util/keyword.h index 7bb11c485..cc78fd6f7 100644 --- a/src/util/keyword.h +++ b/src/util/keyword.h @@ -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); diff --git a/src/util/strings.c b/src/util/strings.c index 23947330b..7f6cad26c 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -18,7 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #ifdef _MSC_VER #include -#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 #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 } diff --git a/src/util/strings.h b/src/util/strings.h index 5308886dd..a381b56b9 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -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); diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 506ce8c12..ac05b91e9 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -133,6 +133,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]; @@ -274,6 +282,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); SUITE_ADD_TEST(suite, test_sbs_strcat); SUITE_ADD_TEST(suite, test_sbs_substr);