#include "stdafx.h" #include "doctest.h" #include "scratch.h" #include "utilities/parser.h" #include "world/worldlogiccache.h" #include #include #include // 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(100'000, std::byte{7})}); contents.package.push_back({world_logic_cache::tape_tag, std::vector(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 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().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()); }