forked from github/server
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:
parent
a91e4c6e71
commit
5bc4f7f144
51
src/report.c
51
src/report.c
|
@ -1344,18 +1344,11 @@ 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) {
|
||||
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)) {
|
||||
|
@ -1364,14 +1357,34 @@ static void durchreisende(stream *out, const region * r, const faction * f)
|
|||
}
|
||||
}
|
||||
}
|
||||
return maxtravel;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
assert(r);
|
||||
assert(f);
|
||||
if (!fval(r, RF_TRAVELUNIT)) {
|
||||
return;
|
||||
}
|
||||
CHECK_ERRNO();
|
||||
abegin = a_find(r->attribs, &at_travelunit);
|
||||
|
||||
/* How many are we listing? For grammar. */
|
||||
maxtravel = count_travelthru(r, f, abegin);
|
||||
if (maxtravel == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Auflisten. */
|
||||
newline(out);
|
||||
|
||||
for (a = abegin; a && a->type == &at_travelunit; a = a->next) {
|
||||
unit *u = (unit *)a->data.v;
|
||||
|
||||
|
@ -1384,6 +1397,7 @@ static void durchreisende(stream *out, const region * r, const faction * f)
|
|||
else {
|
||||
bytes = (int)strlcpy(bufp, unitname(u), size);
|
||||
}
|
||||
CHECK_ERRNO();
|
||||
if (wrptr(&bufp, &size, bytes) != 0) {
|
||||
INFO_STATIC_BUFFER();
|
||||
break;
|
||||
|
@ -1391,6 +1405,7 @@ static void durchreisende(stream *out, const region * r, const faction * f)
|
|||
|
||||
if (counter + 1 < maxtravel) {
|
||||
bytes = (int)strlcpy(bufp, ", ", size);
|
||||
CHECK_ERRNO();
|
||||
if (wrptr(&bufp, &size, bytes) != 0) {
|
||||
INFO_STATIC_BUFFER();
|
||||
break;
|
||||
|
@ -1398,6 +1413,7 @@ static void durchreisende(stream *out, const region * r, const faction * f)
|
|||
}
|
||||
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;
|
||||
|
@ -1416,13 +1432,12 @@ static void durchreisende(stream *out, const region * r, const faction * f)
|
|||
}
|
||||
CHECK_ERRNO();
|
||||
if (wrptr(&bufp, &size, bytes) != 0)
|
||||
WARN_STATIC_BUFFER_EX("durchreisende");
|
||||
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 */
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 *
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue