planetsim/test_events.cpp

51 lines
1.8 KiB
C++

// Headless checks for the viewer event journal (no window needed).
#include "Viewer.hpp"
#include <cstdio>
#include <fstream>
#include <string>
static int failures = 0;
static void check(bool cond, const char* what) {
std::printf(" [%s] %s\n", cond ? "PASS" : "FAIL", what);
if (!cond) ++failures;
}
int main() {
std::printf("Events: cap and save/load\n");
Viewer v;
PlanetConfig cfg; cfg.subdivisions = 2; cfg.seed = 4242;
v.cfg = cfg;
v.planet.generate(cfg);
for (int i = 0; i < 205; ++i) {
int cell = i % (int)v.planet.cells.size();
v.appendEvent(1, (uint8_t)(i % 3), (double)i, cell, (uint32_t)i,
"event " + std::to_string(i), "detail " + std::to_string(i));
}
check((int)v.events.size() == Viewer::EVENT_LOG_MAX, "event journal keeps the newest 200 entries");
check(v.events.front().title == "event 5", "oldest entries are trimmed first");
check(v.events.back().title == "event 204", "newest event is retained");
const char* path = "/tmp/fanworgen_event_test.save";
v.saveGame(path);
Viewer r;
r.loadGame(path);
check((int)r.events.size() == Viewer::EVENT_LOG_MAX, "v16 save/load restores event count");
check(!r.events.empty() && r.events.front().title == "event 5" && r.events.back().title == "event 204",
"v16 save/load restores event contents");
check(r.nextEventId == v.nextEventId, "v16 save/load restores next event id");
{
std::fstream fs(path, std::ios::in | std::ios::out | std::ios::binary);
uint32_t oldVer = 15;
fs.seekp(4);
fs.write((char*)&oldVer, 4);
}
Viewer old;
old.loadGame(path);
check(old.events.empty(), "pre-v16 saves load with an empty event journal");
std::printf(failures ? "\nFAILURES: %d\n" : "\nALL EVENT CHECKS PASSED\n", failures);
return failures ? 1 : 0;
}