18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 08:55:22 +01:00
Files
maszyna/tests/unit
maj00r bdffbccd2a test(editor): the station track picked by bands of height in a unit case; a station failing half way and one at the history cap in self-tests
The review of #508 asked for these two.

- The choice of the track a station starts or ends on is trace_station::pick_station_track(), a free function of
  editor/editorTraceStation: the tracks in reach in plan compared by bands of 3 m of height, then by the distance in plan, the
  first of two alike. editor_mode::station_track_at() collects the tracks as before and takes its pick, in the same order and
  with the same comparisons, so it picks what it picked. The unit case covers a flyover above and a bridge over a cutting
  against the track at the height clicked, a high embankment within one band, a track only far above or below, the reach and
  the width of the bands.
- st_5_station_failed: the station of st_1 made to fail after its first turnout, then after both turnouts with the line
  between them not laid. Each time the turnouts laid are taken back, the main track is as it was, the clicks are kept but the
  last one, the reason is told and Ctrl+Y has nothing to lay; the last click again lays the station in full.
- st_6_station_history_cap: the same station laid with the history capped at 2 steps, laid in full and not taken for failed.
- For them the self-test has stationfail <end|line> (the next station made to fail there, once; lay_station() asks
  selftest_station_fails(), which only the self-test sets), historycap <steps> and expect laypoints <count>.
