From a15cb041654ae307add0b998b526c87c3f42bf5f Mon Sep 17 00:00:00 2001 From: David Moc Date: Tue, 2 Jun 2026 13:50:21 +0200 Subject: Add plugin hooks and mode plugins --- tests/test_core.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 tests/test_core.c (limited to 'tests') diff --git a/tests/test_core.c b/tests/test_core.c new file mode 100644 index 0000000..42dfd99 --- /dev/null +++ b/tests/test_core.c @@ -0,0 +1,88 @@ +#include "ecex.h" + +#include +#include +#include + +static void test_plugin_identity(void) { + ecex_t ed; + memset(&ed, 0, sizeof(ed)); + ed.plugins = ecex_plugin_runtime_new(); + assert(ed.plugins); + + ecex_plugin_t *a = ecex_plugin_register(&ed, "alpha", ECEX_PLUGIN_API_VERSION); + assert(a); + assert(ecex_plugin_find(&ed, "alpha") == a); + assert(ecex_plugin_register(&ed, "alpha", ECEX_PLUGIN_API_VERSION) == 0); + assert(ecex_plugin_require(&ed, "alpha", ECEX_PLUGIN_API_VERSION) == a); + assert(ecex_plugin_register(&ed, "Bad ID", ECEX_PLUGIN_API_VERSION) == 0); + assert(ecex_plugin_register(&ed, "beta", ECEX_PLUGIN_API_VERSION + 1) == 0); + + ecex_plugin_runtime_free(ed.plugins); +} + +static void test_plugin_slots_and_exports(void) { + ecex_t ed; + memset(&ed, 0, sizeof(ed)); + ed.plugins = ecex_plugin_runtime_new(); + assert(ed.plugins); + + ecex_plugin_t *owner = ecex_plugin_register(&ed, "owner", ECEX_PLUGIN_API_VERSION); + ecex_plugin_t *reader = ecex_plugin_register(&ed, "reader", ECEX_PLUGIN_API_VERSION); + assert(owner); + assert(reader); + + assert(ecex_plugin_slot_i32_set(owner, "score", 0, 42) == 0); + assert(ecex_plugin_slot_i32_get_scalar(owner, "score", -1) == 42); + assert(ecex_plugin_slot_i32_set_2d(owner, "board", 10, 3, 4, 99) == 0); + assert(ecex_plugin_slot_i32_get_2d(owner, "board", 10, 3, 4, -1) == 99); + assert(ecex_plugin_slot_i32_get_2d(owner, "board", 0, 3, 4, -7) == -7); + + int copied = 0; + size_t copied_len = 0; + assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, sizeof(copied), &copied_len) != 0); + assert(ecex_plugin_slot_set_export_flags(owner, "score", ECEX_PLUGIN_I32, ECEX_PLUGIN_EXPORT_READ) == 0); + assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, sizeof(copied), &copied_len) == 0); + assert(copied == 42); + assert(copied_len == sizeof(copied)); + + copied = 7; + assert(ecex_plugin_slot_read_exported(&ed, "owner", "score", &copied, 0, &copied_len) != 0); + assert(copied == 7); + assert(copied_len == sizeof(copied)); + + ecex_plugin_runtime_free(ed.plugins); +} + +static void test_plugin_objects_and_text(void) { + ecex_t ed; + memset(&ed, 0, sizeof(ed)); + ed.plugins = ecex_plugin_runtime_new(); + assert(ed.plugins); + + ecex_plugin_t *plugin = ecex_plugin_register(&ed, "objects", ECEX_PLUGIN_API_VERSION); + assert(plugin); + + unsigned char *object = ecex_plugin_object_calloc(plugin, "state", 1, 16); + assert(object); + assert(ecex_plugin_object_i32_set(plugin, object, 4, 1234) == 0); + assert(ecex_plugin_object_i32_get(plugin, object, 4, -1) == 1234); + assert(ecex_plugin_object_i32_set(plugin, object, 14, 1) != 0); + assert(ecex_plugin_object_valid(plugin, object) == 1); + assert(ecex_plugin_object_free(plugin, object) == 0); + assert(ecex_plugin_object_valid(plugin, object) == 0); + + assert(ecex_plugin_text_set(plugin, 1, "hello", -1) == 0); + assert(strcmp(ecex_plugin_text_get_drawable(&ed, plugin, 1), "hello") == 0); + assert(ecex_plugin_text_free_all(plugin) == 0); + assert(strcmp(ecex_plugin_text_get_drawable(&ed, plugin, 1), "") == 0); + + ecex_plugin_runtime_free(ed.plugins); +} + +int main(void) { + test_plugin_identity(); + test_plugin_slots_and_exports(); + test_plugin_objects_and_text(); + return 0; +} -- cgit v1.2.3