18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 12:57:06 +01:00
Files
maszyna/application/editormodeselftestexpect.cpp
T
maj00r 10f2ac36f9 fix(editor): a switch laid gets the speed of its diverging track, one breaking a hard rule of ST-T1-A6 isn't laid (#454)
* fix(editor): track edits keep the joints, the norms, the records and the saved speed together

- The cant typed in the inspector goes on into the track joined at that end, as the gizmo and the
  joints tool do it; the neighbours go into the same undo step. The joint stepped before.
- A track laid on from a canted joint takes the cant off along the ramp the norms ask for at the
  speed (`alignment::cant_ramp_length`), not at a fixed 1.5 mm a metre whatever the speed and the
  norms: 120 mm at 100 km/h over 120 m, not 80 m.
- Deleting a track of the line under the route tool keeps the laying norms. They went back to the
  defaults, and every later lay, extend and join used those.
- The gauge is a paste group of its own, off by default: pasting how a track rides or sounds no
  longer changes its gauge.
- The speed of a turntable set in the editor is the one saved. The editor changed the speed the
  turntable runs at, while the file keeps the limit it was loaded with.
- Deleting a set of tracks does what Del does with each: a double switch, a slip and a crossover go
  whole, the records of the switches go with them, and the model and events of a turntable too. The
  records stayed behind and on saving were given to another switch, which then showed a wrong type.
- Deleting a switch of a crossover takes the crossover out whole, as the connection tools do: the
  outer track of a crossover on a curve comes down by its lift and the record of the connection
  goes. Alone, the switch left the outer track up for good, and the connection to the next switch
  given its name. A switch bent out of the shape of its record is found by its name.

Tests: a unit test of the norm ramp. The other changes are in the editor's UI, which the unit tests
don't reach.

* test(editor): self-test scripts that fail, a runner, and a case for each fix of the track editing

The editor's self-test scripts only logged: nothing made a run fail. Now:

- `expect` commands check the state and log `SELFTEST PASS` or `SELFTEST FAIL ... got X, expected Y`:
  the cant at a track end, the joint at an end (gap, angle, cant step), the records of a switch and
  the connections naming it, speed, gauge, a track present or removed, the laying norms' gauge,
  heights and lifts against a mark, whether a track is in the line under the route tool, and the
  includes of a turntable. A command no group takes counts as a failure, so a mistyped one can't
  make the checks after it pass.
