mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 16:14:58 +01:00
Merge remote-tracking branch 'origin/master' into fix/freight-brake-setting-cw1
# Conflicts: # utilities/Globals.cpp
This commit is contained in:
@@ -25,15 +25,31 @@ add_executable(${UNIT_TESTS_NAME}
|
||||
|
||||
# the core library brings the game's build settings along
|
||||
target_link_libraries(${UNIT_TESTS_NAME} PRIVATE eu07_core)
|
||||
# the script screen host, tested with the real runtimes (test_script_screen_host.cpp)
|
||||
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/scriptscreens.cpp")
|
||||
if (WITH_LUA)
|
||||
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/luaprogram.cpp" "${CMAKE_SOURCE_DIR}/stb/stb_image.c")
|
||||
endif()
|
||||
if (WITH_WASM)
|
||||
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/wasmprogram.cpp")
|
||||
target_link_libraries(${UNIT_TESTS_NAME} PRIVATE eu07_wamr)
|
||||
endif()
|
||||
target_include_directories(${UNIT_TESTS_NAME} PRIVATE "${DEPS_DIR}/doctest")
|
||||
# the names the tests CAPTURE are often C strings, shown as text rather than pointers
|
||||
target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data" DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING)
|
||||
# the folders of the tests that write files (the scripts of test_script_screen_host.cpp) are made in the build folder,
|
||||
# and removed by the test that made them
|
||||
target_compile_definitions(${UNIT_TESTS_NAME} PRIVATE EU07_UNIT_TESTS_SCRATCH="${CMAKE_CURRENT_BINARY_DIR}")
|
||||
if (MSVC)
|
||||
target_compile_options(${UNIT_TESTS_NAME} PRIVATE /bigobj)
|
||||
endif()
|
||||
set_target_properties(${UNIT_TESTS_NAME} PROPERTIES FOLDER "tests")
|
||||
|
||||
add_test(NAME ${UNIT_TESTS_NAME} COMMAND ${UNIT_TESTS_NAME})
|
||||
if (WIN32)
|
||||
# the libraries of the game (lua51.dll) are next to the game, see run_tests.cmake
|
||||
set_tests_properties(${UNIT_TESTS_NAME} PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<TARGET_FILE_DIR:eu07>")
|
||||
endif()
|
||||
|
||||
# for running the tests at the end of building the game, see the main CMakeLists.txt
|
||||
set(UNIT_TEST_DATA ${UNIT_TEST_DATA} PARENT_SCOPE)
|
||||
|
||||
@@ -2,6 +2,14 @@
|
||||
# prints a one line summary when they pass, the full report when they don't. a failure fails the build and
|
||||
# removes the game binary, so the next build links it again and reruns the tests
|
||||
|
||||
# the tests load the libraries the game loads (lua51.dll for the Lua screens): on Windows they're looked for next to
|
||||
# the game binary as well, where they are for running the game, since nothing puts them next to the tests
|
||||
if(CMAKE_HOST_WIN32)
|
||||
get_filename_component(game_folder "${GAME}" DIRECTORY)
|
||||
file(TO_NATIVE_PATH "${game_folder}" game_folder)
|
||||
set(ENV{PATH} "${game_folder};$ENV{PATH}")
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND "${TESTS}" --no-intro --no-version
|
||||
RESULT_VARIABLE result
|
||||
|
||||
488
tests/unit/test_blackbox.cpp
Normal file
488
tests/unit/test_blackbox.cpp
Normal file
@@ -0,0 +1,488 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "vehicle/blackbox.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
|
||||
// the black box is read by other programs (the starter, a viewer), from the layout in blackbox_doc.md alone.
|
||||
// these read what the writer wrote the way such a reader would, so a change of the layout shows up here
|
||||
|
||||
namespace {
|
||||
|
||||
// reads the numbers the way a .NET BinaryReader does: little-endian, texts after their 7 bit length
|
||||
class reader {
|
||||
public:
|
||||
explicit reader(std::string_view Data) : m_data{Data} {}
|
||||
|
||||
template <typename Type_>
|
||||
Type_ get() {
|
||||
REQUIRE(m_position + sizeof(Type_) <= m_data.size());
|
||||
Type_ value;
|
||||
std::memcpy(&value, m_data.data() + m_position, sizeof(Type_));
|
||||
m_position += sizeof(Type_);
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string text() {
|
||||
std::uint32_t length{0};
|
||||
int shift{0};
|
||||
std::uint8_t part;
|
||||
do {
|
||||
part = get<std::uint8_t>();
|
||||
length |= static_cast<std::uint32_t>(part & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while ((part & 0x80) != 0);
|
||||
REQUIRE(m_position + length <= m_data.size());
|
||||
std::string value{m_data.substr(m_position, length)};
|
||||
m_position += length;
|
||||
return value;
|
||||
}
|
||||
|
||||
std::size_t position() const {
|
||||
return m_position; }
|
||||
bool done() const {
|
||||
return m_position == m_data.size(); }
|
||||
|
||||
private:
|
||||
std::string_view m_data;
|
||||
std::size_t m_position{0};
|
||||
};
|
||||
|
||||
struct record_view {
|
||||
blackbox::record type;
|
||||
std::string payload;
|
||||
};
|
||||
|
||||
// splits the file into its records, checking the header and that the lengths add up to the whole file
|
||||
std::vector<record_view> records(std::string const &File) {
|
||||
reader file{File};
|
||||
REQUIRE(File.size() >= blackbox::header_size);
|
||||
CHECK(std::string_view{File.data(), 8} == std::string_view{blackbox::magic.data(), 8});
|
||||
file.get<std::array<char, 8>>();
|
||||
CHECK(file.get<std::uint16_t>() == blackbox::format_version);
|
||||
CHECK(file.get<std::uint16_t>() == 0);
|
||||
std::vector<record_view> result;
|
||||
while (false == file.done()) {
|
||||
auto const type{file.get<std::uint8_t>()};
|
||||
auto const length{file.get<std::uint32_t>()};
|
||||
REQUIRE(file.position() + length <= File.size());
|
||||
result.push_back({static_cast<blackbox::record>(type), File.substr(file.position(), length)});
|
||||
for (std::uint32_t i = 0; i < length; ++i)
|
||||
file.get<std::uint8_t>();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
blackbox::cab_sample sample() {
|
||||
blackbox::cab_sample s;
|
||||
s.time = 12.5;
|
||||
s.vehicle = 3;
|
||||
s.position = {-1234.5, 87.25, 40321.125};
|
||||
s.speed_kmh = -42.5f;
|
||||
s.acceleration = 0.25f;
|
||||
s.odometer_km = 17.75f;
|
||||
s.driving = blackbox::driver::human;
|
||||
s.flags = blackbox::cab_flags::in_cab | blackbox::cab_flags::ai_valid;
|
||||
s.main_controller = 7;
|
||||
s.scnd_controller = 2;
|
||||
s.reverser = -1;
|
||||
s.train_brake = 1.5f;
|
||||
s.local_brake = 0.5f;
|
||||
s.brake_cylinder = 3.75f;
|
||||
s.brake_pipe = 4.5f;
|
||||
s.motor_current = 640.0f;
|
||||
s.ai = {60.0f, 40.0f, 812.0f, -0.35f, 455.0f, 100.0f, 80.0f};
|
||||
s.devices = blackbox::cab_devices::battery | blackbox::cab_devices::pantograph_rear | blackbox::cab_devices::shunt_mode;
|
||||
s.main_reservoir = 8.25f;
|
||||
s.voltage = 3000.0f;
|
||||
s.engine_rpm = 1250.0f;
|
||||
s.lights_head = 0x15;
|
||||
s.lights_tail = 0x22;
|
||||
s.cab = -1;
|
||||
s.security = blackbox::security_flags::vigilance | blackbox::security_flags::braking;
|
||||
s.lateral_acceleration = -0.75f;
|
||||
s.movement = blackbox::movements::shunting;
|
||||
s.light_hint_head = -1;
|
||||
s.light_hint_tail = 0x22;
|
||||
s.equipped_head = 0x1B5;
|
||||
s.equipped_tail = 0x22;
|
||||
s.engine = blackbox::engines::diesel_electric;
|
||||
s.brake_delay = 2;
|
||||
s.radio_channel = 3;
|
||||
s.driver_valve = 4.75f;
|
||||
s.feed_pipe = 8.5f;
|
||||
s.oil_pressure = 0.5f;
|
||||
s.water_temperature = 78.5f;
|
||||
s.oil_temperature = 70.25f;
|
||||
s.engine_temperature = 81.0f;
|
||||
s.speed_control_kmh = 90.0f;
|
||||
s.engine_power = 1250.5f;
|
||||
s.hand_brake = 5;
|
||||
s.brake_mode = 4;
|
||||
s.horn = 2;
|
||||
s.integrated_controller = -0.5f;
|
||||
s.clutch = 0.75f;
|
||||
s.converter_fill = 0.25f;
|
||||
s.consist_brake_cylinder = 3.5f;
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("blackbox: the file starts with the magic and the version, then come records which say how long they are") {
|
||||
std::ostringstream file;
|
||||
blackbox::writer out{file};
|
||||
out.header();
|
||||
out.info({"$td.scn", "2026-11-08T15:32:58", 2026, 312, 8 * 3600.0, "ET22-1", "26.1", 0.1f, 1.0f, 60.0f});
|
||||
out.track({0, "tor1", blackbox::track_kinds::normal, 0, 1, {{0.f, 0.f, 0.f}, {10.f, 0.5f, 100.f}}});
|
||||
out.signal(0, "sem_a", {10.0, 0.0, 100.0});
|
||||
out.vehicle({1, "et22-1", "ET22", "12345", 19.24f, 3.0f, 1});
|
||||
out.cab(sample());
|
||||
out.vehicles(13.0, true, {{1, {1.f, 2.f, 3.f}, 0.5f, 60.f, blackbox::driver::ai}});
|
||||
out.switch_state(13.0, 0, 1);
|
||||
out.signal_aspect(13.0, 0, 0.0f, 40.0f, "SetVelocity");
|
||||
out.command({13.5, 1, true, 1.0, 0.0, "mastercontrollerincrease"});
|
||||
out.driver_change(14.0, 1, blackbox::driver::ai);
|
||||
out.signal_passed({14.5, 1, 0, "sem_a_sem", {10.0, 0.0, 100.0}, 0.0f, 40.0f, 35.0f, 2.5f, blackbox::passing_flags::at_stop});
|
||||
out.user_vehicle(15.0, 1, false, -1);
|
||||
out.clock(15.0, 2026, 312, 8 * 3600.0 + 15.0);
|
||||
out.vehicle_removed(16.0, 1);
|
||||
out.end(16.0);
|
||||
|
||||
auto const file_bytes{file.str()};
|
||||
CHECK(file_bytes.size() == out.size());
|
||||
auto const all{records(file_bytes)};
|
||||
|
||||
using blackbox::record;
|
||||
std::vector<record> const expected{record::info, record::track, record::signal, record::vehicle, record::cab, record::vehicles, record::switch_state, record::signal_aspect,
|
||||
record::command, record::driver_change, record::signal_passed, record::user_vehicle, record::clock, record::vehicle_removed, record::end};
|
||||
REQUIRE(all.size() == expected.size());
|
||||
for (std::size_t i = 0; i < all.size(); ++i)
|
||||
CHECK(all[i].type == expected[i]);
|
||||
|
||||
SUBCASE("the info") {
|
||||
reader info{all[0].payload};
|
||||
CHECK(info.text() == "$td.scn");
|
||||
CHECK(info.text() == "2026-11-08T15:32:58");
|
||||
CHECK(info.get<std::uint16_t>() == 2026);
|
||||
CHECK(info.get<std::uint16_t>() == 312);
|
||||
CHECK(info.get<double>() == 8 * 3600.0);
|
||||
CHECK(info.text() == "ET22-1");
|
||||
CHECK(info.text() == "26.1");
|
||||
CHECK(info.get<float>() == 0.1f);
|
||||
CHECK(info.get<float>() == 1.0f);
|
||||
CHECK(info.get<float>() == 60.0f);
|
||||
CHECK(info.done());
|
||||
}
|
||||
SUBCASE("a track: its points after their count") {
|
||||
reader track{all[1].payload};
|
||||
CHECK(track.get<std::uint32_t>() == 0);
|
||||
CHECK(track.text() == "tor1");
|
||||
CHECK(track.get<std::uint8_t>() == blackbox::track_kinds::normal);
|
||||
CHECK(track.get<std::uint8_t>() == 0);
|
||||
CHECK(track.get<std::uint8_t>() == 1);
|
||||
CHECK(track.get<std::uint32_t>() == 2);
|
||||
for (int i = 0; i < 3; ++i)
|
||||
CHECK(track.get<float>() == 0.f);
|
||||
CHECK(track.get<float>() == 10.f);
|
||||
CHECK(track.get<float>() == 0.5f);
|
||||
CHECK(track.get<float>() == 100.f);
|
||||
CHECK(track.done());
|
||||
}
|
||||
SUBCASE("a passed signal") {
|
||||
reader passed{all[10].payload};
|
||||
CHECK(passed.get<double>() == 14.5);
|
||||
CHECK(passed.get<std::uint32_t>() == 1);
|
||||
CHECK(passed.get<std::uint32_t>() == 0);
|
||||
CHECK(passed.text() == "sem_a_sem");
|
||||
CHECK(passed.get<double>() == 10.0);
|
||||
CHECK(passed.get<double>() == 0.0);
|
||||
CHECK(passed.get<double>() == 100.0);
|
||||
CHECK(passed.get<float>() == 0.0f);
|
||||
CHECK(passed.get<float>() == 40.0f);
|
||||
CHECK(passed.get<float>() == 35.0f);
|
||||
CHECK(passed.get<float>() == 2.5f);
|
||||
CHECK(passed.get<std::uint8_t>() == blackbox::passing_flags::at_stop);
|
||||
CHECK(passed.done());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("blackbox: a sample of the user's train has the documented size and order") {
|
||||
std::ostringstream file;
|
||||
blackbox::writer out{file};
|
||||
out.header();
|
||||
out.cab(sample());
|
||||
auto const all{records(file.str())};
|
||||
REQUIRE(all.size() == 1);
|
||||
REQUIRE(all[0].payload.size() == blackbox::cab_sample_size);
|
||||
|
||||
reader cab{all[0].payload};
|
||||
CHECK(cab.get<double>() == 12.5);
|
||||
CHECK(cab.get<std::uint32_t>() == 3);
|
||||
CHECK(cab.get<double>() == -1234.5);
|
||||
CHECK(cab.get<double>() == 87.25);
|
||||
CHECK(cab.get<double>() == 40321.125);
|
||||
CHECK(cab.get<float>() == -42.5f);
|
||||
CHECK(cab.get<float>() == 0.25f);
|
||||
CHECK(cab.get<float>() == 17.75f);
|
||||
CHECK(cab.get<std::uint8_t>() == static_cast<std::uint8_t>(blackbox::driver::human));
|
||||
CHECK(cab.get<std::uint8_t>() == (blackbox::cab_flags::in_cab | blackbox::cab_flags::ai_valid));
|
||||
CHECK(cab.get<std::int8_t>() == 7);
|
||||
CHECK(cab.get<std::int8_t>() == 2);
|
||||
CHECK(cab.get<std::int8_t>() == -1);
|
||||
CHECK(cab.get<float>() == 1.5f);
|
||||
CHECK(cab.get<float>() == 0.5f);
|
||||
CHECK(cab.get<float>() == 3.75f);
|
||||
CHECK(cab.get<float>() == 4.5f);
|
||||
CHECK(cab.get<float>() == 640.0f);
|
||||
// what the AI would do
|
||||
CHECK(cab.get<float>() == 60.0f);
|
||||
CHECK(cab.get<float>() == 40.0f);
|
||||
CHECK(cab.get<float>() == 812.0f);
|
||||
CHECK(cab.get<float>() == -0.35f);
|
||||
CHECK(cab.get<float>() == 455.0f);
|
||||
CHECK(cab.get<float>() == 100.0f);
|
||||
CHECK(cab.get<float>() == 80.0f);
|
||||
// what is switched on
|
||||
CHECK(cab.get<std::uint32_t>() == 0x8009);
|
||||
CHECK(cab.get<float>() == 8.25f);
|
||||
CHECK(cab.get<float>() == 3000.0f);
|
||||
CHECK(cab.get<float>() == 1250.0f);
|
||||
CHECK(cab.get<std::uint16_t>() == 0x15);
|
||||
CHECK(cab.get<std::uint16_t>() == 0x22);
|
||||
// the cab, the train protection and the lateral acceleration
|
||||
CHECK(cab.get<std::int8_t>() == -1);
|
||||
CHECK(cab.get<std::uint8_t>() == 0x11);
|
||||
CHECK(cab.get<float>() == -0.75f);
|
||||
// what the driver goes by, and the lights asked for at the head and the tail
|
||||
CHECK(cab.get<std::uint8_t>() == blackbox::movements::shunting);
|
||||
CHECK(cab.get<std::int16_t>() == -1);
|
||||
CHECK(cab.get<std::int16_t>() == 0x22);
|
||||
// the lamps there are at the head and the tail
|
||||
CHECK(cab.get<std::uint16_t>() == 0x1B5);
|
||||
CHECK(cab.get<std::uint16_t>() == 0x22);
|
||||
// the engine, the brakes' setting, the radio, the pressures, the oil and the temperatures, the cruise control, the power
|
||||
CHECK(cab.get<std::uint8_t>() == blackbox::engines::diesel_electric);
|
||||
CHECK(cab.get<std::uint8_t>() == 2);
|
||||
CHECK(cab.get<std::uint8_t>() == 3);
|
||||
CHECK(cab.get<float>() == 4.75f);
|
||||
CHECK(cab.get<float>() == 8.5f);
|
||||
CHECK(cab.get<float>() == 0.5f);
|
||||
CHECK(cab.get<float>() == 78.5f);
|
||||
CHECK(cab.get<float>() == 70.25f);
|
||||
CHECK(cab.get<float>() == 81.0f);
|
||||
CHECK(cab.get<float>() == 90.0f);
|
||||
CHECK(cab.get<float>() == 1250.5f);
|
||||
// the hand brake, the mode of the brakes, the horn, the integrated controller, the clutch, the torque converter
|
||||
CHECK(cab.get<std::int8_t>() == 5);
|
||||
CHECK(cab.get<std::uint8_t>() == 4);
|
||||
CHECK(cab.get<std::uint8_t>() == 2);
|
||||
CHECK(cab.get<float>() == -0.5f);
|
||||
CHECK(cab.get<float>() == 0.75f);
|
||||
CHECK(cab.get<float>() == 0.25f);
|
||||
// the highest brake cylinder pressure of the train
|
||||
CHECK(cab.get<float>() == 3.5f);
|
||||
CHECK(cab.done());
|
||||
}
|
||||
|
||||
TEST_CASE("blackbox: the stations, the stop points and the horn points are written field by field") {
|
||||
std::ostringstream file;
|
||||
blackbox::writer out{file};
|
||||
out.header();
|
||||
out.stop_point(2, "Gdynia Gl.", {100.0, 1.5, -200.0}, -15.0f, 250.0f, 1.5f);
|
||||
out.horn_point(0, "w6_most", {10.0, 0.0, 20.0}, 2.0f, 1.0f);
|
||||
out.timetable({30.0, 4, "61110", "MOJ", "Gdynia", "Hel", 120.0f, {{"Gdynia", 0.0f, -1.0f, -1.0f, 10 * 60 + 5.0f, false}, {"Reda", 21.5f, 100.0f, 10 * 60 + 22.0f, 10 * 60 + 23.0f, true}}});
|
||||
out.station({40.5, 4, blackbox::station_kinds::stopped, 2, "Reda", {100.0, 1.5, -200.0}, 3.25f, 15.0f, 66.5f, 250.0f, 10 * 60 + 22.0f, 10 * 60 + 23.0f});
|
||||
out.horn_request(41.0, 4, 2.0f, 1.0f, {10.0, 0.0, 20.0});
|
||||
out.user_vehicle(42.0, 5, true, 1);
|
||||
auto const all{records(file.str())};
|
||||
using blackbox::record;
|
||||
REQUIRE(all.size() == 6);
|
||||
CHECK(all[0].type == record::stop_point);
|
||||
CHECK(all[1].type == record::horn_point);
|
||||
CHECK(all[2].type == record::timetable);
|
||||
CHECK(all[3].type == record::station);
|
||||
CHECK(all[4].type == record::horn_request);
|
||||
CHECK(all[5].type == record::user_vehicle);
|
||||
|
||||
SUBCASE("a stop point") {
|
||||
reader point{all[0].payload};
|
||||
CHECK(point.get<std::uint32_t>() == 2);
|
||||
CHECK(point.text() == "Gdynia Gl.");
|
||||
CHECK(point.get<double>() == 100.0);
|
||||
CHECK(point.get<double>() == 1.5);
|
||||
CHECK(point.get<double>() == -200.0);
|
||||
CHECK(point.get<float>() == -15.0f);
|
||||
CHECK(point.get<float>() == 250.0f);
|
||||
CHECK(point.get<float>() == 1.5f);
|
||||
CHECK(point.done());
|
||||
}
|
||||
SUBCASE("a horn point") {
|
||||
reader point{all[1].payload};
|
||||
CHECK(point.get<std::uint32_t>() == 0);
|
||||
CHECK(point.text() == "w6_most");
|
||||
CHECK(point.get<double>() == 10.0);
|
||||
CHECK(point.get<double>() == 0.0);
|
||||
CHECK(point.get<double>() == 20.0);
|
||||
CHECK(point.get<float>() == 2.0f);
|
||||
CHECK(point.get<float>() == 1.0f);
|
||||
CHECK(point.done());
|
||||
}
|
||||
SUBCASE("a timetable: its rows after their count") {
|
||||
reader timetable{all[2].payload};
|
||||
CHECK(timetable.get<double>() == 30.0);
|
||||
CHECK(timetable.get<std::uint32_t>() == 4);
|
||||
CHECK(timetable.text() == "61110");
|
||||
CHECK(timetable.text() == "MOJ");
|
||||
CHECK(timetable.text() == "Gdynia");
|
||||
CHECK(timetable.text() == "Hel");
|
||||
CHECK(timetable.get<float>() == 120.0f);
|
||||
CHECK(timetable.get<std::uint32_t>() == 2);
|
||||
CHECK(timetable.text() == "Gdynia");
|
||||
CHECK(timetable.get<float>() == 0.0f);
|
||||
CHECK(timetable.get<float>() == -1.0f);
|
||||
CHECK(timetable.get<float>() == -1.0f);
|
||||
CHECK(timetable.get<float>() == 605.0f);
|
||||
CHECK(timetable.get<std::uint8_t>() == 0);
|
||||
CHECK(timetable.text() == "Reda");
|
||||
CHECK(timetable.get<float>() == 21.5f);
|
||||
CHECK(timetable.get<float>() == 100.0f);
|
||||
CHECK(timetable.get<float>() == 622.0f);
|
||||
CHECK(timetable.get<float>() == 623.0f);
|
||||
CHECK(timetable.get<std::uint8_t>() == 1);
|
||||
CHECK(timetable.done());
|
||||
}
|
||||
SUBCASE("a train at a station") {
|
||||
reader station{all[3].payload};
|
||||
CHECK(station.get<double>() == 40.5);
|
||||
CHECK(station.get<std::uint32_t>() == 4);
|
||||
CHECK(station.get<std::uint8_t>() == blackbox::station_kinds::stopped);
|
||||
CHECK(station.get<std::uint16_t>() == 2);
|
||||
CHECK(station.text() == "Reda");
|
||||
CHECK(station.get<double>() == 100.0);
|
||||
CHECK(station.get<double>() == 1.5);
|
||||
CHECK(station.get<double>() == -200.0);
|
||||
CHECK(station.get<float>() == 3.25f);
|
||||
CHECK(station.get<float>() == 15.0f);
|
||||
CHECK(station.get<float>() == 66.5f);
|
||||
CHECK(station.get<float>() == 250.0f);
|
||||
CHECK(station.get<float>() == 622.0f);
|
||||
CHECK(station.get<float>() == 623.0f);
|
||||
CHECK(station.done());
|
||||
}
|
||||
SUBCASE("a horn asked for") {
|
||||
reader horn{all[4].payload};
|
||||
CHECK(horn.get<double>() == 41.0);
|
||||
CHECK(horn.get<std::uint32_t>() == 4);
|
||||
CHECK(horn.get<float>() == 2.0f);
|
||||
CHECK(horn.get<float>() == 1.0f);
|
||||
CHECK(horn.get<double>() == 10.0);
|
||||
CHECK(horn.get<double>() == 0.0);
|
||||
CHECK(horn.get<double>() == 20.0);
|
||||
CHECK(horn.done());
|
||||
}
|
||||
SUBCASE("the user's vehicle, with the cab") {
|
||||
reader user{all[5].payload};
|
||||
CHECK(user.get<double>() == 42.0);
|
||||
CHECK(user.get<std::uint32_t>() == 5);
|
||||
CHECK(user.get<std::uint8_t>() == 1);
|
||||
CHECK(user.get<std::int8_t>() == 1);
|
||||
CHECK(user.done());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("blackbox: the camera and the couplings are written field by field") {
|
||||
std::ostringstream file;
|
||||
blackbox::writer out{file};
|
||||
out.header();
|
||||
out.camera({12.5, blackbox::camera_modes::external, 2, 7, {1.0, 2.0, 3.0}, 1.25f, -0.5f});
|
||||
out.coupling({13.0, 3, 1, 4, 0, 0x03, 0x01, 0, {5.0, 0.5, -6.0}});
|
||||
auto const all{records(file.str())};
|
||||
using blackbox::record;
|
||||
REQUIRE(all.size() == 2);
|
||||
CHECK(all[0].type == record::camera);
|
||||
CHECK(all[1].type == record::coupling);
|
||||
|
||||
SUBCASE("the camera") {
|
||||
reader camera{all[0].payload};
|
||||
CHECK(camera.get<double>() == 12.5);
|
||||
CHECK(camera.get<std::uint8_t>() == blackbox::camera_modes::external);
|
||||
CHECK(camera.get<std::uint8_t>() == 2);
|
||||
CHECK(camera.get<std::uint32_t>() == 7);
|
||||
CHECK(camera.get<double>() == 1.0);
|
||||
CHECK(camera.get<double>() == 2.0);
|
||||
CHECK(camera.get<double>() == 3.0);
|
||||
CHECK(camera.get<float>() == 1.25f);
|
||||
CHECK(camera.get<float>() == -0.5f);
|
||||
CHECK(camera.done());
|
||||
}
|
||||
SUBCASE("a coupling") {
|
||||
reader coupling{all[1].payload};
|
||||
CHECK(coupling.get<double>() == 13.0);
|
||||
CHECK(coupling.get<std::uint32_t>() == 3);
|
||||
CHECK(coupling.get<std::uint8_t>() == 1);
|
||||
CHECK(coupling.get<std::uint32_t>() == 4);
|
||||
CHECK(coupling.get<std::uint8_t>() == 0);
|
||||
CHECK(coupling.get<std::uint8_t>() == 0x03);
|
||||
CHECK(coupling.get<std::uint8_t>() == 0x01);
|
||||
CHECK(coupling.get<std::uint32_t>() == 0);
|
||||
CHECK(coupling.get<double>() == 5.0);
|
||||
CHECK(coupling.get<double>() == 0.5);
|
||||
CHECK(coupling.get<double>() == -6.0);
|
||||
CHECK(coupling.done());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("blackbox: a look at the vehicles takes 25 bytes a vehicle") {
|
||||
std::ostringstream file;
|
||||
blackbox::writer out{file};
|
||||
out.header();
|
||||
out.vehicles(3.0, false, {{1, {1.f, 2.f, 3.f}, 0.5f, 60.f, blackbox::driver::ai}, {2, {4.f, 5.f, 6.f}, -0.5f, -20.f, blackbox::driver::none}, {7, {}, 0.f, 0.f, blackbox::driver::human}});
|
||||
auto const all{records(file.str())};
|
||||
REQUIRE(all.size() == 1);
|
||||
CHECK(all[0].payload.size() == 8 + 1 + 4 + 3 * blackbox::vehicle_state_size);
|
||||
|
||||
reader look{all[0].payload};
|
||||
CHECK(look.get<double>() == 3.0);
|
||||
CHECK(look.get<std::uint8_t>() == 0);
|
||||
CHECK(look.get<std::uint32_t>() == 3);
|
||||
CHECK(look.get<std::uint32_t>() == 1);
|
||||
CHECK(look.get<float>() == 1.f);
|
||||
CHECK(look.get<float>() == 2.f);
|
||||
CHECK(look.get<float>() == 3.f);
|
||||
CHECK(look.get<float>() == 0.5f);
|
||||
CHECK(look.get<float>() == 60.f);
|
||||
CHECK(look.get<std::uint8_t>() == static_cast<std::uint8_t>(blackbox::driver::ai));
|
||||
CHECK(look.get<std::uint32_t>() == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("blackbox: texts are UTF-8 after their length in 7 bit groups, low first") {
|
||||
std::ostringstream file;
|
||||
blackbox::writer out{file};
|
||||
out.header();
|
||||
std::string const long_name(200, 'x');
|
||||
std::string const polish{"\xc5\xbc\xc3\xb3\xc5\x82w"}; // żółw, 7 bytes
|
||||
out.signal(0, long_name, {});
|
||||
out.signal(1, polish, {});
|
||||
out.signal(2, "", {});
|
||||
auto const all{records(file.str())};
|
||||
REQUIRE(all.size() == 3);
|
||||
|
||||
// 200 = 0b1'1001000: 0xc8 (low 7 bits and the more flag), then 0x01
|
||||
CHECK(static_cast<std::uint8_t>(all[0].payload[4]) == 0xc8);
|
||||
CHECK(static_cast<std::uint8_t>(all[0].payload[5]) == 0x01);
|
||||
reader first{all[0].payload};
|
||||
first.get<std::uint32_t>();
|
||||
CHECK(first.text() == long_name);
|
||||
|
||||
CHECK(static_cast<std::uint8_t>(all[1].payload[4]) == 7);
|
||||
reader second{all[1].payload};
|
||||
second.get<std::uint32_t>();
|
||||
CHECK(second.text() == polish);
|
||||
|
||||
CHECK(static_cast<std::uint8_t>(all[2].payload[4]) == 0);
|
||||
CHECK(all[2].payload.size() == 4 + 1 + 24);
|
||||
}
|
||||
578
tests/unit/test_coupler_physics.cpp
Normal file
578
tests/unit/test_coupler_physics.cpp
Normal file
@@ -0,0 +1,578 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
#include "vehicle.h"
|
||||
#include "consist.h"
|
||||
#include "McZapkie/couplerphysics.h"
|
||||
#include "utilities/Globals.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
// the physical model of the couplings (McZapkie/couplerphysics.h, couplerphysics in eu07.ini): progressive buffers and
|
||||
// draw gear with friction hysteresis, pressed in series, the play of the coupling, its yield and its breaking
|
||||
|
||||
using test::definition;
|
||||
using test::vehicle;
|
||||
namespace cp = coupler_physics;
|
||||
|
||||
namespace {
|
||||
|
||||
// the given setting of a global flag for the test, restored after it
|
||||
class flag_setting {
|
||||
public:
|
||||
flag_setting(bool &Flag, bool Enabled) : m_flag{Flag}, m_previous{Flag} { m_flag = Enabled; }
|
||||
~flag_setting() { m_flag = m_previous; }
|
||||
flag_setting(flag_setting const &) = delete;
|
||||
flag_setting &operator=(flag_setting const &) = delete;
|
||||
|
||||
private:
|
||||
bool &m_flag;
|
||||
bool m_previous;
|
||||
};
|
||||
|
||||
// one end of the wagon of data/wagon.json as the model takes it, with the play of a loose screw coupling
|
||||
cp::end_parameters wagon_end() {
|
||||
cp::end_parameters wagon;
|
||||
wagon.buffers = {9.2e6, 0.11, 0.5};
|
||||
wagon.draw_gear = {4.5e6, 0.1, 0.3};
|
||||
wagon.slack = 0.05;
|
||||
wagon.buffers_unloading = 0.58;
|
||||
wagon.draw_gear_unloading = 0.58;
|
||||
wagon.link_stiffness = 850e3 / 0.003;
|
||||
wagon.crush_force = 2125e3;
|
||||
wagon.link = {680e3, 850e3, 170e3 / 0.02, true};
|
||||
return wagon;
|
||||
}
|
||||
|
||||
// the force between two such ends at the gap, the state of the pair fresh
|
||||
cp::pair_result fresh_pair(double Gap, bool Coupled = true, cp::end_parameters const &End = wagon_end()) {
|
||||
cp::pair_state state;
|
||||
return cp::update_pair(End, End, state, {Gap, 0.0, Coupled, false, true});
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("coupler physics: a progressive spring is soft at first and reaches its end force at the end of its travel") {
|
||||
cp::spring_curve const buffers{9.2e6, 0.11, 0.5};
|
||||
CHECK(buffers.force(0.0) == 0.0);
|
||||
CHECK(buffers.force(0.11) == doctest::Approx(buffers.end_force()));
|
||||
CHECK(buffers.end_force() == doctest::Approx(1012e3));
|
||||
// held at the end of the travel
|
||||
CHECK(buffers.force(0.2) == doctest::Approx(buffers.end_force()));
|
||||
CHECK(buffers.force(0.055) < 0.5 * buffers.end_force());
|
||||
CHECK(buffers.energy(0.11) == doctest::Approx(1012e3 * 0.11 * (0.5 - 0.5 / 4.0)));
|
||||
for (auto const deflection : {0.001, 0.01, 0.05, 0.1}) {
|
||||
CAPTURE(deflection);
|
||||
CHECK(buffers.deflection(buffers.force(deflection)) == doctest::Approx(deflection));
|
||||
}
|
||||
CHECK(buffers.deflection(2.0 * buffers.end_force()) == doctest::Approx(0.11));
|
||||
|
||||
cp::spring_curve const linear{9.2e6, 0.11, 0.0};
|
||||
CHECK(linear.force(0.05) == doctest::Approx(9.2e6 * 0.05));
|
||||
CHECK(linear.deflection(9.2e6 * 0.05) == doctest::Approx(0.05));
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: the devices of both ends carry the same force, their deflections add up") {
|
||||
SUBCASE("alike, they share the deflection") {
|
||||
cp::spring_curve const draw{4.5e6, 0.1, 0.3};
|
||||
auto const split{cp::series(draw, draw, 0.1)};
|
||||
CHECK(split.first == doctest::Approx(0.05));
|
||||
CHECK(split.second == doctest::Approx(0.05));
|
||||
CHECK(split.force == doctest::Approx(draw.force(0.05)));
|
||||
}
|
||||
SUBCASE("the softer one gives more") {
|
||||
cp::spring_curve const soft{4.5e6, 0.1, 0.3};
|
||||
cp::spring_curve const stiff{9e6, 0.1, 0.3};
|
||||
auto const split{cp::series(soft, stiff, 0.12)};
|
||||
CHECK(split.first + split.second == doctest::Approx(0.12));
|
||||
CHECK(split.first > split.second);
|
||||
CHECK(soft.force(split.first) == doctest::Approx(split.force));
|
||||
CHECK(stiff.force(split.second) == doctest::Approx(split.force));
|
||||
// the same force whichever end asks, as both vehicles of the pair do
|
||||
CHECK(cp::series(stiff, soft, 0.12).force == split.force);
|
||||
}
|
||||
SUBCASE("past the travel of one, the other takes the rest") {
|
||||
cp::spring_curve const short_travel{1e6, 0.02, 0.0};
|
||||
cp::spring_curve const long_travel{1e6, 0.2, 0.0};
|
||||
auto const split{cp::series(short_travel, long_travel, 0.1)};
|
||||
CHECK(split.first == doctest::Approx(0.02));
|
||||
CHECK(split.second == doctest::Approx(0.08));
|
||||
CHECK(split.force == doctest::Approx(80e3));
|
||||
// both solid
|
||||
CHECK(cp::series(short_travel, long_travel, 0.3).force == doctest::Approx(200e3));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: the friction of a spring device takes energy on each stroke") {
|
||||
cp::spring_curve const draw{4.5e6, 0.1, 0.3};
|
||||
auto const unloading{0.5};
|
||||
auto const transition{3.0 * draw.end_force() / draw.travel};
|
||||
cp::hysteresis device;
|
||||
auto work{0.0};
|
||||
auto deflection{0.0};
|
||||
auto const move_to = [&device, &work, &deflection, &draw, unloading, transition](double Target) {
|
||||
while (std::abs(Target - deflection) > 1e-9) {
|
||||
auto const next{deflection + std::clamp(Target - deflection, -1e-4, 1e-4)};
|
||||
auto const before{device.force};
|
||||
auto const after{device.update(next, draw.force(next), unloading * draw.force(next), transition)};
|
||||
work += 0.5 * (before + after) * (next - deflection);
|
||||
deflection = next;
|
||||
}
|
||||
};
|
||||
|
||||
move_to(0.08);
|
||||
// loading, along the loading curve
|
||||
CHECK(device.force == doctest::Approx(draw.force(0.08)));
|
||||
move_to(0.06);
|
||||
// unloading, on the unloading one
|
||||
CHECK(device.force == doctest::Approx(unloading * draw.force(0.06)));
|
||||
// loaded again, the friction holds the device stiff until it slips onto the loading curve
|
||||
auto const held{device.force};
|
||||
device.update(0.0601, draw.force(0.0601), unloading * draw.force(0.0601), transition);
|
||||
CHECK(device.force == doctest::Approx(held + transition * 0.0001));
|
||||
device.update(0.06, draw.force(0.06), unloading * draw.force(0.06), transition);
|
||||
move_to(0.0);
|
||||
CHECK(device.force == doctest::Approx(0.0));
|
||||
// the stroke took part of the energy of the loading curve, less than the unloading curve alone would have: the
|
||||
// device springs back stiffly at first, along the transition
|
||||
CHECK(work > 0.3 * draw.energy(0.08));
|
||||
CHECK(work < (1.0 - unloading) * draw.energy(0.08));
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: the link stretches for good past its yield force and breaks at its ultimate one") {
|
||||
cp::link_strength const link{600e3, 850e3, 250e3 / 0.02, true};
|
||||
CHECK(cp::plastic_elongation(link, 500e3, 0.0) == 0.0);
|
||||
CHECK(cp::plastic_elongation(link, 700e3, 0.0) == doctest::Approx(0.008));
|
||||
// it doesn't get shorter again
|
||||
CHECK(cp::plastic_elongation(link, 650e3, 0.008) == doctest::Approx(0.008));
|
||||
// an articulated joint holds whatever
|
||||
auto joint{link};
|
||||
joint.breakable = false;
|
||||
CHECK(cp::plastic_elongation(joint, 700e3, 0.0) == 0.0);
|
||||
|
||||
// all the kinetic energy of the relative motion into the stop
|
||||
CHECK(cp::impact_force(1.0, 1e8, 2e4, 2e4) == doctest::Approx(1e6));
|
||||
CHECK(cp::impact_force(-1.0, 1e8, 2e4, 2e4) == 0.0);
|
||||
|
||||
for (int index = 0; index < 2; ++index) {
|
||||
auto const scatter{cp::strength_scatter("wagon", index)};
|
||||
CHECK(scatter >= 1.0);
|
||||
CHECK(scatter <= 1.2);
|
||||
CHECK(cp::strength_scatter("wagon", index) == scatter);
|
||||
}
|
||||
CHECK(cp::strength_scatter("wagon", 0) != cp::strength_scatter("wagon", 1));
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: the coupling pulls only past its play, the buffers push") {
|
||||
auto const wagon{wagon_end()};
|
||||
// the play of both ends: 0.1 m
|
||||
CHECK(fresh_pair(0.05).force == 0.0);
|
||||
CHECK(fresh_pair(0.099).force == 0.0);
|
||||
|
||||
auto const pulled{fresh_pair(0.15)};
|
||||
CHECK(pulled.force > 0.0);
|
||||
CHECK(pulled.tension == doctest::Approx(cp::series(wagon.draw_gear, wagon.draw_gear, 0.05).force));
|
||||
// uncoupled vehicles don't pull each other
|
||||
CHECK(fresh_pair(0.15, false).force == 0.0);
|
||||
|
||||
auto const pushed{fresh_pair(-0.05)};
|
||||
CHECK(pushed.force == doctest::Approx(-cp::series(wagon.buffers, wagon.buffers, 0.05).force));
|
||||
CHECK(pushed.buffers_deflection == doctest::Approx(0.025));
|
||||
CHECK(fresh_pair(-0.05, false).force == pushed.force);
|
||||
|
||||
SUBCASE("a tightened screw coupling presses the buffers in, both carry force at rest") {
|
||||
auto tight{wagon_end()};
|
||||
tight.slack = -0.02;
|
||||
auto const atrest{fresh_pair(-0.02, true, tight)};
|
||||
CHECK(atrest.tension > 0.0);
|
||||
CHECK(atrest.compression > 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: the springs of a pair go solid at the end of their travel") {
|
||||
cp::pair_state state;
|
||||
auto const wagon{wagon_end()};
|
||||
cp::update_pair(wagon, wagon, state, {0.1 + 0.2, 0.0, true, false, true});
|
||||
CHECK(state.stop == 1);
|
||||
CHECK(state.stop_force == doctest::Approx(450e3));
|
||||
cp::update_pair(wagon, wagon, state, {-0.25, 0.0, true, false, true});
|
||||
CHECK(state.stop == -1);
|
||||
CHECK(state.stop_force == doctest::Approx(1012e3));
|
||||
cp::update_pair(wagon, wagon, state, {0.0, 0.0, true, false, true});
|
||||
CHECK(state.stop == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: a pull past the ultimate force breaks the coupling, past the yield force stretches it") {
|
||||
auto weak{wagon_end()};
|
||||
weak.link = {300e3, 400e3, 100e3 / 0.02, true};
|
||||
|
||||
SUBCASE("broken, with crash damage") {
|
||||
cp::pair_state state;
|
||||
auto const result{cp::update_pair(weak, weak, state, {0.1 + 0.19, 0.0, true, false, true})};
|
||||
CHECK(result.broken);
|
||||
CHECK(result.tension == 0.0);
|
||||
CHECK(result.force == 0.0);
|
||||
}
|
||||
SUBCASE("held, without it") {
|
||||
cp::pair_state state;
|
||||
auto const result{cp::update_pair(weak, weak, state, {0.1 + 0.19, 0.0, true, false, false})};
|
||||
CHECK_FALSE(result.broken);
|
||||
CHECK(result.tension > 400e3);
|
||||
CHECK(state.elongation == 0.0);
|
||||
}
|
||||
SUBCASE("stretched for good, which eases the pull") {
|
||||
auto stronger{weak};
|
||||
stronger.link.ultimate = 1000e3;
|
||||
cp::pair_state state;
|
||||
auto const first{cp::update_pair(stronger, stronger, state, {0.1 + 0.19, 0.0, true, false, true})};
|
||||
CHECK(first.overloaded);
|
||||
CHECK(state.elongation == doctest::Approx((first.tension - 300e3) / stronger.link.hardening));
|
||||
auto const second{cp::update_pair(stronger, stronger, state, {0.1 + 0.19, 0.0, true, false, true})};
|
||||
CHECK(second.tension < first.tension);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: a jerk on the solid draw gear breaks the coupling by the force of the hit") {
|
||||
auto const wagon{wagon_end()};
|
||||
constexpr double mass{42200.0};
|
||||
auto const stopped = [&wagon](cp::pair_state &State) { cp::update_pair(wagon, wagon, State, {0.1 + 0.21, 0.0, true, false, true}); };
|
||||
|
||||
cp::pair_state state;
|
||||
stopped(state);
|
||||
auto const hard{cp::impact(wagon, wagon, state, 1.0, mass, mass)};
|
||||
// 450 kN of the springs and about 1.7 MN of the hit
|
||||
CHECK(hard.force == doctest::Approx(450e3 + std::sqrt(850e3 / 0.003 / 2.0 * mass / 2.0)));
|
||||
CHECK(hard.broken);
|
||||
|
||||
cp::pair_state gentle;
|
||||
stopped(gentle);
|
||||
CHECK_FALSE(cp::impact(wagon, wagon, gentle, 0.1, mass, mass).broken);
|
||||
CHECK(gentle.elongation == 0.0);
|
||||
|
||||
cp::pair_state harder;
|
||||
stopped(harder);
|
||||
auto const yielding{cp::impact(wagon, wagon, harder, 0.15, mass, mass)};
|
||||
CHECK_FALSE(yielding.broken);
|
||||
CHECK(yielding.force > 680e3);
|
||||
CHECK(harder.elongation > 0.0);
|
||||
|
||||
// the buffers crushed by a hit past their travel
|
||||
cp::pair_state pressed;
|
||||
cp::update_pair(wagon, wagon, pressed, {-0.25, 0.0, false, false, true});
|
||||
CHECK_FALSE(cp::impact(wagon, wagon, pressed, 0.1, mass, mass).crushed);
|
||||
CHECK(cp::impact(wagon, wagon, pressed, 1.0, mass, mass).crushed);
|
||||
}
|
||||
|
||||
// the model in the simulation, two vehicles on a track
|
||||
|
||||
namespace {
|
||||
|
||||
// couples the facing ends of the two vehicles, as the scenery does
|
||||
void join(vehicle &First, int FirstEnd, vehicle &Second, int SecondEnd, int Coupling = coupling::coupler) {
|
||||
REQUIRE(First->Attach(FirstEnd, SecondEnd, &Second.mover(), Coupling, true, false));
|
||||
REQUIRE(Second->Attach(SecondEnd, FirstEnd, &First.mover(), Coupling, true, false));
|
||||
}
|
||||
|
||||
// the wagon of data/wagon.json with the given play of its screw couplings
|
||||
vehicle loose_wagon(double Slack, double Velocity, std::string const &Name) {
|
||||
return vehicle{definition::load("wagon").set("BuffCoupl", "Slack", Slack), Velocity, Name};
|
||||
}
|
||||
|
||||
bool coupler_damaged(vehicle &Vehicle) {
|
||||
return TestFlag(Vehicle->DamageFlag, dtrain_coupling);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("coupler physics: the play of the coupling and the parameters of the model come from the vehicle definition") {
|
||||
auto loose{loose_wagon(0.05, 0.0, "loose")};
|
||||
auto const &physics{loose->Couplers[end::front].physics};
|
||||
CHECK(physics.slack == doctest::Approx(0.05));
|
||||
CHECK(loose->Couplers[end::rear].physics.slack == doctest::Approx(0.05));
|
||||
CHECK(physics.buffers.stiffness == doctest::Approx(9200e3));
|
||||
CHECK(physics.buffers.travel == doctest::Approx(0.11));
|
||||
CHECK(physics.draw_gear.end_force() == doctest::Approx(450e3));
|
||||
CHECK(physics.buffers_unloading == doctest::Approx(0.58));
|
||||
// the rated breaking force, and a little more for the coupling as made
|
||||
CHECK(physics.link.ultimate >= 850e3);
|
||||
CHECK(physics.link.ultimate <= 1020e3);
|
||||
CHECK(physics.link.yield == doctest::Approx(0.8 * physics.link.ultimate));
|
||||
CHECK(physics.link.breakable);
|
||||
|
||||
auto tight{vehicle{definition::load("wagon"), 0.0, "tight"}};
|
||||
CHECK(tight->Couplers[end::front].physics.slack == 0.0);
|
||||
auto automatic{vehicle{definition::load("wagon").set("BuffCoupl", "CType", "Automatic"), 0.0, "automatic"}};
|
||||
CHECK(automatic->Couplers[end::front].physics.slack == doctest::Approx(0.005));
|
||||
auto articulated{vehicle{definition::load("wagon").set("BuffCoupl", "CType", "Articulated"), 0.0, "articulated"}};
|
||||
CHECK_FALSE(articulated->Couplers[end::front].physics.link.breakable);
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: a coupling with play pulls only once the play is taken up") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
flag_setting const crashdamage{Global.crash_damage, true};
|
||||
auto front{loose_wagon(0.05, 3.0, "front")};
|
||||
auto rear{loose_wagon(0.05, 0.1, "rear")};
|
||||
join(rear, end::front, front, end::rear);
|
||||
test::pair pair{rear, end::front, front, end::rear, 0.0};
|
||||
front.hold_brake_pipe(5.0);
|
||||
rear.hold_brake_pipe(5.0);
|
||||
|
||||
auto pulledwithinplay{false};
|
||||
for (int i = 0; i < 300 && pair.gap() < 0.09; ++i) {
|
||||
pair.step();
|
||||
pulledwithinplay = pulledwithinplay || rear->Couplers[end::front].CForce != 0.0;
|
||||
}
|
||||
REQUIRE(pair.gap() >= 0.09);
|
||||
CHECK_FALSE(pulledwithinplay);
|
||||
CHECK(rear.speed() == doctest::Approx(0.1).epsilon(0.5));
|
||||
|
||||
pair.run(3.0);
|
||||
CHECK(pair.coupled());
|
||||
CHECK_FALSE(coupler_damaged(rear));
|
||||
CHECK_FALSE(coupler_damaged(front));
|
||||
// the front wagon took the rear one along
|
||||
CHECK(rear.speed() > 1.0);
|
||||
CHECK(front.speed() < 2.5);
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: buffers hit at a walking pace give back less than they took") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
flag_setting const crashdamage{Global.crash_damage, true};
|
||||
auto moving{vehicle{definition::load("wagon"), 5.0, "moving"}};
|
||||
// barely moving, so that its brakes are released
|
||||
auto standing{vehicle{definition::load("wagon"), 0.1, "standing"}};
|
||||
test::pair pair{moving, end::front, standing, end::rear, 0.3};
|
||||
moving.hold_brake_pipe(5.0);
|
||||
standing.hold_brake_pipe(5.0);
|
||||
|
||||
auto touched{false};
|
||||
auto solid{false};
|
||||
for (int i = 0; i < 500; ++i) {
|
||||
pair.step();
|
||||
touched = touched || pair.gap() < 0.0;
|
||||
solid = solid || moving->Couplers[end::front].physics_state.stop != 0;
|
||||
if (touched && pair.gap() > 0.3) {
|
||||
break; // parted
|
||||
}
|
||||
}
|
||||
CAPTURE(moving.speed());
|
||||
CAPTURE(standing.speed());
|
||||
REQUIRE(touched);
|
||||
REQUIRE(pair.gap() > 0.3);
|
||||
// the buffers took the hit within their travel, nothing collided
|
||||
CHECK_FALSE(solid);
|
||||
CHECK_FALSE(pair.coupled());
|
||||
// the momentum stays, rolling resistance aside
|
||||
CHECK(moving.speed() + standing.speed() == doctest::Approx(5.1).epsilon(0.05));
|
||||
// the vehicles part slower than they met: the friction of the buffers took part of the energy
|
||||
auto const restitution{(standing.speed() - moving.speed()) / (5.0 - 0.1)};
|
||||
CHECK(restitution > 0.3);
|
||||
CHECK(restitution < 0.95);
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: coupled vehicles at rest stay at rest") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
auto first{vehicle{definition::load("wagon"), 0.0, "first"}};
|
||||
auto second{vehicle{definition::load("wagon"), 0.0, "second"}};
|
||||
join(first, end::rear, second, end::front);
|
||||
test::pair pair{first, end::rear, second, end::front, 0.0};
|
||||
|
||||
pair.run(5.0);
|
||||
CHECK(std::abs(first.speed()) < 0.01);
|
||||
CHECK(std::abs(second.speed()) < 0.01);
|
||||
CHECK(std::abs(pair.gap()) < 0.001);
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: a jerk on a coupling with play breaks it, the brake hoses torn off") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
auto front{loose_wagon(0.05, 20.0, "front")};
|
||||
auto rear{loose_wagon(0.05, 0.1, "rear")};
|
||||
join(rear, end::front, front, end::rear, coupling::coupler | coupling::brakehose);
|
||||
test::pair pair{rear, end::front, front, end::rear, 0.0};
|
||||
front.hold_brake_pipe(5.0);
|
||||
rear.hold_brake_pipe(5.0);
|
||||
|
||||
SUBCASE("with crash damage, it breaks") {
|
||||
flag_setting const crashdamage{Global.crash_damage, true};
|
||||
pair.run(1.0);
|
||||
CHECK_FALSE(pair.coupled());
|
||||
CHECK((coupler_damaged(rear) || coupler_damaged(front)));
|
||||
// both parts of the train brake, their pipe vented through the torn hoses
|
||||
CHECK(rear->AlarmChainFlag);
|
||||
CHECK(front->AlarmChainFlag);
|
||||
// the front wagon runs away from the rear one
|
||||
CHECK(front.speed() > rear.speed() + 5.0);
|
||||
CHECK(pair.gap() > 0.3);
|
||||
}
|
||||
SUBCASE("without it, the coupling holds and the wagons run on together") {
|
||||
flag_setting const crashdamage{Global.crash_damage, false};
|
||||
pair.run(3.0);
|
||||
CHECK(pair.coupled());
|
||||
CHECK_FALSE(coupler_damaged(rear));
|
||||
CHECK_FALSE(coupler_damaged(front));
|
||||
CHECK(front.speed() == doctest::Approx(rear.speed()).epsilon(0.2));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: a gentle pull on a coupling with play doesn't hurt it") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
flag_setting const crashdamage{Global.crash_damage, true};
|
||||
auto front{loose_wagon(0.05, 3.0, "front")};
|
||||
auto rear{loose_wagon(0.05, 0.1, "rear")};
|
||||
join(rear, end::front, front, end::rear);
|
||||
test::pair pair{rear, end::front, front, end::rear, 0.0};
|
||||
front.hold_brake_pipe(5.0);
|
||||
rear.hold_brake_pipe(5.0);
|
||||
|
||||
pair.run(3.0);
|
||||
CHECK(pair.coupled());
|
||||
CHECK_FALSE(coupler_damaged(rear));
|
||||
CHECK(rear->Couplers[end::front].physics_state.elongation == 0.0);
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: automatic couplers still catch a wagon rolling into a standing one") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
flag_setting const crashdamage{Global.crash_damage, true};
|
||||
auto automatic{definition::load("wagon").set("BuffCoupl", "CType", "Automatic").set("BuffCoupl", "AutomaticFlag", coupling::coupler | coupling::brakehose)};
|
||||
auto moving{vehicle{automatic, 5.0, "moving"}};
|
||||
auto standing{vehicle{automatic, 0.0, "standing"}};
|
||||
test::pair pair{moving, end::front, standing, end::rear, 1.0};
|
||||
|
||||
pair.run(3.0);
|
||||
REQUIRE(pair.coupled());
|
||||
pair.run(2.0);
|
||||
CHECK(moving.speed() == doctest::Approx(standing.speed()).epsilon(0.1));
|
||||
CHECK_FALSE(TestFlag(moving->DamageFlag, dtrain_out));
|
||||
}
|
||||
|
||||
TEST_CASE("couplers: a broken coupling of vehicles facing each other uncouples the ends coupled") {
|
||||
// it used to uncouple the far end of the other vehicle, presuming the vehicles faced the same way
|
||||
auto first{vehicle{definition::load("wagon"), 0.0, "first"}};
|
||||
auto second{vehicle{definition::load("wagon"), 0.0, "second"}};
|
||||
auto third{vehicle{definition::load("wagon"), 0.0, "third"}};
|
||||
join(first, end::front, second, end::front);
|
||||
join(second, end::rear, third, end::front);
|
||||
|
||||
first->damage_coupler(end::front);
|
||||
CHECK(first->Couplers[end::front].CouplingFlag == coupling::faux);
|
||||
CHECK(second->Couplers[end::front].CouplingFlag == coupling::faux);
|
||||
// the other coupling of the second vehicle stays
|
||||
CHECK(second->Couplers[end::rear].CouplingFlag == coupling::coupler);
|
||||
}
|
||||
|
||||
TEST_CASE("couplers: a broken coupler is repaired alone, with the one of the vehicle it was coupled with") {
|
||||
auto first{vehicle{definition::load("wagon"), 0.0, "first"}};
|
||||
auto second{vehicle{definition::load("wagon"), 0.0, "second"}};
|
||||
auto third{vehicle{definition::load("wagon"), 0.0, "third"}};
|
||||
join(first, end::rear, second, end::front, coupling::coupler | coupling::brakehose);
|
||||
join(second, end::rear, third, end::front, coupling::coupler | coupling::brakehose);
|
||||
|
||||
// both couplings of the second wagon break
|
||||
second->damage_coupler(end::front);
|
||||
second->damage_coupler(end::rear);
|
||||
REQUIRE(second->Couplers[end::front].broken);
|
||||
REQUIRE(first->Couplers[end::rear].broken);
|
||||
REQUIRE(third->Couplers[end::front].broken);
|
||||
REQUIRE(TestFlag(second->DamageFlag, dtrain_coupling));
|
||||
|
||||
// nothing to repair at the far end of the first wagon
|
||||
CHECK_FALSE(first->repair_coupler(end::front));
|
||||
|
||||
// repaired from the first wagon: the coupling between the first two, the other one stays broken
|
||||
CHECK(first->repair_coupler(end::rear));
|
||||
CHECK_FALSE(first->Couplers[end::rear].broken);
|
||||
CHECK_FALSE(second->Couplers[end::front].broken);
|
||||
CHECK(second->Couplers[end::rear].broken);
|
||||
CHECK(third->Couplers[end::front].broken);
|
||||
// the second wagon keeps its damage, and its emergency braking, while a coupler of it is broken
|
||||
CHECK(TestFlag(second->DamageFlag, dtrain_coupling));
|
||||
CHECK(second->AlarmChainFlag);
|
||||
|
||||
CHECK(third->repair_coupler(end::front));
|
||||
CHECK_FALSE(second->Couplers[end::rear].broken);
|
||||
CHECK_FALSE(TestFlag(second->DamageFlag, dtrain_coupling));
|
||||
CHECK_FALSE(second->AlarmChainFlag);
|
||||
// repaired, not coupled: the vehicles are coupled again as usual
|
||||
CHECK(second->Couplers[end::rear].CouplingFlag == coupling::faux);
|
||||
CHECK(second->Attach(end::rear, end::front, &third.mover(), coupling::coupler, true, false));
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: with couplerphysics yes, only the pairs with a vehicle covered take the physical model") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, false};
|
||||
auto moving{vehicle{definition::load("wagon"), 5.0, "moving"}};
|
||||
auto standing{vehicle{definition::load("wagon"), 0.1, "standing"}};
|
||||
test::pair pair{moving, end::front, standing, end::rear, 0.1};
|
||||
|
||||
SUBCASE("neither covered: the legacy model") {
|
||||
pair.run(1.0);
|
||||
CHECK_FALSE(moving->coupler_physics_at(end::front));
|
||||
CHECK_FALSE(standing->coupler_physics_at(end::rear));
|
||||
// the state of the physical model untouched
|
||||
CHECK(moving->Couplers[end::front].physics_state.gap == 0.0);
|
||||
}
|
||||
SUBCASE("one covered: both compute the pair physically") {
|
||||
// as the player's train closing in on the other one
|
||||
moving->CouplerPhysics = true;
|
||||
pair.run(1.0);
|
||||
CHECK(moving->coupler_physics_at(end::front));
|
||||
CHECK(standing->coupler_physics_at(end::rear));
|
||||
CHECK(moving->Couplers[end::front].physics_state.gap != 0.0);
|
||||
CHECK(standing->Couplers[end::rear].physics_state.gap == moving->Couplers[end::front].physics_state.gap);
|
||||
}
|
||||
SUBCASE("the setting off: the legacy model, covered or not") {
|
||||
flag_setting const off{Global.coupler_physics, false};
|
||||
moving->CouplerPhysics = true;
|
||||
CHECK_FALSE(moving->coupler_physics_at(end::front));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("coupler physics: a hit on a braked train runs through it as a wave, weaker with each coupling") {
|
||||
flag_setting const physics{Global.coupler_physics, true};
|
||||
flag_setting const everywhere{Global.coupler_physics_all, true};
|
||||
flag_setting const crashdamage{Global.crash_damage, true};
|
||||
constexpr std::size_t wagons{8};
|
||||
test::consist train;
|
||||
for (std::size_t i = 0; i < wagons; ++i) {
|
||||
train.add(definition::load("wagon"));
|
||||
}
|
||||
// a wagon rolling at 15 km/h into the rear of the standing train, its brakes applied
|
||||
train.add(definition::load("wagon"), 0, coupling::faux, 15.0, 0.3);
|
||||
|
||||
std::vector<double> peak(wagons, 0.0);
|
||||
std::vector<double> started(wagons, -1.0);
|
||||
for (int step = 0; step < 300; ++step) {
|
||||
train.step();
|
||||
for (std::size_t i = 0; i < wagons; ++i) {
|
||||
peak[i] = std::max(peak[i], train[i].speed());
|
||||
if (started[i] < 0.0 && train[i].speed() > 0.5) {
|
||||
started[i] = step * 0.01;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (std::size_t i = 0; i < wagons; ++i) {
|
||||
CAPTURE(i);
|
||||
CAPTURE(peak[i]);
|
||||
REQUIRE(started[i] >= 0.0);
|
||||
CHECK_FALSE(TestFlag(train[i]->DamageFlag, dtrain_out));
|
||||
}
|
||||
// the wagon hit first takes less than the hit, the one at the far end the least
|
||||
CHECK(peak[wagons - 1] < 12.0);
|
||||
CHECK(peak[0] < 0.25 * 15.0);
|
||||
for (std::size_t i = 0; i + 1 < wagons; ++i) {
|
||||
CAPTURE(i);
|
||||
CHECK(peak[i] < peak[i + 1] + 0.3);
|
||||
}
|
||||
// the wave takes time to run through the train, the far end starts last
|
||||
CHECK(started[0] - started[wagons - 1] > 0.5);
|
||||
// and the brakes have stopped it all again
|
||||
for (std::size_t i = 0; i < wagons; ++i) {
|
||||
CHECK(std::abs(train[i].speed()) < 0.5);
|
||||
}
|
||||
}
|
||||
256
tests/unit/test_elf_controller.cpp
Normal file
256
tests/unit/test_elf_controller.cpp
Normal file
@@ -0,0 +1,256 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
#include "consist.h"
|
||||
#include "drive.h"
|
||||
|
||||
#include "McZapkie/hamulce.h"
|
||||
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
|
||||
// the master controller of the Elf (EIMCtrlType 2): its positions, and the emergency brake position below
|
||||
// them which EIMCtrlMainEmergency adds (eu07/maszyna#413)
|
||||
|
||||
using test::definition;
|
||||
using test::vehicle;
|
||||
|
||||
namespace {
|
||||
|
||||
// the data of an Elf: without the emergency brake position as now, with it in the files updated for it
|
||||
// (EIMCtrlMainEmergency, MCPN=5, DirChangeMaxPos=4), or with the key alone added to the old files, which the
|
||||
// loader fixes up
|
||||
enum class data { plain, emergency, emergency_old_layout };
|
||||
|
||||
// three-car Elfs coupled into one train, of the given data each, ready to run, standing with their brakes
|
||||
// released and the driver in the front cab of the head car of the first one, which also drives (the
|
||||
// controller's eimic is worked out there)
|
||||
struct elf {
|
||||
explicit elf(bool Emergency) : elf({Emergency ? data::emergency : data::plain}) {}
|
||||
explicit elf(std::initializer_list<data> Units) {
|
||||
for (auto const units : Units) {
|
||||
for (auto const *car : {"elf_v1/21we-a", "elf_v1/21we-b", "elf_v1/21we-c"}) {
|
||||
auto cardefinition{definition::fiz(car)};
|
||||
if (units != data::plain) {
|
||||
cardefinition.set("Cntrl", "EIMCtrlMainEmergency", "Yes");
|
||||
}
|
||||
if (units == data::emergency) {
|
||||
cardefinition.set("Cntrl", "MCPN", 5);
|
||||
if (std::string{car} != "elf_v1/21we-b") {
|
||||
// the cars with a cab have it, at neutral: 3 before, one up with the key
|
||||
cardefinition.set("Cntrl", "DirChangeMaxPos", 4);
|
||||
}
|
||||
}
|
||||
unit.add(cardefinition, unit.size() == 0 ? 1 : 0);
|
||||
}
|
||||
}
|
||||
ready = test::start_up(test::driving(unit), line);
|
||||
// the start-up of the tests leaves the controller at its position 0 (close_line_breaker() in drive.cpp),
|
||||
// the emergency brake one with EIMCtrlMainEmergency; the driver sets it to neutral, as the AI does. a unit
|
||||
// moves its controller only with a direction set
|
||||
for (int i = 0; i < 3 && cab().DirActive <= 0; ++i) {
|
||||
cab().DirectionForward();
|
||||
}
|
||||
while (cab().MainCtrlPos < cab().MainCtrlNoPowerPos() && cab().IncMainCtrl(1)) {
|
||||
// up to neutral
|
||||
}
|
||||
test::release_brakes(cab());
|
||||
recharge();
|
||||
charged = cab().PipePress > 4.5;
|
||||
}
|
||||
// the pipe charged again after an emergency stop: the Elf locks its emptied pipe off the brake handle (LPOn,
|
||||
// LPOff) until the unlock button (UBB1, the first universal brake button) is held, the handle at running
|
||||
void recharge() {
|
||||
cab().BrakeLevelSet(cab().Handle->GetPos(bh_RP));
|
||||
cab().UniversalBrakeButton(0, 1);
|
||||
wait(30.0);
|
||||
cab().UniversalBrakeButton(0, 0);
|
||||
wait(30.0);
|
||||
}
|
||||
TMoverParameters &cab() { return unit[0].mover(); }
|
||||
TMoverParameters &car(std::size_t Index) { return unit[Index].mover(); }
|
||||
// the driver moves the controller notch by notch, which the cab sends on to the coupled vehicles
|
||||
void set_controller(int Position) {
|
||||
while (cab().MainCtrlPos < Position && cab().IncMainCtrl(1)) {
|
||||
}
|
||||
while (cab().MainCtrlPos > Position && cab().DecMainCtrl(1)) {
|
||||
}
|
||||
}
|
||||
void wait(double Seconds) { test::drive(test::driving(unit), line, Seconds); }
|
||||
test::consist unit;
|
||||
test::track line{0.15, 0.0, test::catenary_3kv};
|
||||
bool ready{false};
|
||||
bool charged{false};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("elf controller: without EIMCtrlMainEmergency the positions stay as they were") {
|
||||
elf unit{false};
|
||||
REQUIRE(unit.ready);
|
||||
REQUIRE(unit.charged);
|
||||
auto &mover{unit.cab()};
|
||||
CHECK_FALSE(mover.EIMCtrlMainEmergency);
|
||||
CHECK(mover.EIMCtrlEmergencyPositions() == 0);
|
||||
CHECK(mover.MainCtrlPosNo == 4);
|
||||
CHECK(mover.MainCtrlNoPowerPos() == 3);
|
||||
CHECK(mover.MainCtrlPos == 3);
|
||||
// the lowest position brakes fully, without emptying the pipe
|
||||
mover.MainCtrlPos = 0;
|
||||
CHECK_FALSE(mover.IsMainCtrlEmergencyPos());
|
||||
unit.wait(10.0);
|
||||
CHECK(mover.eimic == doctest::Approx(-1.0));
|
||||
CHECK(mover.PipePress > 4.5);
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: EIMCtrlMainEmergency shifts the positions up by the emergency brake one") {
|
||||
elf unit{true};
|
||||
REQUIRE(unit.ready);
|
||||
REQUIRE(unit.charged);
|
||||
auto &mover{unit.cab()};
|
||||
CHECK(mover.EIMCtrlMainEmergency);
|
||||
CHECK(mover.EIMCtrlEmergencyPositions() == 1);
|
||||
CHECK(mover.MainCtrlPosNo == 5);
|
||||
CHECK(mover.MainCtrlNoPowerPos() == 4);
|
||||
CHECK(mover.MainCtrlPos == 4);
|
||||
CHECK_FALSE(mover.IsMainCtrlEmergencyPos());
|
||||
SUBCASE("the neutral position neither drives nor brakes") {
|
||||
unit.wait(5.0);
|
||||
CHECK(mover.eimic == doctest::Approx(0.0));
|
||||
}
|
||||
SUBCASE("the full braking position, formerly 0, brakes fully without emptying the pipe") {
|
||||
mover.MainCtrlPos = 1;
|
||||
unit.wait(10.0);
|
||||
CHECK(mover.eimic == doctest::Approx(-1.0));
|
||||
CHECK(mover.PipePress > 4.5);
|
||||
}
|
||||
SUBCASE("the braking position, formerly 1, ramps the braking up") {
|
||||
mover.MainCtrlPos = 2;
|
||||
unit.wait(1.0);
|
||||
CHECK(mover.eimic < 0.0);
|
||||
CHECK(mover.eimic > -1.0);
|
||||
CHECK(mover.PipePress > 4.5);
|
||||
}
|
||||
SUBCASE("the emergency brake position brakes fully at once and empties the pipe") {
|
||||
mover.MainCtrlPos = 0;
|
||||
CHECK(mover.IsMainCtrlEmergencyPos());
|
||||
// a single step of the physics
|
||||
unit.unit.step(unit.line);
|
||||
CHECK(mover.eimic == doctest::Approx(-1.0));
|
||||
unit.wait(10.0);
|
||||
CHECK(mover.PipePress < 0.5);
|
||||
CHECK(mover.BrakePress > 1.0);
|
||||
// out of it the pipe charges again
|
||||
mover.MainCtrlPos = mover.MainCtrlNoPowerPos();
|
||||
CHECK_FALSE(mover.IsMainCtrlEmergencyPos());
|
||||
unit.recharge();
|
||||
CHECK(mover.PipePress > 4.5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: EIMCtrlMainEmergency with the old MCPN gets its positions all the same") {
|
||||
vehicle car{definition::fiz("elf_v1/21we-a").set("Cntrl", "EIMCtrlMainEmergency", "Yes")};
|
||||
CHECK(car->MainCtrlPosNo == 5);
|
||||
CHECK(car->MainCtrlNoPowerPos() == 4);
|
||||
// DirChangeMaxPos=3 of the file, the old neutral, moves up with the positions
|
||||
CHECK(car->MainCtrlMaxDirChangePos == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: EIMCtrlMainEmergency leaves DirChangeMaxPos of the files updated for it alone") {
|
||||
vehicle car{definition::fiz("elf_v1/21we-a").set("Cntrl", "EIMCtrlMainEmergency", "Yes").set("Cntrl", "MCPN", 5).set("Cntrl", "DirChangeMaxPos", 4)};
|
||||
CHECK(car->MainCtrlPosNo == 5);
|
||||
CHECK(car->MainCtrlMaxDirChangePos == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: the reverser works at the neutral shifted by EIMCtrlMainEmergency") {
|
||||
// the key added to the old files, DirChangeMaxPos=3 in them
|
||||
elf unit{{data::emergency_old_layout}};
|
||||
REQUIRE(unit.ready);
|
||||
auto &mover{unit.cab()};
|
||||
REQUIRE(mover.MainCtrlPos == mover.MainCtrlNoPowerPos());
|
||||
REQUIRE(mover.DirActive == 1);
|
||||
CHECK(mover.DirectionBackward());
|
||||
CHECK(mover.DirActive == 0);
|
||||
CHECK(mover.DirectionForward());
|
||||
CHECK(mover.DirActive == 1);
|
||||
// not in the power position
|
||||
unit.set_controller(mover.MainCtrlNoPowerPos() + 1);
|
||||
REQUIRE(mover.MainCtrlPos == 5);
|
||||
CHECK_FALSE(mover.DirectionBackward());
|
||||
CHECK(mover.DirActive == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: the emergency brake position vents the pipe only from the vehicle driven from") {
|
||||
elf unit{true};
|
||||
REQUIRE(unit.ready);
|
||||
REQUIRE(unit.charged);
|
||||
// the rear car, with its own cab, has its controller in the emergency brake position, the driven one not
|
||||
auto &rear{unit.car(2)};
|
||||
rear.MainCtrlPos = 0;
|
||||
CHECK(rear.IsMainCtrlEmergencyPos());
|
||||
CHECK_FALSE(rear.IsCabMaster());
|
||||
unit.wait(10.0);
|
||||
CHECK(unit.cab().PipePress > 4.5);
|
||||
CHECK(rear.PipePress > 4.5);
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: an Elf with the emergency brake position leading one without it") {
|
||||
elf unit{{data::emergency, data::plain}};
|
||||
REQUIRE(unit.ready);
|
||||
REQUIRE(unit.charged);
|
||||
auto &lead{unit.cab()};
|
||||
auto &other{unit.car(3)};
|
||||
REQUIRE_FALSE(other.EIMCtrlMainEmergency);
|
||||
// neutral of the lead is neutral of the other
|
||||
CHECK(lead.MainCtrlPos == 4);
|
||||
CHECK(other.MainCtrlPos == other.MainCtrlNoPowerPos());
|
||||
SUBCASE("power") {
|
||||
unit.set_controller(5);
|
||||
CHECK(other.MainCtrlPos == 4);
|
||||
}
|
||||
SUBCASE("full braking") {
|
||||
unit.set_controller(1);
|
||||
CHECK(other.MainCtrlPos == 0);
|
||||
unit.wait(10.0);
|
||||
CHECK(lead.PipePress > 4.5);
|
||||
}
|
||||
SUBCASE("the emergency brake: the other one at its full braking, the pipe vented by the lead") {
|
||||
unit.set_controller(0);
|
||||
REQUIRE(lead.IsMainCtrlEmergencyPos());
|
||||
CHECK(other.MainCtrlPos == 0);
|
||||
unit.wait(10.0);
|
||||
CHECK(lead.PipePress < 0.5);
|
||||
CHECK(other.PipePress < 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: an Elf without the emergency brake position leading one with it") {
|
||||
elf unit{{data::plain, data::emergency}};
|
||||
REQUIRE(unit.ready);
|
||||
REQUIRE(unit.charged);
|
||||
auto &lead{unit.cab()};
|
||||
auto &other{unit.car(3)};
|
||||
REQUIRE(other.EIMCtrlMainEmergency);
|
||||
CHECK(lead.MainCtrlPos == 3);
|
||||
CHECK(other.MainCtrlPos == other.MainCtrlNoPowerPos());
|
||||
SUBCASE("power") {
|
||||
unit.set_controller(4);
|
||||
CHECK(other.MainCtrlPos == 5);
|
||||
}
|
||||
SUBCASE("the full braking of the lead is the full braking of the other, not its emergency brake") {
|
||||
unit.set_controller(0);
|
||||
CHECK(other.MainCtrlPos == 1);
|
||||
CHECK_FALSE(other.IsMainCtrlEmergencyPos());
|
||||
unit.wait(10.0);
|
||||
CHECK(lead.PipePress > 4.5);
|
||||
CHECK(other.PipePress > 4.5);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("elf controller: EIMCtrlMainEmergency leaves the other controllers alone") {
|
||||
// the Impuls (EIMCtrlType 0) has its emergency brake in the brake range of its controller (EIMCtrlEmergency)
|
||||
vehicle impuls{definition::fiz("impuls_v1/31we-a_pr").set("Cntrl", "EIMCtrlMainEmergency", "Yes"), 60.0, "impuls", 1};
|
||||
CHECK(impuls->EIMCtrlEmergencyPositions() == 0);
|
||||
CHECK(impuls->MainCtrlPosNo == 10);
|
||||
impuls->MainCtrlPos = 0;
|
||||
CHECK_FALSE(impuls->IsMainCtrlEmergencyPos());
|
||||
}
|
||||
120
tests/unit/test_script_screen_drawing.cpp
Normal file
120
tests/unit/test_script_screen_drawing.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "scripting/scriptscreendrawing.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
|
||||
// the drawing the Lua screens get from the game: fonts read as scripts/led_util.py reads them, text and images
|
||||
|
||||
using script_screens::bitmap_font;
|
||||
using script_screens::canvas;
|
||||
using script_screens::rgb;
|
||||
|
||||
namespace {
|
||||
|
||||
// an RGBA image Width x Height, white, with the listed pixels set to the colour
|
||||
std::vector<std::uint8_t> image(int const Width, int const Height, std::vector<std::pair<int, int>> const &Pixels,
|
||||
std::array<std::uint8_t, 4> const Color = {0, 0, 0, 255}) {
|
||||
std::vector<std::uint8_t> pixels(static_cast<std::size_t>(Width) * Height * 4, 255);
|
||||
for (auto const &[x, y] : Pixels) {
|
||||
std::copy(Color.begin(), Color.end(), pixels.begin() + (static_cast<std::ptrdiff_t>(y) * Width + x) * 4);
|
||||
}
|
||||
return pixels;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("script screen drawing: UTF-8 text becomes code points, a stray byte stands for itself") {
|
||||
CHECK(script_screens::decode_utf8("ab") == U"ab");
|
||||
CHECK(script_screens::decode_utf8("\xc5\x81\xc3\xb3\xc5\xbc") == U"Łóż"); // Łóż
|
||||
CHECK(script_screens::decode_utf8("a\xb3z") == U"a³z");
|
||||
CHECK(script_screens::decode_utf8("\xe2\x82") == U"â\u0082");
|
||||
}
|
||||
|
||||
TEST_CASE("script screen drawing: a font as led_util reads it, glyphs a column apart") {
|
||||
// "a" 2 wide (x 0-1), "Ł" 1 wide (x 3), " " 1 wide (x 5)
|
||||
auto const pixels{image(6, 2, {{0, 0}, {1, 1}, {3, 0}, {3, 1}})};
|
||||
auto const font{bitmap_font::parse(pixels, 6, 2, "// comment\r\na2\r\n\xc5\x81" "1\r\n 1\r\n")};
|
||||
REQUIRE(font.has_value());
|
||||
CHECK(font->height() == 2);
|
||||
|
||||
auto const &a{font->find(U'a')};
|
||||
REQUIRE(a.width() == 2);
|
||||
CHECK(a.lit(0, 0));
|
||||
CHECK_FALSE(a.lit(0, 1));
|
||||
CHECK(a.lit(1, 1));
|
||||
|
||||
auto const &l{font->find(U'Ł')};
|
||||
REQUIRE(l.width() == 1);
|
||||
CHECK(l.lit(0, 0));
|
||||
CHECK(l.lit(0, 1));
|
||||
|
||||
auto const &space{font->find(U' ')};
|
||||
REQUIRE(space.width() == 1);
|
||||
CHECK_FALSE(space.lit(0, 0));
|
||||
|
||||
// a character the font lacks: its first glyph
|
||||
CHECK(font->find(U'q').width() == 2);
|
||||
// led_util's get_str_len: a spacing column after every character
|
||||
CHECK(font->text_width("a\xc5\x81", 1) == 5);
|
||||
CHECK(font->text_width("", 1) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("script screen drawing: a pixel is lit unless it is opaque white") {
|
||||
auto pixels{image(3, 1, {{0, 0}})};
|
||||
// transparent white: lit, as led_util compares the whole pixel with (255, 255, 255, 255)
|
||||
pixels[1 * 4 + 3] = 0;
|
||||
auto const sprite{bitmap_font::sprite(pixels, 3, 1)};
|
||||
REQUIRE(sprite.width() == 3);
|
||||
CHECK(sprite.lit(0, 0));
|
||||
CHECK(sprite.lit(1, 0));
|
||||
CHECK_FALSE(sprite.lit(2, 0));
|
||||
}
|
||||
|
||||
TEST_CASE("script screen drawing: a description without glyphs, or one wider than its image, does no harm") {
|
||||
auto const pixels{image(2, 1, {})};
|
||||
CHECK_FALSE(bitmap_font::parse(pixels, 2, 1, "// only a comment\n").has_value());
|
||||
CHECK_FALSE(bitmap_font::parse(pixels, 2, 1, "").has_value());
|
||||
// the glyph runs past the image: the columns past its edge are unlit
|
||||
auto const font{bitmap_font::parse(pixels, 2, 1, "//\nw5\n")};
|
||||
REQUIRE(font.has_value());
|
||||
CHECK(font->find(U'w').width() == 5);
|
||||
CHECK_FALSE(font->find(U'w').lit(4, 0));
|
||||
}
|
||||
|
||||
TEST_CASE("script screen drawing: text is drawn from x rightwards and returns where it ends") {
|
||||
auto const pixels{image(4, 1, {{0, 0}, {1, 0}, {3, 0}})};
|
||||
auto const font{bitmap_font::parse(pixels, 4, 1, "//\na2\nb1\n")};
|
||||
REQUIRE(font.has_value());
|
||||
canvas board{8, 1, rgb{0, 0, 0}};
|
||||
auto const end{board.draw_text(*font, 1, 0, "ab", rgb{9, 8, 7}, 1)};
|
||||
CHECK(end == 6);
|
||||
CHECK(board.get(0, 0) == rgb{0, 0, 0});
|
||||
CHECK(board.get(1, 0) == rgb{9, 8, 7});
|
||||
CHECK(board.get(2, 0) == rgb{9, 8, 7});
|
||||
CHECK(board.get(3, 0) == rgb{0, 0, 0});
|
||||
CHECK(board.get(4, 0) == rgb{9, 8, 7});
|
||||
// partly off the image: clipped
|
||||
canvas narrow{2, 1, rgb{0, 0, 0}};
|
||||
narrow.draw_text(*font, -1, 0, "ab", rgb{1, 1, 1}, 1);
|
||||
CHECK(narrow.get(0, 0) == rgb{1, 1, 1});
|
||||
}
|
||||
|
||||
TEST_CASE("script screen drawing: rectangles and images are clipped to the image") {
|
||||
canvas board{3, 2, rgb{0, 0, 0}};
|
||||
board.fill_rect(-5, 1, 100, 100, rgb{1, 2, 3});
|
||||
CHECK(board.get(0, 0) == rgb{0, 0, 0});
|
||||
CHECK(board.get(0, 1) == rgb{1, 2, 3});
|
||||
CHECK(board.get(2, 1) == rgb{1, 2, 3});
|
||||
CHECK(board.get(5, 5) == rgb{0, 0, 0});
|
||||
|
||||
canvas const stamp{2, 2, rgb{7, 7, 7}};
|
||||
board.draw_image(stamp, 2, -1);
|
||||
CHECK(board.get(2, 0) == rgb{7, 7, 7});
|
||||
CHECK(board.get(1, 0) == rgb{0, 0, 0});
|
||||
// every pixel opaque
|
||||
CHECK(board.pixels()[3] == 255);
|
||||
}
|
||||
753
tests/unit/test_script_screen_host.cpp
Normal file
753
tests/unit/test_script_screen_host.cpp
Normal file
@@ -0,0 +1,753 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
// the script screen host (scripting/scriptscreens.cpp) with the real runtimes: a script reads its input, draws an
|
||||
// image that goes to the screen's texture, bottom row first, and sends a command; a script stuck in a loop is stopped;
|
||||
// a Lua script can't reach files, the system or precompiled code. the Lua cases need WITH_LUA, the WebAssembly ones
|
||||
// WITH_WASM
|
||||
|
||||
#include "scripting/scriptscreens.h"
|
||||
#include "input/command.h"
|
||||
#include "interfaces/ITexture.h"
|
||||
#include "utilities/Globals.h"
|
||||
#include "utilities/dictionary.h"
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
// the engine's command queue and command names, which the test build doesn't link: the commands the screens send
|
||||
// are kept here instead
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<std::pair<command_data, std::uint32_t>> &sent_commands() {
|
||||
static std::vector<std::pair<command_data, std::uint32_t>> commands;
|
||||
return commands;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace simulation {
|
||||
command_queue Commands; // NOSONAR the engine's extern global, input/command.h
|
||||
std::unordered_map<std::string, user_command> const commandMap{{"cmdtest", user_command::aidriverenable}}; // NOSONAR the engine's extern global, input/command.h
|
||||
} // namespace simulation
|
||||
|
||||
void command_queue::push(command_data const &Command, uint32_t const Recipient) { // NOSONAR signature from input/command.h
|
||||
sent_commands().emplace_back(Command, Recipient);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// a texture that keeps what was uploaded to it
|
||||
struct recording_texture : ITexture {
|
||||
bool create(bool) override { return true; }
|
||||
int get_width() const override { return width; }
|
||||
int get_height() const override { return height; }
|
||||
size_t get_id() const override { return id; }
|
||||
void release() override { /* nothing held */ }
|
||||
void make_stub() override { /* nothing held */ }
|
||||
std::string_view get_traits() const override { return ""; }
|
||||
std::string_view get_name() const override { return "recording"; }
|
||||
std::string_view get_type() const override { return "make:"; }
|
||||
bool is_stub() const override { return false; }
|
||||
bool get_has_alpha() const override { return true; }
|
||||
bool get_is_ready() const override { return ready; }
|
||||
void set_components_hint(int) override { /* not used */ }
|
||||
void make_from_memory(size_t const Width, size_t const Height, const uint8_t *Data) override { update_from_memory(Width, Height, Data); }
|
||||
void update_from_memory(size_t const Width, size_t const Height, const uint8_t *Data) override {
|
||||
width = static_cast<int>(Width);
|
||||
height = static_cast<int>(Height);
|
||||
pixels.assign(Data, Data + Width * Height * 4);
|
||||
++uploads;
|
||||
}
|
||||
|
||||
bool ready{true};
|
||||
size_t id{1};
|
||||
int width{0};
|
||||
int height{0};
|
||||
int uploads{0};
|
||||
std::vector<std::uint8_t> pixels;
|
||||
};
|
||||
|
||||
// a folder of its own for the scripts of a test, removed with the object. it's made in the build folder of the tests
|
||||
// (EU07_UNIT_TESTS_SCRATCH, see CMakeLists.txt), not in the system's temporary folder, which anyone can write to. the
|
||||
// scripts are one level down in it, so a file a test puts next to their folder ("../name") is still inside the folder
|
||||
// of the test, and is removed with it
|
||||
class script_folder {
|
||||
public:
|
||||
explicit script_folder(std::string const &Name)
|
||||
: m_root{std::filesystem::path{EU07_UNIT_TESTS_SCRATCH} / std::format("eu07_script_screen_test_{}", Name)}, m_folder{m_root / "scripts"} {
|
||||
std::filesystem::remove_all(m_root);
|
||||
std::filesystem::create_directories(m_folder);
|
||||
}
|
||||
~script_folder() {
|
||||
std::error_code error;
|
||||
std::filesystem::remove_all(m_root, error);
|
||||
}
|
||||
script_folder(script_folder const &) = delete;
|
||||
script_folder &operator=(script_folder const &) = delete;
|
||||
|
||||
// writes the file, returns its path as the game names scripts
|
||||
std::string write(std::string const &Name, std::span<std::uint8_t const> const Bytes) const {
|
||||
auto const path{m_folder / Name};
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
std::ofstream file{path, std::ios::binary};
|
||||
file.write(reinterpret_cast<char const *>(Bytes.data()), static_cast<std::streamsize>(Bytes.size()));
|
||||
return path.generic_string();
|
||||
}
|
||||
std::string write(std::string const &Name, std::string_view const Text) const {
|
||||
return write(Name, std::span{reinterpret_cast<std::uint8_t const *>(Text.data()), Text.size()});
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path m_root; // the folder of the test
|
||||
std::filesystem::path m_folder; // the folder of its scripts
|
||||
};
|
||||
|
||||
// runs frames until the condition holds, or gives up after a few seconds
|
||||
bool run_until(script_screen_queue &Queue, std::function<bool()> const &Condition) {
|
||||
auto const deadline{std::chrono::steady_clock::now() + std::chrono::seconds(20)};
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
Queue.update();
|
||||
if (Condition()) {
|
||||
return true;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::shared_ptr<dictionary_source> speed(double const Value) {
|
||||
auto input{std::make_shared<dictionary_source>()};
|
||||
input->insert("speed", Value);
|
||||
return input;
|
||||
}
|
||||
|
||||
script_screen_queue::screen_request request(script_language const Language, std::string const &Script, std::string const &Key, ITexture *Texture,
|
||||
script_screen_kind const Kind = script_screen_kind::cab) {
|
||||
return {.kind = Kind, .language = Language, .script = Script, .key = Key, .texture = Texture, .frames = 0};
|
||||
}
|
||||
|
||||
// submits a refresh as soon as the screen is due
|
||||
void refresh(script_screen_queue &Queue, std::shared_ptr<script_screen> const &Screen, std::shared_ptr<dictionary_source> Input, std::uint32_t const Vehicle) {
|
||||
REQUIRE(run_until(Queue, [&Queue, &Screen] { return Queue.due(*Screen); }));
|
||||
Queue.submit(Screen, std::move(Input), Vehicle);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("script screens: a language that doesn't run gets no screens") {
|
||||
script_screen_queue queue;
|
||||
// not started
|
||||
CHECK_FALSE(queue.runs(script_language::lua));
|
||||
CHECK(queue.acquire(request(script_language::lua, "x.lua", "x", nullptr)) == nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: the language of a script is told by its extension") {
|
||||
CHECK(script_language_of("scripts/led_111a20.lua") == script_language::lua);
|
||||
CHECK(script_language_of("dynamic/pkp/x/ekran.wasm") == script_language::wasm);
|
||||
CHECK_FALSE(script_language_of("scripts/led_111a20").has_value());
|
||||
}
|
||||
|
||||
#ifdef WITH_WASM
|
||||
|
||||
namespace {
|
||||
|
||||
// the modules, encoded by hand. "paint": a 2x2 screen in the colour 0xFF0000vv, vv being the input value
|
||||
// "speed", which sends the command "cmdtest" with the parameters (vv, 7):
|
||||
// (import "eu07" "get_float" (func $get_float (param i32 i32) (result f64)))
|
||||
// (import "eu07" "command" (func $command (param i32 i32 i32 i32)))
|
||||
// (memory (export "memory") 1)
|
||||
// (data (i32.const 0) "speed") (data (i32.const 16) "cmdtest")
|
||||
// (func (export "screen_width") (result i32) i32.const 2)
|
||||
// (func (export "screen_height") (result i32) i32.const 2)
|
||||
// (func (export "screen_render") (result i32) (local $v i32)
|
||||
// (local.set $v (i32.trunc_f64_s (call $get_float (i32.const 0) (i32.const 5))))
|
||||
// (i32.store (i32.const 1024) (i32.or (local.get $v) (i32.const 0xFF000000))) ... the same at 1028, 1032, 1036
|
||||
// (call $command (i32.const 16) (i32.const 7) (local.get $v) (i32.const 7))
|
||||
// (i32.const 1024))
|
||||
constexpr std::array<std::uint8_t, 254> paint_module{
|
||||
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x12, 0x03, 0x60, 0x02, 0x7f, 0x7f, 0x01,
|
||||
0x7c, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, 0x00, 0x60, 0x00, 0x01, 0x7f, 0x02, 0x21, 0x02, 0x04,
|
||||
0x65, 0x75, 0x30, 0x37, 0x09, 0x67, 0x65, 0x74, 0x5f, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x00, 0x00,
|
||||
0x04, 0x65, 0x75, 0x30, 0x37, 0x07, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x00, 0x01, 0x03,
|
||||
0x04, 0x03, 0x02, 0x02, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x39, 0x04, 0x06, 0x6d, 0x65,
|
||||
0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0c, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x5f, 0x77, 0x69,
|
||||
0x64, 0x74, 0x68, 0x00, 0x02, 0x0d, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x5f, 0x68, 0x65, 0x69,
|
||||
0x67, 0x68, 0x74, 0x00, 0x03, 0x0d, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x5f, 0x72, 0x65, 0x6e,
|
||||
0x64, 0x65, 0x72, 0x00, 0x04, 0x0a, 0x5e, 0x03, 0x04, 0x00, 0x41, 0x02, 0x0b, 0x04, 0x00, 0x41,
|
||||
0x02, 0x0b, 0x52, 0x01, 0x01, 0x7f, 0x41, 0x00, 0x41, 0x05, 0x10, 0x00, 0xaa, 0x21, 0x00, 0x41,
|
||||
0x80, 0x08, 0x20, 0x00, 0x41, 0x80, 0x80, 0x80, 0x78, 0x72, 0x36, 0x02, 0x00, 0x41, 0x84, 0x08,
|
||||
0x20, 0x00, 0x41, 0x80, 0x80, 0x80, 0x78, 0x72, 0x36, 0x02, 0x00, 0x41, 0x88, 0x08, 0x20, 0x00,
|
||||
0x41, 0x80, 0x80, 0x80, 0x78, 0x72, 0x36, 0x02, 0x00, 0x41, 0x8c, 0x08, 0x20, 0x00, 0x41, 0x80,
|
||||
0x80, 0x80, 0x78, 0x72, 0x36, 0x02, 0x00, 0x41, 0x10, 0x41, 0x07, 0x20, 0x00, 0x41, 0x07, 0x10,
|
||||
0x01, 0x41, 0x80, 0x08, 0x0b, 0x0b, 0x17, 0x02, 0x00, 0x41, 0x00, 0x0b, 0x05, 0x73, 0x70, 0x65,
|
||||
0x65, 0x64, 0x00, 0x41, 0x10, 0x0b, 0x07, 0x63, 0x6d, 0x64, 0x74, 0x65, 0x73, 0x74};
|
||||
|
||||
// "spin": exports the same functions as "paint", but screen_render never returns: (loop $l (br $l))
|
||||
constexpr std::array<std::uint8_t, 108> spin_module{
|
||||
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f, 0x03,
|
||||
0x04, 0x03, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07, 0x39, 0x04, 0x06, 0x6d, 0x65,
|
||||
0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0c, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x5f, 0x77, 0x69,
|
||||
0x64, 0x74, 0x68, 0x00, 0x00, 0x0d, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x5f, 0x68, 0x65, 0x69,
|
||||
0x67, 0x68, 0x74, 0x00, 0x01, 0x0d, 0x73, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x5f, 0x72, 0x65, 0x6e,
|
||||
0x64, 0x65, 0x72, 0x00, 0x02, 0x0a, 0x15, 0x03, 0x04, 0x00, 0x41, 0x02, 0x0b, 0x04, 0x00, 0x41,
|
||||
0x02, 0x0b, 0x09, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x41, 0x00, 0x0b};
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("wasm screens: a cab screen draws from its input and sends commands to its vehicle") {
|
||||
sent_commands().clear();
|
||||
script_folder const folder{"wasm_paint"};
|
||||
auto const module{folder.write("paint.wasm", paint_module)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
REQUIRE(queue.runs(script_language::wasm));
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::wasm, module, "cab", &texture))};
|
||||
REQUIRE(screen != nullptr);
|
||||
// the same key, the same screen
|
||||
CHECK(queue.acquire(request(script_language::wasm, module, "cab", &texture)) == screen);
|
||||
|
||||
refresh(queue, screen, speed(5.0), 42);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0 && false == sent_commands().empty(); }));
|
||||
|
||||
CHECK(texture.width == 2);
|
||||
CHECK(texture.height == 2);
|
||||
REQUIRE(texture.pixels.size() == 16);
|
||||
std::array<std::uint8_t, 4> const expected{0x05, 0x00, 0x00, 0xFF};
|
||||
for (std::size_t pixel = 0; pixel < 4; ++pixel) {
|
||||
CAPTURE(pixel);
|
||||
CHECK(std::equal(expected.begin(), expected.end(), texture.pixels.begin() + static_cast<std::ptrdiff_t>(pixel * 4)));
|
||||
}
|
||||
|
||||
REQUIRE(sent_commands().size() == 1);
|
||||
auto const &[command, recipient]{sent_commands().front()};
|
||||
CHECK(command.command == user_command::aidriverenable);
|
||||
CHECK(command.param1 == 5.0);
|
||||
CHECK(command.param2 == 7.0);
|
||||
CHECK(recipient == (static_cast<std::uint32_t>(command_target::vehicle) | 42u));
|
||||
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("wasm screens: the image goes up again when the texture is made anew, and waits while it isn't there") {
|
||||
sent_commands().clear();
|
||||
script_folder const folder{"wasm_reupload"};
|
||||
auto const module{folder.write("paint.wasm", paint_module)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
texture.ready = false;
|
||||
auto const screen{queue.acquire(request(script_language::wasm, module, "cab", &texture))};
|
||||
refresh(queue, screen, speed(9.0), 1);
|
||||
REQUIRE(run_until(queue, [] { return false == sent_commands().empty(); }));
|
||||
queue.update();
|
||||
CHECK(texture.uploads == 0);
|
||||
|
||||
texture.ready = true;
|
||||
queue.update();
|
||||
CHECK(texture.uploads == 1);
|
||||
queue.update();
|
||||
CHECK(texture.uploads == 1);
|
||||
|
||||
// released and made again: a new gl texture
|
||||
texture.id = 2;
|
||||
queue.update();
|
||||
CHECK(texture.uploads == 2);
|
||||
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("wasm screens: a screen outside the cab draws, but its commands are dropped") {
|
||||
sent_commands().clear();
|
||||
script_folder const folder{"wasm_sign"};
|
||||
auto const module{folder.write("paint.wasm", paint_module)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::wasm, module, "sign", &texture, script_screen_kind::external))};
|
||||
refresh(queue, screen, speed(3.0), 0);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
queue.update();
|
||||
CHECK(sent_commands().empty());
|
||||
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("wasm screens: a screen stuck in a loop is stopped, and the others keep working") {
|
||||
script_folder const folder{"wasm_spin"};
|
||||
auto const spinning{folder.write("spin.wasm", spin_module)};
|
||||
auto const painting{folder.write("paint.wasm", paint_module)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture stuck;
|
||||
recording_texture working;
|
||||
auto const first{queue.acquire(request(script_language::wasm, spinning, "spin", &stuck))};
|
||||
auto const second{queue.acquire(request(script_language::wasm, painting, "paint", &working))};
|
||||
refresh(queue, first, speed(1.0), 1);
|
||||
refresh(queue, second, speed(2.0), 1);
|
||||
|
||||
REQUIRE(run_until(queue, [&working] { return working.uploads > 0; }));
|
||||
CHECK(stuck.uploads == 0);
|
||||
// the stuck screen ran first, into the instruction limit, and isn't due any more
|
||||
CHECK_FALSE(queue.due(*first));
|
||||
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("wasm screens: the screens outlive the queue without harm") {
|
||||
script_folder const folder{"wasm_outlive"};
|
||||
auto const module{folder.write("paint.wasm", paint_module)};
|
||||
std::shared_ptr<script_screen> screen;
|
||||
recording_texture texture;
|
||||
{
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
screen = queue.acquire(request(script_language::wasm, module, "cab", &texture));
|
||||
refresh(queue, screen, speed(4.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
}
|
||||
// the queue and the runtime are gone, the vehicle still holds its screen
|
||||
CHECK(screen != nullptr);
|
||||
screen.reset();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef WITH_LUA
|
||||
|
||||
#include "stb/stb_image.h"
|
||||
#include "stb/stb_image_write.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// the game's texture loader turns every image stb_image reads bottom row first (model/Texture.cpp); for the length of
|
||||
// a test, the same here
|
||||
class images_flipped_as_in_the_game {
|
||||
public:
|
||||
images_flipped_as_in_the_game() { stbi_set_flip_vertically_on_load(1); }
|
||||
~images_flipped_as_in_the_game() { stbi_set_flip_vertically_on_load(0); }
|
||||
images_flipped_as_in_the_game(images_flipped_as_in_the_game const &) = delete;
|
||||
images_flipped_as_in_the_game &operator=(images_flipped_as_in_the_game const &) = delete;
|
||||
};
|
||||
|
||||
// a 2x2 screen in the colour (speed, 0, 0), sending the command "cmdtest" with (speed, 7)
|
||||
constexpr std::string_view paint_script{R"(
|
||||
local screen = {}
|
||||
function screen.render(state)
|
||||
eu07.command("cmdtest", state.speed, 7)
|
||||
return eu07.image(2, 2, { state.speed, 0, 0 })
|
||||
end
|
||||
return screen
|
||||
)"};
|
||||
|
||||
// 1x2: a red row above a green one
|
||||
constexpr std::string_view rows_script{R"(
|
||||
return {
|
||||
render = function(state)
|
||||
local image = eu07.image(1, 2, { 0, 0, 0 })
|
||||
image:set(0, 0, { 255, 0, 0 })
|
||||
image:set(0, 1, { 0, 255, 0 })
|
||||
return image
|
||||
end
|
||||
}
|
||||
)"};
|
||||
|
||||
// 1x1, white when nothing of the world outside the sandbox is reachable, black otherwise
|
||||
constexpr std::string_view sandbox_script{R"(
|
||||
local reachable = io or os or ffi or debug or package or require or load or loadstring or dofile or loadfile or string.dump or jit
|
||||
local escapes = eu07.read_file("../outside.txt") or eu07.read_file("/etc/hosts") or eu07.read_file("c:/windows/win.ini")
|
||||
local inside = eu07.read_file("data/inside.txt")
|
||||
local ok = not reachable and not escapes and inside == "inside"
|
||||
return {
|
||||
render = function(state)
|
||||
return eu07.image(1, 1, ok and { 255, 255, 255 } or { 0, 0, 0 })
|
||||
end
|
||||
}
|
||||
)"};
|
||||
|
||||
// never returns from render
|
||||
constexpr std::string_view spin_script{R"(
|
||||
return { render = function(state) while true do end end }
|
||||
)"};
|
||||
|
||||
// draws once, then reports nothing new
|
||||
constexpr std::string_view once_script{R"(
|
||||
local drawn = false
|
||||
return {
|
||||
render = function(state)
|
||||
if drawn then return nil end
|
||||
drawn = true
|
||||
return eu07.image(1, 1, { 1, 2, 3 })
|
||||
end
|
||||
}
|
||||
)"};
|
||||
|
||||
// uses a helper script and a font next to it
|
||||
constexpr std::string_view include_script{R"(
|
||||
local helper = eu07.include("helper.lua")
|
||||
local font = eu07.font("font/test")
|
||||
return {
|
||||
render = function(state)
|
||||
local image = eu07.image(8, 2, { 0, 0, 0 })
|
||||
local x = image:draw_text(font, 0, 0, helper.text, { 255, 0, 0 })
|
||||
image:set(7, 1, { x, font:text_width("ab"), font:height() })
|
||||
return image
|
||||
end
|
||||
}
|
||||
)"};
|
||||
|
||||
constexpr std::string_view helper_script{R"(
|
||||
return { text = "ab" }
|
||||
)"};
|
||||
|
||||
// a font of two glyphs a column apart: "a" two columns wide, lit in its top row, "b" one column, lit in both rows.
|
||||
// written as a .png by stb_image_write, the way the game's fonts are
|
||||
std::vector<std::uint8_t> test_font_png() {
|
||||
constexpr int width{4};
|
||||
constexpr int height{2};
|
||||
std::array<std::uint8_t, width * height * 4> pixels{};
|
||||
pixels.fill(255);
|
||||
auto const lit{[&pixels](int const X, int const Y) { pixels[(Y * width + X) * 4] = 0; }};
|
||||
lit(0, 0);
|
||||
lit(1, 0);
|
||||
lit(3, 0);
|
||||
lit(3, 1);
|
||||
std::vector<std::uint8_t> result;
|
||||
auto const append{[](void *Target, void *Data, int const Size) {
|
||||
auto *bytes{static_cast<std::uint8_t const *>(Data)};
|
||||
static_cast<std::vector<std::uint8_t> *>(Target)->insert(static_cast<std::vector<std::uint8_t> *>(Target)->end(), bytes, bytes + Size);
|
||||
}};
|
||||
stbi_write_png_to_func(append, &result, width, height, 4, pixels.data(), width * 4);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("lua screens: a cab screen draws from its input and sends commands to its vehicle") {
|
||||
sent_commands().clear();
|
||||
script_folder const folder{"lua_paint"};
|
||||
auto const script{folder.write("paint.lua", paint_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
REQUIRE(queue.runs(script_language::lua));
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "cab", &texture))};
|
||||
REQUIRE(screen != nullptr);
|
||||
refresh(queue, screen, speed(5.0), 42);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0 && false == sent_commands().empty(); }));
|
||||
|
||||
CHECK(texture.width == 2);
|
||||
CHECK(texture.height == 2);
|
||||
REQUIRE(texture.pixels.size() == 16);
|
||||
CHECK(texture.pixels[0] == 5);
|
||||
CHECK(texture.pixels[1] == 0);
|
||||
CHECK(texture.pixels[3] == 255);
|
||||
|
||||
REQUIRE(sent_commands().size() == 1);
|
||||
auto const &[command, recipient]{sent_commands().front()};
|
||||
CHECK(command.command == user_command::aidriverenable);
|
||||
CHECK(command.param1 == 5.0);
|
||||
CHECK(command.param2 == 7.0);
|
||||
CHECK(recipient == (static_cast<std::uint32_t>(command_target::vehicle) | 42u));
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: the texture gets the bottom row first, as from the python screens") {
|
||||
script_folder const folder{"lua_rows"};
|
||||
auto const script{folder.write("rows.lua", rows_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "rows", &texture))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
REQUIRE(texture.pixels.size() == 8);
|
||||
// green, the bottom row, first
|
||||
CHECK(texture.pixels[0] == 0);
|
||||
CHECK(texture.pixels[1] == 255);
|
||||
CHECK(texture.pixels[4] == 255);
|
||||
CHECK(texture.pixels[5] == 0);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: a script reaches no files outside its folder, nor the system, nor ffi") {
|
||||
script_folder const folder{"lua_sandbox"};
|
||||
// next to the folder of the scripts, where the script mustn't reach
|
||||
folder.write("../outside.txt", std::string_view{"outside"});
|
||||
folder.write("data/inside.txt", std::string_view{"inside"});
|
||||
auto const script{folder.write("sandbox.lua", sandbox_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "sandbox", &texture))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
REQUIRE(texture.pixels.size() == 4);
|
||||
CHECK(texture.pixels[0] == 255);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: precompiled code isn't loaded") {
|
||||
script_folder const folder{"lua_bytecode"};
|
||||
// LuaJIT bytecode starts with ESC "LJ"
|
||||
auto const script{folder.write("compiled.lua", std::string_view{"\x1bLJ\x02\x00 not a script"})};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "compiled", &texture))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
// it fails to load, and isn't refreshed again: a screen that runs would be due again within a few frames
|
||||
for (int frame = 0; frame < 40; ++frame) {
|
||||
queue.update();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
CHECK_FALSE(queue.due(*screen));
|
||||
CHECK(texture.uploads == 0);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: a script stuck in a loop is stopped, and the others keep working") {
|
||||
script_folder const folder{"lua_spin"};
|
||||
auto const spinning{folder.write("spin.lua", spin_script)};
|
||||
auto const painting{folder.write("paint.lua", paint_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture stuck;
|
||||
recording_texture working;
|
||||
auto const first{queue.acquire(request(script_language::lua, spinning, "spin", &stuck))};
|
||||
auto const second{queue.acquire(request(script_language::lua, painting, "paint", &working))};
|
||||
refresh(queue, first, speed(1.0), 1);
|
||||
refresh(queue, second, speed(2.0), 1);
|
||||
|
||||
REQUIRE(run_until(queue, [&working] { return working.uploads > 0; }));
|
||||
CHECK(stuck.uploads == 0);
|
||||
CHECK_FALSE(queue.due(*first));
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: nil from render keeps the image there is") {
|
||||
script_folder const folder{"lua_once"};
|
||||
auto const script{folder.write("once.lua", once_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "once", &texture))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
for (int frame = 0; frame < 20; ++frame) {
|
||||
queue.update();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
CHECK(texture.uploads == 1);
|
||||
CHECK(texture.pixels[2] == 3);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: a script includes another and reads a font of the python screens' format") {
|
||||
script_folder const folder{"lua_include"};
|
||||
auto const script{folder.write("include.lua", include_script)};
|
||||
folder.write("helper.lua", helper_script);
|
||||
folder.write("font/test.png", test_font_png());
|
||||
folder.write("font/test.txt", std::string_view{"// a comment line\r\na2\r\nb1\r\n"});
|
||||
// the fonts are read top row first all the same
|
||||
images_flipped_as_in_the_game const flipped;
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "include", &texture))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
REQUIRE(texture.pixels.size() == 8 * 2 * 4);
|
||||
// bottom row first: the top row of the image is the second row of the texture
|
||||
auto const top{[&texture](int const X) { return texture.pixels[(8 + X) * 4]; }};
|
||||
auto const bottom{[&texture](int const X) { return texture.pixels[X * 4]; }};
|
||||
CHECK(top(0) == 255);
|
||||
CHECK(top(1) == 255);
|
||||
CHECK(top(2) == 0); // the spacing column
|
||||
CHECK(top(3) == 255);
|
||||
CHECK(bottom(0) == 0);
|
||||
CHECK(bottom(3) == 255);
|
||||
// the x after the text (2 + 1 + 1 + 1), its width and the font's height
|
||||
CHECK(texture.pixels[7 * 4] == 5);
|
||||
CHECK(texture.pixels[7 * 4 + 1] == 5);
|
||||
CHECK(texture.pixels[7 * 4 + 2] == 2);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// 2x1, red and green, shown through led_post without the glow and a resize to 4x3
|
||||
constexpr std::string_view posted_script{R"(
|
||||
return {
|
||||
render = function(state)
|
||||
local image = eu07.image(2, 1, { 255, 0, 0 })
|
||||
image:set(1, 0, { 0, 255, 0 })
|
||||
return image:led_post(false):resize(4, 3)
|
||||
end
|
||||
}
|
||||
)"};
|
||||
|
||||
// reads an image after led_post: its size, a pixel of the frame and one of a diode
|
||||
constexpr std::string_view read_posted_script{R"(
|
||||
return {
|
||||
render = function(state)
|
||||
local posted = eu07.image(2, 1, { 255, 255, 255 }):led_post(false)
|
||||
local width, height = posted:width(), posted:height()
|
||||
local frame = posted:get(0, 0)
|
||||
local diode = posted:get(1, 1)
|
||||
return eu07.image(1, 1, { width * 10 + height, frame, diode })
|
||||
end
|
||||
}
|
||||
)"};
|
||||
|
||||
// a gpu that keeps what it was given, or one that can't do it
|
||||
struct recording_processor : script_screens::post_processor {
|
||||
bool apply(ITexture & /*Target*/, std::span<std::uint8_t const> const Pixels, int const Width, int const Height,
|
||||
std::span<script_screens::post_step const> const Steps) override {
|
||||
++calls;
|
||||
pixels.assign(Pixels.begin(), Pixels.end());
|
||||
width = Width;
|
||||
height = Height;
|
||||
steps.assign(Steps.begin(), Steps.end());
|
||||
return works;
|
||||
}
|
||||
|
||||
bool works{true};
|
||||
int calls{0};
|
||||
std::vector<std::uint8_t> pixels;
|
||||
int width{0};
|
||||
int height{0};
|
||||
std::vector<script_screens::post_step> steps;
|
||||
};
|
||||
|
||||
// what posted_script shows, done on the cpu, bottom row first
|
||||
std::vector<std::uint8_t> posted_on_cpu() {
|
||||
script_screens::post_step led;
|
||||
led.type = script_screens::post_step::kind::led;
|
||||
led.bloom = false;
|
||||
script_screens::post_step resize;
|
||||
resize.type = script_screens::post_step::kind::resize;
|
||||
resize.width = 4;
|
||||
resize.height = 3;
|
||||
std::array const steps{led, resize};
|
||||
script_screens::post_image image{{255, 0, 0, 255, 0, 255, 0, 255}, 2, 1};
|
||||
REQUIRE(script_screens::apply_post(image, steps));
|
||||
std::vector<std::uint8_t> result;
|
||||
for (auto row = image.height - 1; row >= 0; --row) {
|
||||
auto const first{image.pixels.begin() + static_cast<std::ptrdiff_t>(script_screens::image_size(image.width, row))};
|
||||
result.insert(result.end(), first, first + image.width * 4);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("lua screens: led_post and resize of the image shown go to the gpu, with the image as drawn") {
|
||||
script_folder const folder{"lua_post_gpu"};
|
||||
auto const script{folder.write("posted.lua", posted_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
recording_processor gpu;
|
||||
queue.set_post_processor(&gpu);
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "posted", &texture, script_screen_kind::external))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&gpu] { return gpu.calls > 0; }));
|
||||
CHECK(texture.uploads == 0);
|
||||
CHECK(gpu.width == 2);
|
||||
CHECK(gpu.height == 1);
|
||||
CHECK(gpu.pixels == std::vector<std::uint8_t>{255, 0, 0, 255, 0, 255, 0, 255});
|
||||
REQUIRE(gpu.steps.size() == 2);
|
||||
CHECK(gpu.steps[0].type == script_screens::post_step::kind::led);
|
||||
CHECK_FALSE(gpu.steps[0].bloom);
|
||||
CHECK(gpu.steps[1].type == script_screens::post_step::kind::resize);
|
||||
CHECK(gpu.steps[1].width == 4);
|
||||
CHECK(gpu.steps[1].height == 3);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: without a gpu, led_post and resize run on the cpu") {
|
||||
script_folder const folder{"lua_post_cpu"};
|
||||
auto const script{folder.write("posted.lua", posted_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "posted", &texture, script_screen_kind::external))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
CHECK(texture.width == 4);
|
||||
CHECK(texture.height == 3);
|
||||
CHECK(texture.pixels == posted_on_cpu());
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: when the gpu can't do the post-processing, the cpu does it from then on") {
|
||||
script_folder const folder{"lua_post_fallback"};
|
||||
auto const script{folder.write("posted.lua", posted_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
recording_processor gpu;
|
||||
gpu.works = false;
|
||||
queue.set_post_processor(&gpu);
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "posted", &texture, script_screen_kind::external))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
CHECK(gpu.calls == 1);
|
||||
CHECK(texture.width == 4);
|
||||
CHECK(texture.pixels == posted_on_cpu());
|
||||
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 1; }));
|
||||
CHECK(gpu.calls == 1);
|
||||
CHECK(texture.pixels == posted_on_cpu());
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
TEST_CASE("lua screens: an image read after led_post has its post-processing done first") {
|
||||
script_folder const folder{"lua_post_read"};
|
||||
auto const script{folder.write("read.lua", read_posted_script)};
|
||||
script_screen_queue queue;
|
||||
REQUIRE(queue.init());
|
||||
recording_processor gpu;
|
||||
queue.set_post_processor(&gpu);
|
||||
|
||||
recording_texture texture;
|
||||
auto const screen{queue.acquire(request(script_language::lua, script, "read", &texture, script_screen_kind::external))};
|
||||
refresh(queue, screen, speed(0.0), 1);
|
||||
REQUIRE(run_until(queue, [&texture] { return texture.uploads > 0; }));
|
||||
CHECK(gpu.calls == 0);
|
||||
REQUIRE(texture.pixels.size() == 4);
|
||||
CHECK(texture.pixels[0] == 64); // 6 x 4
|
||||
CHECK(texture.pixels[1] == 0);
|
||||
CHECK(texture.pixels[2] == 255);
|
||||
queue.exit();
|
||||
}
|
||||
|
||||
#endif
|
||||
154
tests/unit/test_script_screen_kernels.cpp
Normal file
154
tests/unit/test_script_screen_kernels.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "scripting/scriptscreenkernels.h"
|
||||
|
||||
#include <array>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
// the image filters the script screens call through the host give the pixels of the Python screens: led_util's
|
||||
// render_post() and Pillow's resize(..., Image.LANCZOS). the expected images were made by them (Pillow 12.2) from a
|
||||
// 5x3 image of (x*53 + y*97, x*31 + y*17 + 100, x*7 + y*151 + 40) mod 256
|
||||
|
||||
namespace {
|
||||
|
||||
// RGB as Pillow gives it
|
||||
constexpr std::array<std::uint8_t, 45> source{
|
||||
0, 100, 40, 53, 131, 47, 106, 162, 54, 159, 193, 61, 212, 224, 68, 97, 117, 191, 150, 148, 198, 203, 179, 205,
|
||||
0, 210, 212, 53, 241, 219, 194, 134, 86, 247, 165, 93, 44, 196, 100, 97, 227, 107, 150, 2, 114,
|
||||
};
|
||||
constexpr std::array<std::uint8_t, 288> posted{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 144, 77, 28, 122, 65, 99, 194, 97,
|
||||
84, 164, 82, 158, 236, 108, 134, 200, 91, 205, 255, 114, 174, 229, 96, 241, 255, 109, 204, 238, 92, 0, 0, 0,
|
||||
0, 0, 0, 28, 122, 65, 23, 100, 53, 84, 164, 82, 69, 135, 67, 134, 200, 91, 110, 165, 75, 174, 229, 96,
|
||||
143, 189, 79, 204, 238, 92, 168, 196, 76, 0, 0, 0, 0, 0, 0, 144, 173, 231, 122, 147, 196, 215, 229, 255,
|
||||
182, 194, 219, 255, 255, 255, 219, 232, 230, 67, 255, 255, 56, 255, 235, 101, 255, 255, 85, 255, 225, 0, 0, 0,
|
||||
0, 0, 0, 122, 147, 196, 100, 121, 161, 182, 194, 219, 150, 160, 180, 219, 232, 230, 180, 191, 189, 56, 255, 235,
|
||||
46, 213, 193, 85, 255, 225, 70, 212, 185, 0, 0, 0, 0, 0, 0, 234, 178, 126, 198, 151, 107, 255, 231, 148,
|
||||
251, 196, 125, 106, 255, 159, 90, 230, 135, 139, 255, 165, 118, 248, 140, 177, 65, 158, 150, 55, 134, 0, 0, 0,
|
||||
0, 0, 0, 198, 151, 107, 163, 124, 88, 251, 196, 125, 207, 161, 103, 90, 230, 135, 74, 189, 111, 118, 248, 140,
|
||||
97, 204, 115, 150, 55, 134, 123, 45, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
};
|
||||
constexpr std::array<std::uint8_t, 561> enlarged{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 12, 46, 26, 22, 90, 53, 18, 71, 41, 50, 102, 55, 58, 108, 58, 53, 98, 52,
|
||||
88, 131, 65, 87, 128, 62, 84, 119, 58, 114, 144, 69, 102, 139, 63, 106, 133, 60, 136, 144, 67, 139, 144, 68,
|
||||
56, 71, 29, 0, 0, 0, 0, 0, 0, 16, 85, 39, 25, 165, 76, 21, 130, 59, 80, 188, 84, 94, 198, 89,
|
||||
86, 178, 78, 155, 246, 102, 150, 236, 97, 151, 221, 89, 232, 255, 112, 210, 255, 100, 213, 250, 94, 255, 255, 108,
|
||||
255, 255, 107, 113, 129, 44, 0, 0, 0, 0, 0, 0, 19, 67, 38, 31, 130, 74, 25, 100, 57, 72, 148, 78,
|
||||
83, 155, 82, 75, 137, 72, 128, 191, 92, 124, 181, 87, 119, 172, 81, 167, 225, 99, 146, 202, 89, 151, 197, 85,
|
||||
200, 238, 97, 198, 235, 95, 79, 96, 39, 0, 0, 0, 0, 0, 0, 70, 90, 108, 135, 177, 217, 108, 140, 171,
|
||||
172, 198, 208, 188, 211, 215, 167, 186, 192, 218, 235, 219, 227, 238, 214, 175, 222, 204, 95, 245, 222, 62, 245, 213,
|
||||
89, 240, 209, 122, 244, 224, 123, 243, 224, 49, 124, 99, 0, 0, 0, 0, 0, 0, 74, 89, 130, 139, 174, 255,
|
||||
110, 136, 201, 182, 194, 246, 197, 205, 253, 170, 182, 223, 244, 239, 255, 255, 234, 252, 186, 218, 236, 52, 255, 255,
|
||||
15, 245, 249, 49, 244, 240, 72, 255, 255, 73, 255, 255, 29, 130, 113, 0, 0, 0, 0, 0, 0, 76, 82, 96,
|
||||
147, 160, 188, 114, 123, 145, 166, 177, 183, 181, 187, 188, 163, 165, 163, 186, 224, 205, 188, 214, 191, 140, 200, 176,
|
||||
80, 251, 215, 54, 228, 190, 75, 211, 179, 105, 219, 206, 106, 217, 204, 42, 87, 82, 0, 0, 0, 0, 0, 0,
|
||||
124, 96, 69, 250, 189, 137, 195, 149, 108, 225, 207, 138, 250, 221, 143, 237, 195, 127, 134, 244, 156, 80, 245, 148,
|
||||
103, 228, 139, 135, 248, 165, 120, 252, 150, 129, 193, 143, 175, 68, 160, 178, 59, 161, 72, 34, 66, 0, 0, 0,
|
||||
0, 0, 0, 135, 101, 67, 255, 197, 133, 206, 153, 103, 255, 216, 134, 255, 228, 139, 247, 200, 122, 127, 255, 152,
|
||||
67, 255, 144, 98, 237, 134, 147, 255, 162, 131, 255, 146, 138, 187, 138, 189, 48, 157, 190, 37, 156, 76, 26, 64,
|
||||
0, 0, 0, 0, 0, 0, 57, 44, 32, 110, 85, 64, 84, 65, 48, 121, 94, 63, 140, 98, 65, 102, 86, 57,
|
||||
60, 120, 72, 39, 112, 68, 46, 103, 62, 61, 134, 76, 52, 133, 68, 56, 82, 64, 78, 34, 74, 77, 29, 73,
|
||||
31, 16, 29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
};
|
||||
constexpr std::array<std::uint8_t, 105> reduced{
|
||||
2, 22, 10, 14, 58, 25, 35, 70, 30, 53, 83, 33, 74, 90, 34, 95, 99, 36, 37, 40, 13, 17, 60, 41,
|
||||
63, 155, 101, 115, 186, 110, 157, 217, 117, 161, 234, 120, 207, 255, 127, 81, 103, 47, 52, 64, 91, 142, 165, 218,
|
||||
186, 193, 220, 217, 219, 227, 58, 229, 227, 70, 251, 241, 32, 101, 90, 92, 72, 57, 230, 184, 137, 233, 214, 144,
|
||||
110, 238, 155, 114, 255, 157, 158, 131, 166, 64, 23, 60, 32, 24, 16, 84, 60, 38, 82, 70, 41, 27, 79, 44,
|
||||
40, 90, 45, 56, 29, 47, 22, 2, 17,
|
||||
};
|
||||
constexpr std::array<std::uint8_t, 288> plain{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 40, 0, 85, 34, 53, 131, 47,
|
||||
45, 111, 39, 106, 162, 54, 90, 137, 45, 159, 193, 61, 135, 164, 51, 212, 224, 68, 180, 190, 57, 0, 0, 0,
|
||||
0, 0, 0, 0, 85, 34, 0, 70, 28, 45, 111, 39, 37, 91, 32, 90, 137, 45, 74, 113, 37, 135, 164, 51,
|
||||
111, 135, 42, 180, 190, 57, 148, 156, 47, 0, 0, 0, 0, 0, 0, 97, 117, 191, 82, 99, 162, 150, 148, 198,
|
||||
127, 125, 168, 203, 179, 205, 172, 152, 174, 0, 210, 212, 0, 178, 180, 53, 241, 219, 45, 204, 186, 0, 0, 0,
|
||||
0, 0, 0, 82, 99, 162, 67, 81, 133, 127, 125, 168, 105, 103, 138, 172, 152, 174, 142, 125, 143, 0, 178, 180,
|
||||
0, 147, 148, 45, 204, 186, 37, 168, 153, 0, 0, 0, 0, 0, 0, 194, 134, 86, 164, 113, 73, 247, 165, 93,
|
||||
209, 140, 79, 44, 196, 100, 37, 166, 85, 97, 227, 107, 82, 192, 90, 150, 2, 114, 127, 1, 96, 0, 0, 0,
|
||||
0, 0, 0, 164, 113, 73, 135, 93, 60, 209, 140, 79, 172, 115, 65, 37, 166, 85, 30, 137, 70, 82, 192, 90,
|
||||
67, 158, 74, 127, 1, 96, 105, 1, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
};
|
||||
|
||||
std::vector<std::uint8_t> rgba(std::span<std::uint8_t const> const Rgb) {
|
||||
std::vector<std::uint8_t> result;
|
||||
for (std::size_t idx = 0; idx < Rgb.size(); idx += 3) {
|
||||
result.insert(result.end(), {Rgb[idx], Rgb[idx + 1], Rgb[idx + 2], 255});
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("script screen kernels: led_post gives the pixels of led_util.render_post") {
|
||||
auto const input{rgba(source)};
|
||||
std::vector<std::uint8_t> output(script_screens::image_size(12, 8), 7);
|
||||
REQUIRE(script_screens::led_post(input, 5, 3, output, true));
|
||||
CHECK(output == rgba(posted));
|
||||
|
||||
REQUIRE(script_screens::led_post(input, 5, 3, output, false));
|
||||
CHECK(output == rgba(plain));
|
||||
}
|
||||
|
||||
TEST_CASE("script screen kernels: resize_lanczos gives the pixels of Pillow's resize with LANCZOS") {
|
||||
auto const input{rgba(posted)};
|
||||
std::vector<std::uint8_t> larger(script_screens::image_size(17, 11));
|
||||
REQUIRE(script_screens::resize_lanczos(input, 12, 8, larger, 17, 11));
|
||||
CHECK(larger == rgba(enlarged));
|
||||
|
||||
std::vector<std::uint8_t> smaller(script_screens::image_size(7, 5));
|
||||
REQUIRE(script_screens::resize_lanczos(input, 12, 8, smaller, 7, 5));
|
||||
CHECK(smaller == rgba(reduced));
|
||||
}
|
||||
|
||||
TEST_CASE("script screen kernels: images that don't fit their buffers are refused") {
|
||||
std::vector<std::uint8_t> const input(script_screens::image_size(5, 3));
|
||||
std::vector<std::uint8_t> output(script_screens::image_size(12, 8) - 1);
|
||||
CHECK_FALSE(script_screens::led_post(input, 5, 3, output, true));
|
||||
CHECK_FALSE(script_screens::led_post(input, 0, 3, output, true));
|
||||
CHECK_FALSE(script_screens::resize_lanczos(input, 5, 3, output, 12, 8));
|
||||
CHECK_FALSE(script_screens::resize_lanczos(input, 6, 3, output, 2, 2));
|
||||
}
|
||||
|
||||
TEST_CASE("script screen kernels: the post-processing steps of an image run one after another") {
|
||||
script_screens::post_step led;
|
||||
led.type = script_screens::post_step::kind::led;
|
||||
led.bloom = true;
|
||||
script_screens::post_step resize;
|
||||
resize.type = script_screens::post_step::kind::resize;
|
||||
resize.width = 17;
|
||||
resize.height = 11;
|
||||
std::array const steps{led, resize};
|
||||
|
||||
CHECK(script_screens::width_after(led, 5) == 12);
|
||||
CHECK(script_screens::height_after(led, 3) == 8);
|
||||
CHECK(script_screens::width_after(resize, 12) == 17);
|
||||
CHECK(script_screens::height_after(resize, 8) == 11);
|
||||
|
||||
script_screens::post_image image{rgba(source), 5, 3};
|
||||
REQUIRE(script_screens::apply_post(image, steps));
|
||||
CHECK(image.width == 17);
|
||||
CHECK(image.height == 11);
|
||||
CHECK(image.pixels == rgba(enlarged));
|
||||
|
||||
// nothing to do, nothing done
|
||||
script_screens::post_image unchanged{rgba(source), 5, 3};
|
||||
REQUIRE(script_screens::apply_post(unchanged, {}));
|
||||
CHECK(unchanged.pixels == rgba(source));
|
||||
|
||||
// a size the kernels refuse leaves the image as it was
|
||||
script_screens::post_image tooshort{std::vector<std::uint8_t>(10), 5, 3};
|
||||
CHECK_FALSE(script_screens::apply_post(tooshort, steps));
|
||||
CHECK(tooshort.width == 5);
|
||||
CHECK(tooshort.pixels.size() == 10);
|
||||
}
|
||||
103
tests/unit/test_script_screens.cpp
Normal file
103
tests/unit/test_script_screens.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include "stdafx.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include "scripting/scriptscreenrules.h"
|
||||
#include "utilities/parser.h"
|
||||
|
||||
// the parts of the script screens that don't need the runtime: the .mmd entries, the refresh schedule counted in
|
||||
// frames and the files a screen may read
|
||||
|
||||
using script_screens::frame_schedule;
|
||||
using script_screens::screen_config;
|
||||
|
||||
TEST_CASE("script screens: a cab screen entry with every key") {
|
||||
cParser parser("{ ekran.wasm target: screen01 frames: 6 parameters: id=1&mode=full } next:", cParser::buffer_TEXT);
|
||||
screen_config config;
|
||||
config.deserialize(parser);
|
||||
|
||||
CHECK(config.script == "ekran.wasm");
|
||||
CHECK(config.target == "screen01");
|
||||
CHECK(config.frames == 6);
|
||||
CHECK(config.parameters == "id=1&mode=full");
|
||||
// the parser stops at the end of the block
|
||||
CHECK(parser.getToken<std::string>() == "next:");
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: the optional keys keep their defaults, an unknown key is skipped with its value") {
|
||||
cParser parser("{ tablica.wasm later: 12 instance: name background: tablica_off }", cParser::buffer_TEXT);
|
||||
screen_config config;
|
||||
config.deserialize(parser);
|
||||
|
||||
CHECK(config.script == "tablica.wasm");
|
||||
CHECK(config.target == "none");
|
||||
CHECK(config.frames == 0);
|
||||
CHECK(config.parameters.empty());
|
||||
CHECK(config.instancing == "name");
|
||||
CHECK(config.background == "tablica_off");
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: the interval set in eu07.ini, or the screen's own when it is longer") {
|
||||
CHECK(frame_schedule::effective_interval(4, 0) == 4);
|
||||
CHECK(frame_schedule::effective_interval(4, 10) == 10);
|
||||
CHECK(frame_schedule::effective_interval(4, 2) == 4);
|
||||
// never less than every frame
|
||||
CHECK(frame_schedule::effective_interval(0, 0) == 1);
|
||||
CHECK(frame_schedule::effective_interval(-3, -1) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: a screen is due once per interval") {
|
||||
frame_schedule schedule{100, 3, 0};
|
||||
CHECK(schedule.poll(100));
|
||||
// asked again in the same frame (the vehicle drawn in more than one pass)
|
||||
CHECK_FALSE(schedule.poll(100));
|
||||
CHECK_FALSE(schedule.poll(101));
|
||||
CHECK_FALSE(schedule.poll(102));
|
||||
CHECK(schedule.poll(103));
|
||||
CHECK_FALSE(schedule.poll(105));
|
||||
CHECK(schedule.poll(106));
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: a screen out of sight for a while is due on the first frame it is asked again") {
|
||||
frame_schedule schedule{0, 5, 0};
|
||||
CHECK(schedule.poll(0));
|
||||
CHECK(schedule.poll(42));
|
||||
CHECK_FALSE(schedule.poll(46));
|
||||
CHECK(schedule.poll(47));
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: the phase spreads the screens over the frames of an interval") {
|
||||
auto const interval{30};
|
||||
auto const phase{frame_schedule::phase("make:dynamic/pkp/ed72/tablica.wasm?$timetable=ed72-001", interval)};
|
||||
CHECK(phase >= 0);
|
||||
CHECK(phase < interval);
|
||||
// the same name always gets the same phase
|
||||
CHECK(phase == frame_schedule::phase("make:dynamic/pkp/ed72/tablica.wasm?$timetable=ed72-001", interval));
|
||||
// a screen refreshed every frame has no phase
|
||||
CHECK(frame_schedule::phase("anything", 1) == 0);
|
||||
|
||||
frame_schedule schedule{1000, interval, phase};
|
||||
auto first{std::uint64_t{0}};
|
||||
for (auto frame = std::uint64_t{1000}; frame < 1000 + interval; ++frame) {
|
||||
if (schedule.poll(frame)) {
|
||||
first = frame;
|
||||
break;
|
||||
}
|
||||
}
|
||||
CHECK(first == 1000 + static_cast<std::uint64_t>(phase));
|
||||
}
|
||||
|
||||
TEST_CASE("script screens: a screen reads files only below its own folder") {
|
||||
CHECK(script_screens::is_safe_relative_path("font.png"));
|
||||
CHECK(script_screens::is_safe_relative_path("data/font.png"));
|
||||
CHECK(script_screens::is_safe_relative_path("data\\font.png"));
|
||||
CHECK(script_screens::is_safe_relative_path("data/..font.png"));
|
||||
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path(""));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path("../eu07.ini"));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path("data/../../eu07.ini"));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path("data\\..\\x"));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path(".."));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path("/etc/passwd"));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path("\\windows"));
|
||||
CHECK_FALSE(script_screens::is_safe_relative_path("c:/windows/win.ini"));
|
||||
}
|
||||
Reference in New Issue
Block a user