2014-08-14 05:06:36 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
|
|
|
|
#include "save.h"
|
2015-11-04 10:38:12 +01:00
|
|
|
#include "unit.h"
|
2016-02-17 09:24:19 +01:00
|
|
|
#include "group.h"
|
|
|
|
#include "ally.h"
|
2015-11-04 10:38:12 +01:00
|
|
|
#include "faction.h"
|
2014-08-14 05:06:36 +02:00
|
|
|
#include "version.h"
|
|
|
|
#include <CuTest.h>
|
|
|
|
#include <tests.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
static void test_readwrite_data(CuTest * tc)
|
|
|
|
{
|
|
|
|
const char *filename = "test.dat";
|
|
|
|
char path[MAX_PATH];
|
2014-12-12 11:13:25 +01:00
|
|
|
test_cleanup();
|
2014-08-14 05:06:36 +02:00
|
|
|
sprintf(path, "%s/%s", datapath(), filename);
|
|
|
|
CuAssertIntEquals(tc, 0, writegame(filename));
|
2015-05-13 02:18:51 +02:00
|
|
|
CuAssertIntEquals(tc, 0, readgame(filename, false));
|
2014-08-14 05:06:36 +02:00
|
|
|
CuAssertIntEquals(tc, RELEASE_VERSION, global.data_version);
|
|
|
|
CuAssertIntEquals(tc, 0, remove(path));
|
2014-12-12 11:13:25 +01:00
|
|
|
test_cleanup();
|
2014-08-14 05:06:36 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 10:38:12 +01:00
|
|
|
static void test_readwrite_unit(CuTest * tc)
|
|
|
|
{
|
|
|
|
const char *filename = "test.dat";
|
|
|
|
char path[MAX_PATH];
|
|
|
|
gamedata *data;
|
|
|
|
struct unit *u;
|
|
|
|
struct region *r;
|
|
|
|
struct faction *f;
|
|
|
|
int fno;
|
2016-01-29 17:49:27 +01:00
|
|
|
/* FIXME: at some point during this test, errno is set to 17 (File exists), why? */
|
2015-11-04 10:38:12 +01:00
|
|
|
|
2016-02-01 09:26:24 +01:00
|
|
|
create_directories();
|
2015-11-04 10:38:12 +01:00
|
|
|
test_cleanup();
|
|
|
|
r = test_create_region(0, 0, 0);
|
|
|
|
f = test_create_faction(0);
|
|
|
|
fno = f->no;
|
|
|
|
u = test_create_unit(f, r);
|
2016-01-29 19:11:48 +01:00
|
|
|
join_path(datapath(), filename, path, sizeof(path));
|
2015-11-04 10:38:12 +01:00
|
|
|
|
2016-02-01 09:26:24 +01:00
|
|
|
data = gamedata_open(path, "w");
|
2015-11-22 14:36:05 +01:00
|
|
|
CuAssertPtrNotNull(tc, data); // TODO: intermittent test
|
2015-11-04 10:38:12 +01:00
|
|
|
write_unit(data, u);
|
|
|
|
gamedata_close(data);
|
|
|
|
|
|
|
|
free_gamedata();
|
|
|
|
f = test_create_faction(0);
|
|
|
|
renumber_faction(f, fno);
|
2016-02-01 12:27:13 +01:00
|
|
|
data = gamedata_open(path, "r");
|
2015-11-04 10:38:12 +01:00
|
|
|
u = read_unit(data);
|
|
|
|
gamedata_close(data);
|
|
|
|
|
|
|
|
CuAssertPtrNotNull(tc, u);
|
|
|
|
CuAssertPtrEquals(tc, f, u->faction);
|
|
|
|
CuAssertPtrEquals(tc, 0, u->region);
|
|
|
|
CuAssertIntEquals(tc, 0, remove(path));
|
|
|
|
test_cleanup();
|
|
|
|
}
|
|
|
|
|
2016-02-17 09:24:19 +01:00
|
|
|
static void test_readwrite_dead_faction(CuTest *tc) {
|
|
|
|
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);
|
|
|
|
writegame("test.dat");
|
|
|
|
free_gamedata();
|
|
|
|
f = f2 = NULL;
|
|
|
|
readgame("test.dat", false);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-08-14 05:06:36 +02:00
|
|
|
CuSuite *get_save_suite(void)
|
|
|
|
{
|
|
|
|
CuSuite *suite = CuSuiteNew();
|
|
|
|
SUITE_ADD_TEST(suite, test_readwrite_data);
|
2015-11-04 10:38:12 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_readwrite_unit);
|
2016-02-17 09:24:19 +01:00
|
|
|
SUITE_ADD_TEST(suite, test_readwrite_dead_faction);
|
2014-08-14 05:06:36 +02:00
|
|
|
return suite;
|
|
|
|
}
|