From 624d1371304a5ae4b3429ad5e4267aac7cd03dfd Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Mon, 12 Oct 2026 00:09:38 +0300 Subject: missing impls and hardening --- tests/common/test_input_alloc.c | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/common/test_input_alloc.c (limited to 'tests/common/test_input_alloc.c') diff --git a/tests/common/test_input_alloc.c b/tests/common/test_input_alloc.c new file mode 100644 index 0000000..e311dc0 --- /dev/null +++ b/tests/common/test_input_alloc.c @@ -0,0 +1,48 @@ +#include "common/input.h" +#include +#include +#include + +/* This executable compiles input.c with malloc redirected here. */ +static int allocations = -1; +void *yasl_test_malloc(size_t bytes) +{ + if (allocations == 0) return NULL; + if (allocations > 0) allocations--; + return calloc(1, bytes); +} + +int main(void) +{ + struct yasl_surface *surface = (struct yasl_surface *)(uintptr_t)1; + for (int failure = 0; failure < 2; failure++) { + struct input_queue q = {0}; + struct yasl_input_event in = {.surface = surface, .kind = YASL_INPUT_PASTE, + .paste = {"payload", 7}}, out; + /* Fail either payload allocation or the initial event-array allocation. */ + allocations = failure; + assert(!input_queue_push(&q, &in)); + assert(input_queue_pop(&q, &out) && out.kind == YASL_INPUT_RESET); + assert(out.surface == surface); + assert(!input_queue_pop(&q, &out)); + input_queue_clear(&q); + + allocations = -1; + assert(input_queue_push(&q, &in)); + assert(input_queue_pop(&q, &out)); + const char *last = out.paste.text; + for (size_t i = 0; i < q.capacity; i++) assert(input_queue_push(&q, &in)); + allocations = failure; + assert(!input_queue_push(&q, &in)); + assert(strcmp(last, "payload") == 0); + assert(!q.count && q.reset_pending); + allocations = -1; + in = (struct yasl_input_event){.surface = surface, .kind = YASL_INPUT_KEY, + .key.action = YASL_KEY_RELEASE}; + assert(input_queue_push(&q, &in)); + assert(input_queue_pop(&q, &out) && out.kind == YASL_INPUT_RESET); + assert(input_queue_pop(&q, &out) && out.key.action == YASL_KEY_RELEASE); + input_queue_clear(&q); + } + return 0; +} -- cgit v1.2.3