From c8c1d8c27d1d2bd14a6ef5bf54319b90207df25c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 19 May 2012 13:10:19 -0700 Subject: [PATCH 1/3] fix an odd edge-case where there is only a byte left in the destination string, but we're parsing a utf8 character. --- src/util/umlaut.c | 7 +++---- src/util/umlaut_test.c | 9 ++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/util/umlaut.c b/src/util/umlaut.c index d1063b6d4..81f356a14 100644 --- a/src/util/umlaut.c +++ b/src/util/umlaut.c @@ -57,8 +57,8 @@ char * transliterate(char * out, size_t size, const char * in) } len = src-p; size -= len; - while (size>=2 && *src && (*src & 0x80)) { - int advance = 2; + while (size>0 && *src && (*src & 0x80)) { + unsigned int advance = 2; if (src[0]=='\xc3') { if (src[1]=='\xa4' || src[1]=='\x84') { memcpy(dst, "ae", 2); @@ -69,7 +69,6 @@ char * transliterate(char * out, size_t size, const char * in) } else if (src[1]=='\x9f') { memcpy(dst, "ss", 2); } else { - *dst++='?'; advance = 0; } } else if (src[0]=='\xe1') { @@ -83,7 +82,7 @@ char * transliterate(char * out, size_t size, const char * in) advance = 0; } - if (advance) { + if (advance && advance<=size) { src+=advance; dst+=advance; size-=advance; diff --git a/src/util/umlaut_test.c b/src/util/umlaut_test.c index 2b2b4e4e5..518c63930 100644 --- a/src/util/umlaut_test.c +++ b/src/util/umlaut_test.c @@ -16,7 +16,14 @@ static void test_transliterate(CuTest * tc) CuAssertStrEquals(tc, "haerpdaerp", transliterate(buffer, sizeof(buffer), "h\xc3\xa4rpd\xc3\xa4rp")); CuAssertStrEquals(tc, "aeoeuess", transliterate(buffer, sizeof(buffer), "\xc3\xa4\xc3\xb6\xc3\xbc\xc3\x9f")); CuAssertStrEquals(tc, "aeoeuess", transliterate(buffer, sizeof(buffer), "\xc3\x84\xc3\x96\xc3\x9c\xe1\xba\x9e")); - CuAssertStrEquals(tc, 0, transliterate(buffer, 4, "herpderp")); + + /* handle buffer that is too small */ + CuAssertStrEquals(tc, 0, transliterate(buffer, 1, "herpderp")); + CuAssertStrEquals(tc, "", buffer); + CuAssertStrEquals(tc, 0, transliterate(buffer, 3, "herpderp")); + CuAssertStrEquals(tc, "he", buffer); + CuAssertStrEquals(tc, 0, transliterate(buffer, 3, "h\xc3\xa4rpd\xc3\xa4rp")); + CuAssertStrEquals(tc, "h?", buffer); } static void test_umlaut(CuTest * tc) From b57db72396a37df7345ca882498e201b53c5c208 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 19 May 2012 20:49:47 +0200 Subject: [PATCH 2/3] fix broken tests and clear locales (badly) when clearing global stte --- src/kernel/item_test.c | 30 ++++++++++++++++++++++++++++++ src/tests.c | 10 ++++++++-- src/tests_test.c | 30 ++++++++++++++++++++++++++++++ src/util/language.c | 8 +++++++- 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/kernel/item_test.c create mode 100644 src/tests_test.c diff --git a/src/kernel/item_test.c b/src/kernel/item_test.c new file mode 100644 index 000000000..f12845ab4 --- /dev/null +++ b/src/kernel/item_test.c @@ -0,0 +1,30 @@ +#include + +#include + +#include +#include + +void test_resource_type(CuTest * tc) +{ + resource_type *rtype; + const char *names[2] = { 0 , 0 }; + + CuAssertPtrEquals(tc, 0, rt_find("herpderp")); + + names[0] = names[1] = "herpderp"; + new_resourcetype(names, NULL, RTF_NONE); + names[0] = names[1] = "herp"; + rtype = new_resourcetype(names, NULL, RTF_NONE); + names[0] = names[1] = "herpes"; + new_resourcetype(names, NULL, RTF_NONE); + + CuAssertPtrEquals(tc, rtype, rt_find("herp")); +} + +CuSuite *get_item_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_resource_type); + return suite; +} diff --git a/src/tests.c b/src/tests.c index 193f3553a..f0d88806f 100644 --- a/src/tests.c +++ b/src/tests.c @@ -100,6 +100,8 @@ void test_cleanup(void) test_clear_resources(); global.functions.maintenance = NULL; global.functions.wage = NULL; + default_locale = 0; + locales = 0; /* TODO: this is evil and leaky */ free_gamedata(); } @@ -155,9 +157,13 @@ void test_create_world(void) building_type *btype; ship_type *stype; item_type * itype; - const char * names[2] = { "horse", "horse_p" }; + const char * horses[2] = { "horse", "horse_p" }; - itype = test_create_itemtype(names); + make_locale("de"); + init_resources(); + assert(!olditemtype[I_HORSE]); + + itype = test_create_itemtype(horses); olditemtype[I_HORSE] = itype; t_plain = test_create_terrain("plain", LAND_REGION | FOREST_REGION | WALK_INTO | CAVALRY_REGION); diff --git a/src/tests_test.c b/src/tests_test.c new file mode 100644 index 000000000..3f8a65513 --- /dev/null +++ b/src/tests_test.c @@ -0,0 +1,30 @@ +#include +#include + +#include + +static void test_recreate_world(CuTest * tc) +{ + test_cleanup(); + CuAssertPtrEquals(tc, 0, find_locale("de")); + CuAssertPtrEquals(tc, 0, it_find("money")); + CuAssertPtrEquals(tc, 0, it_find("horse")); + test_create_world(); + CuAssertPtrEquals(tc, default_locale, find_locale("de")); + CuAssertPtrNotNull(tc, default_locale); + CuAssertPtrNotNull(tc, it_find("money")); + CuAssertPtrNotNull(tc, it_find("horse")); + CuAssertPtrNotNull(tc, findregion(0, 0)); + test_cleanup(); + CuAssertPtrEquals(tc, 0, find_locale("de")); + CuAssertPtrEquals(tc, 0, it_find("money")); + CuAssertPtrEquals(tc, 0, it_find("horse")); + CuAssertPtrEquals(tc, 0, findregion(0, 0)); +} + +CuSuite *get_tests_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_recreate_world); + return suite; +} diff --git a/src/util/language.c b/src/util/language.c index 9bb36cd2d..2af939234 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -57,6 +57,10 @@ locale *make_locale(const char *name) locale *l = (locale *) calloc(sizeof(locale), 1); locale **lp = &locales; + if (!locales) { + nextlocaleindex = 0; + } + while (*lp && (*lp)->hashkey != hkey) lp = &(*lp)->next; if (*lp) { @@ -164,8 +168,10 @@ void locale_setstring(locale * lang, const char *key, const char *value) unsigned int hkey = hashstring(key); unsigned int id = hkey & (SMAXHASH - 1); struct locale_str *find; - if (lang == NULL) + if (!lang) { lang = default_locale; + } + assert(lang); find = lang->strings[id]; while (find) { if (find->hashkey == hkey && strcmp(key, find->key) == 0) From ee2709e021339a5a93c7e8d5439c1e700c71395b Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 19 May 2012 22:30:57 +0200 Subject: [PATCH 3/3] fix tests --- src/gamecode/market_test.c | 37 +++++++++++++++++++++++-------------- src/kernel/item.c | 4 ++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/gamecode/market_test.c b/src/gamecode/market_test.c index 1e7eefb8e..985538368 100644 --- a/src/gamecode/market_test.c +++ b/src/gamecode/market_test.c @@ -18,7 +18,7 @@ #include -static void market_curse(CuTest * tc) +static void test_market_curse(CuTest * tc) { region *r; building *b; @@ -26,16 +26,21 @@ static void market_curse(CuTest * tc) faction *f; int x, y; const char *names[4] = { "herb", "herbs", "balm", "balms" }; - terrain_type *terrain; - resource_type *hres = new_resourcetype(names, 0, RTF_ITEM | RTF_POOLED); - item_type *htype = new_itemtype(hres, ITF_HERB, 0, 0); - resource_type *lres = new_resourcetype(names + 2, 0, RTF_ITEM | RTF_POOLED); - item_type *ltype = new_itemtype(lres, ITF_NONE, 0, 0); - luxury_type *lux = new_luxurytype(ltype, 0); + const terrain_type *terrain; + resource_type *hres, *lres; + item_type *htype, *ltype; + luxury_type *lux; building_type *btype; - race *rc = rc_add(rc_new("human")); free_gamedata(); + test_cleanup(); + test_create_world(); + + hres = new_resourcetype(names, 0, RTF_ITEM | RTF_POOLED); + htype = new_itemtype(hres, ITF_HERB, 0, 0); + lres = new_resourcetype(names + 2, 0, RTF_ITEM | RTF_POOLED); + ltype = new_itemtype(lres, ITF_NONE, 0, 0); + lux = new_luxurytype(ltype, 0); set_param(&global.parameters, "rules.region_owners", "1"); @@ -43,12 +48,16 @@ static void market_curse(CuTest * tc) btype->_name = "market"; bt_register(btype); - terrain = test_create_terrain("plain", LAND_REGION | WALK_INTO); + terrain = get_terrain("plain"); for (x = 0; x != 3; ++x) { for (y = 0; y != 3; ++y) { - r = new_region(x, y, NULL, 0); - terraform_region(r, terrain); + r = findregion(x, y); + if (!r) { + r = test_create_region(x, y, terrain); + } else { + terraform_region(r, terrain); + } rsetpeasants(r, 5000); r_setdemand(r, lux, 0); rsetherbtype(r, htype); @@ -59,8 +68,8 @@ static void market_curse(CuTest * tc) b->flags |= BLD_WORKING; b->size = b->type->maxsize; - f = addfaction("nobody@eressea.de", NULL, rc, default_locale, 0); - u = create_unit(r, f, 1, f->race, 0, 0, 0); + f = test_create_faction(0); + u = test_create_unit(f, r); u_set_building(u, b); do_markets(); @@ -72,6 +81,6 @@ static void market_curse(CuTest * tc) CuSuite *get_market_suite(void) { CuSuite *suite = CuSuiteNew(); - SUITE_ADD_TEST(suite, market_curse); + SUITE_ADD_TEST(suite, test_market_curse); return suite; } diff --git a/src/kernel/item.c b/src/kernel/item.c index 387112f71..c06a603e1 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -1201,6 +1201,10 @@ void test_clear_resources(void) { int i; + memset((void *)olditemtype, 0, sizeof(olditemtype)); + memset((void *)oldresourcetype, 0, sizeof(oldresourcetype)); + memset((void *)oldpotiontype, 0, sizeof(oldpotiontype)); + for (i=0;i!=IMAXHASH;++i) { item_type * itype = itemtypes[i]; if (itype) {