From 4b3b64bfcbf078f27478627aa427b5a1e6c8a677 Mon Sep 17 00:00:00 2001 From: Darian Leung Date: Mon, 13 Mar 2023 17:18:00 +0800 Subject: [PATCH] Add unit tests for GetStaticBuffer functions Add unit tests for the various ...GetStaticBuffer() functions added in https://github.com/FreeRTOS/FreeRTOS-Kernel/pull/641. --- .../CMock/event_groups/event_groups_utest.c | 49 +++++++++++ .../message_buffer/message_buffer_utest.c | 39 +++++++++ .../CMock/queue/generic/queue_status_utest.c | 40 +++++++++ .../queue/semaphore/semaphore_common_utest.c | 35 ++++++++ .../api/stream_buffer_api_utest.c | 41 ++++++++++ FreeRTOS/Test/CMock/tasks/tasks_1_utest.c | 82 +++++++++++++++++++ FreeRTOS/Test/CMock/timers/timers_1_utest.c | 28 +++++++ 7 files changed, 314 insertions(+) diff --git a/FreeRTOS/Test/CMock/event_groups/event_groups_utest.c b/FreeRTOS/Test/CMock/event_groups/event_groups_utest.c index ff91acea842..6bddaf228ff 100644 --- a/FreeRTOS/Test/CMock/event_groups/event_groups_utest.c +++ b/FreeRTOS/Test/CMock/event_groups/event_groups_utest.c @@ -791,3 +791,52 @@ void test_xEventGroupSetBitsFromISR_Success( void ) /* API to Test */ ( void ) xEventGroupSetBitsFromISR( NULL, BIT_0, &xHigherPriorityTaskWoken ); } + +/*! + * @brief validate xEventGroupGetStaticBuffer on a statically created event group + * @details Test xEventGroupGetStaticBuffer returns the buffer of a statically created event group + * @coverage xEventGroupGetStaticBuffer + */ +void test_xEventGroupGetStaticBuffer_Success( void ) +{ + /* Expectation of Function: xEventGroupCreate */ + vListInitialise_Expect( 0 ); + vListInitialise_IgnoreArg_pxList(); + vListInitialise_ReturnThruPtr_pxList( pxListTemp ); + + /* Set-up */ + StaticEventGroup_t *pxEventGroupBufferRet = NULL; + StaticEventGroup_t xCreatedEventGroup = { 0 }; + xEventGroupHandle = xEventGroupCreateStatic( &xCreatedEventGroup ); + + TEST_ASSERT_EQUAL( pdTRUE, xEventGroupGetStaticBuffer( xEventGroupHandle, &pxEventGroupBufferRet ) ); + TEST_ASSERT_EQUAL( &xCreatedEventGroup, pxEventGroupBufferRet ); +} + +/*! + * @brief validate xEventGroupGetStaticBuffer on a dynamically created event group + * @details Test xEventGroupGetStaticBuffer returns an error when called on a dynamically created event group + * @coverage xEventGroupGetStaticBuffer + */ +void test_xEventGroupGetStaticBuffer_Fail( void ) +{ + /* Expectation of Function: xEventGroupCreate */ + vListInitialise_Expect( 0 ); + vListInitialise_IgnoreArg_pxList(); + vListInitialise_ReturnThruPtr_pxList( pxListTemp ); + /* Expectation of Function: vEventGroupDelete */ + vTaskSuspendAll_Ignore(); + listCURRENT_LIST_LENGTH_ExpectAnyArgsAndReturn( 1 ); + vTaskRemoveFromUnorderedEventList_Ignore(); + listCURRENT_LIST_LENGTH_ExpectAnyArgsAndReturn( 0 ); + xTaskResumeAll_IgnoreAndReturn( 1 ); + + /* Set-up */ + StaticEventGroup_t *pxEventGroupBufferRet = NULL; + xEventGroupHandle = xEventGroupCreate(); + + TEST_ASSERT_EQUAL( pdFALSE, xEventGroupGetStaticBuffer( xEventGroupHandle, &pxEventGroupBufferRet ) ); + TEST_ASSERT_EQUAL( NULL, pxEventGroupBufferRet ); + + vEventGroupDelete( xEventGroupHandle ); +} \ No newline at end of file diff --git a/FreeRTOS/Test/CMock/message_buffer/message_buffer/message_buffer_utest.c b/FreeRTOS/Test/CMock/message_buffer/message_buffer/message_buffer_utest.c index cc353503fec..da45b43392e 100644 --- a/FreeRTOS/Test/CMock/message_buffer/message_buffer/message_buffer_utest.c +++ b/FreeRTOS/Test/CMock/message_buffer/message_buffer/message_buffer_utest.c @@ -798,3 +798,42 @@ void test_xMessageBufferReceiveFromISR_success( void ) vStreamBufferDelete( xMessageBuffer ); } + +/** + * @brief validate xMessageBufferGetStaticBuffers on a statically created message buffer + * @details Test xMessageBufferGetStaticBuffers returns the buffers of a statically created message buffer + */ +void test_xMessageBufferGetStaticBuffers_Success( void ) +{ + StaticMessageBuffer_t messageBufferStruct; + /* The size of message buffer array should be one greater than the required size of message buffer. */ + uint8_t messageBufferArray[ TEST_MESSAGE_BUFFER_SIZE + 1 ] = { 0 }; + uint8_t * pucMessageBufferStorageAreaRet = NULL; + StaticMessageBuffer_t * pxStaticMessageBuffer = NULL; + + xMessageBuffer = xMessageBufferCreateStatic( sizeof( messageBufferArray ), messageBufferArray, &messageBufferStruct ); + + TEST_ASSERT_EQUAL( pdTRUE, xMessageBufferGetStaticBuffers( xMessageBuffer, &pucMessageBufferStorageAreaRet, &pxStaticMessageBuffer ) ); + TEST_ASSERT_EQUAL( messageBufferArray, pucMessageBufferStorageAreaRet ); + TEST_ASSERT_EQUAL( &messageBufferStruct, pxStaticMessageBuffer ); + + vMessageBufferDelete( xMessageBuffer ); +} + +/** + * @brief validate xMessageBufferGetStaticBuffers on a dynamically created message buffer + * @details Test xMessageBufferGetStaticBuffers returns an error when called on a dynamically created message buffer + */ +void test_xMessageBufferGetStaticBuffers_Fail( void ) +{ + uint8_t * pucMessageBufferStorageAreaRet = NULL; + StaticMessageBuffer_t * pxStaticMessageBuffer = NULL; + + xMessageBuffer = xMessageBufferCreate( TEST_MESSAGE_BUFFER_SIZE ); + + TEST_ASSERT_EQUAL( pdFALSE, xMessageBufferGetStaticBuffers( xMessageBuffer, &pucMessageBufferStorageAreaRet, &pxStaticMessageBuffer ) ); + TEST_ASSERT_EQUAL( NULL, pucMessageBufferStorageAreaRet ); + TEST_ASSERT_EQUAL( NULL, pxStaticMessageBuffer ); + + vMessageBufferDelete( xMessageBuffer ); +} \ No newline at end of file diff --git a/FreeRTOS/Test/CMock/queue/generic/queue_status_utest.c b/FreeRTOS/Test/CMock/queue/generic/queue_status_utest.c index db99333e79c..dd01434aa40 100644 --- a/FreeRTOS/Test/CMock/queue/generic/queue_status_utest.c +++ b/FreeRTOS/Test/CMock/queue/generic/queue_status_utest.c @@ -326,3 +326,43 @@ void test_vQueueWaitForMessageRestricted_locked( void ) vQueueDelete( xQueue ); } + +/** + * @brief xQueueGetStaticBuffers with a statically allocated queue. + * @details Test xQueueGetStaticBuffers returns the buffers of a statically allocated queue + * @coverage xQueueGetStaticBuffers + */ +void test_macro_xQueueGetStaticBuffers_static( void ) +{ + uint32_t queueStorage[ 5 ]; + StaticQueue_t queueBuffer; + uint8_t * pucQueueStorageRet = NULL; + StaticQueue_t * pxStaticQueueRet = NULL; + + QueueHandle_t xQueue = xQueueCreateStatic( 5, sizeof( uint32_t ), ( void * ) queueStorage, &queueBuffer ); + + TEST_ASSERT_EQUAL( pdTRUE, xQueueGetStaticBuffers( xQueue, &pucQueueStorageRet, &pxStaticQueueRet ) ); + TEST_ASSERT_EQUAL( queueStorage, ( uint32_t * ) pucQueueStorageRet ); + TEST_ASSERT_EQUAL( &queueBuffer, pxStaticQueueRet ); + + vQueueDelete( xQueue ); +} + +/** + * @brief xQueueGetStaticBuffers with a dynamically allocated queue. + * @details Test xQueueGetStaticBuffers returns an error when called on a dynamically allocated queue. + * @coverage xQueueGetStaticBuffers + */ +void test_macro_xQueueGetStaticBuffers_dynamic( void ) +{ + uint8_t * pucQueueStorageRet = NULL; + StaticQueue_t * pxStaticQueueRet = NULL; + + QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) ); + + TEST_ASSERT_EQUAL( pdFALSE, xQueueGetStaticBuffers( xQueue, &pucQueueStorageRet, &pxStaticQueueRet ) ); + TEST_ASSERT_EQUAL( NULL, pucQueueStorageRet ); + TEST_ASSERT_EQUAL( NULL, pxStaticQueueRet ); + + vQueueDelete( xQueue ); +} \ No newline at end of file diff --git a/FreeRTOS/Test/CMock/queue/semaphore/semaphore_common_utest.c b/FreeRTOS/Test/CMock/queue/semaphore/semaphore_common_utest.c index 4f5282ce9f1..765580812d9 100644 --- a/FreeRTOS/Test/CMock/queue/semaphore/semaphore_common_utest.c +++ b/FreeRTOS/Test/CMock/queue/semaphore/semaphore_common_utest.c @@ -167,3 +167,38 @@ void test_macro_xSemaphoreGiveFromISR_queue_handle( void ) vQueueDelete( xQueue ); } + +/** + * @brief Test xSemaphoreGetStaticBuffer with a Semaphore + * @details Test xSemaphoreGetStaticBuffer returns the buffers of a statically allocated Semaphore + * @coverage xSemaphoreGetStaticBuffer + */ +void test_macro_xSemaphoreGetStaticBuffer_success( void ) +{ + SemaphoreHandle_t xSemaphore = NULL; + StaticSemaphore_t xSemaphoreBuffer; + StaticSemaphore_t * pxSemaphoreBufferRet = NULL; + + xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer ); + + TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGetStaticBuffer( xSemaphore, &pxSemaphoreBufferRet ) ); + TEST_ASSERT_EQUAL( &xSemaphoreBuffer, pxSemaphoreBufferRet ); + + vSemaphoreDelete( xSemaphore ); +} + +/** + * @brief Test xSemaphoreGetStaticBuffer with a dynamic Semaphore + * @details Test xSemaphoreGetStaticBuffer returns an error when called on a dynamically allocated Semaphore + * @coverage xSemaphoreGetStaticBuffer + */ +void test_macro_xSemaphoreGetStaticBuffer_fail( void ) +{ + StaticSemaphore_t * pxSemaphoreBufferRet = NULL; + SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary(); + + TEST_ASSERT_EQUAL( pdFALSE, xSemaphoreGetStaticBuffer( xSemaphore, &pxSemaphoreBufferRet ) ); + TEST_ASSERT_EQUAL( NULL, pxSemaphoreBufferRet ); + + vSemaphoreDelete( xSemaphore ); +} \ No newline at end of file diff --git a/FreeRTOS/Test/CMock/stream_buffer/api/stream_buffer_api_utest.c b/FreeRTOS/Test/CMock/stream_buffer/api/stream_buffer_api_utest.c index 7b650a30549..02f799d75a7 100644 --- a/FreeRTOS/Test/CMock/stream_buffer/api/stream_buffer_api_utest.c +++ b/FreeRTOS/Test/CMock/stream_buffer/api/stream_buffer_api_utest.c @@ -1455,3 +1455,44 @@ void test_xStreamBufferSend_WrapOver( void ) vStreamBufferDelete( xStreamBuffer ); } + +/** + * @brief validate xStreamBufferGetStaticBuffers on a statically created stream buffer + * @details Test xStreamBufferGetStaticBuffers returns the buffers of a statically created stream buffer + */ +void test_xStreamBufferGetStaticBuffers_Success( void ) +{ + StaticStreamBuffer_t streamBufferStruct; + + /* The size of stream buffer array should be one greater than the required size of stream buffer. */ + uint8_t streamBufferArray[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 }; + uint8_t * pucStreamBufferStorageAreaRet = NULL; + StaticStreamBuffer_t * pxStaticStreamBufferRet = NULL; + + xStreamBuffer = xStreamBufferCreateStatic( sizeof( streamBufferArray ), TEST_STREAM_BUFFER_TRIGGER_LEVEL, streamBufferArray, &streamBufferStruct ); + + TEST_ASSERT_EQUAL( pdTRUE, xStreamBufferGetStaticBuffers( xStreamBuffer, &pucStreamBufferStorageAreaRet, &pxStaticStreamBufferRet ) ); + TEST_ASSERT_EQUAL( streamBufferArray, pucStreamBufferStorageAreaRet ); + TEST_ASSERT_EQUAL( &streamBufferStruct, pxStaticStreamBufferRet ); + + vStreamBufferDelete( xStreamBuffer ); +} + +/** + * @brief validate xStreamBufferGetStaticBuffers on a dynamically created stream buffer + * @details Test xStreamBufferGetStaticBuffers returns an error when called on a dynamically created stream buffer + */ +void test_xStreamBufferGetStaticBuffers_Fail( void ) +{ + uint8_t * pucStreamBufferStorageAreaRet = NULL; + StaticStreamBuffer_t * pxStaticStreamBufferRet = NULL; + + xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL ); + TEST_ASSERT_NOT_EQUAL( NULL, xStreamBuffer ); + + TEST_ASSERT_EQUAL( pdFALSE, xStreamBufferGetStaticBuffers( xStreamBuffer, &pucStreamBufferStorageAreaRet, &pxStaticStreamBufferRet ) ); + TEST_ASSERT_EQUAL( NULL, pucStreamBufferStorageAreaRet ); + TEST_ASSERT_EQUAL( NULL, pxStaticStreamBufferRet ); + + vStreamBufferDelete( xStreamBuffer ); +} \ No newline at end of file diff --git a/FreeRTOS/Test/CMock/tasks/tasks_1_utest.c b/FreeRTOS/Test/CMock/tasks/tasks_1_utest.c index ed2757eb729..f735e55c766 100644 --- a/FreeRTOS/Test/CMock/tasks/tasks_1_utest.c +++ b/FreeRTOS/Test/CMock/tasks/tasks_1_utest.c @@ -5440,3 +5440,85 @@ void test_ulTaskGenericNotifyValueClear_success_null_handle() TEST_ASSERT_EQUAL( 2, task_to_notify->ulNotifiedValue[ uxIndexToClear ] ); } /* ---------- end testing configUSE_TASK_NOTIFICATIONS --------------- */ + +/* ---------------------- testing xTaskGetStaticBuffers ----------------------*/ + +/** + * @brief Test xTaskGetStaticBuffers with a static task + * @details Test xTaskGetStaticBuffers returns the buffers of a statically allocated task + * @coverage xTaskGetStaticBuffers + */ +void test_xTaskGetStaticBuffers_success( void ) +{ + StackType_t puxStackBuffer[ 300 ]; + StaticTask_t * pxTaskBuffer = malloc( sizeof( TCB_t ) ); + TaskFunction_t pxTaskCode = NULL; + const char * const pcName = { __FUNCTION__ }; + const uint32_t ulStackDepth = 300; + void * const pvParameters = NULL; + UBaseType_t uxPriority = 3; + TaskHandle_t ret; + StackType_t * puxStackBufferRet = NULL; + StaticTask_t * pxTaskBufferRet = NULL; + + memset( puxStackBuffer, 0xa5U, ulStackDepth * sizeof( StackType_t ) ); + + vListInitialiseItem_ExpectAnyArgs(); + vListInitialiseItem_ExpectAnyArgs(); + + /* set owner */ + listSET_LIST_ITEM_VALUE_ExpectAnyArgs(); + /* set owner */ + pxPortInitialiseStack_ExpectAnyArgsAndReturn( puxStackBuffer ); + + for( int i = ( UBaseType_t ) 0U; i < ( UBaseType_t ) configMAX_PRIORITIES; i++ ) + { + vListInitialise_ExpectAnyArgs(); + } + + /* Delayed Task List 1 */ + vListInitialise_ExpectAnyArgs(); + /* Delayed Task List 2 */ + vListInitialise_ExpectAnyArgs(); + /* Pending Ready List */ + vListInitialise_ExpectAnyArgs(); + /* INCLUDE_vTaskDelete */ + vListInitialise_ExpectAnyArgs(); + /* INCLUDE_vTaskSuspend */ + vListInitialise_ExpectAnyArgs(); + + listINSERT_END_ExpectAnyArgs(); + + ret = xTaskCreateStatic( pxTaskCode, + pcName, + ulStackDepth, + pvParameters, + uxPriority, + puxStackBuffer, + pxTaskBuffer ); + + + TEST_ASSERT_EQUAL( pdTRUE, xTaskGetStaticBuffers( ret, &puxStackBufferRet, &pxTaskBufferRet ) ); + TEST_ASSERT_EQUAL( &puxStackBuffer, puxStackBufferRet ); + TEST_ASSERT_EQUAL( pxTaskBuffer, pxTaskBufferRet ); + + free ( pxTaskBuffer ); +} + +/** + * @brief Test xTaskGetStaticBuffers with a dynamic task + * @details Test xTaskGetStaticBuffers returns an error when called on a dynamically allocated task + * @coverage xTaskGetStaticBuffers + */ +void test_xTaskGetStaticBuffers_fail( void ) +{ + StackType_t * puxStackBufferRet = NULL; + StaticTask_t * pxTaskBufferRet = NULL; + TaskHandle_t taskHandle = create_task(); + + TEST_ASSERT_EQUAL( pdFALSE, xTaskGetStaticBuffers( taskHandle, &puxStackBufferRet, &pxTaskBufferRet ) ); + TEST_ASSERT_EQUAL( NULL, puxStackBufferRet ); + TEST_ASSERT_EQUAL( NULL, pxTaskBufferRet ); +} + +/* -------------------- end testing xTaskGetStaticBuffers --------------------*/ \ No newline at end of file diff --git a/FreeRTOS/Test/CMock/timers/timers_1_utest.c b/FreeRTOS/Test/CMock/timers/timers_1_utest.c index 04d74e757f3..6053e75494d 100644 --- a/FreeRTOS/Test/CMock/timers/timers_1_utest.c +++ b/FreeRTOS/Test/CMock/timers/timers_1_utest.c @@ -1784,3 +1784,31 @@ void test_timer_function_success_wrap_timer( void ) /* Validations */ TEST_ASSERT_EQUAL( 1, *retVal ); } + +void test_xEventGroupGetStaticBuffer_Success( void ) +{ + TimerHandle_t ret_timer_create; + UBaseType_t pvTimerID; + StaticTimer_t pxTimerBuffer[ sizeof( StaticTimer_t ) ]; + StaticTimer_t * pxTimerBufferRet = NULL; + + /* Setup */ + /* Expectations */ + /* prvInitialiseNewTimer */ + /* prvCheckForValidListAndQueue */ + vListInitialise_ExpectAnyArgs(); + vListInitialise_ExpectAnyArgs(); + xQueueGenericCreateStatic_ExpectAnyArgsAndReturn( NULL ); + /* Back prvInitialiseNewTimer */ + vListInitialiseItem_ExpectAnyArgs(); + /* API Call */ + ret_timer_create = xTimerCreateStatic( "ut_timer_task", + pdMS_TO_TICKS( 1000 ), + pdTRUE, + ( void * ) &pvTimerID, + xCallback_Test, + pxTimerBuffer ); + + TEST_ASSERT_EQUAL( pdTRUE, xTimerGetStaticBuffer( ret_timer_create, &pxTimerBufferRet ) ); + TEST_ASSERT_EQUAL( pxTimerBuffer, pxTimerBufferRet ); +}