mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 13:35:26 +01:00
6f64f1cadf
The copy of the shared random engine, kept and put back around the look-ahead and in its test, is marked NOSONAR for S2245: nothing is drawn from it for security. The entry name is made with std::format, and the two new error messages use raw strings.
143 lines
5.8 KiB
C++
143 lines
5.8 KiB
C++
#include "stdafx.h"
|
|
#include "doctest.h"
|
|
#include "scratch.h"
|
|
|
|
#include "vehicle/cabdefinitions.h"
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/parser.h"
|
|
|
|
#include <optional>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// the cabs a vehicle's .mmd defines, and where the player goes when it doesn't define the cab the vehicle is occupied from
|
|
// or the cab a cab change leads to (TTrain::CabChange(), TTrain::Init(), TTrain::UpdateCab()): a single-desk locomotive
|
|
// may define its one cab only, and the player stays in it instead of ending up in a cab with no model and no controls
|
|
|
|
using cab_definitions::ChangeStep;
|
|
using cab_definitions::Definitions;
|
|
using cab_definitions::Fallback;
|
|
|
|
namespace {
|
|
|
|
Definitions cabs(bool const Middle, bool const Front, bool const Rear) {
|
|
Definitions definitions;
|
|
definitions.defined = {Middle, Front, Rear};
|
|
return definitions;
|
|
}
|
|
|
|
Definitions const all{cabs(true, true, true)};
|
|
Definitions const frontOnly{cabs(false, true, false)};
|
|
Definitions const noMiddle{cabs(false, true, true)};
|
|
Definitions const none{cabs(false, false, false)};
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("cab definitions: the cab numbers of the physics name the entries of the .mmd") {
|
|
CHECK(cab_definitions::Index(1) == 1);
|
|
CHECK(cab_definitions::Index(-1) == 2);
|
|
CHECK(cab_definitions::Index(0) == 0);
|
|
CHECK(frontOnly.Has(1));
|
|
CHECK_FALSE(frontOnly.Has(-1));
|
|
CHECK_FALSE(frontOnly.Has(0));
|
|
CHECK(frontOnly.Any());
|
|
CHECK_FALSE(none.Any());
|
|
}
|
|
|
|
TEST_CASE("cab definitions: the .mmd is read for its cab entries, in its includes as well") {
|
|
test::scratch_folder folder{"cab_definitions"};
|
|
folder.write("single.mmd", "internaldata:\nsounds: none\ncab1definition:\ncab1model: kabina.t3d\ncomponents:\nend\n");
|
|
folder.write("double.mmd", "cab1definition:\ncab1model: a.t3d\ncab2definition:\ncab2model: b.t3d\ncab0definition:\ncab0model: none\n");
|
|
folder.write("nocab.mmd", "internaldata:\nsounds: none\n");
|
|
folder.write("included.mmd", "include cab.inc end\n");
|
|
folder.write("cab.inc", "cab2definition:\ncab2model: b.t3d\n");
|
|
|
|
auto const single{cab_definitions::Find("single.mmd", folder.path())};
|
|
CHECK(single.Has(1));
|
|
CHECK_FALSE(single.Has(-1));
|
|
CHECK_FALSE(single.Has(0));
|
|
|
|
auto const both{cab_definitions::Find("double.mmd", folder.path())};
|
|
CHECK(both.Has(1));
|
|
CHECK(both.Has(-1));
|
|
CHECK(both.Has(0));
|
|
|
|
CHECK_FALSE(cab_definitions::Find("nocab.mmd", folder.path()).Any());
|
|
|
|
auto const included{cab_definitions::Find("included.mmd", folder.path())};
|
|
CHECK(included.Has(-1));
|
|
CHECK_FALSE(included.Has(1));
|
|
}
|
|
|
|
TEST_CASE("cab definitions: looking for the cabs leaves the shared random engine as it was") {
|
|
// a random set of includes draws from the engine kept in sync across the network; the look ahead before the cab is
|
|
// loaded mustn't make the cab loaded afterwards, or anything later in the run, draw differently
|
|
test::scratch_folder folder{"cab_definitions_random"};
|
|
folder.write("random.mmd", "include [ a.inc b.inc c.inc ] end\n");
|
|
folder.write("a.inc", "cab1definition:\n");
|
|
folder.write("b.inc", "cab1definition:\n");
|
|
folder.write("c.inc", "cab1definition:\n");
|
|
|
|
auto const before{Global.random_engine}; // NOSONAR(cpp:S2245) a copy of the simulation's engine, to compare and put back; not security
|
|
CHECK(cab_definitions::Find("random.mmd", folder.path()).Has(1));
|
|
CHECK(Global.random_engine == before);
|
|
|
|
// the same file read the way the cab is loaded does draw from it, so the check above means something
|
|
{
|
|
cParser parser("random.mmd", cParser::buffer_FILE, folder.path(), true, std::vector<std::string>(), true);
|
|
std::string token;
|
|
parser.getTokens();
|
|
parser >> token;
|
|
CHECK(token == "cab1definition:");
|
|
}
|
|
CHECK_FALSE(Global.random_engine == before);
|
|
Global.random_engine = before;
|
|
}
|
|
|
|
TEST_CASE("cab definitions: with every cab defined the cab change goes one cab at a time, as it always did") {
|
|
CHECK(ChangeStep(1, -1, all) == -1);
|
|
CHECK(ChangeStep(0, -1, all) == -1);
|
|
CHECK(ChangeStep(-1, 1, all) == 1);
|
|
CHECK(ChangeStep(0, 1, all) == 1);
|
|
// past the end of the vehicle the step is still made, and fails as it did, so the player can go on to the next one
|
|
CHECK(ChangeStep(-1, -1, all) == -1);
|
|
CHECK(ChangeStep(1, 1, all) == 1);
|
|
}
|
|
|
|
TEST_CASE("cab definitions: a single-desk vehicle keeps the player in its one cab") {
|
|
// the 401Da with cab1definition: only: the middle and the rear cab aren't there, the player stays put
|
|
CHECK(ChangeStep(1, -1, frontOnly) == 0);
|
|
// past the end, as with every vehicle
|
|
CHECK(ChangeStep(1, 1, frontOnly) == 1);
|
|
// occupied from the rear, the way the AI or the scenery may leave it: the change forward takes the player to the cab
|
|
// there is, past the middle one that isn't
|
|
CHECK(ChangeStep(-1, 1, frontOnly) == 2);
|
|
CHECK(ChangeStep(0, 1, frontOnly) == 1);
|
|
CHECK(ChangeStep(0, -1, frontOnly) == 0);
|
|
}
|
|
|
|
TEST_CASE("cab definitions: a vehicle without the middle cab changes from one end straight to the other") {
|
|
CHECK(ChangeStep(1, -1, noMiddle) == -2);
|
|
CHECK(ChangeStep(-1, 1, noMiddle) == 2);
|
|
}
|
|
|
|
TEST_CASE("cab definitions: a vehicle without any cab leaves the player where they are") {
|
|
// a car the player walked into: the change fails and the player goes on through the gangway, if there is one
|
|
CHECK(ChangeStep(1, -1, none) == 0);
|
|
CHECK(ChangeStep(0, 1, none) == 0);
|
|
CHECK(ChangeStep(-1, -1, none) == -1);
|
|
}
|
|
|
|
TEST_CASE("cab definitions: the cab loaded in place of one the .mmd doesn't define") {
|
|
CHECK(Fallback(1, all) == std::optional<int>{1});
|
|
CHECK(Fallback(-1, all) == std::optional<int>{-1});
|
|
CHECK(Fallback(0, all) == std::optional<int>{0});
|
|
CHECK(Fallback(-1, frontOnly) == std::optional<int>{1});
|
|
CHECK(Fallback(0, frontOnly) == std::optional<int>{1});
|
|
CHECK(Fallback(0, noMiddle) == std::optional<int>{1});
|
|
CHECK(Fallback(1, cabs(false, false, true)) == std::optional<int>{-1});
|
|
CHECK(Fallback(1, cabs(true, false, false)) == std::optional<int>{0});
|
|
CHECK_FALSE(Fallback(1, none).has_value());
|
|
}
|