18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 07:45:25 +01:00
Files
maszyna/tests/unit/test_door_component.cpp
jerrrrycho 7eb73ad9d4 fix: the five bugs SonarQube Cloud reports on staging (reliability E)
- application/driveruipanels.cpp (S3584, blocker): the UART section of the
  debug panel leaked a new char*[] of the port names every frame it was open;
  the names go through a vector now.
- rendering/vulkan14/vulkan14swapchain.cpp (S1048): the destructor called
  destroy(), whose release_images() keeps the presentation semaphores in a
  vector and may throw on allocation; the destructor catches it.
- rendering/vulkan14/vulkan14scene.cpp (S2259): walk_shadow_material()
  dereferenced m_context, which own_shadow_program() just before treats as
  possibly empty; it returns without a context.
- tests/unit/test_door_component.cpp, test_consist_braking.cpp (S2193): the
  float loop counters become while loops summing the same time, so the cases
  take exactly the steps they took before.
2026-11-30 00:29:19 +01:00

96 lines
3.0 KiB
C++

#include "stdafx.h"
#include "doctest.h"
#include "physics/components/devices.h"
// the doors of a vehicle as a part of their own (physics::door_data::Update()), stepped without a vehicle
namespace {
// a door one metre wide, opened and closed from the cab at a metre a second, without a step or a lock
physics::door_data cab_controlled_doors() {
physics::door_data doors;
doors.open_control = control_t::driver;
doors.close_control = control_t::driver;
doors.range = 1.f;
doors.open_rate = 1.f;
doors.close_rate = 1.f;
return doors;
}
void run(physics::door_data &Doors, physics::door_data::StepInputs const &Inputs, double Seconds) {
// steps of 0.05 s while the time summed so far is short of Seconds (as the cases were written: the sum rounds below a whole
// number of steps, so it takes one more); the time isn't a loop counter (S2193)
auto time{0.0};
while (time < Seconds) {
Doors.Update(Inputs, 0.05);
time += 0.05;
}
}
physics::door_data::StepInputs standing_powered() {
physics::door_data::StepInputs inputs;
inputs.powered = true;
return inputs;
}
} // namespace
TEST_CASE("door component: a door opened from the cab opens at its rate, and closes when asked to") {
auto doors{cab_controlled_doors()};
auto &door{doors.instances[VehicleSide::left]};
door.remote_open = true;
run(doors, standing_powered(), 0.5);
CHECK(door.position > 0.f);
CHECK(door.position < 1.f);
CHECK_FALSE(door.is_closed);
run(doors, standing_powered(), 1.0);
CHECK(door.is_open);
CHECK(door.position == doctest::Approx(1.f));
CHECK(doors.instances[VehicleSide::right].is_closed);
door.remote_close = true;
run(doors, standing_powered(), 1.5);
CHECK(door.is_closed);
CHECK(door.position == doctest::Approx(0.f));
}
TEST_CASE("door component: without the voltage it works on, a door doesn't move") {
auto doors{cab_controlled_doors()};
auto &door{doors.instances[VehicleSide::left]};
door.remote_open = true;
auto inputs{standing_powered()};
inputs.powered = false;
run(doors, inputs, 2.0);
CHECK(door.is_closed);
CHECK(door.position == doctest::Approx(0.f));
}
TEST_CASE("door component: the lock holds the doors closed over its speed") {
auto doors{cab_controlled_doors()};
doors.has_lock = true;
auto &door{doors.instances[VehicleSide::left]};
door.remote_open = true;
auto inputs{standing_powered()};
inputs.velocity = doors.doorLockSpeed + 5.0;
run(doors, inputs, 2.0);
CHECK(doors.is_locked);
CHECK(door.is_closed);
CHECK(doors.Closure() == door_closure_t::locked);
}
TEST_CASE("door component: doors that close after a while close by themselves, unless the passengers are getting in") {
auto doors{cab_controlled_doors()};
doors.auto_duration = 1.f;
doors.auto_include_remote = true;
auto &door{doors.instances[VehicleSide::left]};
door.remote_open = true;
run(doors, standing_powered(), 1.5);
REQUIRE(door.is_open);
auto exchanging{standing_powered()};
exchanging.exchangingLoad = true;
run(doors, exchanging, 3.0);
CHECK(door.is_open);
run(doors, standing_powered(), 3.0);
CHECK(door.is_closed);
}