summaryrefslogtreecommitdiff
path: root/libcr_ipc/tests
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-02-21 10:34:00 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-02-21 10:34:00 -0700
commit19a41387633e53d64d8a0ae69f3d3d3e35641c8d (patch)
treea89dead6dd34f95b3e644edeb02fe1ca70a28784 /libcr_ipc/tests
parent4ba0b95dc825a83748b7cb2aa528411026d5bada (diff)
parent5dab625d981e0039a5d874f5d8a6f795472785bc (diff)
Merge branch 'lukeshu/misc'
Diffstat (limited to 'libcr_ipc/tests')
-rw-r--r--libcr_ipc/tests/config.h2
-rw-r--r--libcr_ipc/tests/test_sema.c74
2 files changed, 75 insertions, 1 deletions
diff --git a/libcr_ipc/tests/config.h b/libcr_ipc/tests/config.h
index e06c876..2a5cbc9 100644
--- a/libcr_ipc/tests/config.h
+++ b/libcr_ipc/tests/config.h
@@ -7,7 +7,7 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_
-#define CONFIG_COROUTINE_DEFAULT_STACK_SIZE (4*1024)
+#define CONFIG_COROUTINE_STACK_SIZE_DEFAULT (4*1024)
#define CONFIG_COROUTINE_NAME_LEN 16
#define CONFIG_COROUTINE_NUM 8
diff --git a/libcr_ipc/tests/test_sema.c b/libcr_ipc/tests/test_sema.c
new file mode 100644
index 0000000..3208237
--- /dev/null
+++ b/libcr_ipc/tests/test_sema.c
@@ -0,0 +1,74 @@
+/* libcr_ipc/tests/test_sema.c - Tests for <libcr_ipc/sema.h>
+ *
+ * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libcr/coroutine.h>
+#include <libcr_ipc/sema.h>
+
+#include "test.h"
+
+int counter = 0;
+
+COROUTINE cr_first(void *_sema) {
+ cr_sema_t *sema = _sema;
+ cr_begin();
+
+ cr_sema_wait(sema);
+ counter++;
+ cr_sema_signal(sema);
+
+ cr_exit();
+}
+
+COROUTINE cr_second(void *_sema) {
+ cr_sema_t *sema = _sema;
+ cr_begin();
+
+ cr_sema_signal(sema); /* should be claimed by cr_first, which has been waiting */
+ cr_sema_wait(sema); /* should block, because cr_first claimed it */
+ test_assert(counter == 1);
+
+ cr_exit();
+}
+
+COROUTINE cr_producer(void *_sema) {
+ cr_sema_t *sema = _sema;
+ cr_begin();
+
+ for (int i = 0; i < 10; i++)
+ cr_sema_signal(sema);
+
+ cr_end();
+}
+
+COROUTINE cr_consumer(void *_sema) {
+ cr_sema_t *sema = _sema;
+ cr_begin();
+
+ for (int i = 0; i < 5; i++)
+ cr_sema_wait(sema);
+
+ cr_end();
+}
+
+int main() {
+ cr_sema_t sema = {0};
+
+ printf("== test 1 =========================================\n");
+ coroutine_add("first", cr_first, &sema);
+ coroutine_add("second", cr_second, &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_main();
+ coroutine_add("consumer", cr_consumer, &sema);
+ coroutine_main();
+ test_assert(sema.cnt == 0);
+
+ return 0;
+}