Skip to content

Commit

Permalink
Add unit tests for GetStaticBuffer functions
Browse files Browse the repository at this point in the history
Add unit tests for the various ...GetStaticBuffer() functions added in
FreeRTOS/FreeRTOS-Kernel#641.
  • Loading branch information
Dazza0 committed Mar 13, 2023
1 parent 4483c16 commit 4b3b64b
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 0 deletions.
49 changes: 49 additions & 0 deletions FreeRTOS/Test/CMock/event_groups/event_groups_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
40 changes: 40 additions & 0 deletions FreeRTOS/Test/CMock/queue/generic/queue_status_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
35 changes: 35 additions & 0 deletions FreeRTOS/Test/CMock/queue/semaphore/semaphore_common_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
41 changes: 41 additions & 0 deletions FreeRTOS/Test/CMock/stream_buffer/api/stream_buffer_api_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
82 changes: 82 additions & 0 deletions FreeRTOS/Test/CMock/tasks/tasks_1_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 --------------------*/
28 changes: 28 additions & 0 deletions FreeRTOS/Test/CMock/timers/timers_1_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

0 comments on commit 4b3b64b

Please sign in to comment.