2026-12-02 17:35:09 +01:00
..
feat(editor): stations traced on the photo (Etap 5): laying from the photo, a first click on a straight track starts the line with a turnout of the catalogue into it, of the rail of the track or of the assumptions, its type, its start along the track, the way it runs and its side fitted to the points clicked along its diverging track and the straight just beyond it, the start held within a few metres of a click on the main track (where the points tell no type: the one the line starts with, the one chosen in the switch tool, the usual one of a station), and a click on a track, or Enter over one, ends the line with one into that track; from a click on a track to one on a track alongside it 2.5 to 15 m away with only the points along the crossover between them, the crossover of the solver of the connections; a run of three points or more over 40 m keeping one spacing from a track beside it within the noise of the clicks laid exactly alongside it, the spacing rounded to 4.0, 4.5, 4.75 or 5.0 m within 0.1 m of it, else to 5 cm: alongside a straight track from a turnout or into one designed, an arc joining each turnout onto the straight alongside, its radius fitted to the points along it, else its points moved onto the spacing and traced; a turnout nearer to the next one on its track than the straight the norms ask between turnouts (ST-T1-A6 11.3 pkt 7, max(0.15 V; 6) m at the speed of its diverging track) moved along the track to where it keeps it, told by how much and why; the turnouts laid by the code of the switch tool, the line between their diverging tracks as the lay tool lays one between free ends, no faster than the turnouts where it runs between two, all of it one step of the history; refused with the reason and the way to do it into a curve, on a line of the model, where there's no room between the turnouts; the turnouts as they'd go in told on the view with their type and speed, the crossover, the run alongside with its spacing, and in the status of the lay tool; expect station of the self-test, cases st_1..st_4, unit tests; Polish translation
2026-11-25 21:06:47 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
2026-11-07 12:37:12 +01:00
feat(editor): a road crossing the rails laid into the plane of the heads of each track, across it with its cant and along it with its grade, the tracks at other heights joined smoothly, the approaches no steeper than 3% within 20 m of the outer rails (Dz.U. 2025 poz. 1105, annex 5), led to the end of a road ending within them, the crossing refused where the road around leaves no room for that; a track canted more than a crossing allows at the angle the road crosses it (the road over the crossing at most 8%, § 31 ust. 4) refused, the fix lowering the cant of its curve with its ramps and limiting its speed; the grade line of the road breaking at the heads of two tracks side by side by more than 5% refused (annex 5 pt 3), its fixes levelling the tracks into one plane by the grade line of one of them, flat over the crossing with ramps within the track norms, or lowering both cants alike as far as keeps the break to 5%, a break up to 5% told in orange; none on a cant ramp (§ 31 ust. 3), the window telling how far the ramp goes; none within a turnout (§ 34); at a skew the decks ending where the axis passes the field side of the heads and the road cut into pieces of half a metre at most there, its lanes on the surface of the crossing within a centimetre; the crossing panels laid per track, bent along it: the panels CBP made of their drawings, rows long enough for the road with their joints over the sleepers, on curves of R 600 m and more (Id-111), or the panels of the game; the road in the plane of the heads under the whole footprint of each panel, meeting its edges at its top, between two tracks straight from the head of the one to the head of the other with the outer panels there bent onto that line, no hump between them (§ 30 ust. 3), the editor asking the ground over the grooves getting the surface; laid again once a change of the road, of a track it crosses or of a panel is done, a step of the history taken back with the change; told in the status line, the windows of a refused crossing with their text wrapped; self-test commands (crossingholes checking a crossing as built, crossingreport, crossingfix level/lower) and cases rx_*, rx_7 the crossings of a single track, at 60 degrees, on a curve and of four double tracks canted and crossed at a skew checked watertight; Polish translation
2026-11-25 09:21:21 +01:00
2026-11-07 12:37:12 +01:00
feat(roads): a road cut along the rails it crosses with its surface in the plane of their heads, as the road makes its geometry, in the editor and in the simulation alike: the head left uncovered, the flangeway beside it a groove whose wall slopes down from the edge of the surface to a closed bottom 40 mm under the head, 67, 75 or 80 mm wide 14 mm under the top by the radius (PKP PLK, STWiORB D.11.01.01; Dz.U. 2025 poz. 1105, § 36 gives no figures); the grooves only seen, the surface over the gaps kept for what asks for the height of the road, the vehicles going along the lanes made of the axis; a piece laid over a crossing taking the surface of the crossing for its geometry and for the ends of its lanes, at a skew too: over each track the plane of its heads, between two tracks facing each other straight from the field side of the heads of the one to the other, taken from the arcs of the heads so it goes on from each without a step, beyond them the plane of the outer track; the piece split where that surface bends and its ends split at the same points as the piece next to it splits them, set onto the surface before it's cut along the rails and under the panels, so two pieces meet without a step over the surface twisted between two tracks on curves; the surface left out under the whole footprint of a crossing panel lying on the road, a model of the panels or a sweep of them, on any piece it reaches over, and the grooves under it, so the road and its lanes stay at the top of the panel and nothing of the road shows through it; the crossing closed: the groove along the whole length of the panels lying along the rail and closed at its ends, the strips beside the rails past the road the panels leave open filled at the heads, the surface cut exactly under the tops of the panels, skirts hanging from its edges along them over any part of their length; the check of a crossing as built, rays straight down, beside the edges of the road, at slants and down the flangeways, for anything showing through it, the road over a panel or a flangeway not clear, a ray slipping through a crack under 0.02 mm (the corners welded to 0.01 mm beside a corner a cut makes on an edge) counted apart; the tracks looked for in the cells of the region around the piece, as the frogs look for theirs, along its axis, both edges and both ends; on a curve in pieces following the rails; a road lying within 2 mm of the plane only, so the roads of the sceneries before stay as they were
2026-11-25 09:20:40 +01:00

Unit tests

C++ tests of the simulation logic: set up the conditions, run the code, check the outcome. They run as part of every build of the game, in CI and locally alike, and from the IDE. They need no game data and no window: the test vehicles are JSON definitions in data/, loaded from memory by the game's own vehicle loader.

The code under test is eu07_core, the library of the simulation parts the game links as well (physics, vehicle definition loader, ...): built once, with the game's settings, and tested as the game uses it. The python library gets linked along with the game's settings, but the tests don't use it and run without it.

COVERAGE.md says how much of the simulation core the tests reach, what's left and why, and what they found.

Running

  • With the build: building the game (WITH_TESTS, on by default) runs them at the end, once the game is linked, and prints Unit tests: 11/11 passed (80 checks). A failing test prints the full report and fails the build; the game binary gets removed, so the next build links it again and reruns them. They also run when only the tests, a definition in data/ or run_tests.cmake changed (Ninja and Makefile generators). -DWITH_TESTS=OFF builds the game without them.
  • From the IDE: run or debug the eu07_unit_tests target. CLion recognises doctest and lists the test cases, so a single one can be run or debugged from the gutter.
  • By hand: ctest --test-dir <build dir> --build-config Release, or the binary itself, which takes doctest options, e.g. eu07_unit_tests -tc="brakes*". EU07_UNIT_TESTS_LOG=1 shows the engine log, quiet otherwise.

