static cJSON *export_a_region(CuTest * tc, const struct terrain_type *terrain, region **_r) { char buf[1024]; stream out = { 0 }; int err; region *r; cJSON *json, *attr; r = test_create_region(0, 0, terrain); mstream_init(&out); err = json_export(&out, EXPORT_REGIONS); CuAssertIntEquals(tc, 0, err); out.api->rewind(out.handle); out.api->read(out.handle, buf, sizeof(buf)); mstream_done(&out); json = cJSON_Parse(buf); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, cJSON_Object, json->type); CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "factions")); CuAssertPtrEquals(tc, 0, cJSON_GetObjectItem(json, "units")); CuAssertPtrNotNull(tc, json = cJSON_GetObjectItem(json, "regions")); CuAssertIntEquals(tc, cJSON_Object, json->type); json = json->child; CuAssertIntEquals(tc, r->uid, atoi(json->string)); CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(json, "x")); CuAssertIntEquals(tc, r->x, attr->valueint); CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(json, "y")); CuAssertIntEquals(tc, r->y, attr->valueint); CuAssertPtrNotNull(tc, attr = cJSON_GetObjectItem(json, "type")); CuAssertStrEquals(tc, terrain->_name, attr->valuestring); if (_r) *_r = r; return json; }
static void test_write_flag(CuTest *tc) { curse_fixture fix; storage store; char buf[1024]; stream out = { 0 }; size_t len; mstream_init(&out); binstore_init(&store, &out); store.handle.data = &out; setup_curse(&fix, "gbdream"); fix.c->flags = 42 | CURSE_ISNEW; curse_write(fix.r->attribs, fix.r, &store); out.api->rewind(out.handle); len = out.api->read(out.handle, buf, sizeof(buf)); buf[len] = '\0'; out.api->rewind(out.handle); curse_read(fix.r->attribs, fix.r, &store); CuAssertIntEquals(tc, 42 | CURSE_ISNEW, ((curse *) fix.r->attribs->data.v)->flags); global.data_version = RELEASE_VERSION; CuAssertIntEquals(tc, RELEASE_VERSION, global.data_version); mstream_done(&out); binstore_done(&store); test_cleanup(); }
static void test_export_no_regions(CuTest * tc) { char buf[1024]; stream out = { 0 }; int err; size_t len; mstream_init(&out); err = json_export(&out, EXPORT_REGIONS); CuAssertIntEquals(tc, 0, err); out.api->rewind(out.handle); len = out.api->read(out.handle, buf, sizeof(buf)); buf[len] = '\0'; CuAssertStrEquals(tc, "{}", strip(buf)); mstream_done(&out); test_cleanup(); }
static void test_group_readwrite(CuTest * tc) { faction * f; group *g; ally *al; int i; gamedata data; storage store; test_cleanup(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); f = test_create_faction(0); new_group(f, "NW", 42); g = new_group(f, "Egoisten", 43); key_set(&g->attribs, 44); al = ally_add(&g->allies, f); al->status = HELP_GIVE; write_groups(&store, f); WRITE_INT(&store, 47); free_group(f->groups); free_group(g); f->groups = 0; data.strm.api->rewind(data.strm.handle); read_groups(&data, f); READ_INT(&store, &i); mstream_done(&data.strm); gamedata_done(&data); CuAssertIntEquals(tc, 47, i); CuAssertPtrNotNull(tc, f->groups); CuAssertIntEquals(tc, 42, f->groups->gid); CuAssertStrEquals(tc, "NW", f->groups->name); CuAssertPtrNotNull(tc, f->groups->next); CuAssertIntEquals(tc, 43, f->groups->next->gid); CuAssertStrEquals(tc, "Egoisten", f->groups->next->name); CuAssertPtrEquals(tc, 0, f->groups->allies); g = f->groups->next; CuAssertTrue(tc, key_get(g->attribs, 44)); CuAssertPtrNotNull(tc, g->allies); CuAssertPtrEquals(tc, 0, g->allies->next); CuAssertPtrEquals(tc, f, g->allies->faction); CuAssertIntEquals(tc, HELP_GIVE, g->allies->status); test_cleanup(); }
static void test_group_readwrite_dead_faction(CuTest *tc) { gamedata data; storage store; faction *f, *f2; unit * u; group *g; ally *al; int fno; test_cleanup(); f = test_create_faction(0); fno = f->no; CuAssertPtrEquals(tc, f, factions); CuAssertPtrEquals(tc, 0, f->next); f2 = test_create_faction(0); CuAssertPtrEquals(tc, f2, factions->next); u = test_create_unit(f2, test_create_region(0, 0, 0)); CuAssertPtrNotNull(tc, u); g = join_group(u, "group"); CuAssertPtrNotNull(tc, g); al = ally_add(&g->allies, f); CuAssertPtrNotNull(tc, al); CuAssertPtrEquals(tc, f, factions); destroyfaction(&factions); CuAssertTrue(tc, !f->_alive); CuAssertPtrEquals(tc, f2, factions); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); write_game(&data); free_gamedata(); f = f2 = NULL; data.strm.api->rewind(data.strm.handle); read_game(&data); mstream_done(&data.strm); gamedata_done(&data); CuAssertPtrEquals(tc, 0, findfaction(fno)); f2 = factions; CuAssertPtrNotNull(tc, f2); u = f2->units; CuAssertPtrNotNull(tc, u); g = get_group(u); CuAssertPtrNotNull(tc, g); CuAssertPtrEquals(tc, 0, g->allies); test_cleanup(); }
static void test_json_write(CuTest * tc) { char buf[256]; cJSON * json = cJSON_CreateObject(); stream strm; mstream_init(&strm); cJSON_AddNumberToObject(json, "turn", 1); json_write(json, &strm); strm.api->rewind(strm.handle); strm.api->readln(strm.handle, buf, sizeof(buf)); CuAssertStrEquals(tc, "{", buf); strm.api->readln(strm.handle, buf, sizeof(buf)); CuAssertStrEquals(tc, "\t\"turn\":\t1", buf); strm.api->readln(strm.handle, buf, sizeof(buf)); CuAssertStrEquals(tc, "}", buf); cJSON_Delete(json); mstream_done(&strm); }
static void test_cr_mallorn(CuTest *tc) { stream strm; char line[1024]; faction *f; region *r; setup_resources(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); r->land->horses = 1; r->land->peasants = 200; r->land->money = 300; r->flags |= RF_MALLORN; rsettrees(r, 0, 1); rsettrees(r, 1, 2); rsettrees(r, 2, 3); mstream_init(&strm); cr_output_resources(&strm, f, r, false); strm.api->rewind(strm.handle); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "3;Baeume", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "2;Schoesslinge", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;Mallorn", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Mallornschoesslinge\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "2;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Mallorn\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "3;number", line); mstream_done(&strm); test_teardown(); }
static void test_attrib_rwint(CuTest *tc) { gamedata data; storage store; attrib a = { 0 }; test_setup(); a.data.i = 42; mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writeint(&a, NULL, &store); a.data.i = 0; data.strm.api->rewind(data.strm.handle); a_readint(&a, NULL, &data); CuAssertIntEquals(tc, 42, a.data.i); mstream_done(&data.strm); gamedata_done(&data); test_cleanup(); }
static void test_attrib_rwstring(CuTest *tc) { gamedata data; storage store; attrib a = { 0 }; test_setup(); a.data.v = _strdup("Hello World"); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writestring(&a, NULL, &store); a_finalizestring(&a); data.strm.api->rewind(data.strm.handle); a_readstring(&a, NULL, &data); CuAssertStrEquals(tc, "Hello World", (const char *)a.data.v); mstream_done(&data.strm); gamedata_done(&data); test_cleanup(); }
static void test_attrib_rwshorts(CuTest *tc) { gamedata data; storage store; attrib a = { 0 }; a.data.sa[0] = -4; a.data.sa[1] = 42; test_setup(); mstream_init(&data.strm); gamedata_init(&data, &store, RELEASE_VERSION); a_writeint(&a, NULL, &store); memset(a.data.ca, 0, 4); data.strm.api->rewind(data.strm.handle); a_readint(&a, NULL, &data); CuAssertIntEquals(tc, -4, a.data.sa[0]); CuAssertIntEquals(tc, 42, a.data.sa[1]); mstream_done(&data.strm); gamedata_done(&data); test_cleanup(); }
static void test_cr_unit(CuTest *tc) { stream strm; char line[1024]; faction *f; region *r; unit *u; test_setup(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); u = test_create_unit(f, r); renumber_unit(u, 1234); mstream_init(&strm); cr_output_unit(&strm, f, u, seen_unit); strm.api->rewind(strm.handle); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, line, "EINHEIT 1234"); mstream_done(&strm); test_teardown(); }
static void test_memstream(CuTest *tc) { storage store; stream out = { 0 }; char buf[1024]; int val=0; mstream_init(&out); binstore_init(&store, &out); store.handle.data = &out; WRITE_INT(&store, 999999); WRITE_TOK(&store, "fortytwo"); WRITE_INT(&store, 42); out.api->rewind(out.handle); READ_INT(&store, &val); READ_TOK(&store, buf, 1024); CuAssertIntEquals(tc, 999999, val); CuAssertStrEquals(tc, "fortytwo", buf); READ_INT(&store, &val); CuAssertIntEquals(tc, 42, val); mstream_done(&out); }
static void test_cr_resources(CuTest *tc) { stream strm; char line[1024]; faction *f; region *r; unit *u; setup_resources(); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); u = test_create_unit(f, r); set_level(u, SK_QUARRYING, 1); r->land->horses = 1; r->land->peasants = 200; r->land->money = 300; rsettrees(r, 0, 1); rsettrees(r, 1, 2); rsettrees(r, 2, 3); region_setresource(r, get_resourcetype(R_STONE), 1); mstream_init(&strm); cr_output_resources(&strm, f, r, true); strm.api->rewind(strm.handle); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "3;Baeume", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "2;Schoesslinge", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;Steine", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Schoesslinge\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "2;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Blumen\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "3;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Silber\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "300;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Bauern\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "200;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Pferde\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;number", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertIntEquals(tc, 0, memcmp(line, "RESOURCE ", 9)); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "\"Steine\";type", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;skill", line); CuAssertIntEquals(tc, 0, strm.api->readln(strm.handle, line, sizeof(line))); CuAssertStrEquals(tc, "1;number", line); mstream_done(&strm); test_teardown(); }
static void test_cr_factionstealth(CuTest *tc) { stream strm; faction *f1, *f2; region *r; unit *u; test_setup(); f1 = test_create_faction(NULL); f2 = test_create_faction(NULL); r = test_create_region(0, 0, NULL); u = test_create_unit(f1, r); /* report to ourselves */ mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... also when we are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); /* we see that our unit is cloaked */ set_factionstealth(u, f2); CuAssertPtrNotNull(tc, u->attribs); mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... also when we are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f1, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); /* we can tell that someone is presenting as us */ mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... but not if they are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); /* we see the same thing as them when we are an ally */ ally_set(&f1->allies, f2, HELP_FSTEALTH); mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Parteitarnung", -1)); mstream_done(&strm); /* ... also when they are anonymous */ u->flags |= UFL_ANON_FACTION; mstream_init(&strm); cr_output_unit(&strm, f2, u, seen_unit); CuAssertIntEquals(tc, f1->no, cr_get_int(&strm, ";Partei", -1)); CuAssertIntEquals(tc, f2->no, cr_get_int(&strm, ";Anderepartei", -1)); CuAssertIntEquals(tc, -1, cr_get_int(&strm, ";Verraeter", -1)); CuAssertIntEquals(tc, 1, cr_get_int(&strm, ";Parteitarnung", -1)); u->flags &= ~UFL_ANON_FACTION; mstream_done(&strm); test_teardown(); }