18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/tests/unit/test_motor_param_bounds.cpp
jerrrrycho 3ccd4fe73f fix(ai): read the motor parameters of ESMVelocity() within the table (#486)
* fix(ai): read the motor parameters of ESMVelocity() within the table

TController::ESMVelocity() indexed MotorParam with the field shunting
step of the next master controller position. For a position with a
step of its own that index is RList[].ScndAct, the sixth column of the
RList rows of the .fiz, which readRList() takes as it is. A step of 11
or more read past the 11 entries of MotorParam.

The pole flux is now read through motor_param(), which keeps the index
inside the table, as MomentumF() in the same loop and Momentum() already
do. Nothing changes for a .fiz with valid steps.

The unit tests show the loader keeping an out-of-range ScndAct and
motor_param() clamping it; ESMVelocity() itself is in Driver.cpp, which
the unit tests don't build.

* test(scenarios): a Traxx, with a brake handle with a time chamber, starting up

None of the cases had a driver's brake handle with a time chamber
(MHZ_K5P, MHZ_6P, M394, Knorr, St113, FVel6, FVE408), for which the AI
getting the vehicle ready has a rule of its own for releasing the train
brake (5775493b2). Three cases run a Traxx (E186, MHZ_6P) on td.scn: the
timetable run from a trainset ready to depart, the same from a cold
start, and the locomotive standing ready to depart with nobody in it
until a driver is put in. In each the driver puts the handle to running
and leaves.

* test: read the EP09 definition into a json with '=', not braces

GCC takes nlohmann::ordered_json in braces for an initializer list and
makes an array holding the parsed object, so at("RList") threw on the
Linux build (type_error 304); MSVC kept the object.
2026-11-25 05:15:27 +01:00

58 lines
2.4 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "vehicle.h"
#include <fstream>
#include <nlohmann/json.hpp>
using test::definition;
using test::vehicle;
// the field shunting step a position of the master controller selects (RList, its sixth column ScndAct) indexes the
// motor parameter table, MotorParam, which has MotorParametersArraySize + 1 entries. The loader takes ScndAct from the
// .fiz as it is, so the readers of the table (TMoverParameters::Momentum(), MomentumF(), TController::ESMVelocity())
// go through motor_param(), which keeps the index inside the table
namespace {
// the RList rows of the EP09 with the ScndAct of the first position replaced
nlohmann::ordered_json ep09_rlist_with_scndact(std::string const &Scndact) {
std::ifstream file{std::string{EU07_UNIT_TESTS_DATA} + "/fiz/ep09_v2/104e.json"};
REQUIRE(file.good());
// '=' rather than braces: GCC takes a json in braces for an initializer list, an array holding it
auto const ep09 = nlohmann::ordered_json::parse(file);
auto rows = ep09.at("RList").at("rows");
REQUIRE(rows.size() > 1);
rows[1][5] = Scndact;
return rows;
}
} // namespace
TEST_CASE("motor parameters: the ScndAct of an RList row is read from the .fiz as it is") {
vehicle ep09{definition::fiz("ep09_v2/104e")};
// the first position of the EP09 runs on field shunting step 2
CHECK(ep09->RList[1].ScndAct == 2);
CHECK(&ep09->motor_param(ep09->RList[1].ScndAct) == &ep09->MotorParam[2]);
// a .fiz giving a step beyond the table: nothing checks it on loading
vehicle broken{definition::fiz("ep09_v2/104e").set("RList", "rows", ep09_rlist_with_scndact("20"))};
CHECK(broken->RList[1].ScndAct == 20);
CHECK(broken->RList[1].ScndAct > MotorParametersArraySize);
}
TEST_CASE("motor parameters: motor_param() keeps a step from the .fiz inside the table") {
vehicle broken{definition::fiz("ep09_v2/104e").set("RList", "rows", ep09_rlist_with_scndact("20"))};
auto const &table{broken->MotorParam};
// past the end: the last entry, rather than memory beyond the table
CHECK(&broken->motor_param(broken->RList[1].ScndAct) == &table[MotorParametersArraySize]);
CHECK(&broken->motor_param(MotorParametersArraySize + 1) == &table[MotorParametersArraySize]);
// below the start: the first entry
CHECK(&broken->motor_param(-1) == &table[0]);
// inside the table: the entry itself
for (int step = 0; step <= MotorParametersArraySize; ++step) {
CHECK(&broken->motor_param(step) == &table[step]);
}
}