Skip to content

Commit

Permalink
test: mostly finish save roundtrip test
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Sep 2, 2023
1 parent 2bb0178 commit f416ffa
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 13 deletions.
6 changes: 1 addition & 5 deletions src/base/zdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2553,6 +2553,7 @@ struct savedportal
bool deleting;

int32_t getUID(){return uid;}
void clearUID(){uid = 0;}

savedportal();
void clear()
Expand All @@ -2572,11 +2573,8 @@ struct savedportal
// Everything needed by the title screen.
struct gamedata_header
{
// https://stackoverflow.com/a/75348474/2788187
// std::strong_ordering operator<=>(const gamedata_header&) const = default;
bool operator==(const gamedata_header&) const = default;

// std::string path;
std::string qstpath;
std::string replay_file;
std::string name;
Expand All @@ -2596,8 +2594,6 @@ struct gamedata_header

struct gamedata
{
// https://stackoverflow.com/a/75348474/2788187
// std::strong_ordering operator<=>(const gamedata&) const = default;
bool operator==(const gamedata&) const = default;

gamedata_header header;
Expand Down
6 changes: 6 additions & 0 deletions src/user_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ enum class ScriptType;
#define MAX_USER_OBJECTS 214748
struct scr_func_exec
{
bool operator==(const scr_func_exec&) const = default;

dword pc;
dword thiskey;
ScriptType type;
Expand All @@ -24,6 +26,8 @@ struct scr_func_exec
};
struct user_object
{
bool operator==(const user_object&) const = default;

bool reserved;
// TODO: here and every other `owned_type`; can we replace -1 with ScriptType::None ?
ScriptType owned_type;
Expand Down Expand Up @@ -70,6 +74,8 @@ struct user_object
};
struct saved_user_object
{
bool operator==(const saved_user_object&) const = default;

int32_t object_index;
user_object obj;
std::map<int32_t,ZScriptArray> held_arrays;
Expand Down
111 changes: 103 additions & 8 deletions src/zc/saves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,22 @@ static bool gd_compare(const char* a, const char* b)

bool saves_test()
{
// First make sure there are not accidentally equalities that are impossible,
// like fields that have pointers to objects.
gamedata* g1 = new gamedata();
gamedata* g2 = new gamedata();
g1->saved_mirror_portal.clearUID();
g2->saved_mirror_portal.clearUID();
if (*g1 != *g2)
{
delete g1;
delete g2;
printf("failed: g1 == g2\n");
return false;
}
delete g1;
delete g2;

gamedata* game = new gamedata();
game->header.qstpath = "somegame.qst";
game->header.title = "I am a test";
Expand Down Expand Up @@ -2330,6 +2346,29 @@ bool saves_test()
return false;\
}

#define SAVE_TEST_FIELD_NOFMT(field) if (!gd_compare(game->field, save.game->field)) {\
printf("game->%s != save.game->%s\n", #field, #field);\
delete game;\
return false;\
}

#define SAVE_TEST_VECTOR(field) for (int i = 0; i < game->field.size(); i++) {\
if (!gd_compare(game->field[i], save.game->field[i])) {\
printf("game->%s[%d] != save.game->%s[%d]\n", #field, i, #field, i);\
printf("%s\n", fmt::format("{} != {}", game->field[i], save.game->field[i]).c_str());\
delete game;\
return false;\
}\
}

#define SAVE_TEST_VECTOR_NOFMT(field) for (int i = 0; i < game->field.size(); i++) {\
if (!gd_compare(game->field[i], save.game->field[i])) {\
printf("game->%s[%d] != save.game->%s[%d]\n", #field, i, #field, i);\
delete game;\
return false;\
}\
}

#define SAVE_TEST_ARRAY(field) for (int i = 0; i < countof(game->field); i++) {\
if (!gd_compare(game->field[i], save.game->field[i])) {\
printf("game->%s[%d] != save.game->%s[%d]\n", #field, i, #field, i);\
Expand All @@ -2339,6 +2378,35 @@ bool saves_test()
}\
}

#define SAVE_TEST_ARRAY_NOFMT(field) for (int i = 0; i < countof(game->field); i++) {\
if (!gd_compare(game->field[i], save.game->field[i])) {\
printf("game->%s[%d] != save.game->%s[%d]\n", #field, i, #field, i);\
delete game;\
return false;\
}\
}

#define SAVE_TEST_ARRAY_2D(field) for (int i = 0; i < countof(game->field); i++) {\
for (int j = 0; j < countof(game->field[0]); j++) {\
if (!gd_compare(game->field[i][j], save.game->field[i][j])) {\
printf("game->%s[%d][%d] != save.game->%s[%d][%d]\n", #field, i, j, #field, i, j);\
printf("%s\n", fmt::format("{} != {}", game->field[i][j], save.game->field[i][j]).c_str());\
delete game;\
return false;\
}\
}\
}

#define SAVE_TEST_ARRAY_2D_NOFMT(field) for (int i = 0; i < countof(game->field); i++) {\
for (int j = 0; j < countof(game->field[0]); j++) {\
if (!gd_compare(game->field[i][j], save.game->field[i][j])) {\
printf("game->%s[%d][%d] != save.game->%s[%d][%d]\n", #field, i, j, #field, i, j);\
delete game;\
return false;\
}\
}\
}

// Test some (but not all ... I got lazy) of the fields, in the order found in the save format.
SAVE_TEST_FIELD(get_name());
SAVE_TEST_FIELD(get_quest());
Expand All @@ -2357,38 +2425,65 @@ bool saves_test()
SAVE_TEST_ARRAY(maps);
SAVE_TEST_ARRAY(guys);
SAVE_TEST_ARRAY(lvlkeys);
// SAVE_TEST_ARRAY(screen_d);
SAVE_TEST_ARRAY_2D(screen_d);
SAVE_TEST_ARRAY(global_d);
SAVE_TEST_ARRAY(_counter);
SAVE_TEST_ARRAY(_maxcounter);
SAVE_TEST_ARRAY(_dcounter);
SAVE_TEST_ARRAY(_generic);
SAVE_TEST_FIELD(awpn);
SAVE_TEST_FIELD(bwpn);
// SAVE_TEST_FIELD(globalRAM);
SAVE_TEST_FIELD_NOFMT(globalRAM);
SAVE_TEST_FIELD(forced_awpn);
SAVE_TEST_FIELD(forced_bwpn);
SAVE_TEST_FIELD(forced_xwpn);
SAVE_TEST_FIELD(forced_ywpn);
SAVE_TEST_FIELD(xwpn);
SAVE_TEST_FIELD(ywpn);
SAVE_TEST_ARRAY(lvlswitches);
SAVE_TEST_ARRAY(item_messages_played);
SAVE_TEST_ARRAY(bottleSlots);

// Now do the header.
if (game->header != save.game->header)
game->saved_mirror_portal.clearUID();
save.game->saved_mirror_portal.clearUID();
if (game->saved_mirror_portal != save.game->saved_mirror_portal)
{
printf("game->header != save.game->header\n");
printf("game->saved_mirror_portal != save.game->saved_mirror_portal\n");
delete game;
return false;
}

// Now do the entire thing.
if (game != save.game)
SAVE_TEST_ARRAY(gen_doscript);
SAVE_TEST_ARRAY(gen_exitState);
SAVE_TEST_ARRAY(gen_reloadState);
SAVE_TEST_ARRAY_2D(gen_initd);
SAVE_TEST_ARRAY(gen_dataSize);
for (int i = 0; i < NUMSCRIPTSGENERIC; i++)
SAVE_TEST_VECTOR(gen_data[i]);
SAVE_TEST_ARRAY(xstates);
SAVE_TEST_ARRAY(gen_eventstate);
SAVE_TEST_ARRAY(gswitch_timers);
SAVE_TEST_VECTOR_NOFMT(user_objects);
SAVE_TEST_VECTOR_NOFMT(user_portals);
SAVE_TEST_ARRAY(OverrideItems);

// Now do the header.
if (game->header != save.game->header)
{
printf("game != save.game\n");
printf("game->header != save.game->header\n");
delete game;
return false;
}

// Now do the entire thing.
// TODO: why is this failing?
// if (game != save.game)
// {
// printf("game != save.game\n");
// delete game;
// return false;
// }

delete game;
return true;
}

0 comments on commit f416ffa

Please sign in to comment.