From 88f2b88c3889e9dd0c894d389d67b17617412094 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Mon, 25 Nov 2024 08:28:13 -0700 Subject: wip: libcr_ipc: Add some tests --- libcr_ipc/tests/test_chan.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 libcr_ipc/tests/test_chan.c (limited to 'libcr_ipc/tests/test_chan.c') diff --git a/libcr_ipc/tests/test_chan.c b/libcr_ipc/tests/test_chan.c new file mode 100644 index 0000000..9d6eecf --- /dev/null +++ b/libcr_ipc/tests/test_chan.c @@ -0,0 +1,49 @@ +/* libcr_ipc/tests/test_chan.c - Tests for + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include +#include + +#include "test.h" + +CR_CHAN_DECLARE(intchan, int) + +COROUTINE cr_producer(void *_ch) { + intchan_t *ch = _ch; + cr_begin(); + + intchan_send(ch, 1); + + while (!intchan_can_send(ch)) + cr_yield(); + + intchan_send(ch, 2); + + + cr_end(); +} + +COROUTINE cr_consumer(void *_ch) { + int x; + intchan_t *ch = _ch; + cr_begin(); + + x = intchan_recv(ch); + test_assert(x == 1); + + x = intchan_recv(ch); + test_assert(x == 2); + + cr_end(); +} + +int main() { + intchan_t ch = {0}; + coroutine_add("producer", cr_producer, &ch); + coroutine_add("consumer", cr_consumer, &ch); + coroutine_main(); + return 0; +} -- cgit v1.2.3-2-g168b