From 5bc4f7f144055afa1a093716c7654b2c46532cc1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 18 Aug 2015 17:08:02 +0200 Subject: [PATCH] add some simple tests for writing travelthru information to the report, fix test that sets errno as a side effect (thanks, Microsoft!) --- src/report.c | 147 +++++++++++++++++++++----------------- src/report.h | 4 +- src/reports.test.c | 38 ++++++++++ src/tests.c | 5 ++ src/util/bsdstring.test.c | 2 + 5 files changed, 129 insertions(+), 67 deletions(-) diff --git a/src/report.c b/src/report.c index 7b8680ed8..ce71d78fa 100644 --- a/src/report.c +++ b/src/report.c @@ -183,7 +183,7 @@ char marker) str = x + 2; hanging_indent -= 2; } - } + } else { mark = ▮ } @@ -1344,84 +1344,99 @@ static void statistics(stream *out, const region * r, const faction * f) i_free(i_remove(&items, items)); } -static void durchreisende(stream *out, const region * r, const faction * f) -{ - if (fval(r, RF_TRAVELUNIT)) { - attrib *abegin = a_find(r->attribs, &at_travelunit), *a; - int counter = 0, maxtravel = 0; - char buf[8192]; - char *bufp = buf; - int bytes; - size_t size = sizeof(buf) - 1; - /* How many are we listing? For grammar. */ - for (a = abegin; a && a->type == &at_travelunit; a = a->next) { - unit *u = (unit *)a->data.v; +static int count_travelthru(const region *r, const faction *f, attrib *alist) { + int maxtravel = 0; + attrib *a; + for (a = alist; a && a->type == &at_travelunit; a = a->next) { + unit *u = (unit *)a->data.v; - if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) { - if (cansee_durchgezogen(f, r, u, 0)) { - ++maxtravel; - } + if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) { + if (cansee_durchgezogen(f, r, u, 0)) { + ++maxtravel; } } + } + return maxtravel; +} - if (maxtravel == 0) { - return; - } +void write_travelthru(stream *out, const region * r, const faction * f) +{ + attrib *abegin, *a; + int counter = 0, maxtravel = 0; + char buf[8192]; + char *bufp = buf; + int bytes; + size_t size = sizeof(buf) - 1; - /* Auflisten. */ - newline(out); + assert(r); + assert(f); + if (!fval(r, RF_TRAVELUNIT)) { + return; + } + CHECK_ERRNO(); + abegin = a_find(r->attribs, &at_travelunit); - for (a = abegin; a && a->type == &at_travelunit; a = a->next) { - unit *u = (unit *)a->data.v; + /* How many are we listing? For grammar. */ + maxtravel = count_travelthru(r, f, abegin); + if (maxtravel == 0) { + return; + } - if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) { - if (cansee_durchgezogen(f, r, u, 0)) { - ++counter; - if (u->ship != NULL) { - bytes = (int)strlcpy(bufp, shipname(u->ship), size); - } - else { - bytes = (int)strlcpy(bufp, unitname(u), size); - } + /* Auflisten. */ + newline(out); + for (a = abegin; a && a->type == &at_travelunit; a = a->next) { + unit *u = (unit *)a->data.v; + + if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) { + if (cansee_durchgezogen(f, r, u, 0)) { + ++counter; + if (u->ship != NULL) { + bytes = (int)strlcpy(bufp, shipname(u->ship), size); + } + else { + bytes = (int)strlcpy(bufp, unitname(u), size); + } + CHECK_ERRNO(); + if (wrptr(&bufp, &size, bytes) != 0) { + INFO_STATIC_BUFFER(); + break; + } + + if (counter + 1 < maxtravel) { + bytes = (int)strlcpy(bufp, ", ", size); + CHECK_ERRNO(); if (wrptr(&bufp, &size, bytes) != 0) { INFO_STATIC_BUFFER(); break; } - - if (counter + 1 < maxtravel) { - bytes = (int)strlcpy(bufp, ", ", size); - if (wrptr(&bufp, &size, bytes) != 0) { - INFO_STATIC_BUFFER(); - break; - } - } - else if (counter + 1 == maxtravel) { - bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size); - if (wrptr(&bufp, &size, bytes) != 0) { - INFO_STATIC_BUFFER(); - break; - } + } + else if (counter + 1 == maxtravel) { + bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size); + CHECK_ERRNO(); + if (wrptr(&bufp, &size, bytes) != 0) { + INFO_STATIC_BUFFER(); + break; } } } } - if (size > 0) { - CHECK_ERRNO(); - if (maxtravel == 1) { - bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_one")); - } - else { - bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_many")); - } - CHECK_ERRNO(); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER_EX("durchreisende"); - CHECK_ERRNO(); - } - *bufp = 0; - paragraph(out, buf, 0, 0, 0); } + if (size > 0) { + CHECK_ERRNO(); + if (maxtravel == 1) { + bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_one")); + } + else { + bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_many")); + } + CHECK_ERRNO(); + if (wrptr(&bufp, &size, bytes) != 0) + WARN_STATIC_BUFFER_EX("write_travelthru"); + CHECK_ERRNO(); + } + *bufp = 0; + paragraph(out, buf, 0, 0, 0); } static int buildingmaintenance(const building * b, const resource_type * rtype) @@ -2325,21 +2340,21 @@ const char *charset) } } guards(out, r, f); - durchreisende(out, r, f); + write_travelthru(out, r, f); } else { if (sr->mode == see_far) { describe(out, sr, f); guards(out, r, f); - durchreisende(out, r, f); + write_travelthru(out, r, f); } else if (sr->mode == see_lighthouse) { describe(out, sr, f); - durchreisende(out, r, f); + write_travelthru(out, r, f); } else { describe(out, sr, f); - durchreisende(out, r, f); + write_travelthru(out, r, f); } } /* Statistik */ diff --git a/src/report.h b/src/report.h index 189a38f0f..4a2299969 100644 --- a/src/report.h +++ b/src/report.h @@ -16,10 +16,12 @@ extern "C" { #endif struct stream; + struct region; + struct faction; void register_nr(void); void report_cleanup(void); void write_spaces(struct stream *out, size_t num); - + void write_travelthru(struct stream *out, const struct region * r, const struct faction * f); #ifdef __cplusplus } #endif diff --git a/src/reports.test.c b/src/reports.test.c index 89bca6e05..780d69b4d 100644 --- a/src/reports.test.c +++ b/src/reports.test.c @@ -3,6 +3,7 @@ #include "reports.h" #include "report.h" #include "creport.h" +#include "move.h" #include #include @@ -178,6 +179,42 @@ static void test_cr_unit(CuTest *tc) { test_cleanup(); } +static void test_write_travelthru(CuTest *tc) { + stream out = { 0 }; + char buf[1024]; + size_t len; + region *r; + faction *f; + unit *u; + + test_cleanup(); + mstream_init(&out); + r = test_create_region(0, 0, 0); + r->flags |= RF_TRAVELUNIT; + f = test_create_faction(0); + u = test_create_unit(f, 0); + + write_travelthru(&out, r, f); + out.api->rewind(out.handle); + len = out.api->read(out.handle, buf, sizeof(buf)); + CuAssertIntEquals_Msg(tc, "no travelers, no report", 0, (int)len); + + travelthru(u, r); + out.api->rewind(out.handle); + write_travelthru(&out, r, f); + len = out.api->read(out.handle, buf, sizeof(buf)); + CuAssertIntEquals_Msg(tc, "report units that moved through", 0, (int)len); + + move_unit(u, r, 0); + out.api->rewind(out.handle); + write_travelthru(&out, r, f); + len = out.api->read(out.handle, buf, sizeof(buf)); + CuAssertPtrNotNull(tc, strstr(buf, unitname(u))); + + mstream_done(&out); + test_cleanup(); +} + CuSuite *get_reports_suite(void) { CuSuite *suite = CuSuiteNew(); @@ -188,5 +225,6 @@ CuSuite *get_reports_suite(void) SUITE_ADD_TEST(suite, test_write_spaces); SUITE_ADD_TEST(suite, test_write_many_spaces); SUITE_ADD_TEST(suite, test_sparagraph); + SUITE_ADD_TEST(suite, test_write_travelthru); return suite; } diff --git a/src/tests.c b/src/tests.c index 028fb888d..2f4dae59c 100644 --- a/src/tests.c +++ b/src/tests.c @@ -85,6 +85,11 @@ void test_cleanup(void) mt_register(mt_new_va("missing_message", "name:string", 0)); mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0)); } + if (errno) { + int error = errno; + errno = 0; + log_error("errno: %d", error); + } } terrain_type * diff --git a/src/util/bsdstring.test.c b/src/util/bsdstring.test.c index 6fcb86515..0fffd187c 100644 --- a/src/util/bsdstring.test.c +++ b/src/util/bsdstring.test.c @@ -1,5 +1,6 @@ #include #include "bsdstring.h" +#include #include static void test_strlcat(CuTest * tc) @@ -38,6 +39,7 @@ static void test_strlcpy(CuTest * tc) CuAssertIntEquals(tc, 8, (int)strlcpy(buffer, "herpderp", 8)); CuAssertStrEquals(tc, "herpder", buffer); CuAssertIntEquals(tc, 0x7f, buffer[8]); + errno = 0; } static void test_slprintf(CuTest * tc)