18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 13:35:26 +01:00
Files
maszyna/tests/unit/test_parser_tape.cpp
T
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

190 lines
6.9 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "scratch.h"
#include "utilities/parser.h"
#include "utilities/parsertape.h"
#include "utilities/Globals.h"
#include <format>
#include <string>
#include <vector>
// the tape of a scenery parser (utilities/parsertape.h): what the outermost parser gave its reader, replayed to the same reader with
// the same answers and without the files (docs/world-format-eu07.md, section 7)
namespace {
// a reader that asks the parser the way the scenery loader does: several tokens at once, single ones in their case, a vector, a
// look ahead, and the header text; it writes down every answer, and the state of the parser after it
std::vector<std::string> read_all(cParser &Input) {
std::vector<std::string> transcript;
auto const state{[&Input, &transcript](std::string const &What) {
transcript.push_back(std::format("{} | {} {} {} {} {} {}", What, Input.Name(), Input.Line(), Input.LineMain(), Input.TokenBegin(), Input.TokenEnd(), Input.InLayerFile()));
}};
while (true) {
auto const header{std::string{Input.headerText(4)}};
auto const keyword{Input.getToken<std::string>()};
if (keyword.empty())
break;
state(std::format("keyword {} [{}]", keyword, header));
if (keyword != "node")
continue;
Input.getTokens(4);
std::string name;
std::string type;
double range_max{0.0};
double range_min{0.0};
Input >> range_max >> range_min >> name >> type;
state(std::format("node {} {} {} {}", range_max, range_min, name, type));
auto const position{Input.getToken<glm::vec3>()};
state(std::format("at {} {} {}", position.x, position.y, position.z));
auto const texture{Input.getToken<std::string>(false)};
state("texture " + texture);
Input.getTokens(1);
state("peek " + Input.peek());
Input.getTokens(1);
std::string end;
Input >> end;
state("end " + end);
}
return transcript;
}
std::string const injected{"node 1 0 Injected model 0 0 0 none 4 endmodel"};
struct recording {
std::vector<std::string> transcript;
std::string bytes;
std::vector<std::string> files;
};
// reads the scenery with recording, and checks that the reader got what it gets with no tape and gets from the replayed tape
recording record_and_replay(test::scratch_folder const &Folder, std::string const &File, bool const Loadtraction) {
recording result;
parser_tape::writer tape;
{
cParser input{File, cParser::buffer_FILE, Folder.path(), Loadtraction};
input.injectString(injected);
input.startRecording(tape);
result.transcript = read_all(input);
}
result.files = tape.files();
result.bytes = tape.finish();
std::vector<std::string> plain;
{
cParser input{File, cParser::buffer_FILE, Folder.path(), Loadtraction};
input.injectString(injected);
plain = read_all(input);
}
CHECK(result.transcript == plain);
std::vector<std::string> files;
std::size_t entries{0};
CHECK(parser_tape::check(result.bytes, files, entries));
CHECK(files == result.files);
std::vector<std::string> replayed;
{
parser_tape::reader reader{result.bytes};
cParser input{reader};
// the loader injects its text again; the tape holds it already
input.injectString(injected);
replayed = read_all(input);
}
CHECK(replayed == result.transcript);
return result;
}
std::string joined(std::vector<std::string> const &Lines) {
std::string all;
for (auto const &line : Lines)
all += line + "\n";
return all;
}
} // namespace
TEST_CASE("a parser replaying its tape gives the reader the same tokens and the same state, with no file opened") {
test::scratch_folder const folder{"parser_tape"};
folder.write("main.scn",
"// the main file\n"
"node 1000 0 Signal_A model 1.5 2.0 -3.25 Textures/Sem.DDS 7 endmodel\n"
"include tpl/pole.inc Pole_1 12.5 endtrack end\n"
"include tr/wire.scm end\n"
"node 500 0 none track 0 0 0 Rail none endtrack\n");
folder.write("tpl/pole.inc",
"node 300 0 (p1) model (p2) 0 0 Pole.dds 1 (p3)\n"
"include tpl/inner.inc end\n");
folder.write("tpl/inner.inc", "node 100 0 Inner model 0 0 0 none 2 endmodel\n");
folder.write("tr/wire.scm", "node 900 0 Wire traction 0 0 0 none 3 endtraction\n");
auto const with_traction{record_and_replay(folder, "main.scn", true)};
CHECK(with_traction.transcript.size() > 30);
// the parameters of the template are in the tokens, the nested include expanded
CHECK(joined(with_traction.transcript).find("node 300 0 pole_1 model") != std::string::npos);
CHECK(joined(with_traction.transcript).find("node 100 0 inner model") != std::string::npos);
CHECK(joined(with_traction.transcript).find("wire traction") != std::string::npos);
// the two templates opened and closed their node groups, and the tape holds that
int creates{0};
int closes{0};
parser_tape::reader reader{with_traction.bytes};
while (reader.next_kind() != parser_tape::entry_kind::end) {
creates += reader.next_kind() == parser_tape::entry_kind::group_create ? 1 : 0;
closes += reader.next_kind() == parser_tape::entry_kind::group_close ? 1 : 0;
reader.pop();
}
CHECK_FALSE(reader.broken());
CHECK(creates == 2);
CHECK(closes == 2);
// with the traction left out, its include is skipped in the tape as in the files
auto const without_traction{record_and_replay(folder, "main.scn", false)};
CHECK(joined(without_traction.transcript).find("wire traction") == std::string::npos);
SUBCASE("a terrain file the binary terrain stands in for is counted on replay as on the first reading") {
folder.write("terrain.scn", "include x_ter.scm end\nnode 1 0 After model 0 0 0 none 1 endmodel\n");
auto const previous_state{Global.file_binary_terrain_state};
Global.file_binary_terrain_state = true;
auto const before{Global.file_binary_terrain_skipped};
parser_tape::writer terrain;
{
cParser input{"terrain.scn", cParser::buffer_FILE, folder.path()};
input.startRecording(terrain);
read_all(input);
}
CHECK(Global.file_binary_terrain_skipped == before + 1);
auto const bytes{terrain.finish()};
{
parser_tape::reader replay{bytes};
cParser input{replay};
read_all(input);
}
CHECK(Global.file_binary_terrain_skipped == before + 2);
Global.file_binary_terrain_state = previous_state;
}
}
TEST_CASE("a cut or foreign tape doesn't read") {
parser_tape::writer tape;
parser_tape::state start;
start.line = 1;
tape.begin("a.scn", start);
parser_tape::state state;
state.line = 3;
state.begin = 10;
state.end = 14;
tape.token("node", "a.scn", state);
tape.side_effect(parser_tape::entry_kind::group_create);
tape.token("track", "b.inc", state);
tape.text("node 1 0 x");
auto const whole{tape.finish()};
std::vector<std::string> files;
std::size_t entries{0};
REQUIRE(parser_tape::check(whole, files, entries));
CHECK(files == std::vector<std::string>{"a.scn", "b.inc"});
CHECK(entries == 4);
for (std::size_t length = 0; length < whole.size(); ++length)
CHECK_FALSE(parser_tape::check(whole.substr(0, length), files, entries));
CHECK_FALSE(parser_tape::check(whole + "x", files, entries));
auto wrong{whole};
wrong[0] = 9; // another version
CHECK_FALSE(parser_tape::check(wrong, files, entries));
}