The serializer recovery reaches this repo as a generated wire schema
(include/generated/sots_stream_schema.h, 386 classes / 2042 items): for each
class, the ordered sequence of items its Write puts on the stream. Facts only —
no field offsets, no sizeof, no strides. This engine reads and writes the on-disk
format; it does not inherit the original's memory layout.
The table is a specification, not a program: the recovery is a linear pass over
Write, so it cannot see Write's branches (StarShip's BQ2 is gated on hbq but
listed unconditionally) and it flattens container loops. A codec driven off it
would desynchronise. The hand-written io() shapes stay the codec; SchemaProbe
(probe.h) walks them with every branch taken and test_wire_schema LCS-aligns
that against the table — 56 shapes bound, 657 items matched, 0 mismatches.
Four defects the check found, all invisible to a round-trip test:
- SystemParams field 1 is a string, not an int. It is the empty string in
every save, and an empty string is four zero bytes — byte-identical to the
int 0, so it round-tripped by luck. A named planet would have desynced.
- ObservedTech/ObservedWeapon odet is a bool, not an int. Byte-safe only
because a 4-char tag makes a bool item and an int item both 12 bytes.
- SpeciesRatios nv and ShipRecords srbd are counts, not fields.
CoverageArchive separates items a field names from items a Node merely carries,
because a byte-identical round trip is not a coverage claim. Typed coverage of a
real save goes 37.9% -> 97.1% (97.2/97.2/97.6 on the others) with the round trip
still byte-identical, by typing TechTree (both NumTechs sections), Events,
ShipRecs, sprjs, civr, comms, spy2, spymgr, aid, Ojvs, AIEnf, FNG, trdmgr and
the Des section/gun-bank tree. Ratchet at 95%.
trdmgr resolves a recorded trap: ServerTradeManager's Read/Write really are the
inherited no-op, but the call is virtual and ServerTradeManagerImpl has the real
serializer. Same shape resolves IServerSpyManager -> ServerSpyManager.
ctest 34/34, clean_room_check OK, test_save skips cleanly with SOTS_SAVES_DIR unset.
178 lines
8.2 KiB
C++
178 lines
8.2 KiB
C++
// Real-save test: reads every *.sav in $SOTS_SAVES_DIR (owner's data, never
|
|
// in the repo) and checks that the walker parses clean, the typed shapes
|
|
// load, both round trips are byte-identical and the RNG blob is a valid
|
|
// MT19937 state. Skips (exit 0) when the variable is unset.
|
|
//
|
|
// With SOTS_DUMP_DIR set, writes <name>.cpp.dump / <name>.cpp.summary there
|
|
// for tests/mars_stream/oracle/compare.py.
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <dirent.h>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "mars/rng/mt19937.h"
|
|
#include "mars/stream/dump.h"
|
|
#include "mars/stream/probe.h"
|
|
#include "mars/stream/save.h"
|
|
|
|
using namespace mars::stream;
|
|
|
|
static int fails = 0;
|
|
#define CHECK(cond) \
|
|
do { \
|
|
if (!(cond)) { \
|
|
std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
++fails; \
|
|
} \
|
|
} while (0)
|
|
|
|
static std::vector<std::string> list_saves(const std::string& dir) {
|
|
std::vector<std::string> out;
|
|
DIR* d = opendir(dir.c_str());
|
|
if (!d) return out;
|
|
while (dirent* e = readdir(d)) {
|
|
std::string n = e->d_name;
|
|
if (n.size() > 4 && n.compare(n.size() - 4, 4, ".sav") == 0) out.push_back(dir + "/" + n);
|
|
}
|
|
closedir(d);
|
|
std::sort(out.begin(), out.end());
|
|
return out;
|
|
}
|
|
|
|
static void check_save(const std::string& path, const char* dump_dir) {
|
|
std::printf("== %s\n", path.c_str());
|
|
SaveDocument doc = read_save_file(path);
|
|
std::printf(" inflated %zu bytes, items %u, frames %u, resyncs %u, hint-failures %u, raw-bytes %u\n",
|
|
doc.inflated.size(), doc.stats.items, doc.stats.frames, doc.stats.resyncs, doc.stats.hint_failures,
|
|
doc.stats.raw_bytes);
|
|
std::printf(" issues: %zu error, %zu warn, %zu info\n", doc.count(Issue::Error), doc.count(Issue::Warn),
|
|
doc.count(Issue::Info));
|
|
for (const Issue& i : doc.issues)
|
|
if (i.level != Issue::Info) std::printf(" %s\n", format_issue(i).c_str());
|
|
|
|
// --- the confirmed format parses clean --------------------------------------
|
|
CHECK(doc.stats.resyncs == 0);
|
|
CHECK(doc.stats.hint_failures == 0);
|
|
CHECK(doc.count(Issue::Error) == 0);
|
|
CHECK(doc.count(Issue::Warn) == 0);
|
|
CHECK(doc.stats.raw_bytes == 2503); // only the opaque RNG blob
|
|
|
|
// --- typed shapes -------------------------------------------------------------
|
|
const auto& s = doc.game.summary;
|
|
const auto& sim = doc.game.sim;
|
|
std::printf(" summary: game='%s' turn=%d numSys=%d players=%zu\n", s.gameName.c_str(), s.turn, s.numSys,
|
|
s.players.size());
|
|
CHECK(!s.gameName.empty());
|
|
CHECK(s.turn >= 1);
|
|
CHECK(s.numSys > 0);
|
|
CHECK(!s.players.empty());
|
|
CHECK(sim.gameName == s.gameName);
|
|
CHECK(int(sim.systems.size()) == s.numSys);
|
|
CHECK(sim.systems.size() == sim.systemIds.size());
|
|
CHECK(sim.players.size() == sim.playerIds.size());
|
|
CHECK(sim.fleets.size() == sim.fleetIds.size());
|
|
CHECK(sim.species.size() == 7);
|
|
CHECK(!sim.players.empty() && !sim.players[0].player.plryName.empty());
|
|
CHECK(doc.game.createParams.name == s.gameName);
|
|
CHECK(doc.game.createParams.nSys == s.numSys);
|
|
CHECK(doc.game.createParams.mapP.planets.size() == size_t(s.numSys));
|
|
for (const auto& se : sim.systems) CHECK(!se.sys.name.empty());
|
|
for (const auto& fe : sim.fleets) CHECK(fe.flt.ships.size() >= 1);
|
|
CHECK(!doc.game.cdTable.ids.empty());
|
|
|
|
// --- RNG blob: mt[624] + left, produced by our MT19937 from RSeed --------------
|
|
CHECK(sim.rng.is_complex() && sim.rng.children.size() == 1);
|
|
const Node& blob = sim.rng.children[0];
|
|
CHECK(blob.kind == Kind::Raw && blob.raw.size() == 2503);
|
|
mars::rng::MT19937 saved(1u);
|
|
CHECK(saved.load_state(blob.raw.data(), blob.raw.size()));
|
|
std::printf(" rng: left=%d (index %d), RSeed=%d\n", saved.left(), saved.index(), doc.game.createParams.rseed);
|
|
CHECK(saved.left() >= 0 && saved.left() <= mars::rng::MT19937::N);
|
|
{
|
|
// the saved block must be reachable from seed(RSeed) by whole twists
|
|
mars::rng::MT19937 gen(uint32_t(doc.game.createParams.rseed));
|
|
int twists = -1;
|
|
for (int k = 0; k < 16 && twists < 0; ++k) {
|
|
if (std::memcmp(gen.state(), saved.state(), sizeof(uint32_t) * mars::rng::MT19937::N) == 0) twists = k;
|
|
else
|
|
for (int i = 0; i < mars::rng::MT19937::N; ++i) gen.next_u32(); // consume a block -> next twist
|
|
}
|
|
std::printf(" rng: state == seed(RSeed) after %d twist(s)\n", twists);
|
|
CHECK(twists >= 0);
|
|
}
|
|
|
|
// --- coverage: what the shapes understand vs what a Node merely carries ---------
|
|
// The round trip below is byte-identical either way, because an opaque Node is
|
|
// copied verbatim. This is the number that actually moves when a body gets typed.
|
|
{
|
|
CoverageArchive cov;
|
|
doc.game.io(cov);
|
|
size_t total = cov.typed + cov.opaque;
|
|
double pct = total ? 100.0 * double(cov.typed) / double(total) : 0.0;
|
|
std::printf(" coverage: %zu typed, %zu opaque (%.1f%% of %u stream items typed)\n", cov.typed,
|
|
cov.opaque, pct, doc.stats.items);
|
|
std::vector<std::pair<size_t, std::string>> worst;
|
|
for (const auto& kv : cov.opaque_by_tag) worst.emplace_back(kv.second, kv.first);
|
|
std::sort(worst.rbegin(), worst.rend());
|
|
std::printf(" still opaque:");
|
|
for (size_t i = 0; i < worst.size() && i < 8; ++i)
|
|
std::printf(" %s=%zu", worst[i].second.c_str(), worst[i].first);
|
|
std::printf("\n");
|
|
// Ratchet, not a target: typing a body must never silently regress.
|
|
CHECK(pct >= 95.0);
|
|
}
|
|
|
|
// --- round trips ------------------------------------------------------------------
|
|
Bytes tree_bytes = write_tree(doc.tree);
|
|
CHECK(tree_bytes == doc.inflated);
|
|
Bytes typed_bytes = write_save(doc.game);
|
|
if (typed_bytes != doc.inflated) {
|
|
size_t i = 0, n = std::min(typed_bytes.size(), doc.inflated.size());
|
|
while (i < n && typed_bytes[i] == doc.inflated[i]) ++i;
|
|
std::printf(" typed round trip differs at 0x%zx (sizes %zu vs %zu)\n", i, typed_bytes.size(),
|
|
doc.inflated.size());
|
|
}
|
|
CHECK(typed_bytes == doc.inflated);
|
|
std::printf(" round trip: tree %s, typed %s\n", tree_bytes == doc.inflated ? "identical" : "DIFFERS",
|
|
typed_bytes == doc.inflated ? "identical" : "DIFFERS");
|
|
|
|
// --- optional dump for the oracle comparison -------------------------------------
|
|
if (dump_dir) {
|
|
std::string base = path.substr(path.find_last_of('/') + 1);
|
|
std::ofstream d(std::string(dump_dir) + "/" + base + ".cpp.dump");
|
|
for (const std::string& l : dump_tree(doc.tree)) d << l << '\n';
|
|
std::ofstream sm(std::string(dump_dir) + "/" + base + ".cpp.summary");
|
|
sm << "summary: game=" << json_quote_cp1252(s.gameName) << " turn=" << s.turn << " numSys=" << s.numSys
|
|
<< " players=" << s.players.size() << '\n';
|
|
sm << "sim: players=" << sim.players.size() << " systems=" << sim.systems.size()
|
|
<< " fleets=" << sim.fleets.size() << '\n';
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
const char* dir = std::getenv("SOTS_SAVES_DIR");
|
|
if (!dir || !*dir) {
|
|
std::printf("test_save: SKIPPED (SOTS_SAVES_DIR not set)\n");
|
|
return 0;
|
|
}
|
|
std::vector<std::string> saves = list_saves(dir);
|
|
if (saves.empty()) {
|
|
std::printf("test_save: SKIPPED (no *.sav in %s)\n", dir);
|
|
return 0;
|
|
}
|
|
const char* dump_dir = std::getenv("SOTS_DUMP_DIR");
|
|
for (const std::string& p : saves) {
|
|
try {
|
|
check_save(p, dump_dir && *dump_dir ? dump_dir : nullptr);
|
|
} catch (const std::exception& e) {
|
|
std::printf("FAIL %s: %s\n", p.c_str(), e.what());
|
|
++fails;
|
|
}
|
|
}
|
|
std::printf("test_save: %s (%zu save(s), %d failures)\n", fails ? "FAILED" : "ok", saves.size(), fails);
|
|
return fails ? 1 : 0;
|
|
}
|