18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-04 23:45:17 +01:00
Files
maszyna/tests/unit/test_parser_tape.cpp
T
maj00r ea1c16cacb fix(world): the review of the statements of the logic cache: the order of the file replays as the tape, and seek keeps no copy
- deserialize_ordered plays each statement as the loop over the tape
  does, up to the next statement: a stray token is a keyword there too,
  and the read that ends the part replays what the tape holds after the
  statement (the closes of the files it ends), before the load ends.
- reader::seek points at the table of files instead of copying it for
  each statement.
- The table of statements and the files are kept only when an order is
  asked for, the only time they are read.
- EU07_WORLD_REPLAY_ORDER is described as the debugging aid it is: the
  region orders don't load a real scenery yet.
- A test replays the statements of a recorded tape one by one through
  seek() and compares them with the tape played through.
2026-12-03 22:41:12 +01:00

231 lines
8.6 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("the statements of a tape replayed one by one through seek() give what the tape gives played through") {
test::scratch_folder const folder{"parser_tape_seek"};
folder.write("main.scn",
"node 1000 0 a model 1 2 3 none 7 endmodel\n"
"include part.scm end\n"
"node 500 0 b track 0 0 0 Rail none endtrack\n");
folder.write("part.scm", "node 10 0 c model 4 5 6 none 8 endmodel\ninclude inner.scm end\n");
folder.write("inner.scm", "node 20 0 d model 7 8 9 none 9 endmodel\n");
// each token is a statement here, taken where the loader takes one: the place and the state before its read
parser_tape::writer tape;
std::vector<std::pair<std::size_t, parser_tape::state>> starts;
std::vector<std::string> played;
{
cParser input{"main.scn", cParser::buffer_FILE, folder.path()};
input.startRecording(tape);
while (true) {
starts.emplace_back(tape.position(), tape.last_state());
auto const token{input.getToken<std::string>()};
if (token.empty())
break;
played.push_back(std::format("{} | {} {}", token, input.Name(), input.Line()));
}
}
starts.pop_back();
auto const files{tape.files()};
auto const bytes{tape.finish()};
std::vector<std::string> sought;
{
parser_tape::reader reader{bytes};
cParser input{reader};
for (std::size_t k = 0; k < starts.size(); ++k) {
auto const end{k + 1 < starts.size() ? starts[k + 1].first : bytes.size() - 1};
reader.seek(starts[k].first, end, starts[k].second, files);
for (auto token{input.getToken<std::string>()}; !token.empty(); token = input.getToken<std::string>())
sought.push_back(std::format("{} | {} {}", token, input.Name(), input.Line()));
}
}
CHECK(sought == played);
CHECK(played.size() > 30);
}
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));
}