add some simple tests for writing travelthru information to the report, fix test that sets errno as a side effect (thanks, Microsoft!)

This commit is contained in:
Enno Rehling 2015-08-18 17:08:02 +02:00
parent a91e4c6e71
commit 5bc4f7f144
5 changed files with 129 additions and 67 deletions

View File

@ -1344,84 +1344,99 @@ static void statistics(stream *out, const region * r, const faction * f)
i_free(i_remove(&items, items)); 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. */ static int count_travelthru(const region *r, const faction *f, attrib *alist) {
for (a = abegin; a && a->type == &at_travelunit; a = a->next) { int maxtravel = 0;
unit *u = (unit *)a->data.v; 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 (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
if (cansee_durchgezogen(f, r, u, 0)) { if (cansee_durchgezogen(f, r, u, 0)) {
++maxtravel; ++maxtravel;
}
} }
} }
}
return maxtravel;
}
if (maxtravel == 0) { void write_travelthru(stream *out, const region * r, const faction * f)
return; {
} attrib *abegin, *a;
int counter = 0, maxtravel = 0;
char buf[8192];
char *bufp = buf;
int bytes;
size_t size = sizeof(buf) - 1;
/* Auflisten. */ assert(r);
newline(out); 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) { /* How many are we listing? For grammar. */
unit *u = (unit *)a->data.v; maxtravel = count_travelthru(r, f, abegin);
if (maxtravel == 0) {
return;
}
if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) { /* Auflisten. */
if (cansee_durchgezogen(f, r, u, 0)) { newline(out);
++counter; for (a = abegin; a && a->type == &at_travelunit; a = a->next) {
if (u->ship != NULL) { unit *u = (unit *)a->data.v;
bytes = (int)strlcpy(bufp, shipname(u->ship), size);
} if (r != u->region && (!u->ship || ship_owner(u->ship) == u)) {
else { if (cansee_durchgezogen(f, r, u, 0)) {
bytes = (int)strlcpy(bufp, unitname(u), size); ++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) { if (wrptr(&bufp, &size, bytes) != 0) {
INFO_STATIC_BUFFER(); INFO_STATIC_BUFFER();
break; break;
} }
}
if (counter + 1 < maxtravel) { else if (counter + 1 == maxtravel) {
bytes = (int)strlcpy(bufp, ", ", size); bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size);
if (wrptr(&bufp, &size, bytes) != 0) { CHECK_ERRNO();
INFO_STATIC_BUFFER(); if (wrptr(&bufp, &size, bytes) != 0) {
break; 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;
}
} }
} }
} }
} }
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) static int buildingmaintenance(const building * b, const resource_type * rtype)
@ -2325,21 +2340,21 @@ const char *charset)
} }
} }
guards(out, r, f); guards(out, r, f);
durchreisende(out, r, f); write_travelthru(out, r, f);
} }
else { else {
if (sr->mode == see_far) { if (sr->mode == see_far) {
describe(out, sr, f); describe(out, sr, f);
guards(out, r, f); guards(out, r, f);
durchreisende(out, r, f); write_travelthru(out, r, f);
} }
else if (sr->mode == see_lighthouse) { else if (sr->mode == see_lighthouse) {
describe(out, sr, f); describe(out, sr, f);
durchreisende(out, r, f); write_travelthru(out, r, f);
} }
else { else {
describe(out, sr, f); describe(out, sr, f);
durchreisende(out, r, f); write_travelthru(out, r, f);
} }
} }
/* Statistik */ /* Statistik */

View File

@ -16,10 +16,12 @@ extern "C" {
#endif #endif
struct stream; struct stream;
struct region;
struct faction;
void register_nr(void); void register_nr(void);
void report_cleanup(void); void report_cleanup(void);
void write_spaces(struct stream *out, size_t num); 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 #ifdef __cplusplus
} }
#endif #endif

View File

@ -3,6 +3,7 @@
#include "reports.h" #include "reports.h"
#include "report.h" #include "report.h"
#include "creport.h" #include "creport.h"
#include "move.h"
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/faction.h> #include <kernel/faction.h>
@ -178,6 +179,42 @@ static void test_cr_unit(CuTest *tc) {
test_cleanup(); 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 *get_reports_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); 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_spaces);
SUITE_ADD_TEST(suite, test_write_many_spaces); SUITE_ADD_TEST(suite, test_write_many_spaces);
SUITE_ADD_TEST(suite, test_sparagraph); SUITE_ADD_TEST(suite, test_sparagraph);
SUITE_ADD_TEST(suite, test_write_travelthru);
return suite; return suite;
} }

View File

@ -85,6 +85,11 @@ void test_cleanup(void)
mt_register(mt_new_va("missing_message", "name:string", 0)); 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)); 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 * terrain_type *

View File

@ -1,5 +1,6 @@
#include <CuTest.h> #include <CuTest.h>
#include "bsdstring.h" #include "bsdstring.h"
#include <errno.h>
#include <string.h> #include <string.h>
static void test_strlcat(CuTest * tc) static void test_strlcat(CuTest * tc)
@ -38,6 +39,7 @@ static void test_strlcpy(CuTest * tc)
CuAssertIntEquals(tc, 8, (int)strlcpy(buffer, "herpderp", 8)); CuAssertIntEquals(tc, 8, (int)strlcpy(buffer, "herpderp", 8));
CuAssertStrEquals(tc, "herpder", buffer); CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[8]); CuAssertIntEquals(tc, 0x7f, buffer[8]);
errno = 0;
} }
static void test_slprintf(CuTest * tc) static void test_slprintf(CuTest * tc)