17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-01 10:22:09 +01:00

test: unit tests of the simulation core, run as part of the build

eu07_unit_tests: C++ tests (doctest) of the simulation, which set up the
conditions, run the code and check the outcome. No game data, no window.

- the code under test is the core library (eu07_core), the very objects
  the game links, built with the game's settings; engine_stubs.cpp stands
  in for the logs, settings and scenery layers the library reaches for.
  python gets linked with the settings but isn't needed to run the tests
- test vehicles are JSON definitions in tests/unit/data, with the sections
  and keys of the game's vehicle definitions (tables as "rows", _keys are
  comments), changed by the tests where needed and loaded from memory by
  the game's own loader; an unknown section is an error rather than
  silently skipped
- test::vehicle creates the mover the way TDynamicObject::Init() does and
  steps it the way the simulation loop does, on a straight track of given
  friction and gradient, optionally holding the brake pipe pressure the
  way a coupled locomotive would
- the random numbers of the simulation are reseeded before every test
  case, so a test gives the same result alone (IDE) or among the others
- building the game runs the tests at the end, once the game is linked,
  and prints a one line summary ("Unit tests: 11/11 passed (80 checks)").
  A failure prints the full report and fails the build, in CI and locally
  alike, and removes the game binary so the next build reruns them. A
  change of the tests alone relinks the game so they run (Ninja and
  Makefile generators). -DWITH_TESTS=OFF leaves them out
- first tests: definitions reach the vehicle, KE service application and
  release, deeper reduction brakes harder, every implemented distributor
  valve charges/applies/releases, service braking stops a moving wagon

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jerrrrycho
2026-10-14 16:36:40 +02:00
parent 6c7aea19c2
commit 3365d42c3a
11 changed files with 648 additions and 0 deletions

View File

@@ -77,6 +77,7 @@ option(WITH_ZMQ "Compile with cppzmq" OFF)
option(WITH_CRASHPAD "Compile with crashpad" OFF)
option(WITH_DISCORD_RPC "Compile with DISCORD RICH PRESENCE" ON)
option(USE_LTO "Use link-time optimization" OFF)
option(WITH_TESTS "Build and run the unit tests with the game, a failing test fails the build" ON)
option(WITH_BETTER_RENDERER "Experimental multi-backend renderer based on NVRHI" OFF)
option(GENERATE_PDB "Generate executable with program debugging symbols" ON)
option(ENABLE_MCC "Enable multicore compilation" ON)
@@ -715,3 +716,22 @@ if(ENABLE_AVX2)
target_compile_options(eu07_settings INTERFACE -mavx2)
endif()
endif()
if(WITH_TESTS)
enable_testing()
add_subdirectory(tests/unit)
# the unit tests run at the end of building the game, once it's linked: a failing one fails the build
# (and removes the game binary, so the next build links it again and reruns them)
add_dependencies(${PROJECT_NAME} eu07_unit_tests)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-DTESTS=$<TARGET_FILE:eu07_unit_tests>
-DGAME=$<TARGET_FILE:${PROJECT_NAME}>
-P "${CMAKE_CURRENT_SOURCE_DIR}/tests/unit/run_tests.cmake"
COMMENT "Running the unit tests"
VERBATIM)
# a change of the tests alone links the game again, so they run (Ninja and Makefile generators;
# Visual Studio builds rerun them with the next change of the game)
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY LINK_DEPENDS
$<TARGET_FILE:eu07_unit_tests> ${UNIT_TEST_DATA} "${CMAKE_CURRENT_SOURCE_DIR}/tests/unit/run_tests.cmake")
endif()