18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 17:04:52 +01:00
Files
maszyna/tests/unit/test_world_logic_cache.cpp
maj00r cbbb0b23fb fix(world): the tape of the logic cache is bytes from the start, read an entry at a time
The parser tape kept a structure of about 70 bytes per token, so a scenery of 12 million tokens (Glinojeck)
took 1.8 GB at its peak while recorded and 1.5 GB while replayed, against 0.5 GB read from its files. The tape
is now written as it's recorded (parser_tape::writer, about 10 bytes a token) and decoded as it's replayed
(parser_tape::reader, tokens as views of the bytes); the names of the files are entries of the tape, and an
end mark tells a tape cut short. Glinojeck now peaks at 712 MB recorded and 601 MB replayed.

Checked in the game, on a test folder made of junctions to the data with a scenery folder of its own (nothing
was written into the SVN working copy): td.scn (23 481 entries, 29 files, a cache of 28 KB) and Glinojeck.scn
(11.9 million entries, 63 files, a cache of 8.5 MB), each run in --test for 120 s of simulation with seed 1
from the files with world.binary 0, then recorded and replayed with world.binary 1: the physics logs of every
train are the same bit for bit in all runs, and so are the error logs.
2026-12-02 23:03:28 +01:00

78 lines
3.3 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "scratch.h"
#include "utilities/parser.h"
#include "world/worldlogiccache.h"
#include <sstream>
#include <string>
#include <vector>
// the cache of the logic of a scenery (world/worldlogiccache.h): the tape of the parser, in package blocks of a cache file, read
// back only while the build, the files and the settings are those it was made from
TEST_CASE("the logic cache is named after the scenario file, without the Rainsted marks") {
CHECK(world_logic_cache::file_name("scenery/", "linia053_wrzosy.scn") == "scenery/linia053_wrzosy.logic.eu07");
CHECK(world_logic_cache::file_name("scenery/", "$$wrzosy.scn") == "scenery/wrzosy.logic.eu07");
CHECK(world_logic_cache::file_name("scenery/", "noextension") == "scenery/noextension.logic.eu07");
}
TEST_CASE("the FNV-1a hash is the standard one") {
CHECK(world_logic_cache::fnv1a("") == 0xCBF2'9CE4'8422'2325ull);
CHECK(world_logic_cache::fnv1a("a") == 0xAF63'DC4C'8601'EC8Cull);
}
TEST_CASE("a cache file holds package blocks next to the tiles") {
world_cache::file_contents contents;
contents.header.layer = world_cache::layer_kind::logic;
contents.package.push_back({world_logic_cache::tape_tag, std::vector<std::byte>(100'000, std::byte{7})});
contents.package.push_back({world_logic_cache::tape_tag, std::vector<std::byte>(10, std::byte{9})});
std::ostringstream output;
REQUIRE(world_cache::write(output, contents).empty());
std::istringstream input{output.str()};
world_cache::reader cache;
REQUIRE(cache.open(input).empty());
REQUIRE(cache.package().size() == 2);
std::vector<std::byte> data;
for (std::size_t k = 0; k < 2; ++k) {
REQUIRE(cache.read_package_block(input, cache.package()[k], data).empty());
CHECK(data == contents.package[k].data);
}
}
TEST_CASE("the logic cache is read back while it's current, and refused when the build, a file or a setting changed") {
test::scratch_folder const folder{"logic_cache"};
folder.write("main.scn", "node 1 0 a model 0 0 0 none 1 endmodel\ninclude part.inc end\n");
folder.write("part.inc", "node 2 0 b model 0 0 0 none 2 endmodel\n");
parser_tape::writer recorder;
{
cParser input{"main.scn", cParser::buffer_FILE, folder.path()};
input.startRecording(recorder);
while (!input.getToken<std::string>().empty()) {
}
}
auto const files{recorder.files()};
REQUIRE(files.size() >= 2);
auto const tape{recorder.finish()};
world_logic_cache::load_settings settings;
auto const cachefile{folder.path("main.logic.eu07")};
constexpr std::uint64_t engine{0x1234};
REQUIRE(world_logic_cache::write(cachefile, tape, files, engine, settings).empty());
std::string back;
CHECK(world_logic_cache::read(cachefile, engine, settings, back).empty());
CHECK(back == tape);
CHECK_FALSE(world_logic_cache::read(cachefile, engine + 1, settings, back).empty());
auto other{settings};
other.load_traction = false;
CHECK_FALSE(world_logic_cache::read(cachefile, engine, other, back).empty());
other = settings;
other.trainset_overrides = "3=trainset x endtrainset;";
CHECK_FALSE(world_logic_cache::read(cachefile, engine, other, back).empty());
// an included file changed
folder.write("part.inc", "node 2 0 b model 0 0 0 none 22 endmodel\n");
CHECK_FALSE(world_logic_cache::read(cachefile, engine, settings, back).empty());
CHECK_FALSE(world_logic_cache::read(folder.path("missing.logic.eu07"), engine, settings, back).empty());
}