add a test for copying resources message arguments.

my plan is to add a new argument type for arrays of resources with fewer allocations.
This commit is contained in:
Enno Rehling 2016-08-29 17:31:09 +01:00
parent fb2b6495e5
commit ef44a4534e
8 changed files with 59 additions and 19 deletions

View File

@ -776,7 +776,7 @@
<arg name="unit" type="unit"/> <arg name="unit" type="unit"/>
<arg name="region" type="region"/> <arg name="region" type="region"/>
<arg name="command" type="order"/> <arg name="command" type="order"/>
<arg name="required" type="resources"/> <arg name="required" type="resource_list"/>
</type> </type>
<text locale="de">"$unit($unit) in $region($region): '$order($command)' - Dafür braucht die Einheit $resources($required)."</text> <text locale="de">"$unit($unit) in $region($region): '$order($command)' - Dafür braucht die Einheit $resources($required)."</text>
<text locale="en">"$unit($unit) in $region($region): '$order($command)' - For this, the unit needs $resources($required)."</text> <text locale="en">"$unit($unit) in $region($region): '$order($command)' - For this, the unit needs $resources($required)."</text>
@ -2374,7 +2374,7 @@
<arg name="unit" type="unit"/> <arg name="unit" type="unit"/>
<arg name="region" type="region"/> <arg name="region" type="region"/>
<arg name="command" type="order"/> <arg name="command" type="order"/>
<arg name="list" type="resources"/> <arg name="list" type="resource_list"/>
</type> </type>
<text locale="de">"$unit($unit) in $region($region): '$order($command)' - Für diesen Zauber fehlen noch $resources($list)."</text> <text locale="de">"$unit($unit) in $region($region): '$order($command)' - Für diesen Zauber fehlen noch $resources($list)."</text>
<text locale="en">"$unit($unit) in $region($region): '$order($command)' - Casting this spell requires an additional $resources($list)."</text> <text locale="en">"$unit($unit) in $region($region): '$order($command)' - Casting this spell requires an additional $resources($list)."</text>

View File