Writing a test

Add a test_<topic>.cpp here, it's picked up on its own.

#include "stdafx.h"
#include "doctest.h"
#include "vehicle.h"

TEST_CASE("brakes: service braking stops a heavier wagon too") {
	// conditions: the wagon from data/wagon.json, heavier, running at 40 km/h
	test::vehicle wagon{test::definition::load("wagon").set("Param", "M", 60000), 40.0 /* km/h */};
	// action: full service application, as a coupled locomotive would make it
	wagon.hold_brake_pipe(3.5); // bar
	wagon.run(40.0 /* s */, test::track{0.15 /* friction */, 0.0 /* gradient */});
	// outcome
	CHECK(std::abs(wagon.speed()) < 0.1);
}
  • definition::load("<name>") reads data/<name>.json, with the sections and keys of the game's vehicle definitions; set() and remove() change it for the test. Table sections give their rows as "rows": [[...], ...], keys starting with _ are comments. A section the loader doesn't know is an error (the table of them is in fiz.cpp); a misspelt key isn't caught, so check in the test that the value the test is about made it to the vehicle. Keep the definitions physically sensible: some parts need keys others don't (the ESt brake valves need Brake.Size), and a test on a half-defined vehicle tests the definition rather than the simulation.
  • definition::fiz("<dir>/<name>") is one of the game's own vehicles, data/fiz/<dir>/<name>.json, in the same form, converted from the game's .fiz with the files it includes (the list and how to add one: data/fiz/README.md). The conversion keeps what the game's loader reads of the file and checks it reads the same of the converted one (fiz.h), so the vehicle is the game's.
  • The random numbers of the simulation are reseeded before every test case, so a test gives the same result alone or among the others. Loading a vehicle draws some too (the scatter of its brake pipe pressure), so a test making vehicles in a loop calls test::reseed_random() before each.
  • Logic which lives in classes too big to set up in a test (the AI driver, the cab) goes into free functions on the vehicle state the class calls, and the test calls them the same way: see vehicle/handover.h, which TController::TakeControl() uses, and test_handover.cpp.
  • vehicle creates the vehicle the way TDynamicObject::Init() does and steps it the way the simulation loop does, on a straight track of the given friction, gradient, catenary voltage and physics iterations a frame. With a cab (vehicle{definition, 0.0, "name", 1}) it has a driver.
  • consist (consist.h) couples vehicles into a train and steps them together, coupler forces, air, control and power passing between them. The physics reaches a neighbour through its TDynamicObject, for its MoverParameters only; the tests have a stand-in of it with just that.
  • drive.h has the driver's part, made of the AI's steps: start_up() (battery, pumps, pantographs, line breaker, converter, compressor), drive() (running, acknowledging the vigilance), release_brakes(), drive_cycle() (a whole run from start-up to a stop).
  • scenario (scenario.h) puts a train, its driver and a golden trace for each vehicle together, for a test of a feature: act() marks an event in the traces, wait() runs the train.
  • pair (vehicle.h) puts two vehicles on that track facing each other with the given ends and runs them together, as the simulation runs the vehicles of a scene: the coupler and buffer forces, collisions and automatic coupling come from the physics (test_couplers.cpp).
  • A test which reads files makes them in a test::scratch_folder (scratch.h): a folder of its own in the build folder of the tests, removed with the object. The files are written byte for byte, so the line ends are the ones the test gives (test_parser.cpp, test_ladder_logic.cpp).
  • Check behaviour (speeds, pressures, states the player sees) rather than internal flags, and make the test check its own preconditions (REQUIRE that the braking skidded, before checking what a skid leads to).

Golden traces

A golden trace pins down how the simulation behaves now, for changes which mustn't alter it, like restructuring a function: the values of some channels of a vehicle over a run, compared with the ones recorded from the code before, in data/golden/<name>.csv. A difference fails the test, naming the channel, the time and both rows.

