18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 17:37:09 +01:00
Files
maszyna/tests/unit/test_door_component.cpp
T
Hirek ea7a770d6c refactor(physics): the step of the doors belongs to the doors
The step of the doors of a vehicle (their lock, the requests to open and to
close from the passengers, from the cab and by themselves, the permits, the
movement of the doors and their steps, the timers of the doors that close
after a while) and the state of their closure signalling move from
physics::Vehicle into physics::door_data (Update(), Closure()). The vehicle
hands the doors what they need as plain values (door_data::StepInputs: its
speed, whether the doors have their voltage, whether it is a multiple unit,
whether passengers are getting in or out) and keeps only that.

The doors can now be tested on their own, without a vehicle
(test_door_component.cpp): opening and closing from the cab, no movement
without the voltage, the lock over its speed, the doors closing by themselves
unless the passengers are getting in.

No change in behaviour: the golden traces recorded on dfb43c4f match bit for bit.
2026-11-29 11:01:47 +01:00

92 lines
2.8 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) {
for (auto time{0.0}; time < Seconds; time += 0.05) {
Doors.Update(Inputs, 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);
}