#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()); } TEST_CASE("the statements of the logic cache: their regions of 8192 m, their transformations 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); world_logic_cache::statement_table table; world_logic_cache::context plain; world_logic_cache::context moved; moved.has_offset = true; moved.offset_x = 24000.123456789; moved.offset_z = -48000.5; moved.rotation_y = 37.5f; moved.has_scale = true; moved.scale_x = 1.5f; CHECK(table.context_index(plain) == 0); CHECK(table.context_index(plain) == 0); CHECK(table.context_index(moved) == 1); CHECK(table.context_index(plain) == 2); table.statements = {{0, 0, 0}, {17, at_zero, 1}, {17'000'000'000ULL, world_logic_cache::statement_region(400'000.0, -3.5), 1}, {17'000'000'123ULL, 0, 2}}; auto const bytes{world_logic_cache::encode_statements(table)}; world_logic_cache::statement_table 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)); }