@ -172,9 +172,9 @@ static unit *unitorders(FILE * F, int enc, struct faction *f)
if (s[0]) { if (s[0]) {
if (s[0] != '@') { if (s[0] != '@') {
char token[128]; char token[64];
const char *stok = s; const char *stok = s;
stok = parse_token(&stok, token, 64); // FIXME: use sizeof, but parse_token overwrites the buffer stok = parse_token(&stok, token, sizeof(token));
if (stok) { if (stok) {
bool quit = false; bool quit = false;

View File

@ -993,6 +993,7 @@ cancast(unit * u, const spell * sp, int level, int range, struct order * ord)
itemhave = get_pooled(u, rtype, GET_DEFAULT, itemanz); itemhave = get_pooled(u, rtype, GET_DEFAULT, itemanz);
if (itemhave < itemanz) { if (itemhave < itemanz) {
// TODO: lots of alloc/dealloc calls here (make var_copy_resources take an array)
resource *res = malloc(sizeof(resource)); resource *res = malloc(sizeof(resource));
res->number = itemanz - itemhave; res->number = itemanz - itemhave;
res->type = rtype; res->type = rtype;

View File

@ -1688,7 +1688,7 @@ static variant var_copy_items(variant x)
return x; return x;
} }
static variant var_copy_resources(variant x) static variant var_copy_resource_list(variant x)
{ {
resource *rsrc; resource *rsrc;
resource *rdst = NULL, **rptr = &rdst; resource *rdst = NULL, **rptr = &rdst;
@ -1705,7 +1705,7 @@ static variant var_copy_resources(variant x)
return x; return x;
} }
static void var_free_resources(variant x) static void var_free_resource_list(variant x)
{ {
resource *rsrc = (resource *)x.v; resource *rsrc = (resource *)x.v;
while (rsrc) { while (rsrc) {
@ -2250,8 +2250,9 @@ void register_reports(void)
register_argtype("int", NULL, NULL, VAR_INT); register_argtype("int", NULL, NULL, VAR_INT);
register_argtype("string", var_free_string, var_copy_string, VAR_VOIDPTR); register_argtype("string", var_free_string, var_copy_string, VAR_VOIDPTR);
register_argtype("order", var_free_order, var_copy_order, VAR_VOIDPTR); register_argtype("order", var_free_order, var_copy_order, VAR_VOIDPTR);
register_argtype("resources", var_free_resources, var_copy_resources, VAR_VOIDPTR); register_argtype("resource_list", var_free_resource_list, var_copy_resource_list, VAR_VOIDPTR);
register_argtype("items", var_free_resources, var_copy_items, VAR_VOIDPTR); //register_argtype("resources", var_free_resources, var_copy_resources, VAR_VOIDPTR);
register_argtype("items", var_free_resource_list, var_copy_items, VAR_VOIDPTR);
register_argtype("regions", var_free_regions, NULL, VAR_VOIDPTR); register_argtype("regions", var_free_regions, NULL, VAR_VOIDPTR);
msg_log_create = &log_orders; msg_log_create = &log_orders;

View File

@ -10,6 +10,7 @@
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/race.h> #include <kernel/race.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/ship.h> #include <kernel/ship.h>
@ -19,6 +20,7 @@
#include <util/language.h> #include <util/language.h>
#include <util/lists.h> #include <util/lists.h>
#include <util/message.h>
#include <quicklist.h> #include <quicklist.h>
#include <stream.h> #include <stream.h>
@ -415,6 +417,39 @@ static void test_write_spell_syntax(CuTest *tc) {
cleanup_spell_fixture(&spell); cleanup_spell_fixture(&spell);
} }
static void test_arg_resources(CuTest *tc) {
variant v1, v2;
arg_type *atype;
resource *res;
item_type *itype;
test_setup();
itype = test_create_itemtype("stone");
v1.v = res = malloc(sizeof(resource)*2);
res[0].number = 10;
res[0].type = itype->rtype;
res[0].next = &res[1];
res[1].number = 5;
res[1].type = itype->rtype;
res[1].next = NULL;
register_reports();
atype = find_argtype("resource_list");
CuAssertPtrNotNull(tc, atype);
v2 = atype->copy(v1);
free(v1.v);
CuAssertPtrNotNull(tc, v2.v);
res = (resource *)v2.v;
CuAssertPtrEquals(tc, itype->rtype, (void *)res->type);
CuAssertIntEquals(tc, 10, res->number);
CuAssertPtrNotNull(tc, res = res->next);
CuAssertPtrEquals(tc, itype->rtype, (void *)res->type);
CuAssertIntEquals(tc, 5, res->number);
CuAssertPtrEquals(tc, 0, res->next);
atype->release(v2);
test_cleanup();
}
CuSuite *get_reports_suite(void) CuSuite *get_reports_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
@ -428,5 +463,6 @@ CuSuite *get_reports_suite(void)
SUITE_ADD_TEST(suite, test_write_travelthru); SUITE_ADD_TEST(suite, test_write_travelthru);
SUITE_ADD_TEST(suite, test_write_unit); SUITE_ADD_TEST(suite, test_write_unit);
SUITE_ADD_TEST(suite, test_write_spell_syntax); SUITE_ADD_TEST(suite, test_write_spell_syntax);
SUITE_ADD_TEST(suite, test_arg_resources);
return suite; return suite;
} }

View File

@ -47,7 +47,7 @@ variant(*copy_arg) (variant), variant_type type)
argtypes = atype; argtypes = atype;
} }
static arg_type *find_argtype(const char *name) arg_type *find_argtype(const char *name)
{ {
arg_type *atype = argtypes; arg_type *atype = argtypes;
while (atype != NULL) { while (atype != NULL) {
@ -83,7 +83,7 @@ message_type *mt_new(const char *name, const char *args[])
mtype->pnames = NULL; mtype->pnames = NULL;
mtype->types = NULL; mtype->types = NULL;
} }
if (args != NULL) if (args != NULL) {
for (i = 0; args[i]; ++i) { for (i = 0; args[i]; ++i) {
const char *x = args[i]; const char *x = args[i];
const char *spos = strchr(x, ':'); const char *spos = strchr(x, ':');
@ -102,6 +102,7 @@ message_type *mt_new(const char *name, const char *args[])
assert(mtype->types[i]); assert(mtype->types[i]);
} }
} }
}
return mtype; return mtype;
} }

View File

@ -46,22 +46,23 @@ extern "C" {
/* mt_new("simple_sentence", "subject:string", "predicate:string", /* mt_new("simple_sentence", "subject:string", "predicate:string",
* "object:string", "lang:locale", NULL); */ * "object:string", "lang:locale", NULL); */
extern struct message *msg_create(const struct message_type *type, struct message *msg_create(const struct message_type *type,
variant args[]); variant args[]);
/* msg_create(&mt_simplesentence, "enno", "eats", "chocolate", &locale_de); /* msg_create(&mt_simplesentence, "enno", "eats", "chocolate", &locale_de);
* parameters must be in the same order as they were for mt_new! */ * parameters must be in the same order as they were for mt_new! */
extern void msg_release(struct message *msg); void msg_release(struct message *msg);
extern struct message *msg_addref(struct message *msg); struct message *msg_addref(struct message *msg);
extern const char *mt_name(const struct message_type *mtype); const char *mt_name(const struct message_type *mtype);
/** message_type registry (optional): **/ /** message_type registry (optional): **/
extern const struct message_type *mt_register(struct message_type *); const struct message_type *mt_register(struct message_type *);
extern const struct message_type *mt_find(const char *); const struct message_type *mt_find(const char *);
extern void register_argtype(const char *name, void(*free_arg) (variant), void register_argtype(const char *name, void(*free_arg) (variant),
variant(*copy_arg) (variant), variant_type); variant(*copy_arg) (variant), variant_type);
arg_type *find_argtype(const char *name);
void(*msg_log_create) (const struct message * msg); void(*msg_log_create) (const struct message * msg);

View File

@ -2,6 +2,6 @@ cd c:\users\enno\documents\eressea\git\tests
"C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\eressea.exe -t184 test-turn.lua "C:\Program Files (x86)\Dr. Memory\bin64\drmemory.exe" ..\build-vs14\eressea\Debug\eressea.exe -t184 test-turn.lua
del reports del /q reports
del datum htpasswd parteien parteien.full passwd score turn del /q datum htpasswd parteien parteien.full passwd score turn
pause pause