From 3a03579a65ee6ac511d889900505a3cea4d140d5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 19 May 2015 08:02:32 +0200 Subject: [PATCH 1/6] rename set_origin/get_origin into faction.c, remove argument from adjust_coordinates. add test for bug 2070. --- conf/e4/config.xml | 2 +- src/bind_faction.c | 4 ++-- src/creport.c | 10 +++++----- src/kernel/faction.c | 37 ++++++++++++++++++++++++++++++++++++- src/kernel/faction.h | 3 +++ src/kernel/faction.test.c | 27 ++++++++++++++++++++++++++- src/kernel/plane.c | 37 +++++++++---------------------------- src/kernel/plane.h | 4 +--- src/kernel/region.c | 2 +- src/kernel/save.c | 2 +- src/laws.c | 4 ++-- src/report.c | 2 +- src/reports.c | 2 +- 13 files changed, 89 insertions(+), 47 deletions(-) diff --git a/conf/e4/config.xml b/conf/e4/config.xml index cdc3bb3e6..adf355157 100644 --- a/conf/e4/config.xml +++ b/conf/e4/config.xml @@ -58,7 +58,7 @@ - + diff --git a/src/bind_faction.c b/src/bind_faction.c index 010bd0f12..1846ea957 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -282,7 +282,7 @@ static int tolua_faction_normalize(lua_State * L) plane *pl = rplane(r); int nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); tolua_pushnumber(L, (lua_Number)nx); tolua_pushnumber(L, (lua_Number)ny); return 2; @@ -297,7 +297,7 @@ static int tolua_faction_set_origin(lua_State * L) plane *pl = rplane(r); int id = pl ? pl->id : 0; - set_origin(f, id, r->x - plane_center_x(pl), r->y - plane_center_y(pl)); + faction_setorigin(f, id, r->x - plane_center_x(pl), r->y - plane_center_y(pl)); return 0; } diff --git a/src/creport.c b/src/creport.c index abe528a1d..890513c8a 100644 --- a/src/creport.c +++ b/src/creport.c @@ -317,7 +317,7 @@ static int cr_region(variant var, char *buffer, const void *userdata) plane *pl = rplane(r); int nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(report, &nx, &ny, pl, r); + adjust_coordinates(report, &nx, &ny, pl); sprintf(buffer, "%d %d %d", nx, ny, plane_id(pl)); return 0; } @@ -435,7 +435,7 @@ static int cr_regions(variant var, char *buffer, const void *userdata) int nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); wp += sprintf(wp, "\"%d %d %d", nx, ny, z); for (i = 1; i != rdata->nregions; ++i) { r = rdata->regions[i]; @@ -1257,7 +1257,7 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr) else { nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); } if (pl) { @@ -1398,7 +1398,7 @@ static void cr_output_region(FILE * F, report_context * ctx, seen_region * sr) plane *plx = rplane(r); pnormalize(&nx, &ny, plx); - adjust_coordinates(f, &nx, &ny, plx, r); + adjust_coordinates(f, &nx, &ny, plx); fprintf(F, "SCHEMEN %d %d\n", nx, ny); fprintf(F, "\"%s\";Name\n", rname(r, f->locale)); rl2 = rl2->next; @@ -1623,7 +1623,7 @@ report_computer(const char *filename, report_context * ctx, const char *charset) int nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); if (!plid) fprintf(F, "BATTLE %d %d\n", nx, ny); else { diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 72643c3cb..c679951b4 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -257,7 +257,7 @@ unit *addplayer(region * r, faction * f) char buffer[32]; assert(f->units == NULL); - set_origin(f, 0, r->x, r->y); + faction_setorigin(f, 0, r->x, r->y); u = create_unit(r, f, 1, f->race, 0, NULL, NULL); equip_items(&u->faction->items, get_equipment("new_faction")); equip_unit(u, get_equipment("first_unit")); @@ -657,3 +657,38 @@ void remove_empty_factions(void) fp = &(*fp)->next; } } + +void faction_getorigin(const faction * f, int id, int *x, int *y) +{ + ursprung *ur; + + assert(f && x && y); + for (ur = f->ursprung; ur; ur = ur->next) { + if (ur->id == id) { + *x = ur->x; + *y = ur->y; + break; + } + } +} + +void faction_setorigin(faction * f, int id, int x, int y) +{ + ursprung *ur; + assert(f != NULL); + for (ur = f->ursprung; ur; ur = ur->next) { + if (ur->id == id) { + ur->x = ur->x + x; + ur->y = ur->y + y; + return; + } + } + + ur = calloc(1, sizeof(ursprung)); + ur->id = id; + ur->x = x; + ur->y = y; + + addlist(&f->ursprung, ur); +} + diff --git a/src/kernel/faction.h b/src/kernel/faction.h index a9482bedd..db8ce4389 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -154,6 +154,9 @@ extern "C" { void faction_setpassword(struct faction *self, const char *password); bool valid_race(const struct faction *f, const struct race *rc); + void faction_getorigin(const struct faction * f, int id, int *x, int *y); + void faction_setorigin(struct faction * f, int id, int x, int y); + struct spellbook * faction_get_spellbook(struct faction *f); /* skills */ diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 4852b08dc..5317b4c04 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -112,15 +112,39 @@ static void test_get_monsters(CuTest *tc) { static void test_set_origin(CuTest *tc) { faction *f; + int x = 0, y = 0; test_cleanup(); test_create_world(); f = test_create_faction(0); CuAssertPtrEquals(tc, 0, f->ursprung); - set_origin(f, 0, 1, 1); + faction_setorigin(f, 0, 1, 1); CuAssertIntEquals(tc, 0, f->ursprung->id); CuAssertIntEquals(tc, 1, f->ursprung->x); CuAssertIntEquals(tc, 1, f->ursprung->y); + faction_getorigin(f, 0, &x, &y); + CuAssertIntEquals(tc, 1, x); + CuAssertIntEquals(tc, 1, y); + test_cleanup(); +} + +static void test_set_origin_bug(CuTest *tc) { + faction *f; + region *r; + plane *pl; + int x = 17, y = 10; + + test_cleanup(); + test_create_world(); + r = test_create_region(x, y, 0); + pl = create_new_plane(0, "", 0, 19, 0, 19, 0); + f = test_create_faction(0); + faction_setorigin(f, 0, -10, 3); + faction_setorigin(f, 0, -13, -4); + adjust_coordinates(f, &x, &y, pl); + CuAssertIntEquals(tc, 0, f->ursprung->id); + CuAssertIntEquals(tc, -9, x); + CuAssertIntEquals(tc, 2, y); test_cleanup(); } @@ -133,5 +157,6 @@ CuSuite *get_faction_suite(void) SUITE_ADD_TEST(suite, test_remove_dead_factions); SUITE_ADD_TEST(suite, test_get_monsters); SUITE_ADD_TEST(suite, test_set_origin); + SUITE_ADD_TEST(suite, test_set_origin_bug); return suite; } diff --git a/src/kernel/plane.c b/src/kernel/plane.c index f610a4a64..4ae3d5016 100644 --- a/src/kernel/plane.c +++ b/src/kernel/plane.c @@ -136,7 +136,7 @@ ursprung_x(const faction * f, const plane * pl, const region * rdefault) } if (!rdefault) return 0; - set_origin((faction *)f, id, rdefault->x - plane_center_x(pl), + faction_setorigin((faction *)f, id, rdefault->x - plane_center_x(pl), rdefault->y - plane_center_y(pl)); return rdefault->x - plane_center_x(pl); } @@ -159,7 +159,7 @@ ursprung_y(const faction * f, const plane * pl, const region * rdefault) } if (!rdefault) return 0; - set_origin((faction *)f, id, rdefault->x - plane_center_x(pl), + faction_setorigin((faction *)f, id, rdefault->x - plane_center_x(pl), rdefault->y - plane_center_y(pl)); return rdefault->y - plane_center_y(pl); } @@ -181,14 +181,15 @@ int plane_center_y(const plane * pl) } void -adjust_coordinates(const faction * f, int *x, int *y, const plane * pl, -const region * r) +adjust_coordinates(const faction * f, int *x, int *y, const plane * pl) { int nx = *x; int ny = *y; if (f) { - nx -= ursprung_x(f, pl, r); - ny -= ursprung_y(f, pl, r); + int ux, uy; + faction_getorigin(f, pl?pl->id:0, &ux, &uy); + nx -= ux; + ny -= uy; } if (pl) { int plx = plane_center_x(pl); @@ -198,8 +199,8 @@ const region * r) int width_2 = width / 2; int height_2 = height / 2; - nx -= plx; - ny -= ply; + nx = (nx - plx) % width; + ny = (ny - ply) % height; if (nx < 0) nx = (width - (-nx) % width); @@ -221,26 +222,6 @@ const region * r) *y = ny; } -void set_origin(faction * f, int id, int x, int y) -{ - ursprung *ur; - assert(f != NULL); - for (ur = f->ursprung; ur; ur = ur->next) { - if (ur->id == id) { - ur->x = ur->x + x; - ur->y = ur->y + y; - return; - } - } - - ur = calloc(1, sizeof(ursprung)); - ur->id = id; - ur->x = x; - ur->y = y; - - addlist(&f->ursprung, ur); -} - plane *create_new_plane(int id, const char *name, int minx, int maxx, int miny, int maxy, int flags) { diff --git a/src/kernel/plane.h b/src/kernel/plane.h index 76c10fdca..2426c6a1a 100644 --- a/src/kernel/plane.h +++ b/src/kernel/plane.h @@ -70,7 +70,6 @@ extern "C" { struct plane *getplanebyid(int id); int plane_center_x(const struct plane *pl); int plane_center_y(const struct plane *pl); - void set_origin(struct faction *f, int id, int x, int y); struct plane *create_new_plane(int id, const char *name, int minx, int maxx, int miny, int maxy, int flags); struct plane *getplanebyname(const char *); @@ -82,8 +81,7 @@ extern "C" { extern int read_plane_reference(plane ** pp, struct storage *store); extern int plane_width(const plane * pl); extern int plane_height(const plane * pl); - void adjust_coordinates(const struct faction *f, int *x, int *y, - const struct plane *pl, const struct region *r); + void adjust_coordinates(const struct faction *f, int *x, int *y, const struct plane *pl); #ifdef __cplusplus } #endif diff --git a/src/kernel/region.c b/src/kernel/region.c index 852a6f0cb..3656c8ef1 100644 --- a/src/kernel/region.c +++ b/src/kernel/region.c @@ -121,7 +121,7 @@ const char *write_regionname(const region * r, const faction * f, char *buffer, plane *pl = rplane(r); int nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); slprintf(buf, size, "%s (%d,%d)", rname(r, lang), nx, ny); } return buffer; diff --git a/src/kernel/save.c b/src/kernel/save.c index ad0c51f61..2aaf4f3b9 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1250,7 +1250,7 @@ faction *readfaction(struct gamedata * data) READ_INT(data->store, &id); READ_INT(data->store, &ux); READ_INT(data->store, &uy); - set_origin(f, id, ux, uy); + faction_setorigin(f, id, ux, uy); } f->newbies = 0; diff --git a/src/laws.c b/src/laws.c index abab90a12..16ead6fc2 100755 --- a/src/laws.c +++ b/src/laws.c @@ -2542,7 +2542,7 @@ int origin_cmd(unit * u, struct order *ord) px = (short)getint(); py = (short)getint(); - set_origin(u->faction, getplaneid(u->region), px, py); + faction_setorigin(u->faction, getplaneid(u->region), px, py); return 0; } @@ -4382,7 +4382,7 @@ void init_processor(void) p += 10; add_proc_order(p, K_QUIT, quit_cmd, 0, NULL); -// add_proc_order(p, K_URSPRUNG, origin_cmd, 0, NULL); + add_proc_order(p, K_URSPRUNG, origin_cmd, 0, NULL); add_proc_order(p, K_ALLY, ally_cmd, 0, NULL); add_proc_order(p, K_PREFIX, prefix_cmd, 0, NULL); add_proc_order(p, K_SETSTEALTH, setstealth_cmd, 0, NULL); diff --git a/src/report.c b/src/report.c index e62adb7e8..fe3aec1de 100644 --- a/src/report.c +++ b/src/report.c @@ -1565,7 +1565,7 @@ report_template(const char *filename, report_context * ctx, const char *charset) int nx = r->x, ny = r->y; pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); newline(out); if (pl && pl->id != 0) { sprintf(buf, "%s %d,%d,%d ; %s", LOC(f->locale, diff --git a/src/reports.c b/src/reports.c index 80aa94fc0..8ec48c3e9 100644 --- a/src/reports.c +++ b/src/reports.c @@ -2001,7 +2001,7 @@ f_regionid(const region * r, const faction * f, char *buffer, size_t size) int nx = r->x, ny = r->y; int named = (name && name[0]); pnormalize(&nx, &ny, pl); - adjust_coordinates(f, &nx, &ny, pl, r); + adjust_coordinates(f, &nx, &ny, pl); len = strlcpy(buffer, rname(r, f ? f->locale : 0), size); _snprintf(buffer + len, size - len, " (%d,%d%s%s)", nx, ny, named ? "," : "", (named) ? name : ""); buffer[size - 1] = 0; From 250880f06794bfc46dc2b99b2fd2ce82d5d209bb Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 20 May 2015 07:19:50 +0200 Subject: [PATCH 2/6] fix gcc warning (unused variable) --- src/kernel/faction.test.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 5317b4c04..42e76d1a8 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -130,13 +130,11 @@ static void test_set_origin(CuTest *tc) { static void test_set_origin_bug(CuTest *tc) { faction *f; - region *r; plane *pl; int x = 17, y = 10; test_cleanup(); test_create_world(); - r = test_create_region(x, y, 0); pl = create_new_plane(0, "", 0, 19, 0, 19, 0); f = test_create_faction(0); faction_setorigin(f, 0, -10, 3); From 32298bd50dfcd35cb693e895f71ebfd89e52eb66 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 20 May 2015 07:30:59 +0200 Subject: [PATCH 3/6] gcc: do not error on warnings that Visual Studio doesn't know about. --- src/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 76b84182c..ed9d5c06f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -20,7 +20,8 @@ IF(CMAKE_COMPILER_IS_GNUCC) endif() SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wconversion -Wno-sign-conversion") - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -Wsign-compare -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -Wsign-compare -Wall -Werror -Wno-unknown-pragmas -Wstrict-prototypes -Wpointer-arith -Wno-char-subscripts -Wno-long-long") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=unused-but-set-variable") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -DHAVE__BOOL") elseif(MSVC) SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Wall /WX /MP") From 5af69f182183daf00e6bc929becdd328757d2d77 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 20 May 2015 08:15:12 +0200 Subject: [PATCH 4/6] re-enable test_peasant_luck_effect, add testing for adjust_coordinates --- src/kernel/faction.test.c | 8 ++++++++ src/laws.test.c | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 42e76d1a8..4b872f6bf 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -113,9 +113,11 @@ static void test_get_monsters(CuTest *tc) { static void test_set_origin(CuTest *tc) { faction *f; int x = 0, y = 0; + plane *pl; test_cleanup(); test_create_world(); + pl = create_new_plane(0, "", 0, 19, 0, 19, 0); f = test_create_faction(0); CuAssertPtrEquals(tc, 0, f->ursprung); faction_setorigin(f, 0, 1, 1); @@ -125,6 +127,12 @@ static void test_set_origin(CuTest *tc) { faction_getorigin(f, 0, &x, &y); CuAssertIntEquals(tc, 1, x); CuAssertIntEquals(tc, 1, y); + adjust_coordinates(f, &x, &y, pl); + CuAssertIntEquals(tc, -9, x); + CuAssertIntEquals(tc, -9, y); + adjust_coordinates(f, &x, &y, 0); + CuAssertIntEquals(tc, -10, x); + CuAssertIntEquals(tc, -10, y); test_cleanup(); } diff --git a/src/laws.test.c b/src/laws.test.c index 9391ad06f..a70f79b38 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -780,7 +780,7 @@ CuSuite *get_laws_suite(void) SUITE_ADD_TEST(suite, test_force_leave_buildings); SUITE_ADD_TEST(suite, test_force_leave_ships); SUITE_ADD_TEST(suite, test_force_leave_ships_on_ocean); - DISABLE_TEST(suite, test_peasant_luck_effect); + SUITE_ADD_TEST(suite, test_peasant_luck_effect); SUITE_ADD_TEST(suite, test_luck_message); return suite; From 465dcf4e1c11eb9ca52b02da3657dfa748c46c64 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 20 May 2015 18:05:25 +0200 Subject: [PATCH 5/6] bugfix sparagraph (github issue #199). adding tests. --- src/report.c | 53 ---------------------------------------------- src/reports.c | 53 +++++++++++++++++++++++++++++++++++++++++++++- src/reports.h | 1 + src/reports.test.c | 25 ++++++++++++++++++++++ 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/src/report.c b/src/report.c index e62adb7e8..b1f61a46c 100644 --- a/src/report.c +++ b/src/report.c @@ -510,59 +510,6 @@ static void nr_spell(stream *out, spellbook_entry * sbe, const struct locale *la newline(out); } -void sparagraph(strlist ** SP, const char *s, unsigned int indent, char mark) -{ - - /* Die Liste SP wird mit dem String s aufgefuellt, mit indent und einer - * mark, falls angegeben. SP wurde also auf 0 gesetzt vor dem Aufruf. - * Vgl. spunit (). */ - - unsigned int width; - int firstline; - static char buf[REPORTWIDTH + 1]; // FIXME: static return value - - width = REPORTWIDTH - indent; - firstline = 1; - - while (s[0]) { - unsigned int j = 0, i; - - for (i=0; s[j]; j=i) { - while (s[j] && s[j] != ' ') - j++; - if (j > width) { - - /* j zeigt auf das ende der aktuellen zeile, i zeigt auf den anfang der - * nächsten zeile. existiert ein wort am anfang der zeile, welches - * länger als eine zeile ist, muss dieses hier abgetrennt werden. */ - - if (i == 0) - i = width - 1; - break; - } - i = j + 1; - } - - for (j = 0; j != indent; j++) - buf[j] = ' '; - - if (firstline && mark) - buf[indent - 2] = mark; - - for (j = 0; j != i - 1; j++) - buf[indent + j] = s[j]; - buf[indent + j] = 0; - - addstrlist(SP, buf); - - if (s[i - 1] == 0) - break; - - s += i; - firstline = 0; - } -} - static void nr_curses_i(stream *out, int indent, const faction *viewer, objtype_t typ, const void *obj, attrib *a, int self) { diff --git a/src/reports.c b/src/reports.c index 80aa94fc0..ce4f2c025 100644 --- a/src/reports.c +++ b/src/reports.c @@ -956,9 +956,60 @@ const struct unit * u, struct skill * sv, int *dh, int days) return tsize; } -void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark) +void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned int width, char mark) { + /* Die Liste SP wird mit dem String s aufgefuellt, mit indent und einer + * mark, falls angegeben. SP wurde also auf 0 gesetzt vor dem Aufruf. + * Vgl. spunit (). */ + bool firstline; + static char buf[REPORTWIDTH + 1]; // FIXME: static buffer, artificial limit + size_t len = strlen(s); + + assert(width <= REPORTWIDTH); + width -= indent; + firstline = (mark!=0 && indent>2); + *SP = 0; + + while (len > 0) { + unsigned int j; + const char *cut = 0, *space = strchr(s, ' '); + while (space && *space && (space - s) <= (ptrdiff_t)width) { + cut = space; + space = strchr(space + 1, ' '); + if (!space && len < width) { + cut = space = s + len; + } + } + + for (j = 0; j != indent; j++) + buf[j] = ' '; + + if (firstline) { + buf[indent - 2] = mark; + firstline = false; + } + if (!cut) { + cut = s + _min(len, REPORTWIDTH); + } + strncpy(buf+indent, s, cut - s); + buf[indent + (cut - s)] = 0; + addstrlist(SP, buf); // TODO: too much string copying, cut out this function + while (*cut == ' ') { + ++cut; + } + len -= (cut - s); + s = cut; + } +} + +void sparagraph(strlist ** SP, const char *s, unsigned int indent, char mark) +{ + split_paragraph(SP, s, indent, REPORTWIDTH, mark); +} + +void lparagraph(struct strlist **SP, char *s, unsigned int indent, char mark) +{ /* Die Liste SP wird mit dem String s aufgefuellt, mit indent und einer * mark, falls angegeben. SP wurde also auf 0 gesetzt vor dem Aufruf. * Vgl. spunit (). */ diff --git a/src/reports.h b/src/reports.h index 2a33d47bd..93b3ab122 100644 --- a/src/reports.h +++ b/src/reports.h @@ -151,6 +151,7 @@ extern "C" { void addstrlist(strlist ** SP, const char *s); void freestrlist(strlist * s); + void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned int width, char mark); #define GR_PLURAL 0x01 /* grammar: plural */ diff --git a/src/reports.test.c b/src/reports.test.c index 29f7ae5aa..9dd9f0913 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -131,6 +131,30 @@ static void test_write_many_spaces(CuTest *tc) { mstream_done(&out); } +static void test_sparagraph(CuTest *tc) { + strlist *sp = 0; + split_paragraph(&sp, "Hello World", 0, 16, 0); + CuAssertPtrNotNull(tc, sp); + CuAssertStrEquals(tc, "Hello World", sp->s); + CuAssertPtrEquals(tc, 0, sp->next); + split_paragraph(&sp, "Hello World", 4, 16, 0); + CuAssertPtrNotNull(tc, sp); + CuAssertStrEquals(tc, " Hello World", sp->s); + CuAssertPtrEquals(tc, 0, sp->next); + split_paragraph(&sp, "Hello World", 4, 16, '*'); + CuAssertPtrNotNull(tc, sp); + CuAssertStrEquals(tc, " * Hello World", sp->s); + CuAssertPtrEquals(tc, 0, sp->next); + split_paragraph(&sp, "12345678 90 12345678", 0, 8, '*'); + CuAssertPtrNotNull(tc, sp); + CuAssertStrEquals(tc, "12345678", sp->s); + CuAssertPtrNotNull(tc, sp->next); + CuAssertStrEquals(tc, "90", sp->next->s); + CuAssertPtrNotNull(tc, sp->next->next); + CuAssertStrEquals(tc, "12345678", sp->next->next->s); + CuAssertPtrEquals(tc, 0, sp->next->next->next); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -139,5 +163,6 @@ CuSuite *get_reports_suite(void) SUITE_ADD_TEST(suite, test_regionid); SUITE_ADD_TEST(suite, test_write_spaces); SUITE_ADD_TEST(suite, test_write_many_spaces); + SUITE_ADD_TEST(suite, test_sparagraph); return suite; } From cf8828059504310be8a4c0b64b2dcdc9dafb06b8 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 21 May 2015 11:52:31 +0200 Subject: [PATCH 6/6] fix build_building tests, require an order argument. test current replace_order behavior. --- src/kernel/build.test.c | 9 ++++++--- src/kernel/order.c | 3 +++ src/kernel/order.test.c | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/kernel/build.test.c b/src/kernel/build.test.c index 3ee3cf056..2befe6733 100644 --- a/src/kernel/build.test.c +++ b/src/kernel/build.test.c @@ -204,7 +204,8 @@ static void test_build_building_no_materials(CuTest *tc) { btype = bt_find("castle"); assert(btype); set_level(u, SK_BUILDING, 1); - CuAssertIntEquals(tc, ENOMATERIALS, build_building(u, btype, 0, 4, 0)); + u->orders = create_order(K_MAKE, u->faction->locale, 0); + CuAssertIntEquals(tc, ENOMATERIALS, build_building(u, btype, 0, 4, u->orders)); CuAssertPtrEquals(tc, 0, u->region->buildings); CuAssertPtrEquals(tc, 0, u->building); test_cleanup(); @@ -222,7 +223,8 @@ static void test_build_building_with_golem(CuTest *tc) { assert(btype->construction); set_level(bf.u, SK_BUILDING, 1); - CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 1, 0)); + u->orders = create_order(K_MAKE, u->faction->locale, 0); + CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 1, u->orders)); CuAssertPtrNotNull(tc, u->region->buildings); CuAssertIntEquals(tc, 1, u->region->buildings->size); CuAssertIntEquals(tc, 0, u->number); @@ -245,7 +247,8 @@ static void test_build_building_success(CuTest *tc) { i_change(&bf.u->items, rtype->itype, 1); set_level(u, SK_BUILDING, 1); - CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 4, 0)); + u->orders = create_order(K_MAKE, u->faction->locale, 0); + CuAssertIntEquals(tc, 1, build_building(u, btype, 0, 4, u->orders)); CuAssertPtrNotNull(tc, u->region->buildings); CuAssertPtrEquals(tc, u->region->buildings, u->building); CuAssertIntEquals(tc, 1, u->building->size); diff --git a/src/kernel/order.c b/src/kernel/order.c index b72b8e6bd..20cce5853 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -65,6 +65,9 @@ static void release_data(order_data * data) void replace_order(order ** dlist, order * orig, const order * src) { + assert(src); + assert(orig); + assert(dlist); while (*dlist != NULL) { order *dst = *dlist; if (dst->data == orig->data) { diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c index 414dd87e7..6900da420 100644 --- a/src/kernel/order.test.c +++ b/src/kernel/order.test.c @@ -143,6 +143,21 @@ static void test_skip_token(CuTest *tc) { CuAssertStrEquals(tc, 0, getstrtoken()); } +static void test_replace_order(CuTest *tc) { + order *orders = 0, *orig, *repl; + struct locale * lang = get_or_create_locale("en"); + + orig = create_order(K_MAKE, lang, 0); + repl = create_order(K_ALLY, lang, 0); + replace_order(&orders, orig, repl); + CuAssertPtrEquals(tc, 0, orders); + orders = orig; + replace_order(&orders, orig, repl); + CuAssertPtrNotNull(tc, orders); + CuAssertPtrEquals(tc, 0, orders->next); + CuAssertIntEquals(tc, getkeyword(repl), getkeyword(orders)); +} + CuSuite *get_order_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -152,6 +167,7 @@ CuSuite *get_order_suite(void) SUITE_ADD_TEST(suite, test_parse_make_temp); SUITE_ADD_TEST(suite, test_parse_maketemp); SUITE_ADD_TEST(suite, test_init_order); + SUITE_ADD_TEST(suite, test_replace_order); SUITE_ADD_TEST(suite, test_skip_token); SUITE_ADD_TEST(suite, test_getstrtoken); return suite;