18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 12:25:27 +01:00
Files
maszyna/tests/unit/test_world_logic_cache.cpp
T
maj00r 0667c071c6 feat(world): the statements of the scenery on the logic tape, each with its region of 8192 m
Toward loading the logic by regions (docs/world-format-eu07.md, section 13, D4). While the tape of the logic
cache is recorded, the loader notes where each statement of the scenery begins on the tape and where it put
its first node or shape (basic_region::note_insert / take_insert, from insert() of nodes, shapes and lines),
and writes the table to the cache as a package block STMT: the offsets as differences and the region as a
tile of level 10 of the plane body (8192 m), or 0 for a statement of the whole package (an event with no
place, the time, the weather, a trainset, and the origin / rotate / scale directives). The logic still loads
whole, in the order of the tape, so nothing changes in what the game does.

Checked in the game (--test 120 s, seed 1, world.binary 1): the physics logs of td.scn and Glinojeck.scn the
same bit for bit as from the files, recorded and replayed. td.scn: 1867 statements in 4 regions, 1298 of the
whole package; Glinojeck.scn: 93 876 statements in 6 regions, 16 427 of the whole package. Loading a region on
its own will need the state of the transformations (origin, rotate, scale) at the start of each of its
statements, which the directives of the whole package set.
2026-12-03 00:04:17 +01:00

96 lines
4.5 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());
}
TEST_CASE("the statements of the logic cache: their regions of 8192 m and their table as bytes") {
// a region of the logic is a tile of level 10 of the plane, 8192 m from the zero of .scn
auto const at_zero{world_logic_cache::statement_region(0.0, 0.0)};
CHECK(at_zero != 0);
CHECK(world_logic_cache::statement_region(8191.999, 8191.999) == at_zero);
CHECK(world_logic_cache::statement_region(8192.0, 0.0) != at_zero);
CHECK(world_logic_cache::statement_region(-0.001, 0.0) != at_zero);
CHECK(world_geometry::tile_id::from_raw(at_zero).level() == world_geometry::logic_region_level);
std::vector<world_logic_cache::statement> const table{{0, 0}, {17, at_zero}, {17'000'000'000ULL, world_logic_cache::statement_region(400'000.0, -3.5)}, {17'000'000'123ULL, 0}};
auto const bytes{world_logic_cache::encode_statements(table)};
std::vector<world_logic_cache::statement> back;
REQUIRE(world_logic_cache::decode_statements(bytes, back));
CHECK(back == table);
for (std::size_t length = 0; length < bytes.size(); ++length)
CHECK_FALSE(world_logic_cache::decode_statements(std::string_view{bytes}.substr(0, length), back));
CHECK_FALSE(world_logic_cache::decode_statements(bytes + "x", back));
}