The samples and their key figures aren't kept in the repository (tens of megabytes, and they only mean something against the code before the change): record them before the change, and compare after it.

git switch master
EU07_UNIT_TESTS_UPDATE_GOLDEN=1 eu07_unit_tests     # records data/golden/**/*.csv, ignored by git
git switch my-refactor
EU07_UNIT_TESTS_GOLDEN_EXACT=1 eu07_unit_tests      # compares every sample, bit for bit

The recorded files stay through switching branches. Without them, a trace checks the invariants and its test's own CHECKs, which is what the CI does: every test states the behaviour it's about in CHECKs, a scenario of a feature what the feature does (the doors open, the train stops), a run of every real vehicle what holds for each of them (it brakes harder by every step, it runs off and stops).

test::trace trace{"eu07/notch up", test::channels::motion() + test::channels::traction()};
eu07.record(trace); // samples every 10th step
eu07->MainSwitch(true);
trace.mark("main switch"); // shows in the event column of the row
eu07.run(30.0);
trace.check();
  • EU07_UNIT_TESTS_UPDATE_GOLDEN=1 records the samples and the key figures, for a new test or a change which alters the behaviour on purpose; the diff of the key figures shows what it altered, review it like code.
  • Without EU07_UNIT_TESTS_GOLDEN_EXACT=1, samples recorded are compared within a relative 1e-9, as GCC and glibc's maths functions can differ from MSVC's in the last bits, and a trace without them passes. With it they're compared exactly, and a trace without them or its key figures fails, so a run of the refactoring check can't pass by having nothing to compare.
  • Keep the runs short (tens of seconds): the simulation feeds back on itself, and a last bit grows.
  • A trace with no assertion about what the behaviour should be is a characterisation, bugs included; put the behaviour the test is about into CHECKs besides.

Key figures

Next to its samples, every trace records the figures a person reads (figures.h), for each part of the run between two events, in data/golden/<name>.figures: the duration, the speeds, the distance, the time to stop, the brake pipe's lowest pressure and the time to get back to 4.9 bar, the peaks of the brake cylinders, the motor current and the tractive force.

[brake]
duration = 60.00 s
speed at the start = 118.5 km/h
distance = 944.3 m
stopped after = 45.97 s
brake cylinders peak = 3.997 bar

They're compared within 0.5% or the last decimal written, loose enough for any compiler, and the differences are told by part and figure:

key figures of "traction/4e_v1/4e" differ:
  [brake] distance: 944.3 m now, 980.0 m recorded

EU07_UNIT_TESTS_UPDATE_GOLDEN=1 records them with the samples. After a change which alters the behaviour, their comparison says in a few lines what the thousands of changed samples mean. A trace sampled by hand, at times of its own (the commands and switches tests), records none.

Invariants

Every trace also checks, on every step of its vehicle and not only on the recorded ones, the rules which hold for any vehicle whatever a test does (invariants.h): no NaN, the brake cylinders under the vehicle's highest pressure, the pipes under the highest governor setting, no motor current with the line breaker open, no tractive force without power, the distance counted as the speed runs, no acceleration over 5 g, and a few more. They compare nothing with a recorded file, so they hold on any compiler, and they still check a behaviour recorded anew on purpose makes sense. A broken rule fails the test, naming the time, the last event and the values:

invariant "no motor current with the line breaker open" broken in traction/et42 fuse/0 at 63.200 s,
after "dynamic brake": Mains=0 Im=-412.5 Itot=-412.5

Where a test makes a vehicle break a rule (a collision), or a game vehicle breaks one as it is, the list of excuses in invariants.cpp names the trace, the rule and the reason; the second kind goes into the findings of COVERAGE.md too.

A source joins the tested code by moving from SOURCES to CORE_SOURCES in the main CMakeLists.txt; the game links it from the library from then on. The library reaches for a little of the rest of the engine, which engine_stubs.cpp stands in for in the tests (logs, settings, scenery layers). If a change makes it use something new, either move the real source into the library too, when it stands on its own, or add a stub to engine_stubs.cpp.