18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 05:16:01 +01:00

wasm display support

This commit is contained in:
2026-11-10 00:57:37 +01:00
parent 685258d787
commit 18c333b089
25 changed files with 2521 additions and 23 deletions

View File

@@ -25,6 +25,11 @@ 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 wasm screen host, tested with the real runtime (test_wasm_host.cpp)
if (WITH_WASM)
target_sources(${UNIT_TESTS_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/scripting/wasmscreens.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)

View File

@@ -0,0 +1,288 @@
#include "stdafx.h"
#include "doctest.h"
// the wasm screen host (scripting/wasmscreens.cpp) with the real runtime: a module reads its input, draws an
// image that goes to the screen's texture and sends a command; a module stuck in a loop is stopped. built with
// WITH_WASM only
#ifdef WITH_WASM
#include "scripting/wasmscreens.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 <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 {
// 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};
// 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;
};
// writes a module into a folder of its own, removed with the object
class module_file {
public:
template <std::size_t Size_>
module_file(std::string const &Name, std::array<std::uint8_t, Size_> const &Bytes)
: m_folder{std::filesystem::temp_directory_path() / std::format("eu07_wasm_test_{}", Name)} {
std::filesystem::create_directories(m_folder);
m_path = (m_folder / (Name + ".wasm")).generic_string();
std::ofstream file{m_path, std::ios::binary};
file.write(reinterpret_cast<char const *>(Bytes.data()), static_cast<std::streamsize>(Bytes.size()));
}
~module_file() {
std::error_code error;
std::filesystem::remove_all(m_folder, error);
}
module_file(module_file const &) = delete;
module_file &operator=(module_file const &) = delete;
std::string const &path() const { return m_path; }
private:
std::filesystem::path m_folder;
std::string m_path;
};
// runs frames until the condition holds, or gives up after a few seconds
bool run_until(wasm_screen_queue &Queue, std::function<bool()> const &Condition) {
auto const deadline{std::chrono::steady_clock::now() + std::chrono::seconds(10)};
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;
}
} // namespace
TEST_CASE("wasm host: a cab screen draws from its input and sends commands to its vehicle") {
sent_commands().clear();
module_file const module{"paint", paint_module};
wasm_screen_queue queue;
REQUIRE(queue.init());
recording_texture texture;
auto const screen{queue.acquire({.kind = wasm_screen_kind::cab, .script = module.path(), .key = "cab", .texture = &texture, .frames = 0})};
REQUIRE(screen != nullptr);
// the same key, the same screen
CHECK(queue.acquire({.kind = wasm_screen_kind::cab, .script = module.path(), .key = "cab", .texture = &texture, .frames = 0}) == screen);
REQUIRE(run_until(queue, [&queue, &screen] { return queue.due(*screen); }));
queue.submit(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 host: the image goes up again when the texture is made anew, and waits while it isn't there") {
sent_commands().clear();
module_file const module{"paint", paint_module};
wasm_screen_queue queue;
REQUIRE(queue.init());
recording_texture texture;
texture.ready = false;
auto const screen{queue.acquire({.kind = wasm_screen_kind::cab, .script = module.path(), .key = "cab", .texture = &texture, .frames = 0})};
REQUIRE(run_until(queue, [&queue, &screen] { return queue.due(*screen); }));
queue.submit(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 host: a screen outside the cab draws, but its commands are dropped") {
sent_commands().clear();
module_file const module{"paint", paint_module};
wasm_screen_queue queue;
REQUIRE(queue.init());
recording_texture texture;
auto const screen{queue.acquire({.kind = wasm_screen_kind::external, .script = module.path(), .key = "sign", .texture = &texture, .frames = 0})};
REQUIRE(run_until(queue, [&queue, &screen] { return queue.due(*screen); }));
queue.submit(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 host: a screen stuck in a loop is stopped, and the others keep working") {
sent_commands().clear();
module_file const spinning{"spin", spin_module};
module_file const painting{"paint", paint_module};
wasm_screen_queue queue;
REQUIRE(queue.init());
recording_texture stuck;
recording_texture working;
auto const first{queue.acquire({.kind = wasm_screen_kind::cab, .script = spinning.path(), .key = "spin", .texture = &stuck, .frames = 0})};
auto const second{queue.acquire({.kind = wasm_screen_kind::cab, .script = painting.path(), .key = "paint", .texture = &working, .frames = 0})};
REQUIRE(run_until(queue, [&queue, &first] { return queue.due(*first); }));
REQUIRE(run_until(queue, [&queue, &second] { return queue.due(*second); }));
queue.submit(first, speed(1.0), 1);
queue.submit(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 host: the screens outlive the queue without harm") {
module_file const module{"paint", paint_module};
std::shared_ptr<wasm_screen> screen;
recording_texture texture;
{
wasm_screen_queue queue;
REQUIRE(queue.init());
screen = queue.acquire({.kind = wasm_screen_kind::cab, .script = module.path(), .key = "cab", .texture = &texture, .frames = 0});
REQUIRE(run_until(queue, [&queue, &screen] { return queue.due(*screen); }));
queue.submit(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

View File

@@ -0,0 +1,121 @@
#include "stdafx.h"
#include "doctest.h"
#include "scripting/wasmscreenkernels.h"
#include <array>
#include <span>
#include <vector>
// the image filters the wasm 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("wasm kernels: led_post gives the pixels of led_util.render_post") {
auto const input{rgba(source)};
std::vector<std::uint8_t> output(wasm_screens::image_size(12, 8), 7);
REQUIRE(wasm_screens::led_post(input, 5, 3, output, true));
CHECK(output == rgba(posted));
REQUIRE(wasm_screens::led_post(input, 5, 3, output, false));
CHECK(output == rgba(plain));
}
TEST_CASE("wasm kernels: resize_lanczos gives the pixels of Pillow's resize with LANCZOS") {
auto const input{rgba(posted)};
std::vector<std::uint8_t> larger(wasm_screens::image_size(17, 11));
REQUIRE(wasm_screens::resize_lanczos(input, 12, 8, larger, 17, 11));
CHECK(larger == rgba(enlarged));
std::vector<std::uint8_t> smaller(wasm_screens::image_size(7, 5));
REQUIRE(wasm_screens::resize_lanczos(input, 12, 8, smaller, 7, 5));
CHECK(smaller == rgba(reduced));
}
TEST_CASE("wasm kernels: images that don't fit their buffers are refused") {
std::vector<std::uint8_t> const input(wasm_screens::image_size(5, 3));
std::vector<std::uint8_t> output(wasm_screens::image_size(12, 8) - 1);
CHECK_FALSE(wasm_screens::led_post(input, 5, 3, output, true));
CHECK_FALSE(wasm_screens::led_post(input, 0, 3, output, true));
CHECK_FALSE(wasm_screens::resize_lanczos(input, 5, 3, output, 12, 8));
CHECK_FALSE(wasm_screens::resize_lanczos(input, 6, 3, output, 2, 2));
}

View File

@@ -0,0 +1,103 @@
#include "stdafx.h"
#include "doctest.h"
#include "scripting/wasmscreenrules.h"
#include "utilities/parser.h"
// the parts of the wasm screens that don't need the runtime: the .mmd entries, the refresh schedule counted in
// frames and the files a screen may read
using wasm_screens::frame_schedule;
using wasm_screens::screen_config;
TEST_CASE("wasm 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("wasm 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("wasm 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("wasm 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("wasm 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("wasm 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("wasm screens: a screen reads files only below its own folder") {
CHECK(wasm_screens::is_safe_relative_path("font.png"));
CHECK(wasm_screens::is_safe_relative_path("data/font.png"));
CHECK(wasm_screens::is_safe_relative_path("data\\font.png"));
CHECK(wasm_screens::is_safe_relative_path("data/..font.png"));
CHECK_FALSE(wasm_screens::is_safe_relative_path(""));
CHECK_FALSE(wasm_screens::is_safe_relative_path("../eu07.ini"));
CHECK_FALSE(wasm_screens::is_safe_relative_path("data/../../eu07.ini"));
CHECK_FALSE(wasm_screens::is_safe_relative_path("data\\..\\x"));
CHECK_FALSE(wasm_screens::is_safe_relative_path(".."));
CHECK_FALSE(wasm_screens::is_safe_relative_path("/etc/passwd"));
CHECK_FALSE(wasm_screens::is_safe_relative_path("\\windows"));
CHECK_FALSE(wasm_screens::is_safe_relative_path("c:/windows/win.ini"));
}