Skip to content

Commit

Permalink
tests/freertos: add test for compat layer
Browse files Browse the repository at this point in the history
Added test for freertos. Only for BOARD:esp32-wroom-32 at the moment.
At the moment it only tests xSemaphoreTake(), xSemaphoreTakeRecursive() and the creation of the semaphore.
It creates the semaphore and calls take for it multiple times and checks whether the return value is correct.
Take should return (quote freertos documention):
"pdPASS Returned only if the call to xSemaphoreTakeRecursive() was successful in obtaining the semaphore."
"pdFAIL Returned if the call to xSemaphoreTakeRecursive() did not successfully obtain the semaphore."
The same for xSemaphoreTake().
The future goal for this test is to test the complete freertos compatibility layer
and to have the test for a general freertos compatibility layer that does not only work for esp32.
  • Loading branch information
JulianHolzwarth committed Mar 27, 2019
1 parent 01fb533 commit 0a7a86d
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/freertos/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include ../Makefile.tests_common

USEMODULE += riot_freertos
USEMODULE += shell
BOARD_WHITELIST := esp32-wroom-32


include $(RIOTBASE)/Makefile.include
103 changes: 103 additions & 0 deletions tests/freertos/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2019 Freie Universitaet 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 freertos testing tool
*
* More detailed information about the file and the functionality implemented.
*
* @author Julian Holzwarth <julian.holzwarth@fu-berlin.de>
*
*/

#include <stdio.h>
#include <stdlib.h>

#include "shell.h"

#include "freertos/FreeRTOS.h"
#include "semaphore_test.h"

/**
* Foward declarations
*/
static int cmd_test_mutex(int argc, char **argv);
static int cmd_test_recursive_mutex(int argc, char **argv);


/**
* @brief List of command for this application.
*/
static const shell_command_t shell_commands[] = {
{ "mutex_semaphore", "tests freertos mutex semaphor", cmd_test_mutex, },
{ "recursive_mutex_semaphore", "tests freertos recursive mutex semaphor", cmd_test_recursive_mutex },
{ NULL, NULL, NULL }
};

/**
* @brief shell command to test freertos mutex semaphore
*
* @param[in] argc Number of arguments
* @param[in] argv Array of arguments
*
* @return 0 on success
*/
static int cmd_test_mutex(int argc, char **argv)
{
(void)argc;
(void)argv;
puts("starting test: mutex semaphore");
if (semaphore_test_mutex_take() == pdPASS) {
puts("OK");
}
else {
puts("mutex semaphore test failed");
}

return 0;
}

/**
* @brief shell command to test freertos recursive mutex semaphore
*
* @param[in] argc Number of arguments
* @param[in] argv Array of arguments
*
* @return 0 on success
*/
static int cmd_test_recursive_mutex(int argc, char **argv)
{
(void)argc;
(void)argv;
puts("starting test: recursive mutex semaphore");
if (semaphore_test_recursive_mutex_take() == pdPASS) {
puts("OK");
}
else {
puts("recursive mutex semaphore test failed");
}
return 0;
}

/**
* @brief main function starting shell
*
* @return 0 on success
*/
int main(void)
{
puts("Starting shell...");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

return 0;
}
82 changes: 82 additions & 0 deletions tests/freertos/semaphore_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2019 Freie Universitaet 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.
*/

#include <stdio.h>
#include <stdlib.h>

#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/FreeRTOS.h"

#include "semaphore_test.h"

/**
* @brief tests the take function for a freertos mutex semaphore
*
* @return pdPASS when the test is passed, pdFail otherwise
*/
int semaphore_test_mutex_take(void)
{
SemaphoreHandle_t testing_semaphore = xSemaphoreCreateMutex();

if (testing_semaphore == NULL) {
puts("test failed: mutex semaphore not created");
return pdFAIL;
}
/*
* Freertos Documentation:
* "pdPASS: Returned only if the call to xSemaphoreTake()
* was successful in obtaining the semaphore."
*/
/* first call should be successful */
if (xSemaphoreTake(testing_semaphore, 0) == pdFAIL) {
puts("error in Take");
vSemaphoreDelete(testing_semaphore);
return pdFAIL;
}
/* after the fist call every call should fail */
if (xSemaphoreTake(testing_semaphore, 0) == pdPASS) {
puts("error in Take");
vSemaphoreDelete(testing_semaphore);
return pdFAIL;
}
vSemaphoreDelete(testing_semaphore);
return pdPASS;
}

/**
* @brief tests the take function for a freertos recursive mutex semaphore
*
* @return pdPASS when the test is passed, pdFAIL otherwise
*/
int semaphore_test_recursive_mutex_take(void)
{
SemaphoreHandle_t testing_semaphore = xSemaphoreCreateRecursiveMutex();

if (testing_semaphore == NULL) {
puts("test failed: recursive mutex semaphore not created");
return pdFAIL;
}
/*
* Freertos Documentation:
* "pdPASS: Returned only if the call to xSemaphoreTakeRecursive()
* was successful in obtaining the semaphore.",
* "once a recursive mutex has been successfully ‘taken’ by a task,
* further calls to xSemaphoreTakeRecursive() made by the
* same task will also be successful."
*/
for (size_t i = 0; i < 3; i++) {
if (xSemaphoreTakeRecursive(testing_semaphore, 0) == pdFAIL) {
puts("error in Take");
vSemaphoreDelete(testing_semaphore);
return pdFAIL;
}
}
vSemaphoreDelete(testing_semaphore);
return pdPASS;
}
47 changes: 47 additions & 0 deletions tests/freertos/semaphore_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2019 Freie Universitaet 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.
*/
#ifndef SEMAPHORE_TEST_H
#define SEMAPHORE_TEST_H

#ifdef __cplusplus
extern "C" {
#endif

/**
* @ingroup tests
* @{
*
* @file
* @brief freertos testing tool
*
* More detailed information about the file and the functionality implemented.
*
* @author Julian Holzwarth <julian.holzwarth@fu-berlin.de>
*
*/


/**
* @brief tests the take function for a freertos mutex semaphore
*
* @return pdPASS when the test is passed, pdFail otherwise
*/
int semaphore_test_mutex_take(void);

/**
* @brief tests the take function for a freertos recursive mutex semaphore
*
* @return pdPASS when the test is passed, pdFAIL otherwise
*/
int semaphore_test_recursive_mutex_take(void);

#ifdef __cplusplus
}
#endif

#endif /* SEMAPHORE_TEST_H */
34 changes: 34 additions & 0 deletions tests/freertos/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

# Copyright (C) 2019 Freie Universitaet 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
import pexpect
from testrunner import run


def testfunc(child):
# Try to wait for the shell
for _ in range(0, 10):
child.sendline("help")
if child.expect_exact(["> ", pexpect.TIMEOUT], timeout=1) == 0:
break
child.sendline("mutex_semaphore")
child.expect("starting test: mutex semaphore")
child.expect("OK")
child.expect_exact("> ")

child.sendline("recursive_mutex_semaphore")
child.expect_exact("starting test: recursive mutex semaphore")
child.expect("OK")
child.expect_exact("> ")


if __name__ == "__main__":
sys.exit(run(testfunc))

0 comments on commit 0a7a86d

Please sign in to comment.