From 6aca6bc4f65f72a9ef60ccc53f8e8dd423c8f03a Mon Sep 17 00:00:00 2001 From: Hirek193 Date: Sun, 29 Nov 2026 04:13:17 +0100 Subject: [PATCH] fix(review): an empty Load= no longer throws, and the stale notes of the review of #531 From the review of #531: - Load= and UnLoad= without the name of a load threw std::out_of_range as soon as ParseVehicleCommand() read them: extract_value() reads past the end of the text when nothing but spaces follows the =. Before this branch the same exception came later, in the RunCommand() of a vehicle standing at the ramp. ParseVehicleCommand() gives an empty name now, which loads and unloads nothing (AssignLoad("") and LoadingDone() with no name, as for an empty vehicle). extract_value() itself is left as it was: it has hundreds of callers and test_utilities.cpp records its exception at the end of the text. New checks in test_vehicle_command.cpp. - compression_at() is an inline function of physics/components/internal.h, as square() is, rather than a global function of external linkage defined in collisions.cpp; its body is the same - the comment of train_type::sn61 says what the AI does for it (the master controller up to a position with torque after setting the direction) rather than that nothing handles it - tests/unit/COVERAGE.md names the files as they are now (physics/components/, vehicle/dynamic/lifecycle.cpp) instead of physics/physics.cpp, a name the renaming script left there The naming of AGENTS.md, which the review asked about, stays as it is. --- physics/components/collisions.cpp | 6 ------ physics/components/internal.h | 8 +++++++- physics/vehiclecommand.cpp | 15 +++++++++++++-- physics/vehicletypes.h | 2 +- tests/unit/COVERAGE.md | 10 +++++----- tests/unit/test_vehicle_command.cpp | 8 ++++++++ 6 files changed, 34 insertions(+), 15 deletions(-) diff --git a/physics/components/collisions.cpp b/physics/components/collisions.cpp index 347100559..e46bae097 100644 --- a/physics/components/collisions.cpp +++ b/physics/components/collisions.cpp @@ -304,12 +304,6 @@ char const *kind_name(collision::vehicle_kind const Kind) } } // namespace - -// the force pressing the end in, of the coupling or of the buffers touching [N]: forwards at the front pulls -double compression_at(TCoupling const &Coupler, VehicleEnd const End) -{ - return std::max(0.0, (End == VehicleEnd::front ? -1.0 : 1.0) * Coupler.CForce); -} namespace { diff --git a/physics/components/internal.h b/physics/components/internal.h index 4cdcbe668..b469abbc5 100644 --- a/physics/components/internal.h +++ b/physics/components/internal.h @@ -11,6 +11,8 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include + #include "physics/vehicle.h" const double CouplerTune = 0.1; // skalowanie tlumiennosci @@ -27,7 +29,11 @@ int DirF(VehicleEnd CouplerN); double TableInterpolation(std::map &Map, double Parameter); -double compression_at(TCoupling const &Coupler, VehicleEnd const End); +// the force pressing the end in, of the coupling or of the buffers touching [N]: forwards at the front pulls +inline double compression_at(TCoupling const &Coupler, VehicleEnd const End) +{ + return std::max(0.0, (End == VehicleEnd::front ? -1.0 : 1.0) * Coupler.CForce); +} bool issection(std::string_view Name, std::string_view Input); diff --git a/physics/vehiclecommand.cpp b/physics/vehiclecommand.cpp index 9b8e6fc72..c07b0546f 100644 --- a/physics/vehiclecommand.cpp +++ b/physics/vehiclecommand.cpp @@ -84,6 +84,17 @@ constexpr std::array, 59> commandNam {unknown, ""}, }}; +// the name of the load after Load= or UnLoad= (key) at the start of the text, empty when nothing follows the = but +// spaces: extract_value() would throw there, reading past the end of the text +std::string loadName(std::string_view const key, std::string_view const text) +{ + if (text.find_first_not_of(' ', key.size() + 1) == std::string_view::npos) + { + return {}; + } + return extract_value(std::string{key}, std::string{text}); +} + } // namespace ParsedVehicleCommand ParseVehicleCommand(std::string const &Text) @@ -95,11 +106,11 @@ ParsedVehicleCommand ParseVehicleCommand(std::string const &Text) // Load= and UnLoad= begin the text, the name of the load follows if (Text.starts_with("Load=")) { - return {load, extract_value("Load", Text)}; + return {load, loadName("Load", Text)}; } if (Text.starts_with("UnLoad=")) { - return {unload, extract_value("UnLoad", Text)}; + return {unload, loadName("UnLoad", Text)}; } auto const named{std::ranges::find(commandNames, std::string_view{Text}, &std::pair::second)}; if (named == commandNames.end() || named->first == load || named->first == unload) diff --git a/physics/vehicletypes.h b/physics/vehicletypes.h index 7c1c01572..79ecd2ea5 100644 --- a/physics/vehicletypes.h +++ b/physics/vehicletypes.h @@ -499,7 +499,7 @@ enum class train_type et42, /// EP05 locomotive. ep05, - /// SN61 railcar: the type is read, but nothing handles it on its own. + /// SN61 railcar: the physics has nothing of its own for it; the AI, after setting the direction, moves the master controller up to a position with engine torque so the engine doesn't stall (TController::DirectionForward()). sn61, /// Pseudo-diesel (an electric drivetrain with the input model of a diesel). pseudodiesel, diff --git a/tests/unit/COVERAGE.md b/tests/unit/COVERAGE.md index 2d9774239..74eacedcd 100644 --- a/tests/unit/COVERAGE.md +++ b/tests/unit/COVERAGE.md @@ -1,6 +1,6 @@ # Coverage of the simulation core by the unit tests -These tests characterise the physics of the vehicles (`physics/physics.cpp`, `brakes.cpp`, `oerlikonest.cpp`, `friction.cpp`). They exist to show that refactors of those files, such as splitting `TractionForce` or `RunCommand`, leave the behaviour as it was. This page lists: +These tests characterise the physics of the vehicles (`physics/components/*.cpp`, once `Mover.cpp`; `physics/brakes.cpp`, `oerlikonest.cpp`, `friction.cpp`). They exist to show that refactors of those files, such as splitting `TractionForce` or `RunCommand`, leave the behaviour as it was. This page lists: - what the tests reach; - what nothing in the game ever runs; @@ -14,7 +14,7 @@ Coverage was measured on Windows with MSVC's SanitizerCoverage (`/fsanitize-cove | File | Blocks run, at the start | Now | |---|---|---| -| `physics.cpp` | 8.9% | 80.7% (3323 of 4118) | +| `Mover.cpp` (now `physics/components/*.cpp`) | 8.9% | 80.7% (3323 of 4118) | | `brakes.cpp` | 30% | 76.3% (649 of 851) | | `oerlikonest.cpp` | 58% | 87.4% (118 of 135) | | `friction.cpp` | 9% | 90.9% (10 of 11) | @@ -35,7 +35,7 @@ Nothing creates these objects or calls these functions anywhere in the game, whi - **Base implementations always overridden:** `TFricMat::GetFC` (every material, the default P10 too, has its own) and `TPrzekladnik::Update` (every relay has its own). - **Functions without callers:** - In `brakes.cpp`: `PF_old`, `TBrake::GetBCF`, `TDriverHandle::OvrldButton`, `TReservoir::pa` and `TBrakeCyl::pa`. - - In `physics.cpp`: `BrakeVP`, `PipeRatio`, `RealPipeRatio`, `IncBrakeLevel`, `DecBrakeLevel`, `IncBrakePress`, `DecBrakePress` and `MainCtrlActualPowerPos`. (`GetExternalCommand` was removed.) + - In `physics/components/pneumatics.cpp`: `BrakeVP`, `PipeRatio`, `RealPipeRatio`, `IncBrakeLevel`, `DecBrakeLevel`, `IncBrakePress` and `DecBrakePress`; in `physics/components/controllers.cpp`: `MainCtrlActualPowerPos`. (`GetExternalCommand` was removed.) - **Inverter failures** of the induction motor drives (`inverter::Failure_Drive`, `Failure_Const`, `Activate`): nothing ever sets them. The cab only shows them. The branches reading them in `TractionForce` can't be reached. ## Code only data no game vehicle has would run @@ -60,7 +60,7 @@ Where it was worth it, a test fits the missing parameter to a real vehicle or bu These are run by the game, by vehicles it has or by the cab, the AI or the scenery, but no test reaches them yet. Most of them are the far sides of conditions inside functions the tests do run. - **Entry points from outside the core:** - - `TBrake::ForceEmptiness` and its overrides: a scenario's vehicle set up with empty brakes (`DynObj.cpp`). + - `TBrake::ForceEmptiness` and its overrides: a scenario's vehicle set up with empty brakes (`vehicle/dynamic/lifecycle.cpp`). - `TBrake::GetSoundFlag`: the sounds of the brakes. - `LoadFIZ` and `reload_FIZ`: the file loading around `LoadFIZ_Text`, which the tests call directly. - `TSecuritySystem::set_cabsignal_lock`. @@ -163,7 +163,7 @@ The tests are written for the first two columns, and for what the game doesn't c The tests record the behaviour as it is. The following points may deserve a look by someone who knows how the real vehicles behave. -- **`Random(10) - Random(10)` (`physics.cpp`, the noise of the control pipe pressure `CntrlPipePress`):** the order the two calls were evaluated in isn't specified. MSVC Release, MSVC Debug and GCC gave different traces, a control pipe pressure mirrored around 5 bar, which failed 32 tests on the Linux CI. Fixed: the draws are sequenced in the order of MSVC Release, which the golden traces were recorded with. +- **`Random(10) - Random(10)` (`physics/components/fizsections.cpp`, the noise of the control pipe pressure `brakes.controlReservoirPressure`, `CntrlPipePress` before):** the order the two calls were evaluated in isn't specified. MSVC Release, MSVC Debug and GCC gave different traces, a control pipe pressure mirrored around 5 bar, which failed 32 tests on the Linux CI. Fixed: the draws are sequenced in the order of MSVC Release, which the golden traces were recorded with. - **`TCommand::Location` isn't initialised** in a new vehicle. The game always sets it through `PutCommand()`, but a test calling `RunCommand()` directly got different results run to run. - **The `CompressorList` table (`air.compressor.list`) holds `int`s** (`physics/components/airsupply.h`), though its factors are fractions: 0.9 is read as 0. No game vehicle has the list yet. - **A train whose pipe leaks under 2.75 bar before its start-up can't be released:** diff --git a/tests/unit/test_vehicle_command.cpp b/tests/unit/test_vehicle_command.cpp index 737d66431..84701e416 100644 --- a/tests/unit/test_vehicle_command.cpp +++ b/tests/unit/test_vehicle_command.cpp @@ -37,6 +37,14 @@ TEST_CASE("vehicle commands: the empty text, an unknown one, the load") { CHECK(unload.command == VehicleCommand::unload); CHECK(unload.argument == "passengers"); + // a load without a name is a load of no name, not an exception: the vehicle loads nothing for it + auto const noname{ParseVehicleCommand("Load=")}; + CHECK(noname.command == VehicleCommand::load); + CHECK(noname.argument.empty()); + CHECK(ParseVehicleCommand("UnLoad= ").argument.empty()); + // a space after the = is skipped, as extract_value() does + CHECK(ParseVehicleCommand("Load= Coal").argument == "Coal"); + CHECK(VehicleCommandName(VehicleCommand::none).empty()); CHECK(VehicleCommandName(VehicleCommand::unknown).empty()); CHECK(VehicleCommandName(VehicleCommand::integratedController) == "EIMIC");