From b5e84c26699d9b2beadf2e44adbd92c2c18b5bfa Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 21 Feb 2025 00:53:45 -0700 Subject: libcr_ipc: Fixes and tests for sema.h --- libcr_ipc/tests/test_sema.c | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 libcr_ipc/tests/test_sema.c (limited to 'libcr_ipc/tests') 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 + * + * Copyright (C) 2025 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include +#include + +#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; +} -- cgit v1.2.3-2-g168b From 5dab625d981e0039a5d874f5d8a6f795472785bc Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 21 Feb 2025 09:02:22 -0700 Subject: Make use of the generated stack.c --- libcr_ipc/tests/config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libcr_ipc/tests') 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 -- cgit v1.2.3-2-g168b