diff --git a/tests/sema/Makefile b/tests/sema/Makefile new file mode 100644 index 0000000000..ce88d28105 --- /dev/null +++ b/tests/sema/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.tests_common + +USEMODULE += sema + +include $(RIOTBASE)/Makefile.include diff --git a/tests/sema/Makefile.ci b/tests/sema/Makefile.ci new file mode 100644 index 0000000000..b9ff275375 --- /dev/null +++ b/tests/sema/Makefile.ci @@ -0,0 +1,3 @@ +BOARD_INSUFFICIENT_MEMORY := \ + nucleo-l011k4 \ + # diff --git a/tests/sema/main.c b/tests/sema/main.c new file mode 100644 index 0000000000..295c434103 --- /dev/null +++ b/tests/sema/main.c @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2020 Freie Universität Berlin, + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief testing sema + * + * + * @author Julian Holzwarth + * + */ + +#include +#include +#include + +#include "msg.h" +#include "sema.h" +#include "thread.h" +#include "xtimer.h" + +#define TEST_TIME_US 1000 +#define TEST_ITERATIONS 4 + +static char stack[THREAD_STACKSIZE_SMALL]; +static sema_t test_sema; +static sema_t test_sema2; + +static void *second_thread(void *arg) +{ + int *thread_success = arg; + + sema_post(&test_sema); + sema_post(&test_sema); + if (sema_try_wait(&test_sema2) != 0) { + printf("THREAD ERROR: sema_try_wait()"); + *thread_success = -1; + } + if (sema_wait_timed(&test_sema, TEST_TIME_US) != 0) { + printf("THREAD ERROR: sema_wait_timed()"); + *thread_success = -1; + } + if (sema_wait_timed(&test_sema2, TEST_TIME_US) != 0) { + printf("THREAD ERROR: sema_wait_timed()"); + *thread_success = -1; + } + if (sema_wait(&test_sema2) != -ECANCELED) { + printf("THREAD ERROR: sema_wait()"); + *thread_success = -1; + } + if (*thread_success != -1) { + *thread_success = 1; + } + + return NULL; +} + + +int main(void) +{ + int thread_success = 0; + + sema_create(&test_sema, 0); + sema_create(&test_sema2, 1); + if (sema_try_wait(&test_sema) != -EAGAIN) { + printf("MAIN ERROR: sema_try_wait()"); + return 1; + } + + if (sema_wait_timed(&test_sema, TEST_TIME_US) != -ETIMEDOUT) { + printf("MAIN ERROR: sema_wait_timed()"); + return 1; + } + + thread_create(stack, + sizeof(stack), + THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, + second_thread, + &thread_success, + "second_thread"); + + if (sema_wait(&test_sema) != 0) { + printf("MAIN ERROR: sema_wait()"); + return 1; + } + sema_post(&test_sema2); + + sema_destroy(&test_sema); + sema_destroy(&test_sema2); + sema_post(&test_sema2); + if (sema_wait_timed(&test_sema, TEST_TIME_US) != -ECANCELED) { + printf("MAIN ERROR: sema_wait_timed()"); + return 1; + } + if (sema_wait(&test_sema) != -ECANCELED) { + printf("MAIN ERROR: sema_wait()"); + return 1; + } + if (sema_try_wait(&test_sema) != -ECANCELED) { + printf("MAIN ERROR: sema_wait()"); + return 1; + } + /* UINT_MAX test */ + test_sema.value = UINT_MAX; + if (sema_post(&test_sema) != -EOVERFLOW) { + printf("MAIN ERROR: sema_post()"); + return 1; + } + + if (thread_success == 1) { + printf("SUCCESS\n"); + + } + else { + printf("FAILURE\n"); + } + return 0; +} diff --git a/tests/sema/tests/01-run.py b/tests/sema/tests/01-run.py new file mode 100755 index 0000000000..ef26f0e1fe --- /dev/null +++ b/tests/sema/tests/01-run.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 Freie Universität Berlin, +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +# @author Julian Holzwarth + +import sys +from testrunner import run + + +def testfunc(child): + child.expect("SUCCESS") + + +if __name__ == "__main__": + sys.exit(run(testfunc))