summaryrefslogtreecommitdiff
path: root/libcr_ipc
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-11-25 08:28:13 -0700
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-11-26 20:23:53 -0700
commit88f2b88c3889e9dd0c894d389d67b17617412094 (patch)
tree2b0f8535735f7eee56cd7ac5abb1d169c9d5e252 /libcr_ipc
parenta8dc3fc4ad08c159243271c403848d23261aa699 (diff)
wip: libcr_ipc: Add some tests
Diffstat (limited to 'libcr_ipc')
-rw-r--r--libcr_ipc/CMakeLists.txt14
-rw-r--r--libcr_ipc/tests/config.h19
-rw-r--r--libcr_ipc/tests/test.h21
-rw-r--r--libcr_ipc/tests/test_chan.c49
4 files changed, 103 insertions, 0 deletions
diff --git a/libcr_ipc/CMakeLists.txt b/libcr_ipc/CMakeLists.txt
index d44203e..2a1c747 100644
--- a/libcr_ipc/CMakeLists.txt
+++ b/libcr_ipc/CMakeLists.txt
@@ -8,3 +8,17 @@ target_include_directories(libcr_ipc SYSTEM INTERFACE ${CMAKE_CURRENT_LIST_DIR}/
target_link_libraries(libcr_ipc INTERFACE
libcr
)
+
+set(ipc_tests
+ chan
+ #select
+ #mutex
+ #rpc
+ #sema
+)
+if (ENABLE_TESTS)
+ foreach(test IN LISTS ipc_tests)
+ add_lib_test(libcr_ipc "test_${test}")
+ target_include_directories("test_${test}" PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/tests)
+ endforeach()
+endif()
diff --git a/libcr_ipc/tests/config.h b/libcr_ipc/tests/config.h
new file mode 100644
index 0000000..949a82f
--- /dev/null
+++ b/libcr_ipc/tests/config.h
@@ -0,0 +1,19 @@
+/* config.h - TODO
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _CONFIG_H_
+#define _CONFIG_H_
+
+#define CONFIG_COROUTINE_DEFAULT_STACK_SIZE (4*1024)
+#define CONFIG_COROUTINE_NAME_LEN 16
+#define CONFIG_COROUTINE_NUM 8
+
+#define CONFIG_COROUTINE_MEASURE_STACK 1
+#define CONFIG_COROUTINE_PROTECT_STACK 1
+#define CONFIG_COROUTINE_DEBUG 1
+#define CONFIG_COROUTINE_VALGRIND 1
+
+#endif /* _CONFIG_H_ */
diff --git a/libcr_ipc/tests/test.h b/libcr_ipc/tests/test.h
new file mode 100644
index 0000000..4928d8d
--- /dev/null
+++ b/libcr_ipc/tests/test.h
@@ -0,0 +1,21 @@
+/* libcr_ipc/tests/test.h - Common test utilities
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _LIBCR_IPC_TESTS_TEST_H_
+#define _LIBCR_IPC_TESTS_TEST_H_
+
+#include <stdio.h>
+#include <stdlib.h> /* for exit() */
+
+#define test_assert(expr) do { \
+ if (!(expr)) { \
+ printf("test failure: %s:%d:%s: %s\n", \
+ __FILE__, __LINE__, __func__, #expr); \
+ exit(1); \
+ } \
+ } while (0)
+
+#endif /* _LIBCR_IPC_TESTS_TEST_H_ */
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 <libcr_ipc/chan.h>
+ *
+ * Copyright (C) 2024 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 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;
+}