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

@ -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 */

View File

@ -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

View File

@ -3,6 +3,7 @@
#include "reports.h"
#include "report.h"
#include "creport.h"
#include "move.h"
#include <kernel/building.h>
#include <kernel/faction.h>
@ -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;
}

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_feedback", "unit:unit", "region:region", "command:order", "name:string", 0));
}
if (errno) {
int error = errno;
errno = 0;
log_error("errno: %d", error);
}
}
terrain_type *

View File

@ -1,5 +1,6 @@
#include <CuTest.h>
#include "bsdstring.h"
#include <errno.h>
#include <string.h>
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)