/* 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 #include #include #include #include #include // the expectations of a script: each logged as SELFTEST PASS or SELFTEST FAIL : got , expected , 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: 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(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 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 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(editor_track::joint_roll(*end.track, end.point)))}; auto const theirs{Cant(static_cast(editor_track::joint_roll(*other.track, other.point)))}; auto const step{std::abs(static_cast(own) + static_cast(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 [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::ranges::count_if(switch_store::records(), counted))}; expectation(count == wanted, What, std::to_string(count), std::to_string(wanted)); } // expect shaped : 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 [tolerance], expect gauge [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(track->fTrackWidth), wanted, tolerance); } // expect track 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 void expect_route(std::string const &What, std::istringstream &Words, std::vector 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 [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 , expect lift