From 2f13f052f73e299b8d3b29ea19ce520888667006 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 11 Nov 2026 22:40:02 +0100 Subject: [PATCH 1/4] test: characterize the parser of the text files The tokens, comments, quotes, parameters and include directives of cParser, its numbers and its place in the text, as they behave now, with the files of the tests made in a folder of the build (scratch.h). The helpers of utilities.h which read from a parser are here too. --- tests/unit/README.md | 3 + tests/unit/scratch.h | 44 ++ tests/unit/test_parser.cpp | 895 +++++++++++++++++++++++++++++++++++++ 3 files changed, 942 insertions(+) create mode 100644 tests/unit/scratch.h create mode 100644 tests/unit/test_parser.cpp diff --git a/tests/unit/README.md b/tests/unit/README.md index a10f1798e..80deab5e4 100644 --- a/tests/unit/README.md +++ b/tests/unit/README.md @@ -77,6 +77,9 @@ TEST_CASE("brakes: service braking stops a heavier wagon too") { - `pair` (`vehicle.h`) puts two vehicles on that track facing each other with the given ends and runs them together, as the simulation runs the vehicles of a scene: the coupler and buffer forces, collisions and automatic coupling come from the physics (`test_couplers.cpp`). +- A test which reads files makes them in a `test::scratch_folder` (`scratch.h`): a folder of its own + in the build folder of the tests, removed with the object. The files are written byte for byte, so + the line ends are the ones the test gives (`test_parser.cpp`, `test_ladder_logic.cpp`). - Check behaviour (speeds, pressures, states the player sees) rather than internal flags, and make the test check its own preconditions (`REQUIRE` that the braking skidded, before checking what a skid leads to). diff --git a/tests/unit/scratch.h b/tests/unit/scratch.h new file mode 100644 index 000000000..ea5d43b83 --- /dev/null +++ b/tests/unit/scratch.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace test { + +// a folder of its own for the files of a test, removed with the object. it's made in the build folder of the tests +// (EU07_UNIT_TESTS_SCRATCH, see CMakeLists.txt), not in the system's temporary folder, which anyone can write to +class scratch_folder { + public: + explicit scratch_folder(std::string const &Name) : m_root{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / ("eu07_unit_test_" + Name)} { + std::filesystem::remove_all(m_root); + std::filesystem::create_directories(m_root); + } + ~scratch_folder() { + std::error_code error; + std::filesystem::remove_all(m_root, error); + } + scratch_folder(scratch_folder const &) = delete; + scratch_folder &operator=(scratch_folder const &) = delete; + + // writes the file byte for byte, so the line ends are the ones the test gives. returns its path, with forward slashes + std::string write(std::string const &Name, std::string_view const Content) const { + auto const path{m_root / Name}; + std::filesystem::create_directories(path.parent_path()); + std::ofstream file{path, std::ios::binary}; + file.write(Content.data(), static_cast(Content.size())); + return path.generic_string(); + } + // the folder with forward slashes and one at the end, the way the game names the folder of a file to the parser + std::string path() const { return m_root.generic_string() + "/"; } + // a file or a folder of that name in the folder, with forward slashes + std::string path(std::string const &Name) const { return (m_root / Name).generic_string(); } + std::filesystem::path const &root() const { return m_root; } + + private: + std::filesystem::path m_root; +}; + +} // namespace test diff --git a/tests/unit/test_parser.cpp b/tests/unit/test_parser.cpp new file mode 100644 index 000000000..2584218d0 --- /dev/null +++ b/tests/unit/test_parser.cpp @@ -0,0 +1,895 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scratch.h" + +#include "scene/scenelayers.h" +#include "utilities/Globals.h" +#include "utilities/parser.h" +#include "utilities/utilities.h" + +// the parser of the game's text files (utilities/parser.cpp), as it behaves now: what a token is, what the comments, +// the quotes and the include directives do to the tokens, how the texts turn into numbers, and what the parser tells +// of its place in the file. these record the behaviour for later changes of the parser, its oddities included; the +// ones which look like mistakes are listed in COVERAGE.md + +namespace { + +char const *const default_break{"\n\r\t ;"}; + +// the tokens left in the parser, joined with '|' +std::string rest(cParser &Parser, bool const ToLower = true, char const *Break = default_break) { + std::string joined; + for (auto token{Parser.getToken(ToLower, Break)}; false == token.empty(); token = Parser.getToken(ToLower, Break)) { + if (false == joined.empty()) { + joined += '|'; + } + joined += token; + } + return joined; +} + +// the tokens of a text +std::string tokens(std::string const &Text, bool const ToLower = true, char const *Break = default_break) { + cParser parser(Text, cParser::buffer_TEXT); + return rest(parser, ToLower, Break); +} + +// puts the settings of the binary terrain back as they were +struct binary_terrain_settings { + bool state{Global.file_binary_terrain_state}; + int skipped{Global.file_binary_terrain_skipped}; + + binary_terrain_settings() = default; + binary_terrain_settings(binary_terrain_settings const &) = delete; + binary_terrain_settings &operator=(binary_terrain_settings const &) = delete; + ~binary_terrain_settings() { + Global.file_binary_terrain_state = state; + Global.file_binary_terrain_skipped = skipped; + } +}; + +} // namespace + +// tokens -------------------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: tokens end at spaces, tabs, line ends and semicolons, and come in lower case unless asked otherwise") { + std::string const text{"Alpha\tBETA;gamma\r\nDelta ;; epsilon"}; + CHECK(tokens(text) == "alpha|beta|gamma|delta|epsilon"); + CHECK(tokens(text, false) == "Alpha|BETA|gamma|Delta|epsilon"); + // commas, colons and brackets are a part of the token for the default separators + CHECK(tokens("pos: [1,2] end") == "pos:|[1,2]|end"); + // only the letters of ascii are changed, the bytes of utf-8 and windows-1250 stay + CHECK(tokens("\xC5\x81\xC3\x93\x44\xC5\xB9 \xA3\xD3\x44\x8F") == "\xC5\x81\xC3\x93\x64\xC5\xB9|\xA3\xD3\x64\x8F"); +} + +TEST_CASE("parser: every token can be read with separators of its own") { + cParser parser("a,b c;d", cParser::buffer_TEXT); + CHECK(parser.getToken(true, ",") == "a"); + CHECK(parser.getToken(true, " ") == "b"); + CHECK(parser.getToken() == "c"); + CHECK(parser.getToken() == "d"); + CHECK(parser.getToken().empty()); +} + +TEST_CASE("parser: with no separators the rest of the text is one token") { + // no separators given at all is taken as none + cParser parser("one two\nthree", cParser::buffer_TEXT); + CHECK(parser.getToken(false, nullptr) == "one two\nthree"); + CHECK(parser.Line() == 2); + CHECK(tokens("one two\nthree", true, "") == "one two\nthree"); +} + +TEST_CASE("parser: the end of the text shows as eof, and a read past it as a failure, the way a stream does it") { + SUBCASE("the text ends with a token") { + cParser parser("a b", cParser::buffer_TEXT); + CHECK(parser.ok()); + CHECK_FALSE(parser.eof()); + CHECK(parser.getToken() == "a"); + CHECK_FALSE(parser.eof()); + // the last token is ended by the end of the text + CHECK(parser.getToken() == "b"); + CHECK(parser.eof()); + CHECK(parser.ok()); + CHECK(parser.getToken().empty()); + CHECK_FALSE(parser.ok()); + } + SUBCASE("the text ends with a separator") { + cParser parser("a\n", cParser::buffer_TEXT); + CHECK(parser.getToken() == "a"); + CHECK_FALSE(parser.eof()); + CHECK(parser.getToken().empty()); + CHECK(parser.eof()); + CHECK(parser.ok()); + CHECK(parser.getToken().empty()); + CHECK_FALSE(parser.ok()); + } + SUBCASE("an empty text fails with its first read") { + // the check for a byte order mark looks for more text after the empty first token + cParser parser("", cParser::buffer_TEXT); + CHECK(parser.ok()); + CHECK(parser.getToken().empty()); + CHECK(parser.eof()); + CHECK_FALSE(parser.ok()); + } +} + +TEST_CASE("parser: a byte order mark at the start of the text isn't a part of the first token") { + cParser parser("\xEF\xBB\xBF" "first second", cParser::buffer_TEXT); + CHECK(parser.getToken() == "first"); + // the place of the token in the text is the one after the mark + CHECK(parser.TokenBegin() == 3); + CHECK(parser.TokenEnd() == 8); + CHECK(parser.getToken() == "second"); + // a mark alone on its line gives no token + CHECK(tokens("\xEF\xBB\xBF\nfirst") == "first"); + // it's a mark only at the start + CHECK(tokens("a \xEF\xBB\xBF" "b") == "a|\xEF\xBB\xBF" "b"); +} + +// comments ------------------------------------------------------------------------------------------------------------ + +TEST_CASE("parser: comments are left out, to the end of the line or of the block") { + cParser parser("one // rest of the line\ntwo /* a\nblock */ three", cParser::buffer_TEXT); + CHECK(rest(parser) == "one|two|three"); + CHECK(parser.Line() == 3); + // a comment ends the token it touches, and the text after it is a token of its own + CHECK(tokens("abc//x\ndef") == "abc|def"); + CHECK(tokens("ab/*x*/cd") == "ab|cd"); + // a slash or a star alone is a character like any other + CHECK(tokens("1/2 a*b / *") == "1/2|a*b|/|*"); +} + +TEST_CASE("parser: the comments stay in the tokens when the parser is told not to skip them") { + cParser parser("a // b /* c */", cParser::buffer_TEXT); + parser.skipComments = false; + CHECK(rest(parser) == "a|//|b|/*|c|*/"); +} + +TEST_CASE("parser: a comment the text ends in leaves the parser failed, not just at the end") { + for (auto const *text : {"a /* never closed", "a // the last line"}) { + CAPTURE(text); + cParser parser(text, cParser::buffer_TEXT); + CHECK(parser.getToken() == "a"); + CHECK(parser.ok()); + CHECK(parser.getToken().empty()); + CHECK(parser.eof()); + CHECK_FALSE(parser.ok()); + } + // with a line end after it, the parser is at the end like after a token + cParser parser("a // the last line\n", cParser::buffer_TEXT); + CHECK(parser.getToken() == "a"); + CHECK(parser.getToken().empty()); + CHECK(parser.eof()); + CHECK(parser.ok()); +} + +TEST_CASE("parser: other comment marks can be added") { + CHECK(tokens("a # note\nb -- c\nd") == "a|#|note|b|--|c|d"); + cParser parser("a # note\nb -- c\nd", cParser::buffer_TEXT); + // also after the parser read something + CHECK(parser.getToken() == "a"); + parser.addCommentStyle("#", "\n"); + parser.addCommentStyle("--", "\n"); + CHECK(rest(parser) == "b|d"); + + SUBCASE("a comment without an end mark takes the rest of the text") { + cParser ending("keep these stop and nothing else", cParser::buffer_TEXT); + ending.addCommentStyle("stop", ""); + CHECK(rest(ending) == "keep|these"); + } + SUBCASE("a mark of letters is compared with the token as it's read, in lower case or not") { + cParser lower("a REM x\nb", cParser::buffer_TEXT); + lower.addCommentStyle("rem", "\n"); + CHECK(rest(lower) == "a|b"); + cParser cased("a REM x\nb", cParser::buffer_TEXT); + cased.addCommentStyle("rem", "\n"); + CHECK(rest(cased, false) == "a|REM|x|b"); + } +} + +// quotes -------------------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: quotes make one token of the text between them, with its case and separators kept") { + CHECK(tokens("Name \"Two Words\" Tail") == "name|Two Words|tail"); + CHECK(tokens("\"a;b\tc\" x") == "a;b\tc|x"); + // the marks of a comment too + CHECK(tokens("\"http://host/path\" x") == "http://host/path|x"); + // the quoted text is glued to what it touches + CHECK(tokens("Pre\"In Side\"Post next") == "preIn Sidepost|next"); + // a backslash takes the next character as it is + CHECK(tokens("\"say \\\"hi\\\" \\\\ done\" x") == "say \"hi\" \\ done|x"); +} + +TEST_CASE("parser: empty quotes give no token") { + CHECK(tokens("\"\" after") == "after"); + CHECK(tokens("x \"\" y") == "x|y"); +} + +TEST_CASE("parser: quotes left open take the rest of the text, and the parser fails") { + cParser parser("\"open ended", cParser::buffer_TEXT); + CHECK(parser.getToken() == "open ended"); + CHECK(parser.eof()); + CHECK_FALSE(parser.ok()); +} + +// place in the text --------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: the line is the one the reading got to, counted from 1") { + cParser parser("one\ntwo\n\n\nthree // note\nfour \"a\nb\" /* x\ny */ five", cParser::buffer_TEXT); + CHECK(parser.Line() == 1); + // the line end which ended the token is read already + CHECK(parser.getToken() == "one"); + CHECK(parser.Line() == 2); + CHECK(parser.getToken() == "two"); + CHECK(parser.Line() == 3); + CHECK(parser.getToken() == "three"); + CHECK(parser.Line() == 5); + CHECK(parser.getToken() == "four"); + CHECK(parser.Line() == 6); + // the lines inside of quotes and comments count too + CHECK(parser.getToken() == "a\nb"); + CHECK(parser.Line() == 7); + CHECK(parser.getToken() == "five"); + CHECK(parser.Line() == 8); + CHECK(parser.LineMain() == 8); +} + +TEST_CASE("parser: a line ended the Windows way is counted at its line feed") { + cParser parser("one\r\ntwo", cParser::buffer_TEXT); + CHECK(parser.getToken() == "one"); + // only the carriage return is read yet + CHECK(parser.Line() == 1); + CHECK(parser.getToken() == "two"); + CHECK(parser.Line() == 2); +} + +TEST_CASE("parser: the place of the token last read is given as offsets into the text") { + cParser parser("ab cd", cParser::buffer_TEXT); + CHECK(parser.getToken() == "ab"); + CHECK(parser.TokenBegin() == 0); + CHECK(parser.TokenEnd() == 2); + CHECK(parser.getToken() == "cd"); + CHECK(parser.TokenBegin() == 4); + CHECK(parser.TokenEnd() == 6); + + SUBCASE("the quotes are a part of it") { + cParser quoted("\"x y\" z", cParser::buffer_TEXT); + CHECK(quoted.getToken() == "x y"); + CHECK(quoted.TokenBegin() == 0); + CHECK(quoted.TokenEnd() == 5); + } + SUBCASE("and so is a comment touching the token") { + cParser commented("ab//c\nd", cParser::buffer_TEXT); + CHECK(commented.getToken() == "ab"); + CHECK(commented.TokenBegin() == 0); + CHECK(commented.TokenEnd() == 6); + } +} + +TEST_CASE("parser: the progress is the part of the text read, in percent") { + cParser parser("aaaa bbbb ", cParser::buffer_TEXT); + CHECK(parser.getProgress() == 0); + CHECK(parser.getToken() == "aaaa"); + CHECK(parser.getProgress() == 50); + CHECK(parser.getFullProgress() == 50); + // a text put in front of the rest takes its share of what is left + parser.injectString("cc dd eeee"); + CHECK(parser.getFullProgress() == 50); + CHECK(parser.getToken() == "cc"); + CHECK(parser.getProgress() == 50); + CHECK(parser.getFullProgress() == 65); + // nothing to read is all read + CHECK(cParser("", cParser::buffer_TEXT).getProgress() == 100); + CHECK(cParser("", cParser::buffer_TEXT).getFullProgress() == 100); +} + +TEST_CASE("parser: a text made by the program is named by the folder given with it, or not at all") { + CHECK(cParser("a", cParser::buffer_TEXT).Name().empty()); + CHECK(cParser("a", cParser::buffer_TEXT, "scenery/").Name() == "scenery/"); + CHECK_FALSE(cParser("a", cParser::buffer_TEXT).InLayerFile()); +} + +// values -------------------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: several tokens are read at once and taken one by one into variables of their types") { + cParser parser("1 2.5 -3 Word yes rest", cParser::buffer_TEXT); + REQUIRE(parser.getTokens(5)); + CHECK(parser.peek() == "1"); + int whole{0}; + double real{0.0}; + float single{0.f}; + std::string word; + bool flag{false}; + parser >> whole >> real >> single >> word >> flag; + CHECK(whole == 1); + CHECK(real == doctest::Approx(2.5)); + CHECK(single == doctest::Approx(-3.0)); + CHECK(word == "word"); + CHECK(flag); + CHECK(parser.peek().empty()); + // the tokens not asked for are still to be read + CHECK(parser.getToken() == "rest"); +} + +TEST_CASE("parser: too few tokens are told, and the variables left without one keep their values") { + cParser parser("a b", cParser::buffer_TEXT); + CHECK_FALSE(parser.getTokens(3)); + std::string first; + std::string second; + std::string third{"untouched"}; + parser >> first >> second >> third; + CHECK(first == "a"); + CHECK(second == "b"); + CHECK(third == "untouched"); + // the same for the other types + int whole{7}; + bool flag{true}; + parser >> whole >> flag; + CHECK(whole == 7); + CHECK(flag); +} + +TEST_CASE("parser: the tokens are read in lower case by default, and as written when asked") { + cParser parser("Alpha Beta", cParser::buffer_TEXT); + REQUIRE(parser.getTokens(2, false)); + std::string first; + std::string second; + parser >> first >> second; + CHECK(first == "Alpha"); + CHECK(second == "Beta"); +} + +TEST_CASE("parser: whole numbers are read up to what isn't a part of one") { + cParser parser("42 -7 3.9 12abc", cParser::buffer_TEXT); + CHECK(parser.getToken() == 42); + CHECK(parser.getToken() == -7); + CHECK(parser.getToken() == 3); + CHECK(parser.getToken() == 12); + // nothing left to read is a zero + CHECK(parser.getToken() == 0); +} + +TEST_CASE("parser: numbers with a fraction take a point, an exponent and a sign, whatever the system's language") { + cParser parser("0.25 -1.5e3 .5 +2.5 7. 1,5", cParser::buffer_TEXT); + CHECK(parser.getToken() == doctest::Approx(0.25)); + CHECK(parser.getToken() == doctest::Approx(-1500.0)); + CHECK(parser.getToken() == doctest::Approx(0.5)); + CHECK(parser.getToken() == doctest::Approx(2.5)); + CHECK(parser.getToken() == doctest::Approx(7.0)); + // a comma isn't a decimal point: the number ends at it + CHECK(parser.getToken() == doctest::Approx(1.0)); + CHECK(cParser("1e-3", cParser::buffer_TEXT).getToken() == doctest::Approx(0.001)); + // a word is a zero + CHECK(cParser("word", cParser::buffer_TEXT).getToken() == 0.0); +} + +TEST_CASE("parser: true, yes and 1 are true, anything else is false") { + cParser parser("Yes TRUE 1 no 0 maybe", cParser::buffer_TEXT); + // read as a flag the token is put in lower case, whatever the caller asks for + CHECK(parser.getToken(false)); + CHECK(parser.getToken(false)); + CHECK(parser.getToken()); + CHECK_FALSE(parser.getToken()); + CHECK_FALSE(parser.getToken()); + CHECK_FALSE(parser.getToken()); + // nothing left to read is false + CHECK_FALSE(parser.getToken()); + + // a token read before with its case kept is compared as it is + cParser cased("Yes yes", cParser::buffer_TEXT); + REQUIRE(cased.getTokens(2, false)); + bool first{true}; + bool second{false}; + cased >> first >> second; + CHECK_FALSE(first); + CHECK(second); +} + +TEST_CASE("parser: three numbers are read as a vector, with commas and brackets as separators") { + cParser parser("[ 1, 2.5 ; -3 ] next", cParser::buffer_TEXT); + auto const vector{parser.getToken()}; + CHECK(vector.x == doctest::Approx(1.0)); + CHECK(vector.y == doctest::Approx(2.5)); + CHECK(vector.z == doctest::Approx(-3.0)); + // the closing bracket is left for the next read + CHECK(rest(parser) == "]|next"); +} + +TEST_CASE("parser: a token can be skipped, or compared with the one expected") { + cParser parser("alpha Beta gamma", cParser::buffer_TEXT); + CHECK(parser.expectToken("alpha")); + parser.ignoreToken(); + CHECK(parser.expectToken("gamma")); + CHECK_FALSE(parser.expectToken("gamma")); + // the token is compared in lower case + CHECK_FALSE(cParser("Beta", cParser::buffer_TEXT).expectToken("Beta")); + CHECK(cParser("Beta", cParser::buffer_TEXT).expectToken("beta")); +} + +// tokens waiting ------------------------------------------------------------------------------------------------------ + +TEST_CASE("parser: the tokens read and not taken are dropped by the next read") { + cParser parser("a b c d e", cParser::buffer_TEXT); + CHECK(parser.autoclear()); + REQUIRE(parser.getTokens(2)); + CHECK(parser.peek() == "a"); + CHECK(parser.getToken() == "c"); + CHECK(parser.peek().empty()); +} + +TEST_CASE("parser: told to keep them, the next reads take the waiting tokens first") { + cParser parser("a b c d e", cParser::buffer_TEXT); + CHECK_FALSE(parser.autoclear(false).autoclear()); + REQUIRE(parser.getTokens(2)); + CHECK(parser.getToken() == "a"); + CHECK(parser.peek() == "b"); + // one waits, so one more is read + REQUIRE(parser.getTokens(2)); + std::string first; + std::string second; + parser >> first >> second; + CHECK(first == "b"); + CHECK(second == "c"); + CHECK(parser.getToken() == "d"); +} + +TEST_CASE("parser: many waiting tokens are taken in their order") { + // enough of them for the queue to drop the ones taken on the way + std::string text; + for (auto number{0}; number < 200; ++number) { + text += std::to_string(number) + ' '; + } + cParser parser(text, cParser::buffer_TEXT); + parser.autoclear(false); + REQUIRE(parser.getTokens(200)); + auto wrong{0}; + for (auto number{0}; number < 200; ++number) { + auto value{-1}; + parser >> value; + if (value != number) { + ++wrong; + } + } + CHECK(wrong == 0); + CHECK(parser.peek().empty()); +} + +// parameters ---------------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: (pN) in a token is replaced with the N-th parameter, or with none when there's no such one") { + std::string const text{"(p1) x(p2)y (p3) (P1) (p0) (p1)-(p2) (p1"}; + std::vector const parameters{"First", "Second"}; + // the parameter is put in lower case with the token + cParser lower(text, cParser::buffer_TEXT, "", true, parameters); + CHECK(rest(lower) == "first|xsecondy|none|first|none|first-second|(p1"); + // with the case kept, a capital P isn't a parameter + cParser cased(text, cParser::buffer_TEXT, "", true, parameters); + CHECK(rest(cased, false) == "First|xSecondy|none|(P1)|none|First-Second|(p1"); + // without parameters nothing is replaced + CHECK(tokens("(p1) (p3)") == "(p1)|(p3)"); +} + +// text put in front --------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: a text given to the parser is read before the rest, the one given last first") { + cParser parser("m1 m2", cParser::buffer_TEXT); + parser.injectString("a b"); + parser.injectString("x y"); + // what to do with the tokens read and not taken is told to the texts given, too + parser.autoclear(false); + CHECK(rest(parser) == "x|y|a|b|m1|m2"); +} + +TEST_CASE("parser: the text after the token last read can be taken as it is and skipped") { + cParser parser("one two three four", cParser::buffer_TEXT); + // the first token is left to the parser + CHECK(parser.remainingText().empty()); + CHECK(parser.getToken() == "one"); + CHECK(std::string{parser.remainingText()} == "two three four"); + parser.skipText(4, 0, 3); + CHECK(std::string{parser.remainingText()} == "three four"); + CHECK(parser.TokenBegin() == 5); + CHECK(parser.TokenEnd() == 8); + CHECK(parser.Line() == 1); + CHECK(parser.getToken() == "three"); + // the lines skipped are counted as the caller says, and the end of the text isn't passed + parser.skipText(1000, 2, 4); + CHECK(parser.Line() == 3); + CHECK(parser.TokenBegin() == 14); + CHECK(parser.TokenEnd() == 18); + CHECK(parser.remainingText().empty()); + CHECK(parser.getToken().empty()); +} + +TEST_CASE("parser: the text isn't given when the parser would change its tokens") { + SUBCASE("tokens wait to be taken") { + cParser parser("one two three", cParser::buffer_TEXT); + REQUIRE(parser.getTokens(2)); + CHECK(parser.remainingText().empty()); + } + SUBCASE("there are parameters to put in") { + cParser parser("one two three", cParser::buffer_TEXT, "", true, {"x"}); + CHECK(parser.getToken() == "one"); + CHECK(parser.remainingText().empty()); + } + SUBCASE("the comments aren't skipped") { + cParser parser("one two three", cParser::buffer_TEXT); + parser.skipComments = false; + CHECK(parser.getToken() == "one"); + CHECK(parser.remainingText().empty()); + } + SUBCASE("there are other comment marks") { + cParser parser("one two three", cParser::buffer_TEXT); + parser.addCommentStyle("#", "\n"); + CHECK(parser.getToken() == "one"); + CHECK(parser.remainingText().empty()); + } +} + +TEST_CASE("parser: the text after the token is the one of the text being read, a text put in front included") { + cParser parser("m1 m2", cParser::buffer_TEXT); + CHECK(parser.getToken() == "m1"); + CHECK(std::string{parser.remainingText()} == "m2"); + parser.injectString("x y z"); + // its first token is left to the parser, too + CHECK(parser.remainingText().empty()); + CHECK(parser.getToken() == "x"); + CHECK(std::string{parser.remainingText()} == "y z"); + parser.skipText(2, 0, 1); + CHECK(rest(parser) == "z|m2"); +} + +// files --------------------------------------------------------------------------------------------------------------- + +TEST_CASE("parser: a file is read from the folder given, whatever its line ends") { + test::scratch_folder const folder{"parser_file"}; + folder.write("plain.txt", "Alpha beta\ngamma"); + folder.write("windows.txt", "a 1\r\nb 2\r\n"); + folder.write("marked.txt", "\xEF\xBB\xBF" "first second"); + + cParser parser("plain.txt", cParser::buffer_FILE, folder.path()); + CHECK(parser.ok()); + CHECK(parser.Line() == 1); + CHECK(parser.Name() == folder.path() + "plain.txt"); + CHECK(parser.InLayerFile()); + CHECK(rest(parser) == "alpha|beta|gamma"); + + cParser windows("windows.txt", cParser::buffer_FILE, folder.path()); + CHECK(rest(windows) == "a|1|b|2"); + cParser marked("marked.txt", cParser::buffer_FILE, folder.path()); + CHECK(rest(marked) == "first|second"); + // the folder can be a part of the name as well + cParser whole(folder.path("plain.txt"), cParser::buffer_FILE); + CHECK(whole.Name() == folder.path("plain.txt")); + CHECK(rest(whole) == "alpha|beta|gamma"); +} + +TEST_CASE("parser: a file named in another case than on the disk is found") { + test::scratch_folder const folder{"parser_case"}; + folder.write("Mixed.TXT", "found"); + cParser parser("mixed.txt", cParser::buffer_FILE, folder.path()); + CHECK(parser.ok()); + CHECK(rest(parser) == "found"); +} + +TEST_CASE("parser: a file which isn't there gives a failed parser with nothing to read") { + test::scratch_folder const folder{"parser_missing"}; + cParser parser("nothing.txt", cParser::buffer_FILE, folder.path()); + CHECK_FALSE(parser.ok()); + // no line to be at + CHECK(parser.Line() == 0); + CHECK(parser.getProgress() == 100); + CHECK(parser.Name() == folder.path() + "nothing.txt"); + CHECK(parser.getToken().empty()); +} + +TEST_CASE("parser: an empty file is read, with nothing in it") { + test::scratch_folder const folder{"parser_empty"}; + folder.write("empty.txt", ""); + cParser parser("empty.txt", cParser::buffer_FILE, folder.path()); + CHECK(parser.ok()); + CHECK(parser.Line() == 1); + CHECK(parser.getProgress() == 100); + CHECK(parser.getToken().empty()); +} + +TEST_CASE("parser: a folder given for a file has nothing to read") { + test::scratch_folder const folder{"parser_folder"}; + std::filesystem::create_directories(folder.root() / "inner.txt"); + // whether it opens at all depends on the system, so the state of the parser does too + cParser parser("inner.txt", cParser::buffer_FILE, folder.path()); + CHECK(parser.getToken().empty()); +} + +TEST_CASE("parser: the tokens of a file are counted, the ones of the files it includes too") { + test::scratch_folder const folder{"parser_count"}; + folder.write("count.txt", "a b\nc // d e\n\"f g\" include extra.txt end\n"); + folder.write("extra.txt", "x y"); + folder.write("empty.txt", ""); + CHECK(cParser::countTokens("count.txt", folder.path()) == 6); + CHECK(cParser::countTokens("empty.txt", folder.path()) == 0); + CHECK(cParser::countTokens("nothing.txt", folder.path()) == 0); +} + +// includes ------------------------------------------------------------------------------------------------------------ + +TEST_CASE("parser: include puts the tokens of another file in place of the directive, with its parameters put in") { + test::scratch_folder const folder{"parser_include"}; + folder.write("part.inc", "Body (p1) (p2) (p3)\n"); + std::string const text{"start include PART.inc One Two end finish"}; + + cParser parser(text, cParser::buffer_TEXT, folder.path()); + CHECK(parser.getToken() == "start"); + CHECK(parser.LineMain() == 1); + CHECK(parser.getToken() == "body"); + // the parser tells of the file it reads now + CHECK(parser.Name() == folder.path() + "part.inc"); + CHECK(parser.Line() == 1); + CHECK(parser.LineMain() == -1); + CHECK_FALSE(parser.InLayerFile()); + // the parameters are read as written, and put in lower case with the tokens they go into + CHECK(rest(parser) == "one|two|none|finish"); + CHECK(parser.Name() == folder.path()); + CHECK(parser.LineMain() == 1); + + // with the case kept, the name of the file is as written, and found in its case on the disk + cParser cased(text, cParser::buffer_TEXT, folder.path()); + CHECK(rest(cased, false) == "start|Body|One|Two|none|finish"); + // a file included without parameters keeps its placeholders + cParser bare("include part.inc end", cParser::buffer_TEXT, folder.path()); + CHECK(rest(bare) == "body|(p1)|(p2)|(p3)"); +} + +TEST_CASE("parser: an included file includes others, named from the folder of the first file, not from its own") { + test::scratch_folder const folder{"parser_nested"}; + folder.write("outer.inc", "o1 include sub/inner.inc (p1) end o2"); + folder.write("sub/inner.inc", "i1 (p1)"); + folder.write("sub/a.inc", "a include b.inc end"); + folder.write("sub/b.inc", "beside"); + folder.write("b.inc", "root"); + + // a parameter can be passed on + cParser parser("m1 include outer.inc X end m2", cParser::buffer_TEXT, folder.path()); + CHECK(rest(parser) == "m1|o1|i1|x|o2|m2"); + cParser relative("include sub/a.inc end", cParser::buffer_TEXT, folder.path()); + CHECK(rest(relative) == "a|root"); + // the folders can be written the Windows way + cParser backslash("include sub\\inner.inc Y end", cParser::buffer_TEXT, folder.path()); + CHECK(rest(backslash) == "i1|y"); +} + +TEST_CASE("parser: an include of a file which isn't there, or is empty, is passed over with its parameters") { + test::scratch_folder const folder{"parser_bad_include"}; + folder.write("empty.inc", ""); + cParser missing("a include nothing.inc 1 2 end b", cParser::buffer_TEXT, folder.path()); + CHECK(rest(missing) == "a|b"); + cParser empty("a include empty.inc 1 2 end b", cParser::buffer_TEXT, folder.path()); + CHECK(rest(empty) == "a|b"); +} + +TEST_CASE("parser: include is a directive only in lower case and when the parser expands them") { + test::scratch_folder const folder{"parser_no_include"}; + folder.write("part.inc", "body"); + // read with its case kept, another spelling is a token like any other + cParser cased("Include part.inc end", cParser::buffer_TEXT, folder.path()); + CHECK(rest(cased, false) == "Include|part.inc|end"); + cParser lower("INCLUDE part.inc end", cParser::buffer_TEXT, folder.path()); + CHECK(rest(lower) == "body"); + cParser plain("include part.inc end", cParser::buffer_TEXT, folder.path()); + plain.expandIncludes = false; + CHECK(rest(plain) == "include|part.inc|end"); +} + +TEST_CASE("parser: read line by line, a line starting with include is the directive") { + test::scratch_folder const folder{"parser_line_include"}; + folder.write("macro.txt", "first (p1)\nsecond (p2)\n"); + folder.write("main.txt", "head\ninclude macro.txt 5 6 end\ntail\n"); + + cParser parser("main.txt", cParser::buffer_FILE, folder.path()); + CHECK(rest(parser, false, "\n\r") == "head|first 5|second 6|tail"); + // also when the parser is told not to expand the directives + cParser plain("main.txt", cParser::buffer_FILE, folder.path()); + plain.expandIncludes = false; + CHECK(rest(plain, false, "\n\r") == "head|first 5|second 6|tail"); + // and any line starting with these letters is taken for one: this one for an include of the file "d" + cParser word("one\nincluded in price\ntwo", cParser::buffer_TEXT, folder.path()); + CHECK(rest(word, false, "\n\r") == "one|two"); +} + +TEST_CASE("parser: an include can name several files to pick one from at random") { + test::scratch_folder const folder{"parser_random_include"}; + folder.write("only.inc", "chosen"); + folder.write("one.inc", "first"); + folder.write("two.inc", "second"); + std::vector const none; + + cParser single("include [ only.inc ] end tail", cParser::buffer_TEXT, folder.path(), true, none, true); + CHECK(rest(single) == "chosen|tail"); + // a single name is still a single file + cParser plain("include only.inc end tail", cParser::buffer_TEXT, folder.path(), true, none, true); + CHECK(rest(plain) == "chosen|tail"); + cParser either("include [ one.inc two.inc ] end tail", cParser::buffer_TEXT, folder.path(), true, none, true); + auto const picked{either.getToken()}; + CHECK((picked == "first" || picked == "second")); + CHECK(rest(either) == "tail"); + // a parser not told to takes the bracket for the name of the file + cParser literal("include [ only.inc ] end tail", cParser::buffer_TEXT, folder.path()); + CHECK(rest(literal) == "tail"); +} + +TEST_CASE("parser: with the traction left out, the includes of the files in a tr or tra folder are passed over") { + // no files needed: the directives are skipped up to their end + cParser parser("before include tr/wires.scm 1 2 end after include other/tra/x.scm end last", cParser::buffer_TEXT, "", false); + CHECK(rest(parser) == "before|after|last"); + // the folder is told by the letters before the slash, so this one is taken for traction too + cParser centre("include centr/dworzec.scm end x", cParser::buffer_TEXT, "", false); + CHECK(rest(centre) == "x"); + + // the other files are read + test::scratch_folder const folder{"parser_traction"}; + folder.write("tory.scm", "track"); + folder.write("tr/siec.scm", "wires"); + cParser without("include tory.scm end include tr/siec.scm end x", cParser::buffer_TEXT, folder.path(), false); + CHECK(rest(without) == "track|x"); + cParser with("include tory.scm end include tr/siec.scm end x", cParser::buffer_TEXT, folder.path(), true); + CHECK(rest(with) == "track|wires|x"); +} + +TEST_CASE("parser: read line by line, a traction include passed over takes the lines up to the next end with it") { + // the directive is skipped in the text of the parser, though its end was on the line of the directive + cParser parser("include tr/wires.scm end\nfirst line\nsecond end\nthird line", cParser::buffer_TEXT, "", false); + CHECK(rest(parser, false, "\n\r") == "third line"); +} + +TEST_CASE("parser: the include of a terrain file is passed over while the binary terrain is in use") { + test::scratch_folder const folder{"parser_terrain"}; + folder.write("map_ter.scm", "terrain"); + std::string const text{"a include map_ter.scm 1 2 end b"}; + binary_terrain_settings restored; + + Global.file_binary_terrain_state = true; + Global.file_binary_terrain_skipped = 0; + cParser skipping(text, cParser::buffer_TEXT, folder.path()); + CHECK(rest(skipping) == "a|b"); + CHECK(Global.file_binary_terrain_skipped == 1); + + Global.file_binary_terrain_state = false; + cParser reading(text, cParser::buffer_TEXT, folder.path()); + CHECK(rest(reading) == "a|terrain|b"); + CHECK(Global.file_binary_terrain_skipped == 1); +} + +TEST_CASE("parser: for a scenery opened for editing the included files are told to the scenery layers") { + // the layers themselves are stubs in the tests (engine_stubs.cpp): the templates used are all they keep + test::scratch_folder const folder{"parser_layers"}; + folder.write("main.scm", "include szablon_testowy.inc end include other.scm 7 end tail"); + folder.write("szablon_testowy.inc", "p"); + folder.write("other.scm", "o (p1)"); + + cParser parser("main.scm", cParser::buffer_FILE, folder.path()); + parser.sceneryLayers = true; + CHECK(parser.InLayerFile()); + // a template isn't a layer file, a scenery file is + CHECK(parser.getToken() == "p"); + CHECK_FALSE(parser.InLayerFile()); + CHECK(scene::Layers.templates().count("szablon_testowy.inc") == 1); + CHECK(parser.getToken() == "o"); + CHECK(parser.InLayerFile()); + CHECK(rest(parser) == "7|tail"); + CHECK(parser.InLayerFile()); +} + +// what the helpers of utilities.h read with the parser ----------------------------------------------------------------- + +TEST_CASE("parser: a point is three numbers") { + cParser parser("1 2.5 -3 4", cParser::buffer_TEXT); + auto const point{LoadPoint(parser)}; + CHECK(point.x == doctest::Approx(1.0)); + CHECK(point.y == doctest::Approx(2.5)); + CHECK(point.z == doctest::Approx(-3.0)); + CHECK(parser.getToken() == 4); + // the ones there's no number for are zero + cParser few("1 2.5", cParser::buffer_TEXT); + auto const flat{LoadPoint(few)}; + CHECK(flat.x == doctest::Approx(1.0)); + CHECK(flat.y == doctest::Approx(2.5)); + CHECK(flat.z == 0.0); +} + +TEST_CASE("parser: a set of names is a single one, or several in brackets") { + cParser single("Dir\\File.T3D next", cParser::buffer_TEXT); + // in lower case, with the slashes turned + CHECK(deserialize_set(single) == std::vector{"dir/file.t3d"}); + CHECK(single.getToken() == "next"); + + cParser several("[ a B\\c d ] next", cParser::buffer_TEXT); + CHECK(deserialize_set(several) == std::vector{"a", "b/c", "d"}); + CHECK(several.getToken() == "next"); + + // the brackets have to stand apart from the names + cParser touching("[a b] next", cParser::buffer_TEXT); + CHECK(deserialize_set(touching) == std::vector{"[a"}); + // a set left open ends with the text, nothing gives an empty one + cParser open("[ a b", cParser::buffer_TEXT); + CHECK(deserialize_set(open) == std::vector{"a", "b"}); + cParser nothing("", cParser::buffer_TEXT); + CHECK(deserialize_set(nothing).empty()); + // the separators are the caller's + cParser commas("[,a,b,]", cParser::buffer_TEXT); + CHECK(deserialize_set(commas, ",") == std::vector{"a", "b"}); +} + +TEST_CASE("parser: one name of a set is picked at random") { + cParser single("Dir\\File.T3D next", cParser::buffer_TEXT); + CHECK(deserialize_random_set(single) == "dir/file.t3d"); + CHECK(single.getToken() == "next"); + + cParser one("[ only ] next", cParser::buffer_TEXT); + CHECK(deserialize_random_set(one) == "only"); + CHECK(one.getToken() == "next"); + + // the whole set is read whichever is picked + cParser several("[ a b c ] next", cParser::buffer_TEXT); + auto const picked{deserialize_random_set(several)}; + CHECK((picked == "a" || picked == "b" || picked == "c")); + CHECK(several.getToken() == "next"); + + // a set inside of a set gives one of its names to the outer one + cParser nested("[ [ x ] ] next", cParser::buffer_TEXT); + CHECK(deserialize_random_set(nested) == "x"); + CHECK(nested.getToken() == "next"); + // an empty set gives nothing + cParser empty("[ ] next", cParser::buffer_TEXT); + CHECK(deserialize_random_set(empty).empty()); + CHECK(empty.getToken() == "next"); +} + +TEST_CASE("parser: the same seed picks the same name of a set") { + std::string const text{"[ a b c d e f g h ]"}; + Global.random_engine.seed(7); + cParser first(text, cParser::buffer_TEXT); + auto const picked{deserialize_random_set(first)}; + Global.random_engine.seed(7); + cParser second(text, cParser::buffer_TEXT); + CHECK(deserialize_random_set(second) == picked); +} + +TEST_CASE("parser: a map is read a line for a key and its value") { + cParser parser("alpha One\nbeta Two Words", cParser::buffer_TEXT); + std::map map; + deserialize_map(map, parser); + CHECK(map.size() == 2); + // with the case kept, the value being the rest of the line + CHECK(map["alpha"] == "One"); + CHECK(map["beta"] == "Two Words"); + + SUBCASE("the values are read as their type") { + cParser numbers("a 1.5\nb 2", cParser::buffer_TEXT); + std::map values; + deserialize_map(values, numbers); + CHECK(values.size() == 2); + CHECK(values["a"] == doctest::Approx(1.5)); + CHECK(values["b"] == doctest::Approx(2.0)); + } + SUBCASE("a line end after the last line adds an empty key") { + cParser ended("alpha One\nbeta Two\n", cParser::buffer_TEXT); + std::map entries; + deserialize_map(entries, ended); + CHECK(entries.size() == 3); + CHECK(entries.count("") == 1); + } + SUBCASE("and so does an empty text") { + cParser empty("", cParser::buffer_TEXT); + std::map entries; + deserialize_map(entries, empty); + CHECK(entries.size() == 1); + CHECK(entries.count("") == 1); + } + SUBCASE("the carriage return of a line ended the Windows way stays in the value") { + cParser windows("alpha One\r\nbeta Two", cParser::buffer_TEXT); + std::map entries; + deserialize_map(entries, windows); + CHECK(entries["alpha"] == "One\r"); + CHECK(entries["beta"] == "Two"); + } +} From 5764692e89625af99ed675016335df9f793efaf4 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 11 Nov 2026 22:40:02 +0100 Subject: [PATCH 2/4] test: characterize the helpers of utilities The texts, numbers, random numbers, file names and times of utilities.cpp and utilities.h as they behave now. The table of Bezogonkow() is checked as the build has it: one byte for its plus-minus sign with MSVC, two with GCC and Clang, which give other letters. --- tests/unit/test_utilities.cpp | 724 ++++++++++++++++++++++++++++++++++ 1 file changed, 724 insertions(+) create mode 100644 tests/unit/test_utilities.cpp diff --git a/tests/unit/test_utilities.cpp b/tests/unit/test_utilities.cpp new file mode 100644 index 000000000..f04d9e34a --- /dev/null +++ b/tests/unit/test_utilities.cpp @@ -0,0 +1,724 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scratch.h" + +#include "utilities/Globals.h" +#include "utilities/utilities.h" + +// the helpers of utilities/utilities.cpp and utilities.h as they behave now: the numbers and texts they give, their +// oddities included. the ones which look like mistakes are listed in COVERAGE.md. the helpers which read from a +// parser (LoadPoint, the sets, the map) are in test_parser.cpp + +namespace { + +// puts the debug mode back as it was +struct debug_mode { + bool const before{DebugModeFlag}; + + explicit debug_mode(bool const Enabled) { DebugModeFlag = Enabled; } + debug_mode(debug_mode const &) = delete; + debug_mode &operator=(debug_mode const &) = delete; + ~debug_mode() { DebugModeFlag = before; } +}; + +// the polish letters in windows-1250, as in test_win1250.cpp +std::string const kWin1250{"\xA5\xC6\xCA\xA3\xD1\xD3\x8C\x8F\xAF\xB9\xE6\xEA\xB3\xF1\xF3\x9C\x9F\xBF"}; + +} // namespace + +// time ---------------------------------------------------------------------------------------------------------------- + +TEST_CASE("utilities: the time between two times of a day is in minutes, the shorter way round midnight") { + CHECK(CompareTime(10, 0, 10, 30) == 30.0); + CHECK(CompareTime(10, 30, 10, 0) == -30.0); + // 23:50 to 0:05 is a quarter later, not a day earlier + CHECK(CompareTime(23, 50, 0, 5) == 15.0); + CHECK(CompareTime(0, 5, 23, 50) == -15.0); + // half a day stays as it is counted + CHECK(CompareTime(0, 0, 12, 0) == 720.0); + CHECK(CompareTime(12, 0, 0, 0) == -720.0); + // a negative hour stands for no time given + CHECK(CompareTime(10, 0, -1, 30) == 0.0); +} + +TEST_CASE("utilities: the current time is given as a text") { + std::string now; + try { + now = Now(); + } + catch (std::runtime_error const &error) { + // the local time needs the time zone database of the system, which a bare one may be without + MESSAGE("Now() has no time zone to tell the local time in: " << error.what()); + return; + } + CHECK_FALSE(now.empty()); +} + +TEST_CASE("utilities: a time is taken apart the same way in every thread") { + // 2001-09-09 01:46:40 UTC, a Sunday + std::time_t const time{1000000000}; + auto const utc{utc_tm(time)}; + CHECK(utc.tm_year == 101); + CHECK(utc.tm_mon == 8); + CHECK(utc.tm_mday == 9); + CHECK(utc.tm_hour == 1); + CHECK(utc.tm_min == 46); + CHECK(utc.tm_sec == 40); + CHECK(utc.tm_wday == 0); + auto const epoch{utc_tm(0)}; + CHECK(epoch.tm_year == 70); + CHECK(epoch.tm_yday == 0); + CHECK(epoch.tm_hour == 0); + CHECK(epoch.tm_wday == 4); + // the local time is the one the system puts back together into the same moment + auto local{local_tm(time)}; + CHECK(std::mktime(&local) == time); +} + +// flags --------------------------------------------------------------------------------------------------------------- + +TEST_CASE("utilities: setting a flag tells whether it changed anything, a negative value clears") { + int flags{0}; + CHECK(SetFlag(flags, 4)); + CHECK(flags == 4); + CHECK_FALSE(SetFlag(flags, 4)); + // some of the bits set already still count as a change + CHECK(SetFlag(flags, 6)); + CHECK(flags == 6); + CHECK(TestFlag(flags, 6)); + CHECK_FALSE(TestFlag(flags, 3)); + CHECK(TestFlagAny(flags, 3)); + CHECK_FALSE(TestFlagAny(flags, 1)); + CHECK(SetFlag(flags, -4)); + CHECK(flags == 2); + CHECK_FALSE(SetFlag(flags, -4)); + CHECK_FALSE(SetFlag(flags, 0)); + CHECK(flags == 2); +} + +TEST_CASE("utilities: clearing a flag needs all of its bits set") { + int flags{1}; + CHECK_FALSE(ClearFlag(flags, 3)); + CHECK(flags == 1); + CHECK(ClearFlag(flags, 1)); + CHECK(flags == 0); + // no bits at all are always there to clear + CHECK(ClearFlag(flags, 0)); + CHECK(flags == 0); +} + +// random numbers ------------------------------------------------------------------------------------------------------ + +TEST_CASE("utilities: random whole numbers are within the bounds, both included, given in any order") { + std::set seen; + for (auto draw{0}; draw < 200; ++draw) { + auto const value{Random(3, 7)}; + CHECK(value >= 3); + CHECK(value <= 7); + seen.insert(value); + auto const swapped{Random(7, 3)}; + CHECK(swapped >= 3); + CHECK(swapped <= 7); + } + CHECK(seen.size() == 5); + CHECK(Random(5, 5) == 5); +} + +TEST_CASE("utilities: random numbers are within the bounds given in any order") { + for (auto draw{0}; draw < 200; ++draw) { + auto const value{Random(2.0, 4.0)}; + CHECK(value >= 2.0); + CHECK(value <= 4.0); + auto const swapped{Random(4.0, 2.0)}; + CHECK(swapped >= 2.0); + CHECK(swapped <= 4.0); + auto const unit{Random()}; + CHECK(unit >= 0.0); + CHECK(unit <= 1.0); + auto const upto{Random(10.0)}; + CHECK(upto >= 0.0); + CHECK(upto <= 10.0); + auto const local{LocalRandom(-1.0, 1.0)}; + CHECK(local >= -1.0); + CHECK(local <= 1.0); + auto const localswapped{LocalRandom(1.0, -1.0)}; + CHECK(localswapped >= -1.0); + CHECK(localswapped <= 1.0); + auto const localunit{LocalRandom()}; + CHECK(localunit >= 0.0); + CHECK(localunit <= 1.0); + auto const localupto{LocalRandom(10.0)}; + CHECK(localupto >= 0.0); + CHECK(localupto <= 10.0); + } +} + +TEST_CASE("utilities: the same seed gives the same random numbers") { + std::vector first; + Global.random_engine.seed(42); + for (auto draw{0}; draw < 5; ++draw) { + first.push_back(Random(0, 1000000)); + } + std::vector second; + Global.random_engine.seed(42); + for (auto draw{0}; draw < 5; ++draw) { + second.push_back(Random(0, 1000000)); + } + CHECK(first == second); +} + +TEST_CASE("utilities: the local random numbers have an engine of their own") { + Global.local_random_engine.seed(5); + auto const first{LocalRandom()}; + Global.local_random_engine.seed(5); + // the numbers of the simulation drawn in between don't move it + Random(); + Random(1, 6); + CHECK(LocalRandom() == first); +} + +TEST_CASE("utilities: a seed of the system's entropy is there to be drawn") { + auto const first{true_random_seed()}; + auto const second{true_random_seed()}; + CHECK((first != 0 || second != 0)); +} + +TEST_CASE("utilities: the seed a text stands for is its number, or its hash the way Java counts it") { + CHECK(seed_of("12345") == 12345u); + CHECK(seed_of("0") == 0u); + CHECK(seed_of("4294967295") == 4294967295u); + CHECK(seed_of("hello") == 99162322u); + CHECK(seed_of("test") == 3556498u); + CHECK(seed_of("") == 0u); + // a number which isn't a seed is a word: negative, or too big + CHECK(seed_of("-5") == 1448u); + CHECK(seed_of("4294967296") == 3632702254u); + // the bytes over ascii count as positive + CHECK(seed_of("\xB9") == 185u); + // a word starting with digits is taken for the number in front of it + CHECK(seed_of("12abc") == 12u); +} + +TEST_CASE("utilities: a version 4 uuid is drawn from the random numbers of the simulation") { + auto const id{generate_uuid_v4()}; + REQUIRE(id.size() == 36); + auto wrong{0}; + for (std::size_t index{0}; index < id.size(); ++index) { + auto const character{id[index]}; + if (index == 8 || index == 13 || index == 18 || index == 23) { + wrong += (character == '-' ? 0 : 1); + } + else { + wrong += ((character >= '0' && character <= '9') || (character >= 'a' && character <= 'f') ? 0 : 1); + } + } + CAPTURE(id); + CHECK(wrong == 0); + // the version and the variant + CHECK(id[14] == '4'); + CHECK((id[19] == '8' || id[19] == '9' || id[19] == 'a' || id[19] == 'b')); + CHECK(generate_uuid_v4() != id); + // the same seed gives the same one + Global.random_engine.seed(11); + auto const first{generate_uuid_v4()}; + Global.random_engine.seed(11); + CHECK(generate_uuid_v4() == first); +} + +TEST_CASE("utilities: a failure is drawn only over the threshold, the likelier the less the value is over it") { + debug_mode const mode{false}; + for (auto draw{0}; draw < 50; ++draw) { + // at the threshold and under it: never + CHECK_FALSE(FuzzyLogic(1.0, 2.0, 1000.0)); + CHECK_FALSE(FuzzyLogic(2.0, 2.0, 1000.0)); + CHECK_FALSE(FuzzyLogicAI(1.0, 2.0, 1000.0)); + CHECK_FALSE(FuzzyLogicAI(2.0, 2.0, 1000.0)); + // a chance over 1 is a certainty, a chance of 0 never comes + CHECK(FuzzyLogic(2.0, 1.0, 10.0)); + CHECK(FuzzyLogicAI(2.0, 1.0, 10.0)); + CHECK_FALSE(FuzzyLogic(2.0, 1.0, 0.0)); + CHECK_FALSE(FuzzyLogicAI(2.0, 1.0, 0.0)); + } +} + +TEST_CASE("utilities: the debug mode keeps the failures away, but not the ones drawn regardless of it") { + debug_mode const mode{true}; + CHECK_FALSE(FuzzyLogic(2.0, 1.0, 10.0)); + CHECK(FuzzyLogicAI(2.0, 1.0, 10.0)); +} + +// texts --------------------------------------------------------------------------------------------------------------- + +TEST_CASE("utilities: a text is split at every separator, the empty parts kept") { + CHECK(Split("a,b,,c", ',') == std::vector{"a", "b", "", "c"}); + CHECK(Split("abc", ',') == std::vector{"abc"}); + CHECK(Split("a,", ',') == std::vector{"a", ""}); + CHECK(Split(",a", ',') == std::vector{"", "a"}); + CHECK(Split("10|20", '|') == std::vector{"10", "20"}); + // an empty text has no parts at all + CHECK(Split("", ',').empty()); +} + +TEST_CASE("utilities: a name is split from the number it ends with") { + CHECK(split_string_and_number("abc12") == std::pair{"abc", 12}); + CHECK(split_string_and_number("t1") == std::pair{"t", 1}); + CHECK(split_string_and_number("abc") == std::pair{"abc", 0}); + CHECK(split_string_and_number("") == std::pair{"", 0}); + CHECK(split_string_and_number("12") == std::pair{"", 12}); + // a minus is a part of the number + CHECK(split_string_and_number("x-5") == std::pair{"x", -5}); + // the number is the first one, what follows it is dropped + CHECK(split_string_and_number("a1b2") == std::pair{"a", 1}); +} + +TEST_CASE("utilities: a name with a hyphen, or with a number too big, is an exception") { + // the hyphen is taken for the start of a number + CHECK_THROWS_AS(split_string_and_number("a-b"), std::invalid_argument); + CHECK_THROWS_AS(split_string_and_number("door-"), std::invalid_argument); + CHECK_THROWS_AS(split_string_and_number("a99999999999"), std::out_of_range); +} + +TEST_CASE("utilities: whole numbers are written to a width, padded with spaces on the left") { + CHECK(to_string(5, 3) == " 5"); + CHECK(to_string(-5, 4) == " -5"); + CHECK(to_string(12345, 3) == "12345"); + CHECK(to_string(7, 0) == "7"); +} + +TEST_CASE("utilities: numbers are written with the decimals asked for, and to a width") { + CHECK(to_string(3.14159, 2) == "3.14"); + CHECK(to_string(1.0, 3) == "1.000"); + CHECK(to_string(-1.26, 1) == "-1.3"); + CHECK(to_string(2.675, 2) == "2.67"); + // a half rounds to the even digit + CHECK(to_string(2.5, 0) == "2"); + CHECK(to_string(3.14159, 2, 8) == " 3.14"); + CHECK(to_string(-1.5, 1, 6) == " -1.5"); + // a number wider than the width isn't cut + CHECK(to_string(123.456, 1, 3) == "123.5"); + CHECK(to_string(true) == "true"); + CHECK(to_string(false) == "false"); +} + +TEST_CASE("utilities: numbers are written in hex with capital digits, the negative ones as unsigned") { + CHECK(to_hex_str(255) == "0x00FF"); + CHECK(to_hex_str(255, 2) == "0xFF"); + CHECK(to_hex_str(0, 8) == "0x00000000"); + CHECK(to_hex_str(0x12345) == "0x12345"); + CHECK(to_hex_str(-1) == "0xFFFFFFFF"); +} + +TEST_CASE("utilities: the minutes of a timetable are written with their tenths as a raised digit") { + // two characters for the minutes, then the digit, a space for no tenths; the digits are utf-8 + CHECK(to_minutes_str(5.5f, true, 3) == "05\xE2\x81\xB5"); + CHECK(to_minutes_str(7.9f, true, 3) == "07\xE2\x81\xB9"); + CHECK(to_minutes_str(0.0f, true, 3) == "00 "); + CHECK(to_minutes_str(5.0f, false, 3) == " 5 "); + CHECK(to_minutes_str(12.3f, false, 3) == "12\xC2\xB3"); + CHECK(to_minutes_str(5.0f, false, 5) == " 5 "); + // only the last digits fit + CHECK(to_minutes_str(123.0f, false, 3) == "23 "); +} + +TEST_CASE("utilities: minutes can't be written a single character wide") { + // the padding is counted as one character less than nothing + CHECK_THROWS_AS(to_minutes_str(5.0f, false, 1), std::length_error); +} + +TEST_CASE("utilities: a text is read as a whole number, with a default for an empty one") { + CHECK(stol_def("42", 7) == 42); + CHECK(stol_def("-3", 7) == -3); + CHECK(stol_def(" 15 ", 7) == 15); + CHECK(stol_def("12abc", 7) == 12); + CHECK(stol_def("", 7) == 7); + CHECK(stol_def(" ", 7) == 7); + // a text which isn't a number gives zero, not the default + CHECK(stol_def("abc", 7) == 0); +} + +TEST_CASE("utilities: the case of the letters of ascii is changed") { + CHECK(ToLower("AbC-12 Zz") == "abc-12 zz"); + CHECK(ToUpper("AbC-12 Zz") == "ABC-12 ZZ"); + CHECK(ToLower("").empty()); + // the other bytes stay, in the "C" locale the tests run in + CHECK(ToUpper("\xB3\xC5\x82") == "\xB3\xC5\x82"); + CHECK(ToLower("\xA3\xC5\x81") == "\xA3\xC5\x81"); +} + +TEST_CASE("utilities: the polish letters of windows-1250 are replaced with the letters of ascii") { + auto text{kWin1250}; + win1250_to_ascii(text); + CHECK(text == "ACELNOSZZacelnoszz"); + // the rest of the text stays, other letters with marks too + std::string other{"tor 1, caf\xE9"}; + win1250_to_ascii(other); + CHECK(other == "tor 1, caf\xE9"); +} + +TEST_CASE("utilities: a text for the timetable loses its marks, control characters and, when asked, underscores") { + // the letters up to 0xB0, which the table has in the same place whatever the compiler + CHECK(Bezogonkow("\xA5\xA3\x8C\x8F\xAF\x9C\x9F") == "ALSZZsz"); + CHECK(Bezogonkow("Warszawa Zachodnia") == "Warszawa Zachodnia"); + CHECK(Bezogonkow("a\tb\nc") == "a b c"); + CHECK(Bezogonkow("Bia\xA3" "a_Podlaska") == "BiaLa_Podlaska"); + CHECK(Bezogonkow("Bia\xA3" "a_Podlaska", true) == "BiaLa Podlaska"); +} + +TEST_CASE("utilities: the polish letters from 0xB2 on lose their marks only in a build which makes one byte of the table's mark") { + // the table of Bezogonkow() has a plus-minus sign for 0xB1, written as the character in the source, a utf-8 file. + // MSVC puts it into the table as the one byte of the system's code page, and the table is as it was meant. + // GCC and Clang put the two bytes of utf-8 there, which moves the entries after it by one (see COVERAGE.md) + if (Bezogonkow("\xB1") == "\xB1") { + CHECK(Bezogonkow(kWin1250) == "ACELNOSZZacelnoszz"); + } + else { + // the first byte of the sign for 0xB1, the second for 0xB2, and each letter after them as the entry before its own + CHECK(Bezogonkow("\xB1\xB2") == "\xC2\xB1"); + CHECK(Bezogonkow(kWin1250) == "ALELDNSZZ,le,dnszl"); + } +} + +TEST_CASE("utilities: utf-8 is decoded into code points of one to four bytes") { + std::u32string decoded{U"old"}; + REQUIRE(utf8_to_utf32("a\xC5\x82\xE2\x82\xAC\xF0\x9F\x98\x80", decoded)); + REQUIRE(decoded.size() == 4); + CHECK(decoded[0] == U'a'); + CHECK(decoded[1] == char32_t{0x142}); + CHECK(decoded[2] == char32_t{0x20AC}); + CHECK(decoded[3] == char32_t{0x1F600}); + REQUIRE(utf8_to_utf32("", decoded)); + CHECK(decoded.empty()); +} + +TEST_CASE("utilities: what isn't utf-8 is refused") { + std::u32string decoded; + // a byte which can't start a sequence + CHECK_FALSE(utf8_to_utf32("\x80", decoded)); + CHECK_FALSE(utf8_to_utf32("\xF8\x88\x80\x80\x80", decoded)); + // a sequence cut short, or continued with a byte which doesn't continue one + CHECK_FALSE(utf8_to_utf32("\xE2\x82", decoded)); + CHECK_FALSE(utf8_to_utf32("\xC5\x41", decoded)); + // a code point written longer than it needs + CHECK_FALSE(utf8_to_utf32("\xC0\x80", decoded)); + CHECK_FALSE(utf8_to_utf32("\xE0\x80\xAF", decoded)); + // a surrogate, and a code point past the last one + CHECK_FALSE(utf8_to_utf32("\xED\xA0\x80", decoded)); + CHECK_FALSE(utf8_to_utf32("\xF4\x90\x80\x80", decoded)); + // what was decoded before the bad byte is left in the output + CHECK_FALSE(utf8_to_utf32("ab\x80", decoded)); + CHECK(decoded == std::u32string{U"ab"}); +} + +TEST_CASE("utilities: windows-1250 is decoded into code points, the unassigned bytes into zeros") { + std::u32string decoded{U"old"}; + win1250_to_utf32("A\xB3\xA5\x80\xFF\x81", decoded); + REQUIRE(decoded.size() == 6); + CHECK(decoded[0] == U'A'); + CHECK(decoded[1] == char32_t{0x142}); + CHECK(decoded[2] == char32_t{0x104}); + CHECK(decoded[3] == char32_t{0x20AC}); + CHECK(decoded[4] == char32_t{0x2D9}); + CHECK(decoded[5] == char32_t{0}); + win1250_to_utf32("", decoded); + CHECK(decoded.empty()); +} + +TEST_CASE("utilities: the value of a key is the text after its equals sign, up to the next space") { + CHECK(extract_value("Key", "a=1 Key=val b=2") == "val"); + CHECK(extract_value("Key", "Key=val") == "val"); + CHECK(extract_value("Key", "a=1 Key= val b=2") == "val"); + CHECK(extract_value("Key", "a=1 b=2").empty()); + // the key is a whole word, in its case + CHECK(extract_value("Key", "MyKey=val key=other").empty()); +} + +TEST_CASE("utilities: a key without a value takes the next key for it, and is an exception at the end of the text") { + CHECK(extract_value("Key", "Key= Other=5") == "Other=5"); + CHECK_THROWS_AS(extract_value("Key", "a=1 Key="), std::out_of_range); + CHECK_THROWS_AS(extract_value("Key", "a=1 Key= "), std::out_of_range); +} + +TEST_CASE("utilities: the value of a key is read into a variable, or the default given when the key isn't there") { + int number{-1}; + CHECK(extract_value(number, "Key", "a=1 Key=12", "")); + CHECK(number == 12); + CHECK_FALSE(extract_value(number, "Other", "a=1 Key=12", "7")); + CHECK(number == 7); + // no default leaves the variable as it is + CHECK_FALSE(extract_value(number, "Other", "a=1 Key=12", "")); + CHECK(number == 7); + double real{0.0}; + CHECK(extract_value(real, "Key", "Key=2.5", "")); + CHECK(real == doctest::Approx(2.5)); + + // a flag is on only for a yes + bool flag{false}; + CHECK(extract_value(flag, "Key", "Key=Yes", "")); + CHECK(flag); + CHECK(extract_value(flag, "Key", "Key=1", "")); + CHECK_FALSE(flag); + CHECK(extract_value(flag, "Key", "Key=true", "")); + CHECK_FALSE(flag); + CHECK_FALSE(extract_value(flag, "Other", "Key=no", "yes")); + CHECK(flag); + CHECK_FALSE(extract_value(flag, "Other", "Key=no", "")); + CHECK(flag); +} + +TEST_CASE("utilities: a text has a hash which is the same on every system") { + // FNV-1a, 32 bits + CHECK(hash_string("") == 2166136261u); + CHECK(hash_string("a") == 0xE40C292Cu); + CHECK(hash_string("foobar") == 0xBF9CF968u); + CHECK(hash_string("EU07-005") == 1926712284u); + // the seed gives another one + CHECK(hash_string("", 5) == 2166136256u); + CHECK(hash_string("EU07-005", 42) == 1563400698u); +} + +TEST_CASE("utilities: the common start of two texts is counted, and a text is looked for in another") { + CHECK(len_common_prefix("abcdef", "abcxyz") == 3); + CHECK(len_common_prefix("abc", "abc") == 3); + CHECK(len_common_prefix("abc", "ab") == 2); + CHECK(len_common_prefix("", "abc") == 0); + CHECK(len_common_prefix("xbc", "abc") == 0); + CHECK(contains("dynamic/pkp/eu07", "pkp")); + CHECK_FALSE(contains("dynamic/pkp/eu07", "PKP")); + CHECK(contains("abc", "")); + CHECK(contains("abc", 'c')); + CHECK_FALSE(contains("abc", 'd')); +} + +// file names ---------------------------------------------------------------------------------------------------------- + +TEST_CASE("utilities: the extension of a file name is cut off at its last dot") { + auto const erased{[](std::string Name) { + auto const removed{erase_extension(Name)}; + return std::pair{removed, Name}; + }}; + CHECK(erased("file.mat") == std::pair{true, "file"}); + CHECK(erased("a.b.c") == std::pair{true, "a.b"}); + CHECK(erased("file.") == std::pair{true, "file"}); + CHECK(erased("../file.mat") == std::pair{true, "../file"}); + CHECK(erased("noext") == std::pair{false, "noext"}); + CHECK(erased("") == std::pair{false, ""}); + // the dots of the folder above aren't one + CHECK(erased("../file") == std::pair{false, "../file"}); + CHECK(erased("textures/../x") == std::pair{false, "textures/../x"}); + // nor is a dot the name starts with + CHECK(erased(".hidden") == std::pair{false, ".hidden"}); + // a dot in the name of a folder is taken for one + CHECK(erased("dir.d/file") == std::pair{true, "dir"}); +} + +TEST_CASE("utilities: the slashes of a file name are turned and the leading ones removed") { + std::string name{"dynamic\\pkp\\eu07.mmd"}; + replace_slashes(name); + CHECK(name == "dynamic/pkp/eu07.mmd"); + std::string leading{"//textures/a.dds"}; + erase_leading_slashes(leading); + CHECK(leading == "textures/a.dds"); + std::string plain{"textures/a.dds"}; + erase_leading_slashes(plain); + CHECK(plain == "textures/a.dds"); + std::string only{"///"}; + erase_leading_slashes(only); + CHECK(only.empty()); + // only the forward ones + std::string backward{"\\textures"}; + erase_leading_slashes(backward); + CHECK(backward == "\\textures"); +} + +TEST_CASE("utilities: the folder of a file name is the part up to its last slash, the slash included") { + CHECK(substr_path("dynamic/pkp/eu07.mmd") == "dynamic/pkp/"); + CHECK(substr_path("/file") == "/"); + CHECK(substr_path("file.t3d").empty()); + CHECK(substr_path("").empty()); + // a folder written the Windows way isn't found + CHECK(substr_path("dynamic\\eu07.mmd").empty()); +} + +TEST_CASE("utilities: a file is told to be there, and looked for among names and extensions") { + test::scratch_folder const folder{"utilities_files"}; + folder.write("tex.png", "x"); + folder.write("other.dds", "x"); + auto const base{folder.path()}; + + CHECK(FileExists(base + "tex.png")); + CHECK_FALSE(FileExists(base + "tex.dds")); + // a folder is there as well + CHECK(FileExists(base)); + + // the first name which has a file with one of the extensions, the extensions tried in their order + auto const found{FileExists(std::vector{base + "missing", base + "tex", base + "other"}, std::vector{".dds", ".png"})}; + CHECK(found.first == base + "tex"); + CHECK(found.second == ".png"); + auto const other{FileExists(std::vector{base + "other", base + "tex"}, std::vector{".png", ".dds"})}; + CHECK(other.first == base + "other"); + CHECK(other.second == ".dds"); + auto const none{FileExists(std::vector{base + "missing"}, std::vector{".dds", ".png"})}; + CHECK(none.first.empty()); + CHECK(none.second.empty()); +} + +TEST_CASE("utilities: a file is found by its name in another case") { + test::scratch_folder const folder{"utilities_cased"}; + auto const name{folder.write("Cased.TXT", "x")}; + + // named as it is, it's given back as it was named + CHECK(FileCased(name) == name); + // named in another case: the name it has on the disk where the case matters, the name given where it doesn't + auto const cased{FileCased(folder.path("cased.txt"))}; + REQUIRE_FALSE(cased.empty()); + CHECK(FileExists(cased)); + CHECK(ToLower(std::filesystem::path{cased}.filename().string()) == "cased.txt"); + // no such file in any case + CHECK(FileCased(folder.path("nothing.txt")).empty()); + CHECK(FileCased("eu07_unit_test_no_such_file.xyz").empty()); +} + +TEST_CASE("utilities: the time a file was changed is given, zero for a file which isn't there") { + test::scratch_folder const folder{"utilities_modified"}; + auto const name{folder.write("fresh.txt", "x")}; + auto const modified{last_modified(name)}; + CHECK(modified != 0); + // written just now + CHECK(std::abs(std::difftime(std::time(nullptr), modified)) < 24.0 * 3600.0); + CHECK(last_modified(folder.path("nothing.txt")) == 0); +} + +TEST_CASE("utilities: the folders of the user's settings and of the cache are named after the game") { + // where they are depends on the system and its environment; no folder at all without the variables + if (auto const config{user_config_path("eu07.ini")}; false == config.empty()) { + CHECK(config.filename().string() == "eu07.ini"); + CHECK(config.parent_path().filename().string() == "MaSzyna"); + } + if (auto const cache{user_cache_path("geoportal")}; false == cache.empty()) { + CHECK(cache.filename().string() == "geoportal"); +#if defined(_WIN32) + CHECK(cache.parent_path().filename().string() == "cache"); + CHECK(cache.parent_path().parent_path().filename().string() == "MaSzyna"); +#else + CHECK(cache.parent_path().filename().string() == "MaSzyna"); +#endif + } +#if defined(_WIN32) + CHECK(user_config_path("eu07.ini").empty() == (std::getenv("APPDATA") == nullptr)); + CHECK(user_cache_path("x").empty() == (std::getenv("LOCALAPPDATA") == nullptr)); +#else + CHECK(user_config_path("eu07.ini").empty() == (std::getenv("HOME") == nullptr)); +#endif +} + +// numbers ------------------------------------------------------------------------------------------------------------- + +TEST_CASE("utilities: the sign of a number, and a number rounded to a whole one") { + CHECK(sign(-2.5) == -1.0); + CHECK(sign(0.0) == 0.0); + CHECK(sign(3) == 1); + // the other one has no zero + CHECK(Sign(0.0) == 1.0); + CHECK(Sign(-0.1) == -1.0); + CHECK(sq(-3) == 9); + CHECK(Round(2.4) == 2); + CHECK(Round(2.5) == 3); + CHECK(Round(0.0) == 0); + // the negative ones are rounded up: half is added, then the fraction cut + CHECK(Round(-0.6) == 0); + CHECK(Round(-1.5) == -1); + CHECK(Round(-1.6) == -1); + CHECK(Round(-2.5) == -2); +} + +TEST_CASE("utilities: a value is kept within bounds, given in any order") { + CHECK(safe_clamp(5, 0, 3) == 3); + CHECK(safe_clamp(5, 3, 0) == 3); + CHECK(safe_clamp(-1, 3, 0) == 0); + CHECK(safe_clamp(2.0, 3.0, 1.0) == 2.0); + CHECK(safe_clamp(2.0, 2.0, 2.0) == 2.0); +} + +TEST_CASE("utilities: a value is wrapped into a range, as an angle into the full circle") { + CHECK(clamp_circular(370.0) == doctest::Approx(10.0)); + CHECK(clamp_circular(-10.0) == doctest::Approx(350.0)); + CHECK(clamp_circular(360.0) == doctest::Approx(0.0)); + CHECK(clamp_circular(720.0) == doctest::Approx(0.0)); + CHECK(clamp_circular(7, 3) == 1); + CHECK(clamp_circular(-1, 3) == 2); + CHECK(clamp_circular(3, 3) == 0); +} + +TEST_CASE("utilities: a size is rounded down to a power of two within its limits") { + CHECK(clamp_power_of_two(100u) == 64u); + CHECK(clamp_power_of_two(1024u) == 1024u); + CHECK(clamp_power_of_two(0u) == 1u); + CHECK(clamp_power_of_two(20000u) == 16384u); + CHECK(clamp_power_of_two(40000u) == 16384u); + CHECK(clamp_power_of_two(100u, 128u, 8192u) == 128u); + CHECK(clamp_power_of_two(10000u, 128u, 8192u) == 8192u); +} + +TEST_CASE("utilities: a value is rounded to a step, a whole number down to it") { + CHECK(quantize(0.12f, 0.05f) == doctest::Approx(0.10)); + CHECK(quantize(1.3, 0.5) == doctest::Approx(1.5)); + CHECK(quantize(-1.3, 0.5) == doctest::Approx(-1.5)); + // the division of whole numbers drops the fraction before the rounding + CHECK(quantize(7u, 4u) == 4u); + CHECK(quantize(8u, 4u) == 8u); +} + +TEST_CASE("utilities: the lower of two speed limits, -1 standing for none") { + CHECK(min_speed(30.0, 50.0) == 30.0); + CHECK(min_speed(-1.0, 50.0) == 50.0); + CHECK(min_speed(50.0, -1.0) == 50.0); + CHECK(min_speed(-1.0, -1.0) == -1.0); + CHECK(min_speed(0.0, 50.0) == 0.0); +} + +TEST_CASE("utilities: a value between two others is eased in and out") { + CHECK(smoothInterpolate(0.0, 10.0, 0.0) == doctest::Approx(0.0)); + CHECK(smoothInterpolate(0.0, 10.0, 0.25) == doctest::Approx(1.5625)); + CHECK(smoothInterpolate(0.0, 10.0, 0.5) == doctest::Approx(5.0)); + CHECK(smoothInterpolate(0.0, 10.0, 1.0) == doctest::Approx(10.0)); + CHECK(smoothInterpolate(10.0, 0.0, 0.25) == doctest::Approx(8.4375)); + // the factor isn't kept between 0 and 1 + CHECK(smoothInterpolate(0.0, 10.0, 2.0) == doctest::Approx(-40.0)); +} + +TEST_CASE("utilities: two numbers are equal within a margin, exactly with none") { + CHECK(is_equal(1.0, 1.000001)); + CHECK_FALSE(is_equal(1.0, 1.1)); + CHECK(is_equal(1.0, 1.05, 0.1)); + CHECK(is_equal(1.0, 1.0, 0.0)); + CHECK_FALSE(is_equal(1.0, 1.000001, 0.0)); +} + +TEST_CASE("utilities: the point of a segment nearest to another point is given as a part of the segment") { + glm::dvec3 const start{0.0, 0.0, 0.0}; + glm::dvec3 const end{10.0, 0.0, 0.0}; + CHECK(nearest_segment_point(start, end, glm::dvec3{5.0, 3.0, 0.0}) == doctest::Approx(0.5)); + CHECK(nearest_segment_point(start, end, glm::dvec3{2.5, -7.0, 1.0}) == doctest::Approx(0.25)); + // past the ends it's the end + CHECK(nearest_segment_point(start, end, glm::dvec3{-4.0, 1.0, 0.0}) == 0.0); + CHECK(nearest_segment_point(start, end, glm::dvec3{14.0, 1.0, 0.0}) == 1.0); +} + +TEST_CASE("utilities: points in a line make no triangle, and a set of points has a box around it") { + glm::dvec3 const a{0.0, 0.0, 0.0}; + glm::dvec3 const b{1.0, 1.0, 1.0}; + CHECK(degenerate(a, b, glm::dvec3{2.0, 2.0, 2.0})); + CHECK(degenerate(a, b, b)); + CHECK_FALSE(degenerate(a, b, glm::dvec3{1.0, 0.0, 0.0})); + + std::vector points{{1.0, -2.0, 3.0}, {-4.0, 5.0, 0.5}, {0.0, 0.0, 9.0}}; + glm::dvec3 low; + glm::dvec3 high; + bounding_box(low, high, std::begin(points), std::end(points)); + CHECK(low.x == -4.0); + CHECK(low.y == -2.0); + CHECK(low.z == 0.5); + CHECK(high.x == 1.0); + CHECK(high.y == 5.0); + CHECK(high.z == 9.0); +} From a53fed0a6f4edaa6cf0ffea51992e455654b4806 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 11 Nov 2026 22:40:02 +0100 Subject: [PATCH 3/4] test: characterize the PLC of the vehicles Programs of ladder logic made up for the tests: loading, the operations, timers and counters of scripting/ladderlogic.cpp. The game doesn't load a program into the controller yet. --- tests/unit/test_ladder_logic.cpp | 438 +++++++++++++++++++++++++++++++ 1 file changed, 438 insertions(+) create mode 100644 tests/unit/test_ladder_logic.cpp diff --git a/tests/unit/test_ladder_logic.cpp b/tests/unit/test_ladder_logic.cpp new file mode 100644 index 000000000..0f21923b7 --- /dev/null +++ b/tests/unit/test_ladder_logic.cpp @@ -0,0 +1,438 @@ +#include "stdafx.h" +#include "doctest.h" +#include "scratch.h" + +#include "scripting/ladderlogic.h" + +// the programmable controller of a vehicle (scripting/ladderlogic.cpp): a program of ladder logic in the instruction +// list form (ld, and, or, out, set, rst...), run once in every step of the vehicle, with variables, timers and +// counters told apart by their names. nothing in the game loads a program into it yet (see COVERAGE.md), so no game +// vehicle has one: the programs here are made up, and the tests record how the controller runs them now + +namespace { + +// a controller, and a folder for the file of its program +class controller_fixture { + public: + explicit controller_fixture(std::string const &Name) : m_folder{"plc_" + Name} {} + + bool load(std::string_view const Program) { return controller.load(m_folder.write("program.plc", Program)); } + // the element of that name, made as a variable if neither the program nor the test used the name yet + int &in(std::string const &Name) { return controller.input(controller.find_or_insert(Name)); } + int out(std::string const &Name) { return controller.output(controller.find_or_insert(Name)); } + // runs the program, a tenth of a second for every cycle: one step of the timers + void cycles(int const Count = 1) { + for (auto cycle{0}; cycle < Count; ++cycle) { + controller.update(0.1); + } + } + // a cycle with the element on, then one with it off + void pulse(std::string const &Name) { + in(Name) = 1; + cycles(); + in(Name) = 0; + cycles(); + } + std::string missing_file() const { return m_folder.path("nothing.plc"); } + + plc::basic_controller controller; + + private: + test::scratch_folder m_folder; +}; + +} // namespace + +// loading ------------------------------------------------------------------------------------------------------------- + +TEST_CASE("plc: a program is read from a file, an operation a line, and run with every update") { + controller_fixture plc{"load"}; + REQUIRE(plc.load("ld start\nout lamp\n")); + plc.in("start") = 1; + CHECK(plc.controller.update(0.1) == 0); + CHECK(plc.out("lamp") == 1); + plc.in("start") = 0; + CHECK(plc.controller.update(0.1) == 0); + CHECK(plc.out("lamp") == 0); +} + +TEST_CASE("plc: a controller without a program runs nothing") { + plc::basic_controller controller; + auto const lamp{controller.find_or_insert("lamp")}; + controller.input(lamp) = 1; + CHECK(controller.update(0.1) == 0); + CHECK(controller.output(lamp) == 1); +} + +TEST_CASE("plc: the commands are read in any case, with tabs, comments and empty lines around them") { + controller_fixture plc{"layout"}; + REQUIRE(plc.load("// the lamp follows the button\r\n\r\nLD\tStart\r\n OUT Lamp // done\r\n\r\n")); + // the names of the elements are put in lower case + plc.in("start") = 1; + plc.cycles(); + CHECK(plc.out("lamp") == 1); + // and looked up as they are given: this one is another element + CHECK(plc.controller.find_or_insert("Start") != plc.controller.find_or_insert("start")); +} + +TEST_CASE("plc: a file which isn't there, or has no line to read, isn't loaded") { + controller_fixture plc{"missing"}; + CHECK_FALSE(plc.controller.load(plc.missing_file())); + CHECK_FALSE(plc.load("")); + // a comment alone is no line + CHECK_FALSE(plc.load("// nothing but a comment\n")); + // any line is enough, also one which is no operation + CHECK(plc.load("end\n")); + CHECK(plc.load("blink lamp\n")); +} + +TEST_CASE("plc: a failed load leaves the controller without a program") { + controller_fixture plc{"unloaded"}; + REQUIRE(plc.load("ld start\nout lamp\n")); + plc.in("start") = 1; + plc.cycles(); + REQUIRE(plc.out("lamp") == 1); + CHECK_FALSE(plc.controller.load(plc.missing_file())); + // nothing drives the lamp any more + plc.in("start") = 0; + plc.cycles(); + CHECK(plc.out("lamp") == 1); +} + +TEST_CASE("plc: an unknown command is passed over, and end doesn't end the program") { + controller_fixture plc{"unknown"}; + REQUIRE(plc.load("ld a\nblink y\nout y\nend\nld b\nout z\n")); + plc.in("a") = 1; + plc.in("b") = 1; + plc.cycles(); + CHECK(plc.out("y") == 1); + CHECK(plc.out("z") == 1); +} + +TEST_CASE("plc: another program loaded keeps the elements of the one before, and their states") { + controller_fixture plc{"reload"}; + REQUIRE(plc.load("ld a\nout b\n")); + plc.in("a") = 1; + plc.cycles(); + REQUIRE(plc.out("b") == 1); + CHECK(plc.controller.find_or_insert("a") == 1); + CHECK(plc.controller.find_or_insert("b") == 2); + + REQUIRE(plc.load("ld b\nout d\n")); + CHECK(plc.controller.find_or_insert("b") == 2); + CHECK(plc.controller.find_or_insert("d") == 3); + plc.cycles(); + CHECK(plc.out("d") == 1); + // nothing drives b any more, it stays as the first program left it + plc.in("a") = 0; + plc.cycles(); + CHECK(plc.out("b") == 1); + CHECK(plc.out("d") == 1); +} + +// elements ------------------------------------------------------------------------------------------------------------ + +TEST_CASE("plc: an element is found by its name, or made on its first use") { + plc::basic_controller controller; + auto const first{controller.find_or_insert("a")}; + CHECK(first == 1); + CHECK(controller.find_or_insert("b") == 2); + CHECK(controller.find_or_insert("a") == 1); + // a variable gives what was put in it + CHECK(controller.output(first) == 0); + controller.input(first) = 5; + CHECK(controller.output(first) == 5); + // it can be made with a value + auto const preset{controller.find_or_insert("preset", plc::basic_element::type_e::variable, 1)}; + CHECK(controller.output(preset) == 1); + // the kind asked for doesn't change an element which is there already + CHECK(controller.find_or_insert("a", plc::basic_element::type_e::counter) == 1); + CHECK(controller.output(first) == 5); +} + +TEST_CASE("plc: a new timer without a time passes its input on, a new counter without a limit is done") { + plc::basic_controller controller; + auto const timer{controller.find_or_insert("delay", plc::basic_element::type_e::timer)}; + CHECK(controller.output(timer) == 0); + controller.input(timer) = 3; + CHECK(controller.output(timer) == 3); + auto const counter{controller.find_or_insert("laps", plc::basic_element::type_e::counter)}; + CHECK(controller.output(counter) == 1); + controller.input(counter) = 0; + CHECK(controller.output(counter) == 1); +} + +TEST_CASE("plc: the kind of an element the program names is told by the letters before its number") { + controller_fixture plc{"kinds"}; + REQUIRE(plc.load("ld x\nout c1\nout c\nout counter.laps\nout cab1\nout count\nout t1 k200\nout ton2 k200\nout timer.delay k200\nout t k200\nout temp k200\n")); + // before the first cycle: a counter has no limit yet and is done, the others are off + for (auto const *counter : {"c1", "c", "counter.laps"}) { + CAPTURE(counter); + CHECK(plc.out(counter) == 1); + } + for (auto const *other : {"cab1", "count", "t1", "ton2", "timer.delay", "t", "temp"}) { + CAPTURE(other); + CHECK(plc.out(other) == 0); + } + // switched on, a variable is on at once and a timer waits for its time + plc.in("x") = 1; + plc.cycles(); + for (auto const *variable : {"cab1", "count", "temp"}) { + CAPTURE(variable); + CHECK(plc.out(variable) == 1); + } + for (auto const *timer : {"t1", "ton2", "timer.delay", "t"}) { + CAPTURE(timer); + CHECK(plc.out(timer) == 0); + } + plc.cycles(2); + for (auto const *timer : {"t1", "ton2", "timer.delay", "t"}) { + CAPTURE(timer); + CHECK(plc.out(timer) == 1); + } +} + +TEST_CASE("plc: a name with a hyphen can't be loaded") { + // the hyphen is taken for the start of the number of the element, and isn't one + controller_fixture plc{"hyphen"}; + CHECK_THROWS_AS(plc.load("ld door-open\nout lamp\n"), std::invalid_argument); +} + +// logic --------------------------------------------------------------------------------------------------------------- + +TEST_CASE("plc: and, or and their inverted forms combine the elements of a rung") { + controller_fixture plc{"logic"}; + REQUIRE(plc.load("ld a\nand b\nout both\n" + "ld a\nor b\nout either\n" + "ldi a\nout nota\n" + "ld a\nani b\nout aonly\n" + "ld a\nori b\nout aornotb\n")); + for (auto const a : {0, 1}) { + for (auto const b : {0, 1}) { + CAPTURE(a); + CAPTURE(b); + plc.in("a") = a; + plc.in("b") = b; + plc.cycles(); + CHECK(plc.out("both") == (a & b)); + CHECK(plc.out("either") == (a | b)); + CHECK(plc.out("nota") == (a == 0 ? 1 : 0)); + CHECK(plc.out("aonly") == (a == 1 && b == 0 ? 1 : 0)); + CHECK(plc.out("aornotb") == (a == 1 || b == 0 ? 1 : 0)); + } + } +} + +TEST_CASE("plc: a second ld starts a block, which anb and orb combine with the one before") { + // the elements can't be named a, b, c and d: c alone is the name of a counter + controller_fixture plc{"blocks"}; + REQUIRE(plc.load("ld p\nand q\nld r\nand s\norb\nout anypair\n" + "ld p\nor q\nld r\nor s\nanb\nout bothsides\n")); + for (auto bits{0}; bits < 16; ++bits) { + auto const p{bits & 1}; + auto const q{(bits >> 1) & 1}; + auto const r{(bits >> 2) & 1}; + auto const s{(bits >> 3) & 1}; + CAPTURE(bits); + plc.in("p") = p; + plc.in("q") = q; + plc.in("r") = r; + plc.in("s") = s; + plc.cycles(); + CHECK(plc.out("anypair") == ((p & q) | (r & s))); + CHECK(plc.out("bothsides") == ((p | q) & (r | s))); + } +} + +TEST_CASE("plc: a rung drives every element it has an out for, and the ld after them starts a new rung") { + controller_fixture plc{"outputs"}; + REQUIRE(plc.load("ld a\nout x\nout y\nld b\nout z\n")); + plc.in("a") = 1; + plc.in("b") = 0; + plc.cycles(); + CHECK(plc.out("x") == 1); + CHECK(plc.out("y") == 1); + CHECK(plc.out("z") == 0); +} + +TEST_CASE("plc: set and rst latch an element") { + controller_fixture plc{"latch"}; + REQUIRE(plc.load("ld start\nset run\nld stop\nrst run\nld run\nout lamp\n")); + plc.cycles(); + CHECK(plc.out("lamp") == 0); + plc.pulse("start"); + CHECK(plc.out("lamp") == 1); + plc.cycles(3); + CHECK(plc.out("lamp") == 1); + plc.pulse("stop"); + CHECK(plc.out("lamp") == 0); + // with both on, the one later in the program has the last word + plc.in("start") = 1; + plc.in("stop") = 1; + plc.cycles(); + CHECK(plc.out("lamp") == 0); +} + +TEST_CASE("plc: an element holds itself on through its own state") { + controller_fixture plc{"holding"}; + REQUIRE(plc.load("ld start\nor run\nani stop\nout run\n")); + plc.pulse("start"); + CHECK(plc.out("run") == 1); + plc.cycles(3); + CHECK(plc.out("run") == 1); + plc.pulse("stop"); + CHECK(plc.out("run") == 0); +} + +TEST_CASE("plc: the states are numbers, combined bit by bit") { + controller_fixture plc{"numbers"}; + REQUIRE(plc.load("ld a\nout same\nld a\nand b\nout both\nld a\nor b\nout either\nldi a\nout nota\n")); + plc.in("a") = 2; + plc.in("b") = 1; + plc.cycles(); + // an out passes the number on + CHECK(plc.out("same") == 2); + // two elements which are both on, and no common bit + CHECK(plc.out("both") == 0); + CHECK(plc.out("either") == 3); + // the inverted forms know only on and off + CHECK(plc.out("nota") == 0); +} + +TEST_CASE("plc: an operation with nothing to work on is passed over") { + controller_fixture plc{"empty"}; + REQUIRE(plc.load("and a\nani a\nor a\nori a\nanb\norb\nout y\nset y\nrst y\nld b\nanb\norb\nout z\n")); + plc.in("a") = 1; + plc.in("b") = 1; + plc.in("y") = 7; + plc.cycles(); + // nothing was loaded for the first rung to give out + CHECK(plc.out("y") == 7); + // a single block has nothing to be combined with + CHECK(plc.out("z") == 1); +} + +// timers -------------------------------------------------------------------------------------------------------------- + +TEST_CASE("plc: a timer is on once its input was on for its time") { + controller_fixture plc{"timer"}; + REQUIRE(plc.load("ld start\nout t1 k300\nld t1\nout lamp\n")); + plc.in("start") = 1; + // the first cycle switches it on, the three after it count 300 ms + plc.cycles(3); + CHECK(plc.out("lamp") == 0); + plc.cycles(); + CHECK(plc.out("lamp") == 1); + plc.cycles(10); + CHECK(plc.out("lamp") == 1); + // it goes off with its input, in the same cycle + plc.in("start") = 0; + plc.cycles(); + CHECK(plc.out("lamp") == 0); + // and counts from the start the next time + plc.in("start") = 1; + plc.cycles(3); + CHECK(plc.out("lamp") == 0); + plc.cycles(); + CHECK(plc.out("lamp") == 1); +} + +TEST_CASE("plc: the time of a timer is the number in its parameter, with letters before it or without") { + controller_fixture plc{"timer_time"}; + // a semicolon parts the words of an operation like a space does + REQUIRE(plc.load("ld start\nout t1 200\nout t2\nout t3;k200\nld t1\nout slow\nld t2\nout fast\nld t3\nout third\n")); + plc.in("start") = 1; + plc.cycles(); + // without a time it's on with its input + CHECK(plc.out("fast") == 1); + CHECK(plc.out("slow") == 0); + CHECK(plc.out("third") == 0); + plc.cycles(); + CHECK(plc.out("slow") == 0); + CHECK(plc.out("third") == 0); + plc.cycles(); + CHECK(plc.out("slow") == 1); + CHECK(plc.out("third") == 1); +} + +TEST_CASE("plc: a timer passes the state of its input on") { + controller_fixture plc{"timer_state"}; + REQUIRE(plc.load("ld start\nout t1 k100\nld t1\nout lamp\n")); + plc.in("start") = 5; + plc.cycles(2); + CHECK(plc.out("lamp") == 5); +} + +TEST_CASE("plc: the timers go on in steps of a tenth of a second, however long the updates are") { + SUBCASE("short updates add up to a step") { + controller_fixture plc{"timer_short"}; + REQUIRE(plc.load("ld start\nout t1 k100\nld t1\nout lamp\n")); + plc.in("start") = 1; + plc.controller.update(0.04); + CHECK(plc.out("lamp") == 0); + plc.controller.update(0.04); + CHECK(plc.out("lamp") == 0); + plc.controller.update(0.04); + CHECK(plc.out("lamp") == 1); + } + SUBCASE("a long update is many steps") { + controller_fixture plc{"timer_long"}; + REQUIRE(plc.load("ld start\nout t1 k900\nld t1\nout lamp\n")); + plc.in("start") = 1; + plc.controller.update(0.1); + CHECK(plc.out("lamp") == 0); + plc.controller.update(1.0); + CHECK(plc.out("lamp") == 1); + } +} + +// counters ------------------------------------------------------------------------------------------------------------ + +TEST_CASE("plc: a counter is done once its input went on as many times as its limit") { + controller_fixture plc{"counter"}; + REQUIRE(plc.load("ld pulse\nout c1 k3\nld c1\nout done\nld clear\nrst c1\n")); + plc.cycles(); + CHECK(plc.out("done") == 0); + plc.pulse("pulse"); + plc.pulse("pulse"); + CHECK(plc.out("done") == 0); + // an input held on counts once + plc.in("pulse") = 1; + plc.cycles(5); + CHECK(plc.out("done") == 1); + plc.in("pulse") = 0; + plc.cycles(); + CHECK(plc.out("done") == 1); + // more of them change nothing + plc.pulse("pulse"); + CHECK(plc.out("done") == 1); + + // rst starts the count again + plc.in("clear") = 1; + plc.cycles(2); + plc.in("clear") = 0; + plc.cycles(); + CHECK(plc.out("done") == 0); + plc.pulse("pulse"); + plc.pulse("pulse"); + CHECK(plc.out("done") == 0); + plc.pulse("pulse"); + CHECK(plc.out("done") == 1); +} + +TEST_CASE("plc: set on a counter makes it done for as long as it's held") { + controller_fixture plc{"counter_set"}; + REQUIRE(plc.load("ld pulse\nout c1 k2\nld force\nset c1\nld c1\nout done\n")); + plc.cycles(); + CHECK(plc.out("done") == 0); + // the limit becomes the count the counter has + plc.in("force") = 1; + plc.cycles(); + CHECK(plc.out("done") == 1); + plc.cycles(3); + CHECK(plc.out("done") == 1); + // the out of the next cycle puts the limit of the program back + plc.in("force") = 0; + plc.cycles(); + CHECK(plc.out("done") == 0); +} From cce66422a30ec2cf5efb4e5a3fad6d8e5e22f0e7 Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 11 Nov 2026 22:40:03 +0100 Subject: [PATCH 4/4] docs: coverage of the rest of the core library, and what its tests found The lines run of the files besides the physics, from the Coverage run of #325, what of the parser, the helpers and the PLC stays untested and what nothing in the game calls, and the behaviour the new tests recorded which may deserve a look. --- tests/unit/COVERAGE.md | 101 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/tests/unit/COVERAGE.md b/tests/unit/COVERAGE.md index 3ed9d14e8..5e2c55696 100644 --- a/tests/unit/COVERAGE.md +++ b/tests/unit/COVERAGE.md @@ -77,6 +77,79 @@ These are run by the game, by vehicles it has or by the cab, the AI or the scene - `UpdatePipePressure` (108 of 130): the pressure switch lock of an electric unit's pantographs, which the EW58's automatic valve keeps away. Also the EP mode of the EN57's handle and the `LockPipe` thresholds (`LPOn`, `LPOff`), which no vehicle sets. - `TCV1L_TR::GetPF` (9 of 20), `TKE::GetPF` (28 of 37) and the MHZ handles: settings (G with the CV1L_TR, the KE's anti-slip pressure `ASBP`) that no real vehicle has together. +## The rest of the core library + +The core library (`eu07_core`) and the test binary hold more than the physics: the parser of the game's text files, the helpers of `utilities.cpp`, the PLC of a vehicle, the parts of the vehicles and of the simulation moved out of the big classes to be tested, and the hosts of the script screens. The sections above don't cover them. Their numbers are lines run, from the report of the Coverage workflow. + +The tables show the run before the tests of the parser, the helpers and the PLC (`test_parser.cpp`, `test_utilities.cpp`, `test_ladder_logic.cpp`) were written. What those tests add is measured by the Coverage run of the pull request which brings them, and shown in the summary of that run. + +**The run measured:** pull request #325 on 2026-11-11 (its commit `33cedc45` merged into master `10c7527a`), GCC 13.3, unoptimised, gcov lines. All 49 files of the game in the report together: lines 84.4% (10305 of 12204), functions 85.0% (927 of 1090), branches 62.3% (10325 of 16578). + +| File | Lines run | Functions run | +|---|---|---| +| `McZapkie/couplerphysics.cpp` | 93.6% (147 of 157) | 20 of 21 | +| `scripting/ladderlogic.cpp` | 4.1% (9 of 217) | 2 of 11 | +| `scripting/ladderlogic.h` | 0% (0 of 25) | 0 of 4 | +| `scripting/luaprogram.cpp` | 74.7% (334 of 447) | 50 of 66 | +| `scripting/scriptscreendrawing.cpp` | 93.8% (135 of 144) | 19 of 19 | +| `scripting/scriptscreenkernels.cpp` | 97.7% (129 of 132) | 11 of 11 | +| `scripting/scriptscreenrules.cpp` | 100% (50 of 50) | 7 of 7 | +| `scripting/scriptscreens.cpp` | 82.8% (216 of 261) | 28 of 33 | +| `scripting/wasmprogram.cpp` | 54.0% (157 of 291) | 35 of 49 | +| `simulation/simulationtestrules.cpp` | 97.4% (75 of 77) | 10 of 10 | +| `utilities/parser.cpp` | 40.0% (170 of 425) | 18 of 41 | +| `utilities/parser.h` | 97.8% (44 of 45) | 11 of 15 | +| `utilities/utilities.cpp` | 28.2% (82 of 291) | 15 of 49 | +| `utilities/utilities.h` | 100% (41 of 41) | 12 of 12 | +| `utilities/framepacer.h` | 100% (17 of 17) | 2 of 2 | +| `vehicle/blackbox.cpp` | 100% (331 of 331) | 41 of 41 | +| `vehicle/enginestart.cpp` | 96.2% (50 of 52) | 6 of 6 | +| `vehicle/handover.cpp` | 89.2% (33 of 37) | 5 of 5 | +| `vehicle/horn.cpp` | 100% (5 of 5) | 2 of 2 | +| `vehicle/linebreaker.cpp` | 100% (6 of 6) | 1 of 1 | +| `vehicle/smoke.cpp` | 100% (12 of 12) | 2 of 2 | +| `vehicle/gaugescale.h` | 97.9% (47 of 48) | 5 of 5 | +| `world/TrackUneven.h` | 100% (111 of 111) | 23 of 25 | + +Notes to the table: + +- The small headers with the logic of a single part are all run in full: `vehicle/doorwings.h`, `gaugeduplicates.h`, `gaugepositions.h`, `holdcoillever.h`, `lightsprogrammer.h`, `mainctrlact.h`, `mirrors.h`, `scripting/scriptscreendrawing.h`, `scriptscreenkernels.h`, `scriptscreenprogram.h`, `scriptscreens.h`, `utilities/dictionary.h` and `vehicle/blackbox.h`. +- `vehicle/brakesetting.cpp` and `vehicle/odometer.cpp` (the brake setting of the wagons, the odometer store) joined the library on master after that run. They have tests (`test_brake_setting.cpp`, `test_odometer.cpp`) and no numbers yet. +- The script hosts (`scriptscreens.cpp`, `luaprogram.cpp`, `wasmprogram.cpp`) aren't in the library. The test binary compiles them itself, with the real runtimes. +- `scene/scenelayers.h`, `scene/scenenode.h` and `scene/scenenodegroups.h` are in the report with a few lines each: the parser reports the files it includes to the scenery, which the tests replace with stubs (`engine_stubs.cpp`). + +### The parser, the helpers and the PLC + +`test_parser.cpp`, `test_utilities.cpp` and `test_ladder_logic.cpp` record how `utilities/parser.cpp`, `utilities/utilities.cpp` and `scripting/ladderlogic.cpp` behave now, oddities included, so that a later change of them can be checked. The lines the measured run left out, by what it takes to run them: + +| File | Not run | Through the public interface | With files of the test | Left: the system or the game state | Nothing in the game calls it | +|---|---|---|---|---|---| +| `utilities/parser.cpp` | 255 lines | 131 | 105 | 2 | 17 | +| `utilities/utilities.cpp` | 209 lines | 154 | 19 | 19 | 17 | +| `scripting/ladderlogic.cpp` | 208 lines | 169 | 37 | 0 | 2 | + +The tests are written for the first two columns, and for what the game doesn't call as far as it can be called from outside. The files of a test are made in the build folder of the tests (`scratch.h`) and removed with it. + +**What stays untested, and why:** + +- **`parser.cpp`:** + - The scenery layers of a scenery opened for editing are stubs in the tests. The parser's calls of them are run (`sceneryLayers`), but only the templates used can be checked. The include of a terrain file left out for the binary terrain marks its layer as binary (2 lines): the stub has no layer to mark, so that isn't run. + - The messages of the log (`Failed to open file`, `Bad include`) are run, and not checked: the log of the tests is a stub. + - A buffer type which is neither a file nor a text (the `default:` of the constructor) can't be given without an invalid value of the type. +- **`utilities.cpp`:** + - `Now()`, `user_config_path()`, `user_cache_path()` and `true_random_seed()` depend on the clock, the environment and the entropy source of the system. The tests call them and check the shape of the result. The branches for a system without the variables or without an entropy source aren't run, and those of the other systems aren't compiled. + - `FileCased()` looks through the working folder for a name given without a folder, and through the folder of the file elsewhere; on Windows it only asks whether the file is there. The search is run on Linux only. + - `Bezogonkow()` gives other letters with MSVC than with GCC and Clang, see the findings. The test takes either, told by the entry of the table the difference starts at, so each build checks the half it is. +- **`ladderlogic.cpp`:** the two `return`s marked "not reachable" after the switches over the kind of an element. + +**Code nothing in the game runs** (checked against the whole source tree): + +- **The PLC as a whole.** A vehicle has one (`TMoverParameters::m_plc`) and runs it in every step (`m_plc.update()`), but nothing calls `load()`, so its program is always empty; nothing calls `find_or_insert()`, `input()` or `output()` either, so no file of the game data can be a program for it. Of `ladderlogic.cpp` the game runs the 9 lines of an update without timers and without operations. The tests run the rest with programs made up for them. +- **In `parser.cpp`:** `countTokens()` with its `count()`, and `addCommentStyle()`. No caller gives the separators as a null pointer either, which `updateCharClasses()` allows for. +- **In `utilities.cpp`:** `generate_uuid_v4()`, `FuzzyLogicAI()` and `last_modified()`. In `utilities.h`: `BorlandTime()`. + +**Cheap to add next:** the Lua host (`luaprogram.cpp`, 113 lines not run) is mostly functions of the scripts' API no test script calls yet: `fill`, `fill_rect`, the aligned texts, `draw_image`, `copy`, sprites, `time`, `log`, a command from a screen outside of the cab. A script of a few lines each runs them, with the folders `test_script_screen_host.cpp` already makes. The WebAssembly host (`wasmprogram.cpp`, 134 lines) has the same functions behind imports, and needs a module encoded by hand for each group of them. + ## What the tests found The tests record the behaviour as it is. The following points may deserve a look by someone who knows how the real vehicles behave. @@ -117,3 +190,31 @@ The tests record the behaviour as it is. The following points may deserve a look - **Game data:** - The 214M's file has `TurboPos` and `MotorParamTable` twice, and the EN57AL's `Engine` twice. The loader reads each in turn, so the later one overrides what both set. - A wagon set up braked keeps its hand brake on until someone releases it. The AI does that for the whole train (`AutoRewident`), so a test has to as well. +- **The parser (`parser.cpp`):** + - Read line by line (`"\n\r"` as the separators), a traction include left out because the traction isn't loaded takes the following lines with it, up to the next `end`: the directive is cut out of its line, but the skipping reads on in the file. + - A traction file is told by `tr/` or `tra/` anywhere in its name, so with the traction off an include from any folder ending in "tr" (`centr/…`) is left out too. + - Read line by line, every line starting with the letters `include` is taken for the directive (`included in price` includes the file `d`), also when the parser is told not to expand includes (`expandIncludes = false`). + - Empty quotes (`""`) give no token: the next token is read in their place, so everything after a value meant to be empty moves by one. + - The text in quotes keeps its case when the caller asks for lower case. That may be meant. + - An include of an empty file is logged as a file which can't be opened (`Bad include: can't open file`, seen in the log of the test, which the test itself doesn't check). + - A text which ends inside a comment or inside quotes leaves the parser failed (`ok()` false), not just at its end; an empty text fails with its first read, through the check for a byte order mark. + - `deserialize_map()` (`utilities.h`, the sound overrides) therefore reads an empty key from an empty file, and one from a file ending with a line break; in a file with Windows line ends its values keep the carriage return. The game's `sound_overrides.txt` is a single line without a line break. + - The separators given as a null pointer are taken as none by the tokenizer, but passed on to `strcmp` for a token of 7 characters or more starting with `i`, which crashes (tried by hand, not kept as a test). No caller gives a null pointer. +- **The helpers (`utilities.cpp`, `utilities.h`):** + - `Bezogonkow()`, which takes the marks off the letters for the timetable and the on-screen texts: its table has a `±` in the source, a UTF-8 file. MSVC makes one byte of it, GCC and Clang two, which moves every entry from 0xB2 on by one. With MSVC the 18 polish letters come out as `ACELNOSZZacelnoszz`; with GCC as `ALELDNSZZ,le,dnszl`: `Ć` as L, `Ń` as D, `Ó` as N, `ą` and `ł` as a comma, `ć` as l, `ń` as d, `ó` as n, `ż` as l, only `Ę` and `ę` by luck as themselves. The Linux and macOS builds of the game show the names of the stations that way. The test checks whichever table the build has (the GCC one was tried with the table alone, compiled by GCC 6.3). + - `to_minutes_str()` reads past its table of raised digits for minutes with a fraction of 0.99 or more, and before it for negative minutes, which `CompareTime()` can give it for a timetable whose times go back. Tried by hand, both ended in `std::bad_alloc`, the text read from outside of the table being no text; not kept as a test. A width under 2 throws `std::length_error`. + - `stol_def()` gives its default only for an empty text. A text which isn't a number gives 0 (the flags of an event, the log setting). + - `split_string_and_number()` throws for a name with a hyphen which no digit follows (`a-b`), and for a number too big. The gamepad bindings and the PLC use it on names from files. + - `extract_value()` throws `std::out_of_range` when the key is the last thing in the text with nothing after its `=`. With a space after the `=`, the next `key=value` is taken for the value. + - `seed_of()` takes a word starting with digits for the number in front (`12abc` is 12), so different words can stand for the same seed. + - `erase_extension()` cuts at the dot of a folder when the file has none (`dir.d/file` becomes `dir`). + - `Round()` adds a half and cuts the fraction, which rounds the negative numbers up: -1.6 becomes -1. +- **The PLC (`ladderlogic.cpp`):** + - Nothing loads a program into it, see above. + - `and` and `or` combine the states bit by bit, and `out` passes a number on as it is: two elements which are both on, with the states 2 and 1, give 0. + - A counter is done until the first cycle of the program gives it its limit. + - `end` is read as "no operation": the lines after it are loaded and run. + - A name with a hyphen (`door-open`) throws out of `load()`, through `split_string_and_number()`. + - The kind of an element is told by its name: `t`, `ton` or `timer.` and `c` or `counter.` before the number, so a variable can't be named `t1` or `c2`, nor `t` or `c` alone: an input named `c` is a counter, on from the start. + - The line of an operation is split with `getTokens(3, "\t ")`, which gives the separators in the place of the lower case flag. The result is the same, the default separators being those and the semicolon. + - An update longer than 32.7 s doesn't fit the milliseconds a timer counts in (`short`). Tried by hand: a timer of 1 s wasn't on after an update of 40 s, nor a second later. Not kept as a test, the conversion being undefined.