#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); }