summaryrefslogtreecommitdiff
path: root/libcr_ipc/tests
diff options
context:
space:
mode:
Diffstat (limited to 'libcr_ipc/tests')
-rw-r--r--libcr_ipc/tests/config.h2
-rw-r--r--libcr_ipc/tests/test_chan.c94
-rw-r--r--libcr_ipc/tests/test_chan_compile.c13
-rw-r--r--libcr_ipc/tests/test_mutex.c6
-rw-r--r--libcr_ipc/tests/test_mutex_compile.c11
-rw-r--r--libcr_ipc/tests/test_rpc.c14
-rw-r--r--libcr_ipc/tests/test_rpc_compile.c13
-rw-r--r--libcr_ipc/tests/test_rwmutex.c2
-rw-r--r--libcr_ipc/tests/test_rwmutex_compile.c11
-rw-r--r--libcr_ipc/tests/test_select.c90
-rw-r--r--libcr_ipc/tests/test_sema.c18
-rw-r--r--libcr_ipc/tests/test_sema_compile.c11
12 files changed, 168 insertions, 117 deletions
diff --git a/libcr_ipc/tests/config.h b/libcr_ipc/tests/config.h
index a648589..a16df1d 100644
--- a/libcr_ipc/tests/config.h
+++ b/libcr_ipc/tests/config.h
@@ -1,4 +1,4 @@
-/* config.h - Compile-time configuration for the libcr_ipc tests
+/* libcr_ipc/tests/config.h - Compile-time configuration for the libcr_ipc tests
*
* Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
diff --git a/libcr_ipc/tests/test_chan.c b/libcr_ipc/tests/test_chan.c
index e5d9dc8..ee1751f 100644
--- a/libcr_ipc/tests/test_chan.c
+++ b/libcr_ipc/tests/test_chan.c
@@ -11,7 +11,9 @@
CR_CHAN_DECLARE(intchan, int);
-COROUTINE cr_producer(void *_ch) {
+/* test 1 *********************************************************************/
+
+COROUTINE test1_producer_cr(void *_ch) {
intchan_t *ch = _ch;
cr_begin();
@@ -22,11 +24,10 @@ COROUTINE cr_producer(void *_ch) {
cr_chan_send(ch, 2);
-
cr_end();
}
-COROUTINE cr_consumer(void *_ch) {
+COROUTINE test1_consumer_cr(void *_ch) {
int x;
intchan_t *ch = _ch;
cr_begin();
@@ -40,10 +41,91 @@ COROUTINE cr_consumer(void *_ch) {
cr_end();
}
+/* test 2 *********************************************************************/
+
+intchan_t test2_ch[10] = {};
+intchan_t test2_fch = {};
+
+COROUTINE test2_consumer_cr(void *) {
+ cr_begin();
+
+ struct cr_select_arg args[11];
+
+ bool chdone[10] = {};
+ int arg2ch[10];
+ for (;;) {
+ int ret_ch;
+ int i_arg = 0;
+ for (int i_ch = 0; i_ch < 10; i_ch++) {
+ if (!chdone[i_ch]) {
+ args[i_arg] = CR_SELECT_RECV(&test2_ch[i_ch], &ret_ch);
+ arg2ch[i_arg] = i_ch;
+ i_arg++;
+ }
+ }
+ if (!i_arg)
+ break;
+ args[i_arg] = CR_SELECT_DEFAULT; /* check that default doesn't trigger */
+ test_assert(i_arg <= 10);
+ int ret_arg = cr_select_v(i_arg+1, args);
+ test_assert(ret_arg < i_arg);
+ test_assert(arg2ch[ret_arg] == ret_ch);
+ chdone[ret_ch] = true;
+ }
+ int ret_ch, ret_arg;
+ args[0] = CR_SELECT_RECV(&test2_ch[0], &ret_ch);
+ args[1] = CR_SELECT_DEFAULT;
+ ret_arg = cr_select_v(2, args);
+ test_assert(ret_arg == 1);
+
+ int send = 567;
+ args[0] = CR_SELECT_SEND(&test2_fch, &send);
+ args[1] = CR_SELECT_DEFAULT;
+ ret_arg = cr_select_v(2, args);
+ test_assert(ret_arg == 0);
+
+ send = 890;
+ ret_arg = cr_select_l(CR_SELECT_SEND(&test2_fch, &send),
+ CR_SELECT_DEFAULT);
+ test_assert(ret_arg == 1);
+
+ cr_end();
+}
+
+COROUTINE test2_producer_cr(void *_n) {
+ int n = *(int *)_n;
+ cr_begin();
+
+ cr_chan_send(&test2_ch[n], n);
+
+ cr_end();
+}
+
+COROUTINE test2_final_cr(void *) {
+ cr_begin();
+
+ int ret = cr_chan_recv(&test2_fch);
+ printf("ret=%d\n", ret);
+ test_assert(ret == 567);
+
+ cr_end();
+}
+
+/******************************************************************************/
+
int main() {
- intchan_t ch = {0};
- coroutine_add("producer", cr_producer, &ch);
- coroutine_add("consumer", cr_consumer, &ch);
+ printf("== test 1 =========================================\n");
+ intchan_t ch = {};
+ coroutine_add("producer", test1_producer_cr, &ch);
+ coroutine_add("consumer", test1_consumer_cr, &ch);
coroutine_main();
+
+ printf("== test 2 =========================================\n");
+ for (int i = 0; i < 10; i++)
+ coroutine_add("producer", test2_producer_cr, &i);
+ coroutine_add("consumer", test2_consumer_cr, NULL);
+ coroutine_add("final", test2_final_cr, NULL);
+ coroutine_main();
+
return 0;
}
diff --git a/libcr_ipc/tests/test_chan_compile.c b/libcr_ipc/tests/test_chan_compile.c
new file mode 100644
index 0000000..00f3bdc
--- /dev/null
+++ b/libcr_ipc/tests/test_chan_compile.c
@@ -0,0 +1,13 @@
+/* libcr_ipc/tests/test_chan_compile.c - Test that <libcr_ipc/chan.h> compiles
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr_ipc/chan.h>
+
+CR_CHAN_DECLARE(intchan, int);
+
+int main() {
+ return 0;
+}
diff --git a/libcr_ipc/tests/test_mutex.c b/libcr_ipc/tests/test_mutex.c
index 43714c9..d08315d 100644
--- a/libcr_ipc/tests/test_mutex.c
+++ b/libcr_ipc/tests/test_mutex.c
@@ -11,7 +11,7 @@
int counter = 0;
-COROUTINE cr_worker(void *_mu) {
+COROUTINE worker_cr(void *_mu) {
cr_mutex_t *mu = _mu;
cr_begin();
@@ -29,8 +29,8 @@ COROUTINE cr_worker(void *_mu) {
int main() {
cr_mutex_t mu = {};
- coroutine_add("a", cr_worker, &mu);
- coroutine_add("b", cr_worker, &mu);
+ coroutine_add("a", worker_cr, &mu);
+ coroutine_add("b", worker_cr, &mu);
coroutine_main();
test_assert(counter == 200);
return 0;
diff --git a/libcr_ipc/tests/test_mutex_compile.c b/libcr_ipc/tests/test_mutex_compile.c
new file mode 100644
index 0000000..c119ee3
--- /dev/null
+++ b/libcr_ipc/tests/test_mutex_compile.c
@@ -0,0 +1,11 @@
+/* libcr_ipc/tests/test_mutex_compile.c - Test that <libcr_ipc/mutex.h> compiles
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr_ipc/mutex.h>
+
+int main() {
+ return 0;
+}
diff --git a/libcr_ipc/tests/test_rpc.c b/libcr_ipc/tests/test_rpc.c
index 910b738..ee88f47 100644
--- a/libcr_ipc/tests/test_rpc.c
+++ b/libcr_ipc/tests/test_rpc.c
@@ -14,7 +14,7 @@ CR_RPC_DECLARE(intrpc, int, int);
/* Test that the RPC is fair, have worker1 start waiting first, and
* ensure that it gets the first request. */
-COROUTINE cr_caller(void *_ch) {
+COROUTINE caller_cr(void *_ch) {
intrpc_t *ch = _ch;
cr_begin();
@@ -27,7 +27,7 @@ COROUTINE cr_caller(void *_ch) {
cr_exit();
}
-COROUTINE cr_worker1(void *_ch) {
+COROUTINE worker1_cr(void *_ch) {
intrpc_t *ch = _ch;
cr_begin();
@@ -38,7 +38,7 @@ COROUTINE cr_worker1(void *_ch) {
cr_exit();
}
-COROUTINE cr_worker2(void *_ch) {
+COROUTINE worker2_cr(void *_ch) {
intrpc_t *ch = _ch;
cr_begin();
@@ -50,10 +50,10 @@ COROUTINE cr_worker2(void *_ch) {
}
int main() {
- intrpc_t ch = {0};
- coroutine_add("worker1", cr_worker1, &ch);
- coroutine_add("caller", cr_caller, &ch);
- coroutine_add("worker2", cr_worker2, &ch);
+ intrpc_t ch = {};
+ coroutine_add("worker1", worker1_cr, &ch);
+ coroutine_add("caller", caller_cr, &ch);
+ coroutine_add("worker2", worker2_cr, &ch);
coroutine_main();
return 0;
}
diff --git a/libcr_ipc/tests/test_rpc_compile.c b/libcr_ipc/tests/test_rpc_compile.c
new file mode 100644
index 0000000..ed0e0c1
--- /dev/null
+++ b/libcr_ipc/tests/test_rpc_compile.c
@@ -0,0 +1,13 @@
+/* libcr_ipc/tests/test_rpc_compile.c - Test that <libcr_ipc/rpc.h> compiles
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr_ipc/rpc.h>
+
+CR_RPC_DECLARE(intrpc, int, int);
+
+int main() {
+ return 0;
+}
diff --git a/libcr_ipc/tests/test_rwmutex.c b/libcr_ipc/tests/test_rwmutex.c
index 77e8c7c..e79e779 100644
--- a/libcr_ipc/tests/test_rwmutex.c
+++ b/libcr_ipc/tests/test_rwmutex.c
@@ -12,7 +12,7 @@
#include "test.h"
cr_rwmutex_t mu = {};
-char out[10] = {0};
+char out[10] = {};
size_t len = 0;
COROUTINE cr1_reader(void *_mu) {
diff --git a/libcr_ipc/tests/test_rwmutex_compile.c b/libcr_ipc/tests/test_rwmutex_compile.c
new file mode 100644
index 0000000..358a535
--- /dev/null
+++ b/libcr_ipc/tests/test_rwmutex_compile.c
@@ -0,0 +1,11 @@
+/* libcr_ipc/tests/test_rwmutex_compile.c - Test that <libcr_ipc/rwmutex.h> compiles
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr_ipc/rwmutex.h>
+
+int main() {
+ return 0;
+}
diff --git a/libcr_ipc/tests/test_select.c b/libcr_ipc/tests/test_select.c
deleted file mode 100644
index f0a71a3..0000000
--- a/libcr_ipc/tests/test_select.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/* libcr_ipc/tests/test_select.c - Tests for <libcr_ipc/select.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);
-
-intchan_t ch[10] = {0};
-intchan_t fch = {0};
-
-COROUTINE cr_consumer(void *) {
- cr_begin();
-
- struct cr_select_arg args[11];
-
- bool chdone[10] = {0};
- int arg2ch[10];
- for (;;) {
- int ret_ch;
- int i_arg = 0;
- for (int i_ch = 0; i_ch < 10; i_ch++) {
- if (!chdone[i_ch]) {
- args[i_arg] = CR_SELECT_RECV(&ch[i_ch], &ret_ch);
- arg2ch[i_arg] = i_ch;
- i_arg++;
- }
- }
- if (!i_arg)
- break;
- args[i_arg] = CR_SELECT_DEFAULT; /* check that default doesn't trigger */
- test_assert(i_arg <= 10);
- int ret_arg = cr_select_v(i_arg+1, args);
- test_assert(ret_arg < i_arg);
- test_assert(arg2ch[ret_arg] == ret_ch);
- chdone[ret_ch] = true;
- }
- int ret_ch, ret_arg;
- args[0] = CR_SELECT_RECV(&ch[0], &ret_ch);
- args[1] = CR_SELECT_DEFAULT;
- ret_arg = cr_select_v(2, args);
- test_assert(ret_arg == 1);
-
- int send = 567;
- args[0] = CR_SELECT_SEND(&fch, &send);
- args[1] = CR_SELECT_DEFAULT;
- ret_arg = cr_select_v(2, args);
- test_assert(ret_arg == 0);
-
- send = 890;
- args[0] = CR_SELECT_SEND(&fch, &send);
- args[1] = CR_SELECT_DEFAULT;
- ret_arg = cr_select_v(2, args);
- test_assert(ret_arg == 1);
-
- cr_end();
-}
-
-COROUTINE cr_producer(void *_n) {
- int n = *(int *)_n;
- cr_begin();
-
- cr_chan_send(&ch[n], n);
-
- cr_end();
-}
-
-COROUTINE cr_final(void *) {
- cr_begin();
-
- int ret = cr_chan_recv(&fch);
- printf("ret=%d\n", ret);
- test_assert(ret == 567);
-
- cr_end();
-}
-
-int main() {
- for (int i = 0; i < 10; i++)
- coroutine_add("producer", cr_producer, &i);
- coroutine_add("consumer", cr_consumer, NULL);
- coroutine_add("final", cr_final, NULL);
- coroutine_main();
- return 0;
-}
diff --git a/libcr_ipc/tests/test_sema.c b/libcr_ipc/tests/test_sema.c
index e5b22a5..435c01a 100644
--- a/libcr_ipc/tests/test_sema.c
+++ b/libcr_ipc/tests/test_sema.c
@@ -13,7 +13,7 @@
int counter = 0;
-COROUTINE cr_first(void *_sema) {
+COROUTINE first_cr(void *_sema) {
cr_sema_t *sema = _sema;
cr_begin();
@@ -24,7 +24,7 @@ COROUTINE cr_first(void *_sema) {
cr_exit();
}
-COROUTINE cr_second(void *_sema) {
+COROUTINE second_cr(void *_sema) {
cr_sema_t *sema = _sema;
cr_begin();
@@ -35,7 +35,7 @@ COROUTINE cr_second(void *_sema) {
cr_exit();
}
-COROUTINE cr_producer(void *_sema) {
+COROUTINE producer_cr(void *_sema) {
cr_sema_t *sema = _sema;
cr_begin();
@@ -45,7 +45,7 @@ COROUTINE cr_producer(void *_sema) {
cr_end();
}
-COROUTINE cr_consumer(void *_sema) {
+COROUTINE consumer_cr(void *_sema) {
cr_sema_t *sema = _sema;
cr_begin();
@@ -59,16 +59,16 @@ int main() {
cr_sema_t sema = {};
printf("== test 1 =========================================\n");
- coroutine_add("first", cr_first, &sema);
- coroutine_add("second", cr_second, &sema);
+ coroutine_add("first", first_cr, &sema);
+ coroutine_add("second", second_cr, &sema);
coroutine_main();
test_assert(sema.cnt == 0);
printf("== test 2 =========================================\n");
- coroutine_add("consumer", cr_consumer, &sema);
- coroutine_add("producer", cr_producer, &sema);
+ coroutine_add("consumer", consumer_cr, &sema);
+ coroutine_add("producer", producer_cr, &sema);
coroutine_main();
- coroutine_add("consumer", cr_consumer, &sema);
+ coroutine_add("consumer", consumer_cr, &sema);
coroutine_main();
test_assert(sema.cnt == 0);
diff --git a/libcr_ipc/tests/test_sema_compile.c b/libcr_ipc/tests/test_sema_compile.c
new file mode 100644
index 0000000..8a635b2
--- /dev/null
+++ b/libcr_ipc/tests/test_sema_compile.c
@@ -0,0 +1,11 @@
+/* libcr_ipc/tests/test_sema_compile.c - Test that <libcr_ipc/sema.h> compiles
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr_ipc/sema.h>
+
+int main() {
+ return 0;
+}