18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-14 07:54:55 +01:00
Files
maszyna/simulation/simulationtestrules.cpp
jerrrrycho e5aafd154f feat(simulation): the pass and the standstill wait for the train under test only
Both waited for every vehicle of the scenery to stand still, so on a scenery
with AI traffic a test could pass only when the traffic happened to stand too,
and the standstill end hardly ever came.

A test scenery names the train under test with test.vehicle <name> in its
config block (once for each train, if several): the pass and the standstill wait
for the consists of these vehicles as they are at the moment, so a train the
tested one couples to counts with it. A name the scenery doesn't have ends the
test with the error code. Without test.vehicle every vehicle counts, as before.
A signal passed at danger counts for every train either way.

run.py puts the case's lead vehicle into test.vehicle. test_mode_standstill_traffic
has the train under test standing on track 2 of Stacja1 while AI traffic leaves
track 1 on the timetable: it ends on its 100 s standstill, where counting every
vehicle runs into the 300 s timeout. Physicslogs of the other trains of a case
(<something>_<vehicle>) are moved out of the game dir too.
2026-11-08 11:39:52 +01:00

150 lines
4.4 KiB
C++

/*
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" || option == "--test-wall-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;
}
if (option == "--test-timeout")
result.timeout = seconds;
else
result.wall_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;
}
double wall_clock_limit(std::optional<double> const Given, double const Simulationtimeout)
{
if (Given.has_value())
return *Given;
return Simulationtimeout > 0.0 ? Simulationtimeout + wall_timeout_loading_allowance : 0.0;
}
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 the tested trains stopped"};
if (Limits.standstill > 0.0 && Progress.resttime >= Limits.standstill)
return test_verdict{Limits.pass_on_standstill ? pass : standstill, std::format("the tested trains 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