forked from github/server
cut the number of allocations for order-data in half.
started writing a test file for orders.
This commit is contained in:
parent
bb9d59bac5
commit
cef068fe9b
|
@ -14,6 +14,7 @@ equipment.test.c
|
||||||
curse.test.c
|
curse.test.c
|
||||||
item.test.c
|
item.test.c
|
||||||
move.test.c
|
move.test.c
|
||||||
|
order.test.c
|
||||||
pool.test.c
|
pool.test.c
|
||||||
reports.test.c
|
reports.test.c
|
||||||
spellbook.test.c
|
spellbook.test.c
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
+-------------------+
|
+-------------------+
|
||||||
| | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
| | Christian Schlittchen <corwin@amber.kn-bremen.de>
|
||||||
| Eressea PBEM host | Enno Rehling <enno@eressea.de>
|
| Eressea PBEM host | Enno Rehling <enno@eressea.de>
|
||||||
| (c) 1998 - 2004 | Katja Zedel <katze@felidae.kn-bremen.de>
|
| (c) 1998 - 2014 | Katja Zedel <katze@felidae.kn-bremen.de>
|
||||||
| |
|
| |
|
||||||
+-------------------+
|
+-------------------+
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ static struct locale_data *locale_array[16];
|
||||||
static int nlocales = 0;
|
static int nlocales = 0;
|
||||||
|
|
||||||
typedef struct order_data {
|
typedef struct order_data {
|
||||||
char *_str;
|
const char *_str;
|
||||||
# ifdef LOMEM
|
# ifdef LOMEM
|
||||||
int _refcount:20;
|
int _refcount:20;
|
||||||
int _lindex:4;
|
int _lindex:4;
|
||||||
|
@ -59,8 +59,6 @@ static void release_data(order_data * data)
|
||||||
{
|
{
|
||||||
if (data) {
|
if (data) {
|
||||||
if (--data->_refcount == 0) {
|
if (--data->_refcount == 0) {
|
||||||
if (data->_str)
|
|
||||||
free(data->_str);
|
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,6 +180,22 @@ void free_orders(order ** olist)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *mkdata(order_data **pdata, size_t len, keyword_t kwd, int lindex, const char *str)
|
||||||
|
{
|
||||||
|
order_data *data;
|
||||||
|
char *result;
|
||||||
|
data = malloc(sizeof(order_data) + len +1);
|
||||||
|
result = (char *)(data + 1);
|
||||||
|
data->_keyword = kwd;
|
||||||
|
data->_lindex = lindex;
|
||||||
|
data->_refcount = 0;
|
||||||
|
data->_str = 0;
|
||||||
|
data->_str = (len > 0) ? result : 0;
|
||||||
|
if (str) strcpy(result, str);
|
||||||
|
if (pdata) *pdata = data;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static order_data *create_data(keyword_t kwd, const char *sptr, int lindex)
|
static order_data *create_data(keyword_t kwd, const char *sptr, int lindex)
|
||||||
{
|
{
|
||||||
const char *s = sptr;
|
const char *s = sptr;
|
||||||
|
@ -204,21 +218,15 @@ static order_data *create_data(keyword_t kwd, const char *sptr, int lindex)
|
||||||
data = locale_array[lindex]->study_orders[sk];
|
data = locale_array[lindex]->study_orders[sk];
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
const char *skname = skillname(sk, lang);
|
const char *skname = skillname(sk, lang);
|
||||||
data = (order_data *)malloc(sizeof(order_data));
|
const char *spc = strchr(skname, ' ');
|
||||||
|
size_t len = strlen(skname);
|
||||||
|
char *dst = mkdata(&data, len + (spc ? 3 : 0), kwd, lindex, spc ? 0 : skname);
|
||||||
locale_array[lindex]->study_orders[sk] = data;
|
locale_array[lindex]->study_orders[sk] = data;
|
||||||
data->_keyword = kwd;
|
if (spc) {
|
||||||
data->_lindex = lindex;
|
dst[0] = '\"';
|
||||||
assert(data->_lindex >= 0);
|
memcpy(dst + 1, skname, len);
|
||||||
if (strchr(skname, ' ') != NULL) {
|
dst[len + 1] = '\"';
|
||||||
size_t len = strlen(skname);
|
dst[len + 2] = '\0';
|
||||||
data->_str = malloc(len + 3);
|
|
||||||
data->_str[0] = '\"';
|
|
||||||
memcpy(data->_str + 1, skname, len);
|
|
||||||
data->_str[len + 1] = '\"';
|
|
||||||
data->_str[len + 2] = '\0';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
data->_str = _strdup(skname);
|
|
||||||
}
|
}
|
||||||
data->_refcount = 1;
|
data->_refcount = 1;
|
||||||
}
|
}
|
||||||
|
@ -231,22 +239,13 @@ static order_data *create_data(keyword_t kwd, const char *sptr, int lindex)
|
||||||
else if (kwd != NOKEYWORD && *sptr == 0) {
|
else if (kwd != NOKEYWORD && *sptr == 0) {
|
||||||
data = locale_array[lindex]->short_orders[kwd];
|
data = locale_array[lindex]->short_orders[kwd];
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
data = (order_data *)malloc(sizeof(order_data));
|
mkdata(&data, 0, kwd, lindex, 0);
|
||||||
locale_array[lindex]->short_orders[kwd] = data;
|
|
||||||
data->_keyword = kwd;
|
|
||||||
data->_lindex = lindex;
|
|
||||||
assert(data->_lindex >= 0);
|
|
||||||
data->_str = NULL;
|
|
||||||
data->_refcount = 1;
|
data->_refcount = 1;
|
||||||
}
|
}
|
||||||
++data->_refcount;
|
++data->_refcount;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
data = (order_data *)malloc(sizeof(order_data));
|
mkdata(&data, s ? strlen(s) : 0, kwd, lindex, s);
|
||||||
data->_keyword = kwd;
|
|
||||||
data->_lindex = lindex;
|
|
||||||
assert(data->_lindex >= 0);
|
|
||||||
data->_str = s ? _strdup(s) : NULL;
|
|
||||||
data->_refcount = 1;
|
data->_refcount = 1;
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
#include <config.h>
|
||||||
|
#include "order.h"
|
||||||
|
|
||||||
|
#include <util/parser.h>
|
||||||
|
#include <util/language.h>
|
||||||
|
|
||||||
|
#include <CuTest.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void test_create_order(CuTest *tc) {
|
||||||
|
char cmd[32];
|
||||||
|
order *ord;
|
||||||
|
struct locale * lang = get_or_create_locale("en");
|
||||||
|
|
||||||
|
locale_setstring(lang, "keyword::move", "MOVE");
|
||||||
|
ord = create_order(K_MOVE, lang, "NORTH");
|
||||||
|
CuAssertPtrNotNull(tc, ord);
|
||||||
|
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
|
||||||
|
init_tokens(ord);
|
||||||
|
CuAssertStrEquals(tc, "MOVE NORTH", get_command(ord, cmd, sizeof(cmd)));
|
||||||
|
CuAssertStrEquals(tc, "MOVE", getstrtoken());
|
||||||
|
CuAssertStrEquals(tc, "NORTH", getstrtoken());
|
||||||
|
free_order(ord);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_parse_order(CuTest *tc) {
|
||||||
|
char cmd[32];
|
||||||
|
order *ord;
|
||||||
|
struct locale * lang = get_or_create_locale("en");
|
||||||
|
|
||||||
|
locale_setstring(lang, "keyword::move", "MOVE");
|
||||||
|
ord = parse_order("MOVE NORTH", lang);
|
||||||
|
CuAssertPtrNotNull(tc, ord);
|
||||||
|
CuAssertIntEquals(tc, K_MOVE, getkeyword(ord));
|
||||||
|
init_tokens(ord);
|
||||||
|
CuAssertStrEquals(tc, "MOVE NORTH", get_command(ord, cmd, sizeof(cmd)));
|
||||||
|
CuAssertStrEquals(tc, "MOVE", getstrtoken());
|
||||||
|
CuAssertStrEquals(tc, "NORTH", getstrtoken());
|
||||||
|
free_order(ord);
|
||||||
|
}
|
||||||
|
|
||||||
|
CuSuite *get_order_suite(void)
|
||||||
|
{
|
||||||
|
CuSuite *suite = CuSuiteNew();
|
||||||
|
SUITE_ADD_TEST(suite, test_create_order);
|
||||||
|
SUITE_ADD_TEST(suite, test_parse_order);
|
||||||
|
return suite;
|
||||||
|
}
|
|
@ -9,36 +9,6 @@
|
||||||
CuSuite *get_##name##_suite(void); \
|
CuSuite *get_##name##_suite(void); \
|
||||||
CuSuiteAddSuite(suite, get_##name##_suite())
|
CuSuiteAddSuite(suite, get_##name##_suite())
|
||||||
|
|
||||||
CuSuite *get_tests_suite(void);
|
|
||||||
CuSuite *get_callback_suite(void);
|
|
||||||
CuSuite *get_jsonconf_suite(void);
|
|
||||||
CuSuite *get_json_suite(void);
|
|
||||||
CuSuite *get_economy_suite(void);
|
|
||||||
CuSuite *get_laws_suite(void);
|
|
||||||
CuSuite *get_market_suite(void);
|
|
||||||
CuSuite *get_battle_suite(void);
|
|
||||||
CuSuite *get_building_suite(void);
|
|
||||||
CuSuite *get_curse_suite(void);
|
|
||||||
CuSuite *get_equipment_suite(void);
|
|
||||||
CuSuite *get_item_suite(void);
|
|
||||||
CuSuite *get_magic_suite(void);
|
|
||||||
CuSuite *get_move_suite(void);
|
|
||||||
CuSuite *get_pool_suite(void);
|
|
||||||
CuSuite *get_build_suite(void);
|
|
||||||
CuSuite *get_reports_suite(void);
|
|
||||||
CuSuite *get_ship_suite(void);
|
|
||||||
CuSuite *get_spellbook_suite(void);
|
|
||||||
CuSuite *get_spell_suite(void);
|
|
||||||
CuSuite *get_base36_suite(void);
|
|
||||||
CuSuite *get_config_suite(void);
|
|
||||||
CuSuite *get_bsdstring_suite(void);
|
|
||||||
CuSuite *get_functions_suite(void);
|
|
||||||
CuSuite *get_umlaut_suite(void);
|
|
||||||
CuSuite *get_ally_suite(void);
|
|
||||||
CuSuite *get_direction_suite(void);
|
|
||||||
CuSuite *get_skill_suite(void);
|
|
||||||
CuSuite *get_keyword_suite(void);
|
|
||||||
|
|
||||||
int RunAllTests(void)
|
int RunAllTests(void)
|
||||||
{
|
{
|
||||||
CuString *output = CuStringNew();
|
CuString *output = CuStringNew();
|
||||||
|
@ -56,6 +26,7 @@ int RunAllTests(void)
|
||||||
ADD_TESTS(suite, direction);
|
ADD_TESTS(suite, direction);
|
||||||
ADD_TESTS(suite, skill);
|
ADD_TESTS(suite, skill);
|
||||||
ADD_TESTS(suite, keyword);
|
ADD_TESTS(suite, keyword);
|
||||||
|
ADD_TESTS(suite, order);
|
||||||
/* util */
|
/* util */
|
||||||
ADD_TESTS(suite, config);
|
ADD_TESTS(suite, config);
|
||||||
ADD_TESTS(suite, base36);
|
ADD_TESTS(suite, base36);
|
||||||
|
|
Loading…
Reference in New Issue