summaryrefslogtreecommitdiff
path: root/libcr_ipc/tests/test_chan.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcr_ipc/tests/test_chan.c')
-rw-r--r--libcr_ipc/tests/test_chan.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/libcr_ipc/tests/test_chan.c b/libcr_ipc/tests/test_chan.c
new file mode 100644
index 0000000..4788dd4
--- /dev/null
+++ b/libcr_ipc/tests/test_chan.c
@@ -0,0 +1,49 @@
+/* libcr_ipc/tests/test_chan.c - Tests for <libcr_ipc/chan.h>
+ *
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr/coroutine.h>
+#include <libcr_ipc/chan.h>
+
+#include "test.h"
+
+CR_CHAN_DECLARE(intchan, int);
+
+COROUTINE producer_cr(void *_ch) {
+ intchan_t *ch = _ch;
+ cr_begin();
+
+ cr_chan_send(ch, 1);
+
+ while (!cr_chan_can_send(ch))
+ cr_yield();
+
+ cr_chan_send(ch, 2);
+
+
+ cr_end();
+}
+
+COROUTINE consumer_cr(void *_ch) {
+ int x;
+ intchan_t *ch = _ch;
+ cr_begin();
+
+ x = cr_chan_recv(ch);
+ test_assert(x == 1);
+
+ x = cr_chan_recv(ch);
+ test_assert(x == 2);
+
+ cr_end();
+}
+
+int main() {
+ intchan_t ch = {0};
+ coroutine_add("producer", producer_cr, &ch);
+ coroutine_add("consumer", consumer_cr, &ch);
+ coroutine_main();
+ return 0;
+}