- At `quit` the result is logged, and a script whose expectations failed ends the editor with exit
  code 2 (a failed start stays 1; the simulator's test mode is unchanged).
- The commit of the inspector's cant field is `track_cant_commit`, which the inspector and the
  `trackcant` command both call; `routegauge` sets the laying norms as the route tool does, and
  `tracksetdelete` deletes a set of tracks by name.
- `tests/editor/run.py` runs each script of `tests/editor/cases` on a copy of the fixture scenery
  in the game folder, with a wall-time limit and a memory cap, reads the exit code and the
  `SELFTEST` lines, and deletes the copy. A `#> reopen` line restarts the editor on the saved
  scenery. See `tests/editor/README.md`.
- One case per fix of the track editing quick fixes (inspector cant, norms after deleting, the
  gauge paste group, turntable speed saved, deleting a set, a set with a double switch, a slip
  and a crossover, deleting a switch of a crossover, a bent switch). All pass; with the fixes
  reverted all fail.

* feat(editor): the track tool panels keep their fields, results and buttons within the panel

- The card of the tool: after a long gesture the action goes under it, at its indent, when what is
  left of the row is narrower than 8 font sizes or the longest word of the action. It was squeezed
  into a sliver and broken in the middle of its words.
- A click on the title row of the card folds it to that row (icon, name, family, key, and a small
  triangle marker) and opens it again. One flag for all the tools, kept with the editor settings
  (`track_card_folded` in eu07_editor.ini), open by default: the card took 250-300 px at the top
  of every tool and repeats the hint bar of the view.
- The grade labels of the vertical profile, on the graph and in the view, write the per mille as
  the panel does (the translated "per mille", the per mille sign in Polish), not "o/oo".
- The track analysis window: the thresholds of the speed check and of the joints keep to their
  widest name and a field of 10 font sizes where the window is wider than that, as docked across
  the bottom (`begin_fields_fitted`). The results are tables with each column as wide as what is in
  it: the speed check with its column names kept at the top as the list scrolls, the joints with
  the kind, the value and the two tracks next to each other. A click on a row shows it as before.
- `same_line_if_fits` puts the next button beside the one before only when it fits in what is left
  of the line, at the start of the next line otherwise. Used where the route design puts buttons
  side by side, for "Forget the design" in the profile panel, which was cut off at the edge, and for
  the buttons of a switch in the inspector.
- The laying options: the speed, the curve radius and the longest path are fields with the name on
  the left in two columns (`begin_fields`/`field_row`), as in the inspector, the route and the
  profile, the chips of the quick values under them. The segmented choices give the short names
  less of the room when a long one doesn't fit in its even share, and go on in a second bar when
  they don't fit side by side: "Through the clicked points" overflowed its third of the bar.

* fix(editor): a switch laid gets the speed of its diverging track, one breaking a hard rule of ST-T1-A6 isn't laid

- Every switch laid writes the speed of its diverging track (`velocity`): the switch tool on a
  straight, into a curve, from a free end and linked, a double switch, the switches of a slip, a
  switch given another template of the catalogue, and a switch moved along the curve it's bent
  into. A switch of the catalogue had no limit before, one copied from a switch of the scenery kept
  that one's, one laid without a track had the laying speed on both its tracks. The limit is for the
  diverging track alone (`velocity -V`); the own limit of the track it goes into stays for both
  where it's the lower. The switch tool says the speed and what gives it.
- The speed is formula 9.9 of PKP PLK ST-T1-A6 the other way round, V = sqrt((I + D) r / 11.8), with
  the normal cant deficiency of the frog of table 9.7 (110 mm a fixed frog, 130 mm a movable nose),
  rounded down to the steps of the speed limits and no more than table 11.2 gives the basic switch.
  For the fixed frogs the formula gives table 11.2 itself (190 40, 300 50, 500 60, 760 80, 1200
  100 km/h); the 2500-1:26.5 with a movable nose stays 130. A switch bent into a curve takes the
  radius of its diverging track from table 11.8 (one-sided r" = (Rr - t^2)/(R + r), two-sided
  (Rr + t^2)/(R - r)) and the cant of the line with its sign: leaning against the diverging track it
  takes speed away, where the speed check counted it as helping. Rz 60E1-190-1:9 bent out of the
  curve R 600 m with 60 mm: 30 km/h, not 40; the 60E1-500-1:12 of the review, out of R 1200 m with
  60 mm: 60 km/h at its fixed frog (54 at I = 100 mm, the 0.65 m/s2 the editor took), not 107.
- A switch breaking a hard rule isn't laid, the tracks left as they were, and the switch tool says
  why, with the value, the limit and the paragraph: the cant changing within it (11.4 pkt 7), a cant
  ramp ending within it or nearer than 6 m (9.6 pkt 16-17; one ending right at an end of the switch
  is let be, as the snapping of the start offers it), a double switch, a slip or a crossing bent
  (11.4 pkt 4), the curvature of its diverging or its main track turning from one side to the other
  (9.7 pkt 12, KZW II). They were warnings shown after the switch was laid, and on a straight the
  cant wasn't looked at at all. The line is looked at 10 m past the ends of the curve or the
  straight too, so the ramp on the straight before a curve counts. The previews show the refusals.
- A switch moved along its straight or the curve it's bent into, a double switch moved along its
  straight, and a switch given another template go through the same checks; one breaking a rule
  stays as it is.
- Warnings still: 1:9.403 and steeper than 1:9 bent (11.4 pkt 4c-d), a rule of the running and the
  main tracks, which the editor doesn't tell from the side ones, as the warning now says; the limits
  of table 11.9 and a cant over 100 mm in a bent switch (table 9.6), as before.

Tests: unit tests of tables 11.8 and 11.9 (the least main radius bends the diverging track to the
least radius the table gives), table 9.7, the speeds of table 11.2 by formula 9.9, the speed of a
bent switch with the cant with its sign (the example of the review), the cant over a switch on a
straight and a double switch bent. Editor cases `q2_1_switch_speed`, `q2_2_switch_refusals` and
`q2_3_switch_move`, with `expect status` (the status of the switch tool) and `expect steps` (the
undo steps), the fixture with a straight taking a cant on and a compound curve without cant. All
pass; with the changes of the editor reverted all three fail.

* test(editor): a mistyped track fails the self-test, indented comments are skipped, case 7b checks the switch got bent

- A blank line or an indented `#` comment in a script is no longer a command no group takes.
- `trackcant`, `routegauge` and `tracksetdelete` with a track that isn't there count as a failed expectation.
- `expect shaped <switch> <yes|no>`: 7b checks that the gizmo drag bent the switch out of its record before deleting it.
- The ramp-length loop of test_alignment counts in an integer (Sonar S2193).
2026-11-27 03:57:25 +01:00

408 lines
14 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 "application/editormode.h"
#include "application/editormodeselftest.h"
#include "editor/editorFormat.hpp"
#include "editor/editorIncludeInfo.hpp"
#include "editor/editorSwitchStore.hpp"
#include "scene/scenelayers.h"
#include "simulation/simulation.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include "world/Track.h"
#include <algorithm>
#include <cmath>
#include <map>
#include <sstream>
#include <string>
#include <vector>
// the expectations of a script: each logged as SELFTEST PASS <what> or SELFTEST FAIL <what>: got <value>, expected <value>, the failed
// ones counted, so the run ends with an error code at quit (editormodeselftest.cpp)
namespace
{
// the end of another track nearest to the end of a path is taken as the one joined there when it's this near, m
double constexpr kJointReach{1.0};
// the conversion of a roll [deg] to the cant [mm] the inspector shows
using cant_conversion = float (*)(float);
void expectation(bool const Passed, std::string const &What, std::string const &Got, std::string const &Wanted)
{
++editor_selftest::expectations();
if (Passed)
{
WriteLog("SELFTEST PASS " + What);
return;
}
++editor_selftest::failures();
WriteLog("SELFTEST FAIL " + What + ": got " + Got + ", expected " + Wanted);
}
void expect_near(std::string const &What, double const Got, double const Wanted, double const Tolerance)
{
expectation(std::abs(Got - Wanted) <= Tolerance, What, format("%.4f", Got), format("%.4f +- %.4f", Wanted, Tolerance));
}
// the arguments of the expectation weren't all there, or weren't numbers where numbers go: it fails
bool malformed(std::string const &What, std::istringstream const &Words)
{
if (false == Words.fail())
return false;
expectation(false, What, "malformed", "the arguments the expectation takes");
return true;
}
// an optional number after the arguments: Fallback when there's none. Read apart, as a failed read gives 0
double optional_number(std::istringstream &Words, double const Fallback)
{
if (double given{0.0}; Words >> given)
return given;
return Fallback;
}
// the end of the first path of the track named, as the expectations name it: <track> <start|end>
struct track_end
{
TTrack const *track{nullptr};
editor_track::point_ref point;
std::string error;
};
track_end read_end(std::istringstream &Words)
{
std::string name;
std::string side;
Words >> name >> side;
track_end result;
result.track = simulation::Paths.find(name);
result.point = {0, side == "end" ? editor_track::point_kind::end : editor_track::point_kind::start};
if (result.track == nullptr || result.track->m_editorremoved || result.track->m_paths.empty())
result.error = "no track " + name;
else if (side != "start" && side != "end")
result.error = "no end " + side;
return result;
}
bool failed_end(std::string const &What, track_end const &End)
{
if (End.error.empty())
return false;
expectation(false, What, End.error, "the end of a track");
return true;
}
// the end of a path of another track nearest to a point, within the reach of a joint
struct joined_end
{
TTrack const *track{nullptr};
editor_track::point_ref point;
double distance{kJointReach};
void take(TTrack const &Track, glm::dvec3 const &Point)
{
for (int path = 0; path < static_cast<int>(Track.m_paths.size()); ++path)
for (auto const kind : {editor_track::point_kind::start, editor_track::point_kind::end})
if (auto const gap{glm::distance(editor_track::point_position(Track, {path, kind}), Point)}; gap < distance)
{
distance = gap;
track = &Track;
point = {path, kind};
}
}
};
// expect cant <track> <start|end> <mm> <tolerance mm>
void expect_cant(std::string const &What, std::istringstream &Words, cant_conversion const Cant)
{
auto const end{read_end(Words)};
double wanted{0.0};
double tolerance{0.0};
Words >> wanted >> tolerance;
if (malformed(What, Words) || failed_end(What, end))
return;
auto const &path{end.track->m_paths.front()};
expect_near(What, Cant(end.point.kind == editor_track::point_kind::start ? path.rolls[0] : path.rolls[1]), wanted, tolerance);
}
// expect joint <track> <start|end> <gap m> <angle deg> <cant step mm>
void expect_joint(std::string const &What, std::istringstream &Words, cant_conversion const Cant)
{
auto const end{read_end(Words)};
double gap{0.0};
double angle{0.0};
double cant{0.0};
Words >> gap >> angle >> cant;
if (malformed(What, Words) || failed_end(What, end))
return;
auto const at{editor_track::point_position(*end.track, end.point)};
joined_end other;
for (auto const *track : simulation::Paths.sequence())
if (track != nullptr && track != end.track && false == track->m_editorremoved)
other.take(*track, at);
if (other.track == nullptr)
{
expectation(false, What, "no end of another track", format("one within %.1f m", kJointReach));
return;
}
auto const out{glm::normalize(editor_track::outward(*end.track, end.point))};
auto const in{-glm::normalize(editor_track::outward(*other.track, other.point))};
auto const kink{glm::degrees(std::acos(std::clamp(glm::dot(out, in), -1.0, 1.0)))};
// the rolls of the two ends are opposite where the joint doesn't step, as editor_track::match_rolls makes them
auto const own{Cant(static_cast<float>(editor_track::joint_roll(*end.track, end.point)))};
auto const theirs{Cant(static_cast<float>(editor_track::joint_roll(*other.track, other.point)))};
auto const step{std::abs(static_cast<double>(own) + static_cast<double>(theirs))};
auto const side{other.point.kind == editor_track::point_kind::start ? "start" : "end"};
expectation(other.distance <= gap && kink <= angle && step <= cant, What, format("gap %.4f m, kink %.4f deg, cant step %.1f mm to %s[%d] %s", other.distance, kink, step, other.track->name().c_str(), other.point.path, side),
format("at most %.4f m, %.4f deg, %.1f mm", gap, angle, cant));
}
// expect records <name> <count> [kind]
void expect_records(std::string const &What, std::istringstream &Words)
{
std::string name;
std::size_t wanted{0};
Words >> name >> wanted;
if (malformed(What, Words))
return;
std::string kind;
Words >> kind;
// the records of the switch, slip or crossing of the name, and the connections having the switch among theirs
auto const counted = [&name, &kind](switch_store::record const &Entry) {
auto const among{Entry.kind == "connection" && std::ranges::find(Entry.parts, name) != Entry.parts.end()};
return (Entry.name == name || among) && (kind.empty() || Entry.kind == kind);
};
auto const count{static_cast<std::size_t>(std::ranges::count_if(switch_store::records(), counted))};
expectation(count == wanted, What, std::to_string(count), std::to_string(wanted));
}
// expect shaped <switch> <yes|no>: the switch keeps the shape its record says, or was bent out of it
void expect_shaped(std::string const &What, std::istringstream &Words)
{
std::string name;
std::string wanted;
Words >> name >> wanted;
if (malformed(What, Words))
return;
auto const *track{simulation::Paths.find(name)};
if (track == nullptr || track->m_editorremoved)
{
expectation(false, What, "no track " + name, "a switch");
return;
}
auto const shaped{switch_store::of(*track).has_value()};
expectation(shaped == (wanted == "yes"), What, shaped ? "yes" : "no", wanted);
}
// expect speed <track> <km/h> [tolerance], expect gauge <track> <m> [tolerance]
void expect_track_value(std::string const &What, std::string const &Kind, std::istringstream &Words)
{
std::string name;
double wanted{0.0};
Words >> name >> wanted;
if (malformed(What, Words))
return;
auto const speed{Kind == "speed"};
auto const tolerance{optional_number(Words, speed ? 0.01 : 0.0005)};
auto const *track{simulation::Paths.find(name)};
if (track == nullptr || track->m_editorremoved)
{
expectation(false, What, "no track " + name, "the track");
return;
}
expect_near(What, speed ? editor_track::velocity(*track) : static_cast<double>(track->fTrackWidth), wanted, tolerance);
}
// expect track <name> <present|removed>
void expect_track(std::string const &What, std::istringstream &Words)
{
std::string name;
std::string state;
Words >> name >> state;
if (malformed(What, Words))
return;
if (state != "present" && state != "removed")
{
expectation(false, What, "no state " + state, "present or removed");
return;
}
auto const *track{simulation::Paths.find(name)};
auto const present{track != nullptr && false == track->m_editorremoved};
expectation(present == (state == "present"), What, present ? "present" : "removed", state);
}
// expect route <track> <in|out>
void expect_route(std::string const &What, std::istringstream &Words, std::vector<TTrack *> const &Line)
{
std::string name;
std::string state;
Words >> name >> state;
if (malformed(What, Words))
return;
if (state != "in" && state != "out")
{
expectation(false, What, "no state " + state, "in or out");
return;
}
auto const *track{simulation::Paths.find(name)};
auto const in{track != nullptr && std::ranges::find(Line, track) != Line.end()};
expectation(in == (state == "in"), What, in ? "in" : "out", state);
}
// expect norms gauge <mm> [tolerance]
void expect_norm(std::string const &What, std::istringstream &Words, alignment::limits const &Norms)
{
std::string key;
double wanted{0.0};
Words >> key >> wanted;
if (malformed(What, Words))
return;
if (key != "gauge")
{
expectation(false, What, "no norm " + key, "gauge");
return;
}
expect_near(What, Norms.gauge, wanted, optional_number(Words, 0.5));
}
// expect height <track> <start|end> <m> <tolerance>, expect lift <track> <start|end> <label> <m> <tolerance>
void expect_height(std::string const &What, std::string const &Kind, std::istringstream &Words, std::map<std::string, double> const &Marks)
{
auto const end{read_end(Words)};
std::string label;
if (Kind == "lift")
Words >> label;
double wanted{0.0};
double tolerance{0.0};
Words >> wanted >> tolerance;
if (malformed(What, Words) || failed_end(What, end))
return;
auto height{editor_track::point_position(*end.track, end.point).y};
if (Kind == "lift")
{
auto const mark{Marks.find(label)};
if (mark == Marks.end())
{
expectation(false, What, "no mark " + label, "a height marked");
return;
}
height -= mark->second;
}
expect_near(What, height, wanted, tolerance);
}
// the include is in the scenery, not deleted, and the name is among the values of its parameters: the model and the events of a
// turntable name its track so
bool names_track(scene::instance_handle const Handle, std::string const &Name)
{
if (false == scene::Layers.tracked(Handle))
return false;
auto const &included{scene::Layers.instance(Handle)};
if (included.removed || included.dead || included.file == nullptr)
return false;
std::string file;
std::vector<std::string> values;
return editor_includes::parse_directive(scene::Layers.directive(Handle), file, values) && std::ranges::any_of(values, [&Name](std::string const &Value) { return ToLower(Value) == Name; });
}
// expect includes <track> <count>
void expect_includes(std::string const &What, std::istringstream &Words)
{
std::string name;
std::size_t wanted{0};
Words >> name >> wanted;
if (malformed(What, Words))
return;
std::size_t count{0};
for (scene::instance_handle handle = 1; handle <= scene::Layers.instance_count(); ++handle)
if (names_track(handle, name))
++count;
expectation(count == wanted, What, std::to_string(count), std::to_string(wanted));
}
// expect status <text>: the status the switch tool shows in its panel holds the text, the rest of the line
void expect_status(std::string const &What, std::istringstream &Words, std::string const &Status)
{
std::string wanted;
std::getline(Words >> std::ws, wanted);
if (wanted.empty())
{
expectation(false, What, "malformed", "the text the status holds");
return;
}
expectation(Status.find(wanted) != std::string::npos, What, "\"" + Status + "\"", "a status holding \"" + wanted + "\"");
}
// expect steps <count>: the steps Ctrl+Z takes back, the ones made together counted once
void expect_steps(std::string const &What, std::istringstream &Words, std::size_t const Steps)
{
std::size_t wanted{0};
Words >> wanted;
if (malformed(What, Words))
return;
expectation(Steps == wanted, What, std::to_string(Steps), std::to_string(wanted));
}
} // namespace
editor_mode::selftest_result editor_mode::selftest_expect(std::string const &command, std::istringstream &words)
{
if (command == "mark")
{
std::string label;
words >> label;
auto const end{read_end(words)};
if (failed_end("mark " + label, end))
return selftest_result::handled;
auto const height{editor_track::point_position(*end.track, end.point).y};
m_selftest.marks[label] = height;
WriteLog(format("SELFTEST mark %s: %.4f m", label.c_str(), height));
return selftest_result::handled;
}
if (command != "expect")
return selftest_result::unhandled;
// what is expected as the script says it, for the log
auto what{words.str()};
what.erase(0, std::min(what.size(), what.find("expect") + 6));
what.erase(0, std::min(what.size(), what.find_first_not_of(' ')));
std::string kind;
words >> kind;
if (kind == "cant")
expect_cant(what, words, &editor_mode::cant_of_roll);
else if (kind == "joint")
expect_joint(what, words, &editor_mode::cant_of_roll);
else if (kind == "records")
expect_records(what, words);
else if (kind == "shaped")
expect_shaped(what, words);
else if (kind == "speed" || kind == "gauge")
expect_track_value(what, kind, words);
else if (kind == "track")
expect_track(what, words);
else if (kind == "route")
expect_route(what, words, m_route.chain.tracks);
else if (kind == "norms")
expect_norm(what, words, laying_norms());
else if (kind == "height" || kind == "lift")
expect_height(what, kind, words, m_selftest.marks);
else if (kind == "includes")
expect_includes(what, words);
else if (kind == "status")
expect_status(what, words, m_switch.status);
else if (kind == "steps")
expect_steps(what, words, static_cast<std::size_t>(std::ranges::count_if(m_history, [](EditorSnapshot const &Step) { return false == Step.joined; })));
else
expectation(false, what, "no expectation " + kind, "cant, joint, records, shaped, speed, gauge, track, route, norms, height, lift, includes, status or steps");
return selftest_result::handled;
}