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/config.h | 19 ++++++++++++++++++ libcr_ipc/tests/test.h | 21 +++++++++++++++++++ libcr_ipc/tests/test_chan.c | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 libcr_ipc/tests/config.h create mode 100644 libcr_ipc/tests/test.h create mode 100644 libcr_ipc/tests/test_chan.c (limited to 'libcr_ipc/tests') 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 + * 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 + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBCR_IPC_TESTS_TEST_H_ +#define _LIBCR_IPC_TESTS_TEST_H_ + +#include +#include /* 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 + * + * 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