1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

tests/sema/: added sema test

This commit is contained in:
JulianHolzwarth 2020-09-22 10:57:02 +02:00 committed by Francisco Molina
parent 796175591d
commit 20a3a929c8
No known key found for this signature in database
GPG Key ID: 3E94EAC3DBDEEDA8
4 changed files with 155 additions and 0 deletions

5
tests/sema/Makefile Normal file
View File

@ -0,0 +1,5 @@
include ../Makefile.tests_common
USEMODULE += sema
include $(RIOTBASE)/Makefile.include

3
tests/sema/Makefile.ci Normal file
View File

@ -0,0 +1,3 @@
BOARD_INSUFFICIENT_MEMORY := \
nucleo-l011k4 \
#

127
tests/sema/main.c Normal file
View File

@ -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 <julian.holzwarth@fu-berlin.de>
*
*/
#include <errno.h>
#include <stdio.h>
#include <limits.h>
#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;
}

20
tests/sema/tests/01-run.py Executable file
View File

@ -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 <julian.holzwarth@fu-berlin.de>
import sys
from testrunner import run
def testfunc(child):
child.expect("SUCCESS")
if __name__ == "__main__":
sys.exit(run(testfunc))