[修改] 增加freeRTOS
1. 版本FreeRTOSv202212.01,命名为kernel;
This commit is contained in:
136
kernel/FreeRTOS/Test/CMock/queue/semaphore/FreeRTOSConfig.h
Normal file
136
kernel/FreeRTOS/Test/CMock/queue/semaphore/FreeRTOSConfig.h
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef FREERTOS_CONFIG_H
|
||||
#define FREERTOS_CONFIG_H
|
||||
|
||||
#include "fake_assert.h"
|
||||
|
||||
/*-----------------------------------------------------------
|
||||
* Application specific definitions.
|
||||
*
|
||||
* These definitions should be adjusted for your particular hardware and
|
||||
* application requirements.
|
||||
*
|
||||
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
|
||||
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. See
|
||||
* https://www.FreeRTOS.org/a00110.html
|
||||
*----------------------------------------------------------*/
|
||||
|
||||
#define configUSE_PREEMPTION 1
|
||||
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
|
||||
#define configUSE_IDLE_HOOK 1
|
||||
#define configUSE_TICK_HOOK 1
|
||||
#define configUSE_DAEMON_TASK_STARTUP_HOOK 1
|
||||
#define configTICK_RATE_HZ ( 1000 ) /* In this non-real time simulated environment the tick frequency has to be at least a multiple of the Win32 tick frequency, and therefore very slow. */
|
||||
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 70 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the win32 thread. */
|
||||
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 52 * 1024 ) )
|
||||
#define configMAX_TASK_NAME_LEN ( 12 )
|
||||
#define configUSE_TRACE_FACILITY 0
|
||||
#define configUSE_16_BIT_TICKS 0
|
||||
#define configIDLE_SHOULD_YIELD 1
|
||||
#define configUSE_MUTEXES 1
|
||||
#define configCHECK_FOR_STACK_OVERFLOW 0
|
||||
#define configUSE_RECURSIVE_MUTEXES 1
|
||||
#define configQUEUE_REGISTRY_SIZE 0
|
||||
#define configUSE_MALLOC_FAILED_HOOK 1
|
||||
#define configUSE_APPLICATION_TASK_TAG 1
|
||||
#define configUSE_COUNTING_SEMAPHORES 1
|
||||
#define configUSE_ALTERNATIVE_API 0
|
||||
#define configUSE_QUEUE_SETS 0
|
||||
#define configUSE_TASK_NOTIFICATIONS 1
|
||||
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 5
|
||||
#define configSUPPORT_STATIC_ALLOCATION 1
|
||||
#define configINITIAL_TICK_COUNT ( ( TickType_t ) 0 ) /* For test. */
|
||||
#define configSTREAM_BUFFER_TRIGGER_LEVEL_TEST_MARGIN 1 /* As there are a lot of tasks running. */
|
||||
|
||||
/* Software timer related configuration options. */
|
||||
#define configUSE_TIMERS 1
|
||||
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
|
||||
#define configTIMER_QUEUE_LENGTH 20
|
||||
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 )
|
||||
|
||||
#define configMAX_PRIORITIES ( 7 )
|
||||
|
||||
/* Run time stats gathering configuration options. */
|
||||
unsigned long ulGetRunTimeCounterValue( void ); /* Prototype of function that returns run time counter. */
|
||||
void vConfigureTimerForRunTimeStats( void ); /* Prototype of function that initialises the run time counter. */
|
||||
#define configGENERATE_RUN_TIME_STATS 1
|
||||
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats()
|
||||
#define portGET_RUN_TIME_COUNTER_VALUE() ulGetRunTimeCounterValue()
|
||||
|
||||
/* This demo makes use of one or more example stats formatting functions. These
|
||||
* format the raw data provided by the uxTaskGetSystemState() function in to human
|
||||
* readable ASCII form. See the notes in the implementation of vTaskList() within
|
||||
* FreeRTOS/Source/tasks.c for limitations. */
|
||||
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
|
||||
|
||||
/* Set the following definitions to 1 to include the API function, or zero
|
||||
* to exclude the API function. In most cases the linker will remove unused
|
||||
* functions anyway. */
|
||||
#define INCLUDE_vTaskPrioritySet 1
|
||||
#define INCLUDE_uxTaskPriorityGet 1
|
||||
#define INCLUDE_vTaskDelete 1
|
||||
#define INCLUDE_vTaskCleanUpResources 0
|
||||
#define INCLUDE_vTaskSuspend 1
|
||||
#define INCLUDE_vTaskDelayUntil 1
|
||||
#define INCLUDE_vTaskDelay 1
|
||||
#define INCLUDE_uxTaskGetStackHighWaterMark 1
|
||||
#define INCLUDE_xTaskGetSchedulerState 1
|
||||
#define INCLUDE_xTimerGetTimerDaemonTaskHandle 1
|
||||
#define INCLUDE_xTaskGetIdleTaskHandle 1
|
||||
#define INCLUDE_xTaskGetHandle 1
|
||||
#define INCLUDE_eTaskGetState 1
|
||||
#define INCLUDE_xSemaphoreGetMutexHolder 1
|
||||
#define INCLUDE_xTimerPendFunctionCall 1
|
||||
#define INCLUDE_xTaskAbortDelay 1
|
||||
|
||||
/* It is a good idea to define configASSERT() while developing. configASSERT()
|
||||
* uses the same semantics as the standard C assert() macro. */
|
||||
#define configASSERT( x ) \
|
||||
do \
|
||||
{ \
|
||||
if( x ) \
|
||||
{ \
|
||||
vFakeAssert( true, __FILE__, __LINE__ ); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
vFakeAssert( false, __FILE__, __LINE__ ); \
|
||||
} \
|
||||
} while( 0 )
|
||||
|
||||
|
||||
#define mtCOVERAGE_TEST_MARKER() __asm volatile ( "NOP" )
|
||||
|
||||
#define configINCLUDE_MESSAGE_BUFFER_AMP_DEMO 0
|
||||
#if ( configINCLUDE_MESSAGE_BUFFER_AMP_DEMO == 1 )
|
||||
extern void vGenerateCoreBInterrupt( void * xUpdatedMessageBuffer );
|
||||
#define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreBInterrupt( pxStreamBuffer )
|
||||
#endif /* configINCLUDE_MESSAGE_BUFFER_AMP_DEMO */
|
||||
|
||||
#endif /* FREERTOS_CONFIG_H */
|
||||
52
kernel/FreeRTOS/Test/CMock/queue/semaphore/Makefile
Normal file
52
kernel/FreeRTOS/Test/CMock/queue/semaphore/Makefile
Normal file
@ -0,0 +1,52 @@
|
||||
# Indent with spaces
|
||||
.RECIPEPREFIX := $(.RECIPEPREFIX) $(.RECIPEPREFIX)
|
||||
|
||||
# Do not move this line below the include
|
||||
MAKEFILE_ABSPATH := $(abspath $(lastword $(MAKEFILE_LIST)))
|
||||
include ../../makefile.in
|
||||
|
||||
# PROJECT_SRC lists the .c files under test
|
||||
PROJECT_SRC += queue.c
|
||||
|
||||
# PROJECT_DEPS_SRC list the .c file that are dependencies of PROJECT_SRC files
|
||||
# Files in PROJECT_DEPS_SRC are excluded from coverage measurements
|
||||
PROJECT_DEPS_SRC += list.c
|
||||
|
||||
# PROJECT_HEADER_DEPS: headers that should be excluded from coverage measurements.
|
||||
PROJECT_HEADER_DEPS += FreeRTOS.h
|
||||
|
||||
# SUITE_UT_SRC: .c files that contain test cases (must end in _utest.c)
|
||||
SUITE_UT_SRC += semaphore_create_utest.c
|
||||
SUITE_UT_SRC += binary_semaphore_utest.c
|
||||
SUITE_UT_SRC += counting_semaphore_utest.c
|
||||
SUITE_UT_SRC += semaphore_common_utest.c
|
||||
SUITE_UT_SRC += mutex_utest.c
|
||||
SUITE_UT_SRC += recursive_mutex_utest.c
|
||||
|
||||
# SUITE_SUPPORT_SRC: .c files used for testing that do not contain test cases.
|
||||
# Paths are relative to PROJECT_DIR
|
||||
SUITE_SUPPORT_SRC += queue_utest_common.c
|
||||
SUITE_SUPPORT_SRC += td_task.c
|
||||
SUITE_SUPPORT_SRC += td_port.c
|
||||
|
||||
# List the headers used by PROJECT_SRC that you would like to mock
|
||||
MOCK_FILES_FP += $(KERNEL_DIR)/include/task.h
|
||||
MOCK_FILES_FP += $(UT_ROOT_DIR)/config/fake_assert.h
|
||||
MOCK_FILES_FP += $(UT_ROOT_DIR)/config/fake_port.h
|
||||
|
||||
# List any addiitonal flags needed by the preprocessor
|
||||
CPPFLAGS += -DportUSING_MPU_WRAPPERS=0
|
||||
|
||||
# List any addiitonal flags needed by the compiler
|
||||
CFLAGS +=
|
||||
|
||||
# Try not to edit beyond this line unless necessary.
|
||||
|
||||
# Project / Suite are determined based on path: $(UT_ROOT_DIR)/$(PROJECT)/$(SUITE)
|
||||
PROJECT := $(lastword $(subst /, ,$(dir $(abspath $(MAKEFILE_ABSPATH)/../))))
|
||||
SUITE := $(lastword $(subst /, ,$(dir $(MAKEFILE_ABSPATH))))
|
||||
|
||||
# Make variables available to included makefile
|
||||
export
|
||||
|
||||
include ../../testdir.mk
|
||||
@ -0,0 +1,901 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
/*! @file binary_semaphore_utest.c */
|
||||
|
||||
#include "../queue_utest_common.h"
|
||||
#include "mock_fake_port.h"
|
||||
|
||||
/* Queue includes */
|
||||
#include "FreeRTOS.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "semphr.h"
|
||||
|
||||
/* =============================== CONSTANTS =============================== */
|
||||
|
||||
/* ============================ GLOBAL VARIABLES =========================== */
|
||||
|
||||
/* Used to share a semaphore handle between a test case and callback */
|
||||
static SemaphoreHandle_t xSemaphoreHandleStatic;
|
||||
|
||||
/* ========================== CALLBACK FUNCTIONS =========================== */
|
||||
|
||||
/* ============================= Unity Fixtures ============================= */
|
||||
|
||||
void setUp( void )
|
||||
{
|
||||
commonSetUp();
|
||||
}
|
||||
|
||||
void tearDown( void )
|
||||
{
|
||||
commonTearDown();
|
||||
}
|
||||
|
||||
void suiteSetUp()
|
||||
{
|
||||
commonSuiteSetUp();
|
||||
}
|
||||
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return commonSuiteTearDown( numFailures );
|
||||
}
|
||||
|
||||
/* ========================== Helper functions =========================== */
|
||||
|
||||
/* ============================= Test Cases ============================== */
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with a Binary Semaphore
|
||||
* @details Create a binary semaphore using xSemaphoreCreateBinary
|
||||
* and verify that an immediate call to xSemaphoreTake fails.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_xSemaphoreCreateBinary_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreTake operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with xSemaphoreCreateBinary
|
||||
* @details Create a binary semaphore using xSemaphoreCreateBinary
|
||||
* and verify that an immediate call to xSemaphoreGive succeeds.
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_xSemaphoreCreateBinary_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @brief Test xSemaphoreTake with vSemaphoreCreateBinary
|
||||
* @details Create a semaphore using vSemaphoreCreateBinary and verify that a
|
||||
* subsequent call to xSemaphoreTake succeeds.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_vSemaphoreCreateBinary_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
|
||||
vSemaphoreCreateBinary( xSemaphore );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @brief Test xSemaphoreGive with vSemaphoreCreateBinary
|
||||
* @details Create a semaphore using vSemaphoreCreateBinary and verify that a
|
||||
* subsequent call to xSemaphoreGive fails.
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_vSemaphoreCreateBinary_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
|
||||
vSemaphoreCreateBinary( xSemaphore );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreGive operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive and xSemaphoreTake with xSemaphoreCreateBinary
|
||||
* @details Create a binary semaphore using xSemaphoreCreateBinary
|
||||
* and verify that an immediate call to xSemaphoreGive succeeds and a subsequent
|
||||
* call to xSemaphoreTake succeeds.
|
||||
* @coverage xQueueGenericSend xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_xSemaphoreTake_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Verify that a subsequent xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive multiple times on a Binary Semaphore
|
||||
* @details Create a binary semaphore using xSemaphoreCreateBinary
|
||||
* and verify that an immediate call to xSemaphoreGive succeeds and a subsequent
|
||||
* call to xSemaphoreGive fails.
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_multiple_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Verify that the second xSemaphoreGive operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake multiple times on a Binary Semaphore
|
||||
* @details Create a binary semaphore using xSemaphoreCreateBinary,
|
||||
* verify that an immediate call to xSemaphoreGive succeeds, a subsequent
|
||||
* call to xSemaphoreTake succeds, but a second call to xSemaphoreTake fails.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_multiple_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Verify that a subsequent xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Verify that a second xSemaphoreTake operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test uxSemaphoreGetCount with a Binary Semaphore
|
||||
* @details Create a binary semaphore using vSemaphoreCreateBinary.
|
||||
* validate the return value of uxSemaphoreGetCount(),
|
||||
* call xSemaphoreTake() and validate the return value of uxSemaphoreGetCount()
|
||||
* @coverage uxQueueMessagesWaiting
|
||||
*/
|
||||
void test_macro_uxSemaphoreGetCount( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
|
||||
vSemaphoreCreateBinary( xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
( void ) xSemaphoreTake( xSemaphore, 0 );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTakeFromISR with a Binary Semaphore
|
||||
* @coverage xQueueReceiveFromISR
|
||||
**/
|
||||
void test_macro_xSemaphoreTakeFromISR_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
/* Give the Binary Semaphore */
|
||||
( void ) xSemaphoreGive( xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTakeFromISR( xSemaphore, NULL ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief xSemaphoreGiveFromISR with an empty queue
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief xSemaphoreGiveFromISR with a full queue
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
TEST_ASSERT_EQUAL( errQUEUE_FULL, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with a higher priority task waiting and a null pointer for pxHigherPriorityTaskWoken
|
||||
* @details Test xSemaphoreGiveFromISR with a higher priority task waiting and
|
||||
* verifies that a null pxHigherPriorityTaskWoken is handled correctly.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_high_priority_pending_null_ptr( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
/* Give the Binary Semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, td_task_getYieldPending() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with a higher priority task waiting
|
||||
* @details Test xSemaphoreGiveFromISR with a higher priority task waiting and
|
||||
* verify that xHigherPriorityTaskWoken is set accordingly.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_high_priority_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
/* Give the semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xHigherPriorityTaskWoken );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, td_task_getYieldPending() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with a lower priority task waiting
|
||||
* @details Test xSemaphoreGiveFromISR with a lower priority task waiting and
|
||||
* verify that xHigherPriorityTaskWoken is not modified.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_low_priority_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
/* Give the semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xHigherPriorityTaskWoken );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, td_task_getYieldPending() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with no tasks waiting
|
||||
* @details Test xSemaphoreGiveFromISR with no tasks waiting and verify that xHigherPriorityTaskWoken is not modified.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_no_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
/* Give the semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xHigherPriorityTaskWoken );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, td_task_getYieldPending() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR on a semaphore that is locked
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_xSemaphoreGiveFromISR_locked( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* Set private lock counters */
|
||||
vSetQueueRxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
vSetQueueTxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
/* Verify that the cRxLock counter has not changed */
|
||||
TEST_ASSERT_EQUAL( queueLOCKED_UNMODIFIED, cGetQueueRxLock( xSemaphore ) );
|
||||
|
||||
/* Verify that the cTxLock counter has been incremented */
|
||||
TEST_ASSERT_EQUAL( queueLOCKED_UNMODIFIED + 1, cGetQueueTxLock( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR on a semaphore that is locked and cRxLock overflows.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_xSemaphoreGiveFromISR_locked_overflow( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* Set private lock counters */
|
||||
vSetQueueRxLock( xSemaphore, INT8_MAX );
|
||||
vSetQueueTxLock( xSemaphore, INT8_MAX );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
/* The number of tasks need to be more than 127 to trigger the
|
||||
* overflow assertion. */
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 128 );
|
||||
|
||||
|
||||
/* Expect an assertion since the cTxLock value has overflowed */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
/* Verify that the cRxLock counter has not changed */
|
||||
TEST_ASSERT_EQUAL( INT8_MAX, cGetQueueRxLock( xSemaphore ) );
|
||||
|
||||
/* Verify that the cTxLock counter has been incremented */
|
||||
TEST_ASSERT_EQUAL( INT8_MIN, cGetQueueTxLock( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with an occupied semaphore with higher priority tasks waiting
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_tasks_waiting_higher_priority( void )
|
||||
{
|
||||
/* Create a new binary semaphore */
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
( void ) xSemaphoreGive( xSemaphore );
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
|
||||
td_task_addFakeTaskWaitingToSendToQueue( xSemaphore );
|
||||
|
||||
/* take the semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 1, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 1, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with an occupied semaphore with an equal priority task waiting
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_tasks_waiting_equal_priority( void )
|
||||
{
|
||||
/* Create a new binary semaphore */
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
( void ) xSemaphoreGive( xSemaphore );
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY );
|
||||
td_task_addFakeTaskWaitingToSendToQueue( xSemaphore );
|
||||
|
||||
/* take the semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with an occupied semaphore with lower priority tasks waiting.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_tasks_waiting_lower_priority( void )
|
||||
{
|
||||
/* Create a new binary semaphore */
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
( void ) xSemaphoreGive( xSemaphore );
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
|
||||
td_task_addFakeTaskWaitingToSendToQueue( xSemaphore );
|
||||
|
||||
/* take the semaphore */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with taskSCHEDULER_SUSPENDED and timeout=10
|
||||
* @details This should cause xSemaphoreTake to configASSERT because it would
|
||||
* block forever when the semaphore is empty.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_suspended_assert( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
fakeAssertExpectFail();
|
||||
|
||||
vTaskSuspendAll_Stub( td_task_vTaskSuspendAllStubNoCheck );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_SUSPENDED );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, fakeAssertGetFlagAndClear() );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_RUNNING );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with taskSCHEDULER_SUSPENDED and timeout=0
|
||||
* @details This should not cause xSemaphoreTake to configASSERT because
|
||||
* xSemaphoreTake is non-blocking when timeout=0.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_nonblocking_suspended_noassert( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_SUSPENDED );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_RUNNING );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback which calls xSemaphoreGive on xSemaphoreHandleStatic
|
||||
*/
|
||||
static BaseType_t blocking_success_xTaskCheckForTimeOut_cb( TimeOut_t * const pxTimeOut,
|
||||
TickType_t * const pxTicksToWait,
|
||||
int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
( void ) xSemaphoreGiveFromISR( xSemaphoreHandleStatic, NULL );
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a taken Binary Semaphore
|
||||
* which becomes available while a call to xSemaphoreTake is blocking.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* Export for blocking_success_xTaskCheckForTimeOut_cb callback */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &blocking_success_xTaskCheckForTimeOut_cb );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback which calls xSemaphoreGive on xSemaphoreHandleStatic when
|
||||
* cmock_num_calls == TICKS_TO_WAIT
|
||||
*/
|
||||
static BaseType_t blocking_last_chance_xTaskCheckForTimeOut_cb( TimeOut_t * const pxTimeOut,
|
||||
TickType_t * const pxTicksToWait,
|
||||
int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == TICKS_TO_WAIT )
|
||||
{
|
||||
( void ) xSemaphoreGiveFromISR( xSemaphoreHandleStatic, NULL );
|
||||
return pdTRUE;
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a Binary Semaphore that is initially taken,
|
||||
* but becomes available at the end of the blocking time period.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success_last_chance( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* Export for blocking_success_xTaskCheckForTimeOut_cb callback */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &blocking_last_chance_xTaskCheckForTimeOut_cb );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a taken binary semaphore
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_timeout( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a taken locked semaphore
|
||||
* @details This test case verifies a situation that should never occur
|
||||
* ( xSemaphoreTake called on a locked semaphore ).
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_locked( void )
|
||||
{
|
||||
/* Create a new binary semaphore */
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* Set private lock counters */
|
||||
vSetQueueRxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
vSetQueueTxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
|
||||
/* Run xSemaphoreTake in blocking mode with the semaphore locked */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
/* Verify that the semaphore is now unlocked */
|
||||
TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueRxLock( xSemaphore ) );
|
||||
TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueTxLock( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback for test_xSemaphoreTake_blocking_success_locked_no_pending
|
||||
* which adds an item to it's test queue.
|
||||
*/
|
||||
static BaseType_t xSemaphoreTake_xTaskCheckForTimeOutCB( TimeOut_t * const pxTimeOut,
|
||||
TickType_t * const pxTicksToWait,
|
||||
int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
TEST_ASSERT_TRUE( xSemaphoreGiveFromISR( xSemaphoreHandleStatic, NULL ) );
|
||||
TEST_ASSERT_EQUAL( 1, uxQueueMessagesWaiting( xSemaphoreHandleStatic ) );
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test a blocking call to xSemaphoreTake with a locked binary semaphore.
|
||||
* @details Test a blocking call to xSemaphoreTake with a locked binary semaphore with no
|
||||
* tasks in the binary semaphore WaitingToReceiveFrom event list.
|
||||
* @coverage xQueueSemaphoreTake prvUnlockQueue
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success_locked_no_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
/* Export for callbacks */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &xSemaphoreTake_xTaskCheckForTimeOutCB );
|
||||
xTaskResumeAll_Stub( &td_task_xTaskResumeAllStub );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vQueueDelete( xSemaphore );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Callback for xTaskResumeAll used by tests for blocking calls to
|
||||
* xSemaphoreTake
|
||||
*/
|
||||
static BaseType_t xSemaphoreTake_xTaskResumeAllCallback( int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskResumeAllStub( cmock_num_calls );
|
||||
|
||||
/* If td_task_xTaskResumeAllStub returns pdTRUE, a higher priority task is pending
|
||||
* Receive from an ISR to block */
|
||||
if( pdTRUE == xReturnValue )
|
||||
{
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphoreHandleStatic ) );
|
||||
TEST_ASSERT_TRUE( xSemaphoreTakeFromISR( xSemaphoreHandleStatic, NULL ) );
|
||||
}
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test a blocking call to xSemaphoreTake with a locked binary semaphore.
|
||||
* @details Test a blocking call to xSemaphoreTake with a locked binary semaphore with a
|
||||
* higher priority task in the binary semaphore WaitingToReceiveFrom event list.
|
||||
* @coverage xQueueSemaphoreTake prvUnlockQueue
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_timeout_locked_high_prio_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
/* Export for callbacks */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &xSemaphoreTake_xTaskCheckForTimeOutCB );
|
||||
xTaskResumeAll_Stub( &xSemaphoreTake_xTaskResumeAllCallback );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
|
||||
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT + 1, td_task_getCount_YieldFromTaskResumeAll() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT - 1, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 1, td_task_getCount_vTaskMissedYield() );
|
||||
|
||||
vQueueDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test a blocking call to xSemaphoreTake with a locked binary semaphore.
|
||||
* @details Test a blocking call to xSemaphoreTake with a locked binary semaphore with a
|
||||
* lower priority task in the semaphore WaitingToReceiveFrom event list.
|
||||
* @coverage xQueueSemaphoreTake prvUnlockQueue
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success_locked_low_prio_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
/* Export for callbacks */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &xSemaphoreTake_xTaskCheckForTimeOutCB );
|
||||
xTaskResumeAll_Stub( &xSemaphoreTake_xTaskResumeAllCallback );
|
||||
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
|
||||
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vQueueDelete( xSemaphore );
|
||||
}
|
||||
@ -0,0 +1,704 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
/*! @file counting_semaphore_utest.c */
|
||||
|
||||
#include "../queue_utest_common.h"
|
||||
|
||||
/* Queue includes */
|
||||
#include "FreeRTOS.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "semphr.h"
|
||||
#include "mock_fake_port.h"
|
||||
|
||||
|
||||
/* ============================ GLOBAL VARIABLES =========================== */
|
||||
|
||||
static SemaphoreHandle_t xSemaphoreHandleStatic;
|
||||
|
||||
/* ========================== CALLBACK FUNCTIONS =========================== */
|
||||
|
||||
/* ============================= Unity Fixtures ============================= */
|
||||
|
||||
void setUp( void )
|
||||
{
|
||||
commonSetUp();
|
||||
xSemaphoreHandleStatic = NULL;
|
||||
}
|
||||
|
||||
void tearDown( void )
|
||||
{
|
||||
commonTearDown();
|
||||
}
|
||||
|
||||
void suiteSetUp()
|
||||
{
|
||||
commonSuiteSetUp();
|
||||
}
|
||||
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return commonSuiteTearDown( numFailures );
|
||||
}
|
||||
|
||||
/* =========================== Helper functions ============================ */
|
||||
|
||||
/* ============================== Test Cases =============================== */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with xSemaphoreCreateCounting( 1, 0 )
|
||||
* @details Test xSemaphoreTake with a binary semaphore constructed with
|
||||
* xSemaphoreCreateCounting.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_CountingSemaphore_one_zero_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 1, 0 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an xSemaphoreTake fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with xSemaphoreCreateCounting( 1, 0 )
|
||||
* @details Test xSemaphoreGive with a binary semaphore constructed with
|
||||
* xSemaphoreCreateCounting.
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_CountingSemaphore_one_zero_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 1, 0 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an xSemaphoreGive succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive and xSemaphoreTake with xSemaphoreCreateCounting( 1, 0 )
|
||||
* @details Test xSemaphoreGive and xSemaphoreTake with a binary semaphore
|
||||
* constructed with xSemaphoreCreateCounting.
|
||||
* @coverage xQueueGenericSend xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_xSemaphoreTake_CountingSemaphore_one_zero_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 1, 0 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an xSemaphoreGive succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Verify that an xSemaphoreTake succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Verify that an xSemaphoreGive succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with xSemaphoreCreateCounting( 100, 50 )
|
||||
* @details Test multiple xSemaphoreGive calls on a counting semaphore
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_CountingSemaphore_100_50( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 100, 50 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Call xSemaphoreGive 50 times and verify success */
|
||||
for( int i = 0; i < 50; i++ )
|
||||
{
|
||||
/* Verify that an xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
}
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( 100, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Veirfy that a subsequent call to xSemaphoreGive fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Verify that an xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( 99, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with xSemaphoreCreateCounting( 100, 50 )
|
||||
* @details Test multiple xSemaphoreTake calls on a counting semaphore
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_CountingSemaphore_100_50( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 100, 50 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Call xSemaphoreTake 50 times and verify success */
|
||||
for( int i = 0; i < 50; i++ )
|
||||
{
|
||||
/* Verify that an xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
}
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Veirfy that a subsequent call to xSemaphoreGive fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Verify that an xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that this test case is running on a 64 bit platform.
|
||||
*/
|
||||
void test_size_of_UBaseType_is_64_bits( void )
|
||||
{
|
||||
if( TEST_PROTECT() )
|
||||
{
|
||||
/* Check that UBaseType_t is 64 bits on this platform */
|
||||
TEST_ASSERT_EQUAL( sizeof( uint64_t ), sizeof( UBaseType_t ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX-1 )
|
||||
* @details Test xSemaphoreGive with a counting semaphore with uxMaxCount=UINT64_MAX and
|
||||
* uxInitialCount=UINT64_MAX-1
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_CountingSemaphore_upper_bound( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX - 1 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Check the initial count */
|
||||
TEST_ASSERT_EQUAL( UINT64_MAX - 1, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that a xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( UINT64_MAX, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that the next xSemaphoreGive operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX )
|
||||
* @details Test xSemaphoreTake with a counting semaphore with uxMaxCount=UINT64_MAX and
|
||||
* uxInitialCount=UINT64_MAX
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_CountingSemaphore_upper_bound( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Verify that a xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( UINT64_MAX - 1, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that a subsequent xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( UINT64_MAX, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that the next xSemaphoreGive operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief xSemaphoreTake with xSemaphoreCreateCounting( UINT64_MAX, 1)
|
||||
* @details Test xSemaphoreTake with a counting semaphore with uxMaxCount=UINT64_MAX and
|
||||
* uxInitialCount=1
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_CountingSemaphore_lower_bound( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX, 1 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Check the initial count */
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that a xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that the next xSemaphoreTake operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief xSemaphoreGive with xSemaphoreCreateCounting( UINT64_MAX, 0)
|
||||
* @details Test xSemaphoreGive with a counting semaphore with uxMaxCount=UINT64_MAX and
|
||||
* uxInitialCount=0
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_CountingSemaphore_lower_bound( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX, 0 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Check the initial count */
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Verify that a xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Check the count */
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with taskSCHEDULER_SUSPENDED and timeout=10
|
||||
* @details This should cause xSemaphoreTake to configASSERT because it would
|
||||
* block forever when the semaphore is empty.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_suspended_assert( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
fakeAssertExpectFail();
|
||||
|
||||
vTaskSuspendAll_Stub( td_task_vTaskSuspendAllStubNoCheck );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_SUSPENDED );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, fakeAssertGetFlagAndClear() );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_RUNNING );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with taskSCHEDULER_SUSPENDED and timeout=0
|
||||
* @details This should not cause xSemaphoreTake to configASSERT because
|
||||
* xSemaphoreTake is non-blocking when timeout=0.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_nonblocking_suspended_noassert( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_SUSPENDED );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
td_task_setSchedulerState( taskSCHEDULER_RUNNING );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback which calls xSemaphoreGive on xSemaphoreHandleStatic when
|
||||
* cmock_num_calls == NUM_CALLS_TO_INTERCEPT
|
||||
*/
|
||||
static BaseType_t blocking_xTaskCheckForTimeOut_cb( TimeOut_t * const pxTimeOut,
|
||||
TickType_t * const pxTicksToWait,
|
||||
int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
( void ) xSemaphoreGiveFromISR( xSemaphoreHandleStatic, NULL );
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphoreHandleStatic ) );
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a fully taken Counting Semaphore
|
||||
* which becomes available while a call to xSemaphoreTake is blocking.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
/* Export for blocking_success_xTaskCheckForTimeOut_cb callback */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &blocking_xTaskCheckForTimeOut_cb );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback which calls xSemaphoreGive on xSemaphoreHandleStatic when
|
||||
* cmock_num_calls == TICKS_TO_WAIT
|
||||
*/
|
||||
static BaseType_t blocking_last_chance_xTaskCheckForTimeOut_cb( TimeOut_t * const pxTimeOut,
|
||||
TickType_t * const pxTicksToWait,
|
||||
int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == TICKS_TO_WAIT )
|
||||
{
|
||||
( void ) xSemaphoreGiveFromISR( xSemaphoreHandleStatic, NULL );
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphoreHandleStatic ) );
|
||||
return pdTRUE;
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a Counting Semaphore that is
|
||||
* initially fully taken, but becomes available at the end of the blocking time.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success_last_chance( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
/* Export for blocking_success_xTaskCheckForTimeOut_cb callback */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &blocking_last_chance_xTaskCheckForTimeOut_cb );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Test xSemaphoreTake in blocking mode with a taken binary semaphore
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_timeout( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode with a taken locked semaphore
|
||||
* @details This test case verifies a situation that should never occur
|
||||
* ( xSemaphoreTake called on a locked semaphore ).
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_locked( void )
|
||||
{
|
||||
/* Create a new binary semaphore */
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
/* Set private lock counters */
|
||||
vSetQueueRxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
vSetQueueTxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
|
||||
/* Run xSemaphoreTake in blocking mode with the semaphore locked */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
/* Verify that the semaphore is now unlocked */
|
||||
TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueRxLock( xSemaphore ) );
|
||||
TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueTxLock( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback for test_xSemaphoreTake_blocking_success_locked_no_pending
|
||||
* which adds an item to it's test queue.
|
||||
*/
|
||||
static BaseType_t xSemaphoreTake_xTaskCheckForTimeOutCB( TimeOut_t * const pxTimeOut,
|
||||
TickType_t * const pxTicksToWait,
|
||||
int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskCheckForTimeOutStub( pxTimeOut, pxTicksToWait, cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
TEST_ASSERT_TRUE( xSemaphoreGiveFromISR( xSemaphoreHandleStatic, NULL ) );
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphoreHandleStatic ) );
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test a blocking call to xSemaphoreTake with a locked counting semaphore.
|
||||
* @details Test a blocking call to xSemaphoreTake with a locked counting semaphore with no
|
||||
* tasks in the counting semaphore WaitingToReceiveFrom event list.
|
||||
* @coverage xQueueSemaphoreTake prvUnlockQueue
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success_locked_no_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
/* Export for callbacks */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &xSemaphoreTake_xTaskCheckForTimeOutCB );
|
||||
xTaskResumeAll_Stub( &td_task_xTaskResumeAllStub );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vQueueDelete( xSemaphore );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Callback for xTaskResumeAll used by tests for blocking calls to
|
||||
* xSemaphoreTake
|
||||
*/
|
||||
static BaseType_t xSemaphoreTake_xTaskResumeAllCallback( int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskResumeAllStub( cmock_num_calls );
|
||||
|
||||
/* If td_task_xTaskResumeAllStub returns pdTRUE, a higher priority task is pending
|
||||
* Receive from an ISR to block */
|
||||
if( pdTRUE == xReturnValue )
|
||||
{
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
TEST_ASSERT_EQUAL( 1, uxSemaphoreGetCount( xSemaphoreHandleStatic ) );
|
||||
TEST_ASSERT_TRUE( xSemaphoreTakeFromISR( xSemaphoreHandleStatic, NULL ) );
|
||||
}
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test a blocking call to xSemaphoreTake with a locked counting semaphore.
|
||||
* @details Test a blocking call to xSemaphoreTake with a locked counting semaphore with a
|
||||
* higher priority task in the counting semaphore WaitingToReceiveFrom event list.
|
||||
* @coverage xQueueSemaphoreTake prvUnlockQueue
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_timeout_locked_high_prio_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
/* Export for callbacks */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &xSemaphoreTake_xTaskCheckForTimeOutCB );
|
||||
xTaskResumeAll_Stub( &xSemaphoreTake_xTaskResumeAllCallback );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
|
||||
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT + 1, td_task_getCount_YieldFromTaskResumeAll() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT - 1, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
TEST_ASSERT_EQUAL( 1, td_task_getCount_vTaskMissedYield() );
|
||||
|
||||
vQueueDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test a blocking call to xSemaphoreTake with a locked counting semaphore.
|
||||
* @details Test a blocking call to xSemaphoreTake with a locked counting semaphore with a
|
||||
* lower priority task in the semaphore WaitingToReceiveFrom event list.
|
||||
* @coverage xQueueSemaphoreTake prvUnlockQueue
|
||||
*/
|
||||
void test_xSemaphoreTake_blocking_success_locked_low_prio_pending( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
|
||||
/* Export for callbacks */
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
xTaskCheckForTimeOut_Stub( &xSemaphoreTake_xTaskCheckForTimeOutCB );
|
||||
xTaskResumeAll_Stub( &xSemaphoreTake_xTaskResumeAllCallback );
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
|
||||
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vQueueDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR on a semaphore that is locked
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_locked( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 2, 0 );
|
||||
|
||||
/* Set private lock counters */
|
||||
vSetQueueRxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
vSetQueueTxLock( xSemaphore, queueLOCKED_UNMODIFIED );
|
||||
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Ignore();
|
||||
uxTaskGetNumberOfTasks_IgnoreAndReturn( 1 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
/* Verify that the cRxLock counter has not changed */
|
||||
TEST_ASSERT_EQUAL( queueLOCKED_UNMODIFIED, cGetQueueRxLock( xSemaphore ) );
|
||||
|
||||
/* Verify that the cTxLock counter has only been incremented by one
|
||||
* even after 2 calls to xQueueSendFromISR because there is only
|
||||
* one task in the system as returned from uxTaskGetNumberOfTasks. */
|
||||
TEST_ASSERT_EQUAL( queueLOCKED_UNMODIFIED + 1, cGetQueueTxLock( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
588
kernel/FreeRTOS/Test/CMock/queue/semaphore/mutex_utest.c
Normal file
588
kernel/FreeRTOS/Test/CMock/queue/semaphore/mutex_utest.c
Normal file
@ -0,0 +1,588 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
/*! @file mutex_utest.c */
|
||||
|
||||
#include "../queue_utest_common.h"
|
||||
|
||||
/* Queue includes */
|
||||
#include "FreeRTOS.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "semphr.h"
|
||||
#include "mock_fake_port.h"
|
||||
|
||||
/* ============================ GLOBAL VARIABLES =========================== */
|
||||
static SemaphoreHandle_t xSemaphoreHandleStatic = NULL;
|
||||
|
||||
/* ========================== CALLBACK FUNCTIONS =========================== */
|
||||
|
||||
/* ============================= Unity Fixtures ============================= */
|
||||
|
||||
void setUp( void )
|
||||
{
|
||||
commonSetUp();
|
||||
}
|
||||
|
||||
void tearDown( void )
|
||||
{
|
||||
commonTearDown();
|
||||
}
|
||||
|
||||
void suiteSetUp()
|
||||
{
|
||||
commonSuiteSetUp();
|
||||
}
|
||||
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return commonSuiteTearDown( numFailures );
|
||||
}
|
||||
|
||||
/* ========================== Helper functions =========================== */
|
||||
|
||||
/* ========================== Test Cases =========================== */
|
||||
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateMutex where the call to malloc fails
|
||||
* @coverage xQueueCreateMutex
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateMutex_malloc_fail( void )
|
||||
{
|
||||
UnityMalloc_MakeMallocFailAfterCount( 0 );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateMutex
|
||||
* @details Create a mutex using xSemaphoreCreateMutex
|
||||
* @coverage xQueueCreateMutex
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateMutex_success( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateMutexStatic with a null buffer
|
||||
* @coverage xQueueCreateMutexStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateMutexStatic_nullptr( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
/* Expect that xQueueCreate will assert due to the NULL buffer */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
xSemaphore = xSemaphoreCreateMutexStatic( NULL );
|
||||
|
||||
/* Check that configASSERT was called twice */
|
||||
fakeAssertVerifyNumAssertsAndClear( 2 );
|
||||
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateMutexStatic with a valid buffer.
|
||||
* @details Create a semaphore using xSemaphoreCreateMutexStatic
|
||||
* @coverage xQueueCreateMutexStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateMutexStatic_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
StaticSemaphore_t xSemaphoreBuffer;
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer );
|
||||
|
||||
/* Check that no call to malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with a Mutex
|
||||
* @details Create a mutex using xSemaphoreCreateMutex
|
||||
* and verify that an immediate call to xSemaphoreTake succeeds.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_success( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
pvTaskIncrementMutexHeldCount_IgnoreAndReturn( NULL );
|
||||
|
||||
/* Verify that an immediate xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with xSemaphoreCreateMutex
|
||||
* @details Create a mutex using xSemaphoreCreateMutex
|
||||
* and verify that an immediate call to xSemaphoreGive fails.
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_fail( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Verify that an immediate xSemaphoreGive operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive and xSemaphoreTake with xSemaphoreCreateMutex
|
||||
* @details Create a mutex using xSemaphoreCreateMutex
|
||||
* and verify that an immediate call to xSemaphoreGive succeeds and a subsequent
|
||||
* call to xSemaphoreTake succeeds.
|
||||
* @coverage xQueueGenericSend xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_xSemaphoreTake_success( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( 0 );
|
||||
|
||||
/* Verify that an immediate xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
/* Verify that a subsequent xSemaphoreGive operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive multiple times on a Mutex.
|
||||
* @details Create a mutex using xSemaphoreCreateMutex
|
||||
* and verify that an immediate call to xSemaphoreGive succeeds and a subsequent
|
||||
* call to xSemaphoreGive fails.
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_multiple_Mutex_fail( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( 0 );
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
xSemaphoreTake( xSemaphore, 0 );
|
||||
|
||||
/* Verify that the first xSemaphoreGive call succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
/* Verify that a second xSemaphoreGive call fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGive( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake multiple times on a Mutex.
|
||||
* @details Create a Mutex using xSemaphoreCreateMutex,
|
||||
* verify that an immediate call to xSemaphoreTake succeeds, a subsequent
|
||||
* call to xSemaphoreGive succeds, but a second call to xSemaphoreGive fails.
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_multiple_Mutex_fail( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( 0 );
|
||||
|
||||
/* Verify that an immediate xSemaphoreTake operation succeeds */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Verify that the second xSemaphoreTake operation fails */
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test uxSemaphoreGetCount with a Mutex
|
||||
* @details Create a Mutex using xSemaphoreCreateMutex.
|
||||
* validate the return value of uxSemaphoreGetCount(),
|
||||
* call xSemaphoreTake() and validate the return value of uxSemaphoreGetCount()
|
||||
* @coverage uxQueueMessagesWaiting
|
||||
*/
|
||||
void test_macro_uxSemaphoreGetCount_Mutex( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( 0 );
|
||||
|
||||
xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
xSemaphoreTake( xSemaphore, 0 );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGetMutexHolder with an invalid (null) SemaphoreHandle_t
|
||||
* @coverage xQueueGetMutexHolder
|
||||
*/
|
||||
void test_macro_xSemaphoreGetMutexHolder_NULL( void )
|
||||
{
|
||||
/* This previously caused a segfault, but I patched queue.c */
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGetMutexHolder( NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGetMutexHolder with a handle of a binary semaphore
|
||||
* @details Verify that xSemaphoreGetMutexHolder returns NULL when given a handle to a binary semaphore rather than a mutex.
|
||||
* @coverage xQueueGetMutexHolder
|
||||
*/
|
||||
void test_macro_xSemaphoreGetMutexHolder_binary_semaphore( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphoreGetMutexHolder( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGetMutexHolder with a valid SemaphoreHandle_t
|
||||
* @coverage xQueueGetMutexHolder
|
||||
*/
|
||||
void test_macro_xSemaphoreGetMutexHolder_Mutex( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( xMutexHolder, xSemaphoreGetMutexHolder( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGetMutexHolderFromISR with an invalid (null) SemaphoreHandle_t
|
||||
* @coverage xQueueGetMutexHolderFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGetMutexHolderFromISR_Mutex_NULL( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGetMutexHolderFromISR( NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGetMutexHolderFromISR with a handle of a binary semaphore
|
||||
* @details Verify that xSemaphoreGetMutexHolderFromISR returns NULL when given a handle to a binary semaphore rather than a mutex.
|
||||
* @coverage xQueueGetMutexHolderFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGetMutexHolderFromISR_binary_semaphore( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphoreGetMutexHolderFromISR( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGetMutexHolderFromISR with a valid SemaphoreHandle_t
|
||||
* @coverage xQueueGetMutexHolderFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGetMutexHolderFromISR_Mutex( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( xMutexHolder, xSemaphoreGetMutexHolderFromISR( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with a Mutex that has an owner.
|
||||
* @details Verify that xSemaphoreGiveFromISR configASSERTs when an owned mutex is given as the xSemaphore argument.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_mutex_owned( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* Setup mocks for xSemaphoretake */
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
xSemaphoreTake( xSemaphore, 0 );
|
||||
|
||||
/* Expect that xSemaphoreGiveFromISR will assert due to the mutex usage */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with a Mutex that has a NULL owner.
|
||||
* @details Verify that xSemaphoreGiveFromISR does not configASSERT when a mutex with a NULL owner is given in the xSemaphore argument.
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_mutex_not_owned( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
/* Setup mocks for xSemaphoretake */
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( NULL );
|
||||
vFakePortAssertIfInterruptPriorityInvalid_Expect();
|
||||
|
||||
xSemaphoreTake( xSemaphore, 0 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode on an already owned mutex.
|
||||
* @details Test xSemaphoreTake on a mutex owned by another task where the owning task has lower priority.
|
||||
* @coverage xQueueSemaphoreTake prvGetDisinheritPriorityAfterTimeout
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_blocking_mutex_inherit_timeout( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
BaseType_t xFakeMutexHolder = getNextMonotonicTestValue();
|
||||
|
||||
/* Return a test value from the call to pvTaskIncrementMutexHeldCount */
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( ( void * ) xFakeMutexHolder );
|
||||
|
||||
/* Take the mutex */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
for( int i = 0; i < TICKS_TO_WAIT; i++ )
|
||||
{
|
||||
/* Return pdTRUE to signify that priority inheritance occurred */
|
||||
xTaskPriorityInherit_ExpectAndReturn( ( void * ) xFakeMutexHolder, pdTRUE );
|
||||
}
|
||||
|
||||
vTaskPriorityDisinheritAfterTimeout_Expect( ( void * ) ( BaseType_t ) getLastMonotonicTestValue(), tskIDLE_PRIORITY );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake in blocking mode on an already owned mutex.
|
||||
* @details Test xSemaphoreTake on a mutex owned by another task with another high priority task waiting and the current task timing out.
|
||||
* @coverage xQueueSemaphoreTake prvGetDisinheritPriorityAfterTimeout
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_blocking_mutex_inherit_timeout_high_prio_waiting( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
TaskHandle_t xFakeMutexHolder = ( TaskHandle_t ) ( ( uint64_t ) 0 + getNextMonotonicTestValue() );
|
||||
|
||||
/* Return a test value from the call to pvTaskIncrementMutexHeldCount */
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xFakeMutexHolder );
|
||||
|
||||
/* Take the mutex */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
/* Insert an item into the event list */
|
||||
td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
|
||||
td_task_addFakeTaskWaitingToReceiveFromQueue( xSemaphore );
|
||||
|
||||
for( int i = 0; i < TICKS_TO_WAIT; i++ )
|
||||
{
|
||||
/* Return pdTRUE to signify that priority inheritance occurred */
|
||||
xTaskPriorityInherit_ExpectAndReturn( xFakeMutexHolder, pdTRUE );
|
||||
}
|
||||
|
||||
vTaskPriorityDisinheritAfterTimeout_Expect( xFakeMutexHolder, DEFAULT_PRIORITY + 1 );
|
||||
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT + 1, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( TICKS_TO_WAIT + 1, td_task_getCount_YieldFromTaskResumeAll() );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Callback for test_macro_xSemaphoreTake_blocking_mutex_inherit_disinherit
|
||||
*/
|
||||
static BaseType_t xSemaphoreTake_blocking_xTaskResumeAllStub( int cmock_num_calls )
|
||||
{
|
||||
BaseType_t xReturnValue = td_task_xTaskResumeAllStub( cmock_num_calls );
|
||||
|
||||
if( cmock_num_calls == NUM_CALLS_TO_INTERCEPT )
|
||||
{
|
||||
TEST_ASSERT_TRUE( xSemaphoreGive( xSemaphoreHandleStatic ) );
|
||||
}
|
||||
|
||||
return xReturnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test priority inheritance during a successful blocking call to xSemaphoreTake
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_blocking_mutex_inherit_disinherit( void )
|
||||
{
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( NULL, pdFALSE );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateMutex();
|
||||
|
||||
xSemaphoreHandleStatic = xSemaphore;
|
||||
|
||||
TaskHandle_t xFakeMutexHolder = ( TaskHandle_t ) ( ( uint64_t ) 0 + getNextMonotonicTestValue() );
|
||||
|
||||
xTaskResumeAll_Stub( &xSemaphoreTake_blocking_xTaskResumeAllStub );
|
||||
|
||||
/* Return a test value from the call to pvTaskIncrementMutexHeldCount */
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xFakeMutexHolder );
|
||||
|
||||
/* Take the mutex */
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, 0 ) );
|
||||
|
||||
for( int i = 0; i < NUM_CALLS_TO_INTERCEPT + 1; i++ )
|
||||
{
|
||||
/* Return pdTRUE to signify that priority inheritance occurred */
|
||||
xTaskPriorityInherit_ExpectAndReturn( xFakeMutexHolder, pdTRUE );
|
||||
}
|
||||
|
||||
xTaskPriorityDisinherit_ExpectAndReturn( xFakeMutexHolder, pdTRUE );
|
||||
|
||||
/* Return a test value from the call to pvTaskIncrementMutexHeldCount */
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xFakeMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xSemaphore, TICKS_TO_WAIT ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT + 2, td_task_getYieldCount() );
|
||||
|
||||
TEST_ASSERT_EQUAL( NUM_CALLS_TO_INTERCEPT + 2, td_task_getCount_vPortYieldWithinAPI() );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
@ -0,0 +1,355 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
/*! @file recursive_mutex_utest.c */
|
||||
|
||||
#include "../queue_utest_common.h"
|
||||
|
||||
/* Queue includes */
|
||||
#include "FreeRTOS.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "semphr.h"
|
||||
#include "mock_fake_port.h"
|
||||
|
||||
/* ============================ GLOBAL VARIABLES =========================== */
|
||||
|
||||
/* ========================== CALLBACK FUNCTIONS =========================== */
|
||||
|
||||
/* ============================= Unity Fixtures ============================= */
|
||||
|
||||
void setUp( void )
|
||||
{
|
||||
commonSetUp();
|
||||
xTaskPriorityDisinherit_IgnoreAndReturn( pdFALSE );
|
||||
}
|
||||
|
||||
void tearDown( void )
|
||||
{
|
||||
commonTearDown();
|
||||
}
|
||||
|
||||
void suiteSetUp()
|
||||
{
|
||||
commonSuiteSetUp();
|
||||
}
|
||||
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return commonSuiteTearDown( numFailures );
|
||||
}
|
||||
|
||||
/* ========================== Helper functions =========================== */
|
||||
|
||||
/* ============================= Test Cases ============================== */
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateRecursiveMutex where the call to malloc fails
|
||||
* @coverage xQueueCreateMutex
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateRecursiveMutex_malloc_fail( void )
|
||||
{
|
||||
UnityMalloc_MakeMallocFailAfterCount( 0 );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateRecursiveMutex
|
||||
* @details Create a mutex using xSemaphoreCreateRecursiveMutex
|
||||
* @coverage xQueueCreateMutex
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateRecursiveMutex_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateRecursiveMutexStatic with a null buffer
|
||||
* @coverage xQueueCreateMutexStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateRecursiveMutexStatic_nullptr( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
/* Expect that xQueueCreate will assert due to the NULL buffer */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
xSemaphore = xSemaphoreCreateRecursiveMutexStatic( NULL );
|
||||
|
||||
/* Check that configASSERT was called twice */
|
||||
fakeAssertVerifyNumAssertsAndClear( 2 );
|
||||
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that calling xSemaphoreTakeRecursive with a NULL mutex handle causes a configASSERT failure.
|
||||
* @coverage xQueueTakeMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeRecursive_null_handle( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreTakeRecursive( NULL, 0 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTakeRecursive with a mutex that is not owned by any task.
|
||||
* @coverage xQueueTakeMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeRecursive_not_owned_once( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTakeRecursive( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTakeRecursive with a mutex that is already owned by the current task
|
||||
* @coverage xQueueTakeMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeRecursive_self_owned_twice( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTakeRecursive( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTakeRecursive( xSemaphore, 0 ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTakeRecursive on a mutex that is already owned by a different task
|
||||
* @coverage xQueueTakeMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeRecursive_owned_other_task( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder1 = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
TaskHandle_t xMutexHolder2 = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
/* Take the recursive mutex with task handle == xMutexHolder1 */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder1 );
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder1 );
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTakeRecursive( xSemaphore, 0 ) );
|
||||
|
||||
/* Attempt to take the recursive mutex with task handle == xMutexHolder2 */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder2 );
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreTakeRecursive( xSemaphore, 0 ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Verify that calling xSemaphoreGiveRecursive with a NULL mutex handle causes a configASSERT failure.
|
||||
* @coverage xQueueGiveMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveRecursive_null_handle( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGiveRecursive( NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveRecursive with a mutex that is not already owned by any task.
|
||||
* @coverage xQueueGiveMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveRecursive_unowned( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGiveRecursive( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveRecursive with a mutex that is not owned by any task with a NULL TaskHandle returned by calls to xTaskGetCurrentTaskHandle.
|
||||
* @coverage xQueueGiveMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveRecursive_unowned_null_taskhandle( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( NULL );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveRecursive( xSemaphore ) );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveRecursive with a mutex that is already owned by the current task
|
||||
* @coverage xQueueGiveMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveRecursive_self_owned( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
/* Take the Recursive Mutex from taskHandle == xMutexHolder */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
xSemaphoreTakeRecursive( xSemaphore, 0 );
|
||||
|
||||
/* Verify that the Recursive Mutex is in the taken state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* call xSemaphoreGiveRecursive to release the Recursive Mutex */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveRecursive( xSemaphore ) );
|
||||
|
||||
/* Verify that the Recursive Mutex is now in the available state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveRecursive on a mutex that is already owned by a different task
|
||||
* @coverage xQueueGiveMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveRecursive_owned_other_task( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder1 = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
TaskHandle_t xMutexHolder2 = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
/* Take the Recursive Mutex from taskHandle == xMutexHolder */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder1 );
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder1 );
|
||||
|
||||
xSemaphoreTakeRecursive( xSemaphore, 0 );
|
||||
|
||||
/* Verify that the Recursive Mutex is in the taken state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* call xSemaphoreGiveRecursive to release the Recursive Mutex */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder2 );
|
||||
|
||||
TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGiveRecursive( xSemaphore ) );
|
||||
|
||||
/* Verify that the Recursive Mutex remains in the taken state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Call xSemaphoreTakeRecursive and xSemaphoreGiveRecursive each multiple times
|
||||
* @coverage xQueueTakeMutexRecursive xQueueGiveMutexRecursive
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeRecursive_xSemaphoreGiveRecursive_recursive( void )
|
||||
{
|
||||
TaskHandle_t xMutexHolder = ( void * ) ( BaseType_t ) getNextMonotonicTestValue();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
/* Take the Recursive Mutex */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
pvTaskIncrementMutexHeldCount_ExpectAndReturn( xMutexHolder );
|
||||
xSemaphoreTakeRecursive( xSemaphore, 0 );
|
||||
|
||||
/* Verify that the Recursive Mutex is in the taken state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* Take the Recursive Mutex a second time */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
xSemaphoreTakeRecursive( xSemaphore, 0 );
|
||||
|
||||
/* Verify that the Recursive Mutex remains in the taken state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* call xSemaphoreGiveRecursive to release the Recursive Mutex (first time) */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveRecursive( xSemaphore ) );
|
||||
|
||||
/* Verify that the Recursive Mutex remains in the taken state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
/* call xSemaphoreGiveRecursive to release the Recursive Mutex (second time) */
|
||||
xTaskGetCurrentTaskHandle_ExpectAndReturn( xMutexHolder );
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveRecursive( xSemaphore ) );
|
||||
|
||||
/* Verify that the Recursive Mutex is now in the available state */
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
@ -0,0 +1,169 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
/*! @file semaphore_common_utest.c */
|
||||
|
||||
#include "../queue_utest_common.h"
|
||||
|
||||
/* Queue includes */
|
||||
#include "FreeRTOS.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "semphr.h"
|
||||
|
||||
|
||||
/* ============================ GLOBAL VARIABLES =========================== */
|
||||
|
||||
/* ========================== CALLBACK FUNCTIONS =========================== */
|
||||
|
||||
/* ============================= Unity Fixtures ============================= */
|
||||
|
||||
void setUp( void )
|
||||
{
|
||||
commonSetUp();
|
||||
}
|
||||
|
||||
void tearDown( void )
|
||||
{
|
||||
commonTearDown();
|
||||
}
|
||||
|
||||
void suiteSetUp()
|
||||
{
|
||||
commonSuiteSetUp();
|
||||
}
|
||||
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return commonSuiteTearDown( numFailures );
|
||||
}
|
||||
|
||||
/* =========================== Helper functions ============================ */
|
||||
|
||||
/* ============================== Test Cases =============================== */
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with an invalid (NULL) handle
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_invalid_handle( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreTake( NULL, 0 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTake with a QueueHandle rather than a SemaphoreHandle
|
||||
* @coverage xQueueSemaphoreTake
|
||||
*/
|
||||
void test_macro_xSemaphoreTake_queue_handle( void )
|
||||
{
|
||||
QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
|
||||
|
||||
uint32_t testVal = getNextMonotonicTestValue();
|
||||
|
||||
xQueueSend( xQueue, &testVal, 0 );
|
||||
|
||||
/* Expect that xSemaphoreTake will assert because xQueue is not a semaphore */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreTake( xQueue, 0 ) );
|
||||
|
||||
/* verify that configASSERT was called */
|
||||
TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
|
||||
|
||||
vQueueDelete( xQueue );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with an invalid (NULL) handle
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_invalid_handle( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGive( NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGive with a QueueHandle rather than a SemaphoreHandle
|
||||
* @coverage xQueueGenericSend
|
||||
*/
|
||||
void test_macro_xSemaphoreGive_queue_handle( void )
|
||||
{
|
||||
QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
|
||||
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGive( xQueue ) );
|
||||
|
||||
vQueueDelete( xQueue );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTakeFromISR with an invalid (NULL) handle
|
||||
* @coverage xQueueReceiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeFromISR_invalid_handle( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreTakeFromISR( NULL, NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreTakeFromISR with a QueueHandle rather than a SemaphoreHandle
|
||||
* @coverage xQueueReceiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreTakeFromISR_queue_handle( void )
|
||||
{
|
||||
QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
|
||||
|
||||
/* Expect that xSemaphoreTake will assert because xQueue is not a semaphore */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
uint32_t testVal = getNextMonotonicTestValue();
|
||||
|
||||
xQueueSend( xQueue, &testVal, 0 );
|
||||
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreTakeFromISR( xQueue, NULL ) );
|
||||
|
||||
vQueueDelete( xQueue );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with an invalid (NULL) handle
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_invalid_handle( void )
|
||||
{
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGiveFromISR( NULL, NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreGiveFromISR with a QueueHandle rather than a SemaphoreHandle
|
||||
* @coverage xQueueGiveFromISR
|
||||
*/
|
||||
void test_macro_xSemaphoreGiveFromISR_queue_handle( void )
|
||||
{
|
||||
QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
|
||||
|
||||
EXPECT_ASSERT_BREAK( xSemaphoreGiveFromISR( xQueue, NULL ) );
|
||||
|
||||
vQueueDelete( xQueue );
|
||||
}
|
||||
@ -0,0 +1,443 @@
|
||||
/*
|
||||
* FreeRTOS V202212.01
|
||||
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
/*! @file semaphore_create_utest.c */
|
||||
|
||||
#include "../queue_utest_common.h"
|
||||
#include "mock_fake_port.h"
|
||||
|
||||
/* Queue includes */
|
||||
#include "FreeRTOS.h"
|
||||
#include "FreeRTOSConfig.h"
|
||||
#include "semphr.h"
|
||||
|
||||
/* ================================ CONSTANTS ============================== */
|
||||
|
||||
/* ============================ GLOBAL VARIABLES =========================== */
|
||||
|
||||
/* ========================== CALLBACK FUNCTIONS =========================== */
|
||||
|
||||
/* ============================= Unity Fixtures ============================= */
|
||||
|
||||
void setUp( void )
|
||||
{
|
||||
commonSetUp();
|
||||
}
|
||||
|
||||
void tearDown( void )
|
||||
{
|
||||
commonTearDown();
|
||||
}
|
||||
|
||||
void suiteSetUp()
|
||||
{
|
||||
commonSuiteSetUp();
|
||||
}
|
||||
|
||||
int suiteTearDown( int numFailures )
|
||||
{
|
||||
return commonSuiteTearDown( numFailures );
|
||||
}
|
||||
|
||||
/* ========================== Helper functions =========================== */
|
||||
|
||||
/* ============================= Test Cases ============================== */
|
||||
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateBinary where the call to malloc fails
|
||||
* @coverage xQueueGenericCreate
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateBinary_malloc_fail( void )
|
||||
{
|
||||
UnityMalloc_MakeMallocFailAfterCount( 0 );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateBinary
|
||||
* @details Create a semaphore using xSemaphoreCreateBinary and verify that it
|
||||
* is in the "taken" state upon creation.
|
||||
* @coverage xQueueGenericCreateStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateBinary_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateBinaryStatic with a null buffer
|
||||
* @coverage xQueueGenericCreateStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateBinaryStatic_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
/* Expect that xSemaphoreCreate will assert due to the NULL buffer */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
xSemaphore = xSemaphoreCreateBinaryStatic( NULL );
|
||||
|
||||
/* verify that configASSERT was called twice */
|
||||
fakeAssertVerifyNumAssertsAndClear( 2 );
|
||||
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateBinaryStatic with a valid buffer.
|
||||
* @details Create a semaphore using xSemaphoreCreateBinaryStatic and verify
|
||||
* that it is in the "taken" state upon creation.
|
||||
* @coverage xQueueGenericCreateStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateBinaryStatic_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
StaticSemaphore_t xSemaphoreBuffer;
|
||||
|
||||
xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );
|
||||
|
||||
/* Check that no call to malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_TAKEN, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @brief Test vSemaphoreCreateBinary where the call to malloc fails
|
||||
* @coverage xQueueGenericCreate
|
||||
*/
|
||||
void test_macro_vSemaphoreCreateBinary_malloc_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
UnityMalloc_MakeMallocFailAfterCount( 0 );
|
||||
|
||||
vSemaphoreCreateBinary( xSemaphore );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @brief Test vSemaphoreCreateBinary
|
||||
* @details Create a semaphore using vSemaphoreCreateBinary and verify that it
|
||||
* is in the "given" state upon creation
|
||||
* @coverage xQueueGenericCreate xQueueGenericSend
|
||||
*/
|
||||
void test_macro_vSemaphoreCreateBinary_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
|
||||
vSemaphoreCreateBinary( xSemaphore );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting where the call to malloc fails
|
||||
* @coverage
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_malloc_fail( void )
|
||||
{
|
||||
UnityMalloc_MakeMallocFailAfterCount( 0 );
|
||||
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
xSemaphore = xSemaphoreCreateCounting( 10, 0 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting with uxMaxCount=1 and uxInitialCount=2
|
||||
* @details This is an invalid initial condition for a counting semaphore since
|
||||
* uxMaxCount >= uxInitialCount.
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_one_two( void )
|
||||
{
|
||||
/* Expect that xSemaphoreCreateCounting will assert because
|
||||
* uxInitialCount > xMaxCount is invalid */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 1, 2 );
|
||||
|
||||
fakeAssertGetFlagAndClear();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Check that no call to malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting with uxMaxCount=UINT64_MAX - 1 and uxInitialCount=UINT64_MAX
|
||||
* @details This is an invalid initial condition for a counting semaphore since
|
||||
* uxMaxCount >= uxInitialCount.
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_over_upper_bound( void )
|
||||
{
|
||||
/* Expect that xSemaphoreCreateCounting will configASSERT because
|
||||
* uxInitialCount > xMaxCount is invalid */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX - 1, UINT64_MAX );
|
||||
|
||||
/* verify that configASSERT was called */
|
||||
TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Check that no call to malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting with uxMaxCount=1 and uxInitialCount=0
|
||||
* @details Create a binary semaphore using xSemaphoreCreateCounting.
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_one_zero( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 1, 0 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Check the initial count */
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting with uxMaxCount=0 and uxInitialCount=0
|
||||
* @details This is an invalid counting semaphore since the following condition
|
||||
* is always true for a counting semaphore: uxMaxCount > 0
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_zero_zero( void )
|
||||
{
|
||||
/* Expect that xSemaphoreCreateCounting will assert because uxMaxCount=0 is invalid */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 0, 0 );
|
||||
|
||||
/* verify that configASSERT was called */
|
||||
TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Check that no call to malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting( 100, 50 )
|
||||
* @details Test xSemaphoreCreateCounting with uxMaxCount=100 and uxInitialCount=50
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_100_50_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( 100, 50 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
/* Check the initial count */
|
||||
TEST_ASSERT_EQUAL( 50, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX-1 )
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_upper_bound_1( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX - 1 );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( UINT64_MAX - 1, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX )
|
||||
* @coverage xQueueCreateCountingSemaphore
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCounting_upper_bound_2( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = xSemaphoreCreateCounting( UINT64_MAX, UINT64_MAX );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
TEST_ASSERT_EQUAL( QUEUE_T_SIZE, getLastMallocSize() );
|
||||
|
||||
TEST_ASSERT_EQUAL( UINT64_MAX, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCountingStatic with a null buffer
|
||||
* @details Calls xSemaphoreCreateCountingStatic with a null buffer and
|
||||
* uxMaxCount=2 and uxInitialCount=1
|
||||
* @coverage xQueueCreateCountingSemaphoreStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCountingStatic_null_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
|
||||
/* Expect that xQueueCreate will assert due to the NULL buffer */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
xSemaphore = xSemaphoreCreateCountingStatic( 2, 1, NULL );
|
||||
|
||||
/* verify that configASSERT was called twice */
|
||||
fakeAssertVerifyNumAssertsAndClear( 2 );
|
||||
|
||||
/* Verify that the returned handle is NULL */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* check that no call to malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCountingStatic with uxMaxCount=0 and uxInitialCount=0
|
||||
* @details This is an invalid counting semaphore since uxMaxCount > 0
|
||||
* @coverage xQueueCreateCountingSemaphoreStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCountingStatic_zero_zero_fail( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
StaticSemaphore_t xSemaphoreBuffer;
|
||||
|
||||
/* Expect that xQueueCreate will assert due to the NULL buffer */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
xSemaphore = xSemaphoreCreateCountingStatic( 0, 0, &xSemaphoreBuffer );
|
||||
|
||||
/* verify that configASSERT was called */
|
||||
TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* Check that no malloc occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCountingStatic with uxMaxCount=1 and uxInitialCount=2
|
||||
* @details This is an invalid initial condition for a counting semaphore since
|
||||
* uxMaxCount >= uxInitialCount.
|
||||
* @coverage xQueueCreateCountingSemaphoreStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCountingStatic_one_two( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = NULL;
|
||||
StaticSemaphore_t xSemaphoreBuffer;
|
||||
|
||||
/* Expect that xSemaphoreCreateCountingStatic will assert because
|
||||
* uxInitialCount > xMaxCount is invalid */
|
||||
fakeAssertExpectFail();
|
||||
|
||||
xSemaphore = xSemaphoreCreateCountingStatic( 1, 2, &xSemaphoreBuffer );
|
||||
|
||||
fakeAssertGetFlagAndClear();
|
||||
|
||||
/* validate returned semaphore handle */
|
||||
TEST_ASSERT_EQUAL( NULL, xSemaphore );
|
||||
|
||||
/* verify that no heap memory allocation occurred */
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test xSemaphoreCreateCountingStatic with uxMaxCount=1 and uxInitialCount=0
|
||||
* @details Create a binary semaphore using xSemaphoreCreateCountingStatic
|
||||
* @coverage xQueueCreateCountingSemaphoreStatic
|
||||
*/
|
||||
void test_macro_xSemaphoreCreateCountingStatic_one_zero_success( void )
|
||||
{
|
||||
SemaphoreHandle_t xSemaphore = INVALID_PTR;
|
||||
StaticSemaphore_t xSemaphoreBuffer;
|
||||
|
||||
xSemaphore = xSemaphoreCreateCountingStatic( 1, 0, &xSemaphoreBuffer );
|
||||
|
||||
TEST_ASSERT_NOT_EQUAL( NULL, xSemaphore );
|
||||
TEST_ASSERT_EQUAL( 0, getLastMallocSize() );
|
||||
|
||||
/* Check the initial count */
|
||||
TEST_ASSERT_EQUAL( 0, uxSemaphoreGetCount( xSemaphore ) );
|
||||
|
||||
vSemaphoreDelete( xSemaphore );
|
||||
}
|
||||
Reference in New Issue
Block a user