mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-19 08:46:00 +01:00
refactor(simulation): the rules of the test mode in a file of their own, with unit tests
The command line options of the test mode and the end conditions checked after each frame are now plain functions in simulation/simulationtestrules.cpp, which needs nothing of the running simulation: parse_test_command_line() and evaluate_frame(). The file is part of the core library, so the unit tests check them (tests/unit/test_test_mode.cpp): the --test-timeout value, values of other options not taken for options, a pass that waits for the stop, the standstill, the timeout. test_manager only gathers what a frame brought and acts on the verdict. No change in behaviour.
This commit is contained in:
@@ -25,16 +25,6 @@ http://mozilla.org/MPL/2.0/.
|
||||
#include <format>
|
||||
#include <memory>
|
||||
|
||||
namespace {
|
||||
|
||||
// the cell holds what the scenery wrote, so the values are whole numbers; the margin only keeps a stray fraction out
|
||||
bool holds(double const Value, double const Expected)
|
||||
{
|
||||
return std::abs(Value - Expected) < 1e-6;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace simulation {
|
||||
|
||||
test_manager &test()
|
||||
@@ -43,21 +33,6 @@ test_manager &test()
|
||||
return instance;
|
||||
}
|
||||
|
||||
std::string_view to_string(test_result const Result)
|
||||
{
|
||||
using enum test_result;
|
||||
switch (Result)
|
||||
{
|
||||
case pass: return "pass";
|
||||
case fail: return "fail";
|
||||
case signal_passed_at_danger: return "signal passed at danger";
|
||||
case timeout: return "timeout";
|
||||
case standstill: return "standstill";
|
||||
case error: return "error";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
void test_manager::init_result_cell() const
|
||||
{
|
||||
auto cell { std::make_unique<TMemCell>(scene::node_data{0, -1, std::string(result_cell), "memcell"}) };
|
||||
@@ -73,8 +48,7 @@ void test_manager::start()
|
||||
if (m_started)
|
||||
return;
|
||||
m_started = true;
|
||||
m_time = 0.0;
|
||||
m_resttime = 0.0;
|
||||
m_progress = test_progress {};
|
||||
if (simulation::Vehicles.sequence().empty())
|
||||
{
|
||||
// most likely the scenery wasn't found, or it isn't a test scenery
|
||||
@@ -94,48 +68,25 @@ void test_manager::update(double const Deltatime)
|
||||
if (false == m_started || m_finished)
|
||||
return;
|
||||
|
||||
m_time += Deltatime;
|
||||
|
||||
test_frame frame { Deltatime, std::nullopt, {},
|
||||
std::ranges::none_of(simulation::Vehicles.sequence(), [](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }) };
|
||||
if (auto const *cell = simulation::Memory.find(std::string(result_cell)))
|
||||
{
|
||||
auto const value { cell->Value1() };
|
||||
auto const reason { cell->Text().empty() ? std::string() : std::format(" ({})", cell->Text()) };
|
||||
if (holds(value, 0.0))
|
||||
{
|
||||
finish(test_result::fail, std::format("__test.result set to 0{}", reason));
|
||||
return;
|
||||
}
|
||||
if (holds(value, 1.0) && false == Global.test.no_movement_before_pass)
|
||||
{
|
||||
finish(test_result::pass, std::format("__test.result set to 1{}", reason));
|
||||
return;
|
||||
}
|
||||
if (holds(value, 1.0) && false == m_passrequested)
|
||||
{
|
||||
m_passrequested = true;
|
||||
WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_time, reason));
|
||||
}
|
||||
frame.reported = cell->Value1();
|
||||
frame.reason = cell->Text();
|
||||
}
|
||||
|
||||
auto const atrest { std::ranges::none_of(simulation::Vehicles.sequence(),
|
||||
[](TDynamicObject const *Vehicle) { return std::abs(Vehicle->GetVelocity()) > 0.01; }) };
|
||||
m_resttime = atrest ? m_resttime + Deltatime : 0.0;
|
||||
|
||||
if (m_passrequested && m_resttime >= pass_settle_time)
|
||||
auto const requestedbefore { m_progress.passrequested };
|
||||
auto const verdict { evaluate_frame(m_progress, frame, test_limits { Global.test.timeout, Global.test.standstill, Global.test.pass_on_standstill, Global.test.no_movement_before_pass }) };
|
||||
if (verdict.has_value())
|
||||
{
|
||||
finish(test_result::pass, "__test.result set to 1 and every vehicle stopped");
|
||||
finish(verdict->result, verdict->reason);
|
||||
return;
|
||||
}
|
||||
if (Global.test.standstill > 0.0 && m_resttime >= Global.test.standstill)
|
||||
if (m_progress.passrequested && false == requestedbefore)
|
||||
{
|
||||
auto const reason { std::format("every vehicle stood still for {:.0f} s", Global.test.standstill) };
|
||||
finish(Global.test.pass_on_standstill ? test_result::pass : test_result::standstill, reason);
|
||||
return;
|
||||
}
|
||||
if (Global.test.timeout > 0.0 && m_time >= Global.test.timeout)
|
||||
{
|
||||
finish(test_result::timeout, std::format("no result after {:.0f} s", Global.test.timeout));
|
||||
return;
|
||||
WriteLog(std::format("test: pass requested at {:.1f} s{}, waiting for every vehicle to stop", m_progress.time,
|
||||
frame.reason.empty() ? std::string() : std::format(" ({})", frame.reason)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +100,7 @@ void test_manager::signal_passed_at_danger(std::string_view const Vehicle, std::
|
||||
}
|
||||
if (false == Global.test.fail_if_stop_overran)
|
||||
{
|
||||
WriteLog(std::format("test: {} at {:.1f} s, allowed by test.fail_if_stop_overran", reason, m_time));
|
||||
WriteLog(std::format("test: {} at {:.1f} s, allowed by test.fail_if_stop_overran", reason, m_progress.time));
|
||||
return;
|
||||
}
|
||||
finish(test_result::signal_passed_at_danger, reason);
|
||||
@@ -168,7 +119,7 @@ bool test_manager::conclude(test_result const Result, std::string_view const Rea
|
||||
m_finished = true;
|
||||
m_result = Result;
|
||||
|
||||
auto const line { std::format("test: {} (exit code {}) after {:.1f} s of simulation time: {}", to_string(Result), static_cast<int>(Result), m_time, Reason) };
|
||||
auto const line { std::format("test: {} (exit code {}) after {:.1f} s of simulation time: {}", to_string(Result), static_cast<int>(Result), m_progress.time, Reason) };
|
||||
if (Result == test_result::pass)
|
||||
WriteLog(line);
|
||||
else
|
||||
|
||||
@@ -9,32 +9,19 @@ http://mozilla.org/MPL/2.0/.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "simulation/simulationtestrules.h"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
// integration test mode (eu07 --test -s <scenery>, eu07/maszyna#541): the scenery runs unattended in a tight
|
||||
// fixed-step loop and the simulator quits with an exit code saying how the test went
|
||||
namespace simulation {
|
||||
|
||||
// exit codes of a test run; public, scripts and CI read them
|
||||
enum class test_result : int {
|
||||
pass = 0, // __test.result set to 1, then every vehicle came to rest (or test.pass_on_standstill)
|
||||
fail = 1, // __test.result set to 0
|
||||
signal_passed_at_danger = 2, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no
|
||||
timeout = 3, // test.timeout seconds of simulation time went by without a result
|
||||
standstill = 4, // every vehicle stood still for test.standstill seconds without a result
|
||||
error = 5, // the run could not start (no -s, the scenery failed to load) or the simulator threw
|
||||
};
|
||||
|
||||
std::string_view to_string(test_result Result);
|
||||
|
||||
class test_manager {
|
||||
|
||||
public:
|
||||
// name of the memory cell a scenery reports its result to
|
||||
static constexpr std::string_view result_cell { "__test.result" };
|
||||
// how long everything has to stand before a requested pass is granted [s]
|
||||
static constexpr double pass_settle_time { 1.0 };
|
||||
// fixed simulation time step of one frame in test mode [s]: 3 physics steps of 10 ms
|
||||
static constexpr double frame_step { 0.03 };
|
||||
|
||||
@@ -60,9 +47,7 @@ private:
|
||||
bool m_started { false };
|
||||
bool m_finished { false };
|
||||
test_result m_result { test_result::error };
|
||||
bool m_passrequested { false };
|
||||
double m_time { 0.0 }; // simulation time since the start [s]
|
||||
double m_resttime { 0.0 }; // how long every vehicle has been standing [s]
|
||||
test_progress m_progress;
|
||||
};
|
||||
|
||||
// the test of the run
|
||||
|
||||
139
simulation/simulationtestrules.cpp
Normal file
139
simulation/simulationtestrules.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "simulation/simulationtestrules.h"
|
||||
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
|
||||
namespace {
|
||||
|
||||
// the cell holds what the scenery wrote, so the values are whole numbers; the margin only keeps a stray fraction out
|
||||
bool holds(double const Value, double const Expected)
|
||||
{
|
||||
return std::abs(Value - Expected) < 1e-6;
|
||||
}
|
||||
|
||||
// the text of the result cell, as said after the result
|
||||
std::string reason_suffix(std::string_view const Reason)
|
||||
{
|
||||
return Reason.empty() ? std::string() : std::format(" ({})", Reason);
|
||||
}
|
||||
|
||||
// options of the simulator which take a value, see eu07_application::init_settings()
|
||||
bool takes_value(std::string_view const Option)
|
||||
{
|
||||
return Option == "-s" || Option == "-v" || Option == "-seed" || Option == "-ini";
|
||||
}
|
||||
|
||||
// a number of seconds given to Option, at Index of Arguments; the index is moved past the value
|
||||
std::optional<double> option_seconds(std::span<char const *const> const Arguments, std::size_t &Index)
|
||||
{
|
||||
if (Index + 1 >= Arguments.size())
|
||||
return std::nullopt;
|
||||
++Index;
|
||||
return simulation::parse_seconds(Arguments[Index]);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace simulation {
|
||||
|
||||
std::string_view to_string(test_result const Result)
|
||||
{
|
||||
using enum test_result;
|
||||
switch (Result)
|
||||
{
|
||||
case pass: return "pass";
|
||||
case fail: return "fail";
|
||||
case signal_passed_at_danger: return "signal passed at danger";
|
||||
case timeout: return "timeout";
|
||||
case standstill: return "standstill";
|
||||
case error: return "error";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
std::optional<double> parse_seconds(std::string_view const Text)
|
||||
{
|
||||
auto const *const last{Text.data() + Text.size()};
|
||||
double value{0.0};
|
||||
if (auto const [parsed, status]{std::from_chars(Text.data(), last, value)}; status != std::errc{} || parsed != last || false == std::isfinite(value) || value < 0.0)
|
||||
return std::nullopt;
|
||||
return value;
|
||||
}
|
||||
|
||||
test_command_line parse_test_command_line(std::span<char const *const> const Arguments)
|
||||
{
|
||||
test_command_line result;
|
||||
for (std::size_t i = 1; i < Arguments.size(); ++i)
|
||||
{
|
||||
std::string_view const option{Arguments[i]};
|
||||
if (option == "--test")
|
||||
{
|
||||
result.enabled = true;
|
||||
}
|
||||
else if (option == "--test-timeout")
|
||||
{
|
||||
auto const seconds{option_seconds(Arguments, i)};
|
||||
if (false == seconds.has_value())
|
||||
{
|
||||
// a typo would otherwise turn the limit off
|
||||
if (result.error.empty())
|
||||
result.error = std::format("{} needs a number of seconds", option);
|
||||
continue;
|
||||
}
|
||||
result.timeout = seconds;
|
||||
}
|
||||
else if (takes_value(option))
|
||||
{
|
||||
++i;
|
||||
}
|
||||
else if (option == "-edit" && i + 1 < Arguments.size() && Arguments[i + 1][0] != '-')
|
||||
{
|
||||
// the scenery to edit is optional
|
||||
++i;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::optional<test_verdict> evaluate_frame(test_progress &Progress, test_frame const &Frame, test_limits const &Limits)
|
||||
{
|
||||
using enum test_result;
|
||||
|
||||
Progress.time += Frame.deltatime;
|
||||
|
||||
if (Frame.reported.has_value())
|
||||
{
|
||||
auto const value{*Frame.reported};
|
||||
if (holds(value, 0.0))
|
||||
return test_verdict{fail, std::format("__test.result set to 0{}", reason_suffix(Frame.reason))};
|
||||
if (holds(value, 1.0))
|
||||
{
|
||||
if (false == Limits.no_movement_before_pass)
|
||||
return test_verdict{pass, std::format("__test.result set to 1{}", reason_suffix(Frame.reason))};
|
||||
Progress.passrequested = true;
|
||||
}
|
||||
}
|
||||
|
||||
Progress.resttime = Frame.atrest ? Progress.resttime + Frame.deltatime : 0.0;
|
||||
|
||||
if (Progress.passrequested && Progress.resttime >= pass_settle_time)
|
||||
return test_verdict{pass, "__test.result set to 1 and every vehicle stopped"};
|
||||
if (Limits.standstill > 0.0 && Progress.resttime >= Limits.standstill)
|
||||
return test_verdict{Limits.pass_on_standstill ? pass : standstill, std::format("every vehicle stood still for {:.0f} s", Limits.standstill)};
|
||||
if (Limits.timeout > 0.0 && Progress.time >= Limits.timeout)
|
||||
return test_verdict{timeout, std::format("no result after {:.0f} s", Limits.timeout)};
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace simulation
|
||||
82
simulation/simulationtestrules.h
Normal file
82
simulation/simulationtestrules.h
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
This Source Code Form is subject to the
|
||||
terms of the Mozilla Public License, v.
|
||||
2.0. If a copy of the MPL was not
|
||||
distributed with this file, You can
|
||||
obtain one at
|
||||
http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// the rules of the test mode (eu07 --test, eu07/maszyna#541) which need nothing of the running simulation: the exit
|
||||
// codes, the command line and the end conditions. kept apart so the unit tests can check them
|
||||
namespace simulation {
|
||||
|
||||
// exit codes of a test run; public, scripts and CI read them
|
||||
enum class test_result : int {
|
||||
pass = 0, // __test.result set to 1, then every vehicle came to rest (or test.pass_on_standstill)
|
||||
fail = 1, // __test.result set to 0
|
||||
signal_passed_at_danger = 2, // a train went past a stop signal (SetVelocity 0 0), unless test.fail_if_stop_overran no
|
||||
timeout = 3, // test.timeout seconds of simulation time went by without a result
|
||||
standstill = 4, // every vehicle stood still for test.standstill seconds without a result
|
||||
error = 5, // the run could not start (a bad command line, the scenery failed to load) or the simulator threw
|
||||
};
|
||||
|
||||
std::string_view to_string(test_result Result);
|
||||
|
||||
// how a test ended, and why
|
||||
struct test_verdict {
|
||||
test_result result{test_result::error};
|
||||
std::string reason;
|
||||
};
|
||||
|
||||
// the test mode options of the command line
|
||||
struct test_command_line {
|
||||
bool enabled{false}; // --test
|
||||
std::optional<double> timeout; // --test-timeout <seconds of simulation time>
|
||||
std::string error; // what's wrong with them; empty if nothing
|
||||
};
|
||||
|
||||
// seconds given on the command line: a number, not negative. nothing for anything else
|
||||
std::optional<double> parse_seconds(std::string_view Text);
|
||||
|
||||
// picks the test mode options out of a command line (the first argument is the program). the values of the other
|
||||
// options are skipped, so a scenery named like an option isn't taken for one; options it doesn't know are left alone
|
||||
test_command_line parse_test_command_line(std::span<char const *const> Arguments);
|
||||
|
||||
// the limits and switches the end conditions go by
|
||||
struct test_limits {
|
||||
double timeout{3600.0}; // [s] of simulation time; 0 = never
|
||||
double standstill{300.0}; // [s] of every vehicle standing without a result; 0 = never
|
||||
bool pass_on_standstill{false};
|
||||
bool no_movement_before_pass{true};
|
||||
};
|
||||
|
||||
// what a frame of the simulation brought
|
||||
struct test_frame {
|
||||
double deltatime{0.0}; // [s] of simulation time
|
||||
std::optional<double> reported; // value of the result cell, if there is one
|
||||
std::string_view reason; // text of the result cell
|
||||
bool atrest{false}; // every vehicle stands
|
||||
};
|
||||
|
||||
// the state of a test between frames
|
||||
struct test_progress {
|
||||
double time{0.0}; // simulation time since the start [s]
|
||||
double resttime{0.0}; // how long every vehicle has been standing [s]
|
||||
bool passrequested{false}; // __test.result was set to 1, the pass waits for the vehicles to stop
|
||||
};
|
||||
|
||||
// how long everything has to stand before a requested pass is granted [s]
|
||||
inline constexpr double pass_settle_time{1.0};
|
||||
|
||||
// checks the end conditions after a frame and moves the progress on. returns the verdict once the test is over
|
||||
std::optional<test_verdict> evaluate_frame(test_progress &Progress, test_frame const &Frame, test_limits const &Limits);
|
||||
|
||||
} // namespace simulation
|
||||
Reference in New Issue
Block a user