]> begriffs open source - cmsis-freertos/blob - Test/CMock/stream_buffer/stream_buffer_utest.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Test / CMock / stream_buffer / stream_buffer_utest.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 /*! @file stream_buffer_utest.c */
27
28 /* C runtime includes. */
29 #include <stdlib.h>
30 #include <stdbool.h>
31
32 /* Stream Buffer includes */
33 #include "FreeRTOS.h"
34 #include "FreeRTOSConfig.h"
35 #include "stream_buffer.h"
36
37 /* Test includes. */
38 #include "unity.h"
39 #include "unity_memory.h"
40 #include "CException.h"
41
42 /* Mock includes. */
43 #include "mock_task.h"
44 #include "mock_fake_assert.h"
45 #include "mock_fake_port.h"
46
47 /**
48  * @brief Sample size in bytes of the stream buffer used for test.
49  * The size is kept short enough so that the buffer can be allocated on stack.
50  */
51 #define TEST_STREAM_BUFFER_SIZE             ( 64U )
52
53 /**
54  * @brief Sample trigger level in bytes used for stream buffer tests.
55  * When a receiver task is blocked waiting for data, trigger level determines how much bytes should
56  * be available before which receiver task can be unblocked.
57  */
58 #define TEST_STREAM_BUFFER_TRIGGER_LEVEL    ( 32U )
59
60 /**
61  * @brief Maximum unsigned long value that can be passed as a stream buffer size so as to
62  * trigger an integer overflow.
63  */
64 #define TEST_STREAM_BUFFER_MAX_UINT_SIZE    ( ~( size_t ) ( 0UL ) )
65
66 /**
67  * @brief A value used to test setting and getting stream buffer number.
68  */
69 #define TEST_STREAM_BUFFER_NUMBER           ( 0xFFU )
70
71 /**
72  * @brief Wait ticks passed into from tests if the stream buffer is full while sending data or
73  * empty while receiving data.
74  */
75 #define TEST_STREAM_BUFFER_WAIT_TICKS       ( 1000U )
76
77 /**
78  * @brief CException code for when a configASSERT should be intercepted.
79  */
80 #define configASSERT_E                      0xAA101
81
82 /**
83  * @brief Expect a configASSERT from the function called.
84  *  Break out of the called function when this occurs.
85  * @details Use this macro when the call passed in as a parameter is expected
86  * to cause invalid memory access.
87  */
88 #define EXPECT_ASSERT_BREAK( call )                 \
89     do                                              \
90     {                                               \
91         shouldAbortOnAssertion = true;              \
92         CEXCEPTION_T e = CEXCEPTION_NONE;           \
93         Try                                         \
94         {                                           \
95             call;                                   \
96             TEST_FAIL();                            \
97         }                                           \
98         Catch( e )                                  \
99         {                                           \
100             TEST_ASSERT_EQUAL( configASSERT_E, e ); \
101         }                                           \
102     } while( 0 )
103
104
105 /* ============================  GLOBAL VARIABLES =========================== */
106
107 /**
108  * @brief Global counter for the number of assertions in code.
109  */
110 static int assertionFailed = 0;
111
112 /**
113  * @brief Global counter to keep track of how many times a sender task was woken up by a task receiving from the stream buffer.
114  */
115 static int senderTaskWoken = 0;
116
117 /**
118  * @brief Global counter to keep track of how many times a receiver task was woken up by a task sending to the buffer.
119  */
120 static int receiverTaskWoken = 0;
121
122 /**
123  * @brief Dummy sender task handle to which the stream buffer receive APIs will send notification.
124  */
125 static TaskHandle_t senderTask = ( TaskHandle_t ) ( 0xAABBCCDD );
126
127 /**
128  * @brief Dummy receiver task handle to which the stream buffer send APIs will send notifications.
129  */
130 static TaskHandle_t receiverTask = ( TaskHandle_t ) ( 0xABCDEEFF );
131
132 /**
133  * @brief Global Stream buffer handle used for tests.
134  */
135 static StreamBufferHandle_t xStreamBuffer;
136
137 /**
138  * @brief Flag which denotes if test need to abort on assertion.
139  */
140 static BaseType_t shouldAbortOnAssertion;
141
142 /**
143  * @brief Variable used to record the total dynamic size allocated in a test.
144  */
145 static size_t dynamicMemoryAllocated = 0;
146
147 /* ==========================  CALLBACK FUNCTIONS =========================== */
148
149 void * pvPortMalloc( size_t xSize )
150 {
151     dynamicMemoryAllocated += xSize;
152     return unity_malloc( xSize );
153 }
154 void vPortFree( void * pv )
155 {
156     return unity_free( pv );
157 }
158
159 static void vFakeAssertStub( bool x,
160                              char * file,
161                              int line,
162                              int cmock_num_calls )
163 {
164     if( !x )
165     {
166         assertionFailed++;
167
168         if( shouldAbortOnAssertion == pdTRUE )
169         {
170             Throw( configASSERT_E );
171         }
172     }
173 }
174
175 static BaseType_t streamBufferReceiveCallback( UBaseType_t uxIndexToWaitOn,
176                                                uint32_t ulBitsToClearOnEntry,
177                                                uint32_t ulBitsToClearOnExit,
178                                                uint32_t * pulNotificationValue,
179                                                TickType_t xTicksToWait,
180                                                int cmock_num_calls )
181 {
182     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0 };
183     size_t dataReceived = 0;
184
185     /* Receive enough bytes (full size) from stream buffer to wake up sender task. */
186     dataReceived = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, 0 );
187     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, dataReceived );
188     return pdTRUE;
189 }
190
191 static BaseType_t streamBufferReceiveFromISRCallback( UBaseType_t uxIndexToWaitOn,
192                                                       uint32_t ulBitsToClearOnEntry,
193                                                       uint32_t ulBitsToClearOnExit,
194                                                       uint32_t * pulNotificationValue,
195                                                       TickType_t xTicksToWait,
196                                                       int cmock_num_calls )
197 {
198     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0 };
199     size_t dataReceived = 0;
200     BaseType_t senderTaskWokenFromISR = pdFALSE;
201
202     /* Receive enough bytes (full size) from stream buffer to wake up sender task. */
203     dataReceived = xStreamBufferReceiveFromISR( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, &senderTaskWokenFromISR );
204     TEST_ASSERT_EQUAL( pdTRUE, senderTaskWokenFromISR );
205     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, dataReceived );
206     return pdTRUE;
207 }
208
209 static BaseType_t resetWhenBlockedCallback( UBaseType_t uxIndexToWaitOn,
210                                             uint32_t ulBitsToClearOnEntry,
211                                             uint32_t ulBitsToClearOnExit,
212                                             uint32_t * pulNotificationValue,
213                                             TickType_t xTicksToWait,
214                                             int cmock_num_calls )
215 {
216     BaseType_t status;
217
218     /* Reset when the task is blocked on stream buffer, should fail and return pdFAIL. */
219     status = xStreamBufferReset( xStreamBuffer );
220     TEST_ASSERT_EQUAL( pdFAIL, status );
221     return pdTRUE;
222 }
223
224 static BaseType_t sendCompletedFromISRCallback( UBaseType_t uxIndexToWaitOn,
225                                                 uint32_t ulBitsToClearOnEntry,
226                                                 uint32_t ulBitsToClearOnExit,
227                                                 uint32_t * pulNotificationValue,
228                                                 TickType_t xTicksToWait,
229                                                 int cmock_num_calls )
230 {
231     BaseType_t status = pdFALSE, highPriorityTaskWoken = pdFALSE;
232
233     /* Executing the send completed API from ISR should unblock a high priority task waiting to receive from stream buffer. */
234     status = xStreamBufferSendCompletedFromISR( xStreamBuffer, &highPriorityTaskWoken );
235     TEST_ASSERT_EQUAL( pdTRUE, status );
236     TEST_ASSERT_EQUAL( pdTRUE, highPriorityTaskWoken );
237     return pdTRUE;
238 }
239
240 static BaseType_t receiveCompletedFromISRCallback( UBaseType_t uxIndexToWaitOn,
241                                                    uint32_t ulBitsToClearOnEntry,
242                                                    uint32_t ulBitsToClearOnExit,
243                                                    uint32_t * pulNotificationValue,
244                                                    TickType_t xTicksToWait,
245                                                    int cmock_num_calls )
246 {
247     BaseType_t status = pdFALSE, highPriorityTaskWoken = pdFALSE;
248
249     /* Executing the receive completed API from ISR should unblock a high priority task waiting to send to stream buffer. */
250     status = xStreamBufferReceiveCompletedFromISR( xStreamBuffer, &highPriorityTaskWoken );
251     TEST_ASSERT_EQUAL( pdTRUE, status );
252     TEST_ASSERT_EQUAL( pdTRUE, highPriorityTaskWoken );
253     return pdTRUE;
254 }
255
256 static BaseType_t senderTaskNotificationCallback( TaskHandle_t xTaskToNotify,
257                                                   UBaseType_t uxIndexToNotify,
258                                                   uint32_t ulValue,
259                                                   eNotifyAction eAction,
260                                                   uint32_t * pulPreviousNotificationValue,
261                                                   int cmock_num_calls )
262 {
263     TEST_ASSERT_EQUAL( senderTask, xTaskToNotify );
264     senderTaskWoken++;
265     return pdTRUE;
266 }
267
268 static BaseType_t senderTaskNotificationFromISRCallback( TaskHandle_t xTaskToNotify,
269                                                          UBaseType_t uxIndexToNotify,
270                                                          uint32_t ulValue,
271                                                          eNotifyAction eAction,
272                                                          uint32_t * pulPreviousNotificationValue,
273                                                          BaseType_t * pxHigherPriorityTaskWoken,
274                                                          int cmock_num_calls )
275 {
276     TEST_ASSERT_EQUAL( senderTask, xTaskToNotify );
277     senderTaskWoken++;
278     *pxHigherPriorityTaskWoken = pdTRUE;
279
280     return pdTRUE;
281 }
282
283 static BaseType_t streamBufferSendCallback( UBaseType_t uxIndexToWaitOn,
284                                             uint32_t ulBitsToClearOnEntry,
285                                             uint32_t ulBitsToClearOnExit,
286                                             uint32_t * pulNotificationValue,
287                                             TickType_t xTicksToWait,
288                                             int cmock_num_calls )
289 {
290     uint8_t data[ TEST_STREAM_BUFFER_TRIGGER_LEVEL ] = { 0 };
291     size_t dataSent = 0;
292
293     /* Send enough (trigger level) bytes to stream buffer to wake up the receiver Task. */
294     dataSent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_TRIGGER_LEVEL, 0 );
295     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_TRIGGER_LEVEL, dataSent );
296     return pdTRUE;
297 }
298
299 static BaseType_t streamBufferSendFromISRCallback( UBaseType_t uxIndexToWaitOn,
300                                                    uint32_t ulBitsToClearOnEntry,
301                                                    uint32_t ulBitsToClearOnExit,
302                                                    uint32_t * pulNotificationValue,
303                                                    TickType_t xTicksToWait,
304                                                    int cmock_num_calls )
305 {
306     uint8_t data[ TEST_STREAM_BUFFER_TRIGGER_LEVEL ] = { 0 };
307     size_t dataSent = 0;
308     BaseType_t receiverTaskWokenFromISR = pdFALSE;
309
310     /* Send enough (trigger level) bytes to stream buffer to wake up the receiver Task. */
311     dataSent = xStreamBufferSendFromISR( xStreamBuffer, data, TEST_STREAM_BUFFER_TRIGGER_LEVEL, &receiverTaskWokenFromISR );
312     TEST_ASSERT_EQUAL( pdTRUE, receiverTaskWokenFromISR );
313     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_TRIGGER_LEVEL, dataSent );
314     return pdTRUE;
315 }
316
317 static BaseType_t sendLessThanTriggerLevelBytesCallback( UBaseType_t uxIndexToWaitOn,
318                                                          uint32_t ulBitsToClearOnEntry,
319                                                          uint32_t ulBitsToClearOnExit,
320                                                          uint32_t * pulNotificationValue,
321                                                          TickType_t xTicksToWait,
322                                                          int cmock_num_calls )
323 {
324     uint8_t data[ TEST_STREAM_BUFFER_TRIGGER_LEVEL ] = { 0 };
325     size_t dataSent = 0;
326
327     /* Sending less than trigger level bytes should not wake up the receiver Task. */
328     dataSent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_TRIGGER_LEVEL - 1, 0 );
329     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_TRIGGER_LEVEL - 1, dataSent );
330     return pdTRUE;
331 }
332
333 static BaseType_t sendLessThanTriggerLevelBytesFromISRCallback( UBaseType_t uxIndexToWaitOn,
334                                                                 uint32_t ulBitsToClearOnEntry,
335                                                                 uint32_t ulBitsToClearOnExit,
336                                                                 uint32_t * pulNotificationValue,
337                                                                 TickType_t xTicksToWait,
338                                                                 int cmock_num_calls )
339 {
340     uint8_t data[ TEST_STREAM_BUFFER_TRIGGER_LEVEL ] = { 0 };
341     size_t dataSent = 0;
342     BaseType_t receiverTaskWokenFromISR = pdFALSE;
343
344     /* Sending less than trigger level bytes should not wake up the receiver Task. */
345     dataSent = xStreamBufferSendFromISR( xStreamBuffer, data, TEST_STREAM_BUFFER_TRIGGER_LEVEL - 1, &receiverTaskWokenFromISR );
346     TEST_ASSERT_EQUAL( pdFALSE, receiverTaskWokenFromISR );
347     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_TRIGGER_LEVEL - 1, dataSent );
348     return pdTRUE;
349 }
350
351 static BaseType_t receiverTaskNotificationFromISRCallback( TaskHandle_t xTaskToNotify,
352                                                            UBaseType_t uxIndexToNotify,
353                                                            uint32_t ulValue,
354                                                            eNotifyAction eAction,
355                                                            uint32_t * pulPreviousNotificationValue,
356                                                            BaseType_t * pxHigherPriorityTaskWoken,
357                                                            int cmock_num_calls )
358 {
359     TEST_ASSERT_EQUAL( receiverTask, xTaskToNotify );
360     receiverTaskWoken++;
361     *pxHigherPriorityTaskWoken = pdTRUE;
362
363     return pdTRUE;
364 }
365
366 static BaseType_t receiverTaskNotificationCallback( TaskHandle_t xTaskToNotify,
367                                                     UBaseType_t uxIndexToNotify,
368                                                     uint32_t ulValue,
369                                                     eNotifyAction eAction,
370                                                     uint32_t * pulPreviousNotificationValue,
371                                                     int cmock_num_calls )
372 {
373     TEST_ASSERT_EQUAL( receiverTask, xTaskToNotify );
374     receiverTaskWoken++;
375     return pdTRUE;
376 }
377
378 /*******************************************************************************
379  * Unity fixtures
380  ******************************************************************************/
381 void setUp( void )
382 {
383     assertionFailed = 0;
384     xStreamBuffer = NULL;
385     senderTaskWoken = 0;
386     receiverTaskWoken = 0;
387     shouldAbortOnAssertion = pdTRUE;
388     dynamicMemoryAllocated = 0;
389
390
391     mock_task_Init();
392     mock_fake_assert_Init();
393     mock_fake_port_Init();
394
395     vFakePortEnterCriticalSection_Ignore();
396     vFakePortExitCriticalSection_Ignore();
397     ulFakePortSetInterruptMaskFromISR_IgnoreAndReturn( 0U );
398     vFakePortClearInterruptMaskFromISR_Ignore();
399     vFakeAssert_StubWithCallback( vFakeAssertStub );
400     /* Track calls to malloc / free */
401     UnityMalloc_StartTest();
402 }
403
404 /*! called before each test case */
405 void tearDown( void )
406 {
407     TEST_ASSERT_EQUAL_MESSAGE( 0, assertionFailed, "Assertion check failed in code." );
408     UnityMalloc_EndTest();
409     mock_task_Verify();
410     mock_task_Destroy();
411     mock_fake_assert_Verify();
412     mock_fake_assert_Destroy();
413     mock_fake_port_Verify();
414     mock_fake_port_Destroy();
415 }
416
417 /*! called at the beginning of the whole suite */
418 void suiteSetUp()
419 {
420 }
421
422 /*! called at the end of the whole suite */
423 int suiteTearDown( int numFailures )
424 {
425     return numFailures;
426 }
427
428 static void validate_stream_buffer_init_state( StreamBufferHandle_t xStreamBuffer,
429                                                size_t bufferSize )
430 {
431     TEST_ASSERT_TRUE( xStreamBufferIsEmpty( xStreamBuffer ) );
432     TEST_ASSERT_FALSE( xStreamBufferIsFull( xStreamBuffer ) );
433     TEST_ASSERT_EQUAL( bufferSize, xStreamBufferSpacesAvailable( xStreamBuffer ) );
434     TEST_ASSERT_EQUAL( 0U, xStreamBufferBytesAvailable( xStreamBuffer ) );
435     TEST_ASSERT_EQUAL( 0U, xStreamBufferNextMessageLengthBytes( xStreamBuffer ) );
436     TEST_ASSERT_EQUAL( 0, ucStreamBufferGetStreamBufferType( xStreamBuffer ) );
437 }
438
439 static void validate_and_clear_assertions( void )
440 {
441     TEST_ASSERT_EQUAL( 1, assertionFailed );
442     assertionFailed = 0;
443 }
444
445 /* ==============================  Test Cases  ============================== */
446
447 /**
448  * @brief Validates that stream buffer of sample size is created successfully.
449  */
450 void test_xStreamBufferCreate_success( void )
451 {
452     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
453     TEST_ASSERT_NOT_EQUAL( NULL, xStreamBuffer );
454     validate_stream_buffer_init_state( xStreamBuffer, TEST_STREAM_BUFFER_SIZE );
455
456     /* Verify internal memory allocated is equal to size of the struct + buffer size + 1. */
457     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE + 1U + sizeof( StaticStreamBuffer_t ), dynamicMemoryAllocated );
458
459     /* Set a stream buffer number and get it. */
460     vStreamBufferSetStreamBufferNumber( xStreamBuffer, TEST_STREAM_BUFFER_NUMBER );
461     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_NUMBER, uxStreamBufferGetStreamBufferNumber( xStreamBuffer ) );
462
463     vStreamBufferDelete( xStreamBuffer );
464 }
465
466 /**
467  * Returns NULL if there is an integer overflow in the buffer size.
468  */
469 void test_xStreamBufferCreate_integer_overflow( void )
470 {
471     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_MAX_UINT_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
472     TEST_ASSERT_EQUAL( NULL, xStreamBuffer );
473 }
474
475 /**
476  * @brief Returns NULL if internal memory allocation of the stream buffer fails.
477  */
478 void test_xStreamBufferCreate_malloc_fail( void )
479 {
480     UnityMalloc_MakeMallocFailAfterCount( 0 );
481
482     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
483     TEST_ASSERT_EQUAL( NULL, xStreamBuffer );
484 }
485
486 /**
487  * @brief Assertion fails if a zero buffer size is passed as  the parameter.
488  */
489 void test_xStreamBufferCreate_zero_buffer_size( void )
490 {
491     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferCreate( 0, TEST_STREAM_BUFFER_TRIGGER_LEVEL ) );
492     validate_and_clear_assertions();
493 }
494
495 /**
496  * @brief Assertion fails if trigger level is greater than the stream buffer size.
497  */
498 void test_xStreamBufferCreate_invalid_trigger_level( void )
499 {
500     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, ( TEST_STREAM_BUFFER_SIZE + 1 ) ) );
501     validate_and_clear_assertions();
502 }
503
504 /**
505  * @brief Assertion fails while trying to delete a null stream buffer.
506  */
507 void test_xStreamBufferDelete_null_stream_buffer( void )
508 {
509     EXPECT_ASSERT_BREAK( vStreamBufferDelete( NULL ) );
510     validate_and_clear_assertions();
511 }
512
513 /**
514  * @brief Validates stream buffer create using a static buffer is successful.
515  */
516 void test_xStreamBufferCreateStatic_success( void )
517 {
518     StaticStreamBuffer_t streamBufferStruct;
519
520     /* The size of stream buffer array should be one greater than the required size of stream buffer. */
521     uint8_t streamBufferArray[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 };
522
523     xStreamBuffer = xStreamBufferCreateStatic( sizeof( streamBufferArray ), TEST_STREAM_BUFFER_TRIGGER_LEVEL, streamBufferArray, &streamBufferStruct );
524
525     TEST_ASSERT_NOT_NULL( xStreamBuffer );
526     validate_stream_buffer_init_state( xStreamBuffer, TEST_STREAM_BUFFER_SIZE );
527
528     vStreamBufferDelete( xStreamBuffer );
529 }
530
531 /**
532  * @brief Validates stream buffer static create fails if NULL array is passed.
533  */
534 void test_xStreamBufferCreateStatic_null_array( void )
535 {
536     StaticStreamBuffer_t streamBufferStruct;
537
538     /* The size of stream buffer array should be one greater than the required size of stream buffer. */
539     uint8_t streamBufferArray[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 };
540
541     /* Tests should abort if assertion is enabled or return NULL. */
542     shouldAbortOnAssertion = pdFALSE;
543
544     /* Returns NULL if a NULL pointer is passed as the stream buffer storage area. */
545     xStreamBuffer = xStreamBufferCreateStatic( sizeof( streamBufferArray ), TEST_STREAM_BUFFER_TRIGGER_LEVEL, NULL, &streamBufferStruct );
546     TEST_ASSERT_NULL( xStreamBuffer );
547     validate_and_clear_assertions();
548 }
549
550 /**
551  * @brief Validates stream buffer static create fails if NULL struct is passed.
552  */
553 void test_xStreamBufferCreateStatic_invalid_null_struct( void )
554 {
555     /* The size of stream buffer array should be one greater than the required size of stream buffer. */
556     uint8_t streamBufferArray[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 };
557
558     /* Tests should abort if assertion is enabled or return NULL. */
559     shouldAbortOnAssertion = pdFALSE;
560
561     /* Returns NULL if a NULL struct is passed as a parameter. */
562     xStreamBuffer = xStreamBufferCreateStatic( sizeof( streamBufferArray ), TEST_STREAM_BUFFER_TRIGGER_LEVEL, streamBufferArray, NULL );
563     TEST_ASSERT_NULL( xStreamBuffer );
564     validate_and_clear_assertions();
565 }
566
567 /**
568  * @brief Validates stream buffer static create fails on passing invalid trigger level
569  */
570 void test_xStreamBufferCreateStatic_invalid_trigger_level( void )
571 {
572     StaticStreamBuffer_t streamBufferStruct;
573     /* The size of stream buffer array should be one greater than the required size of stream buffer. */
574     uint8_t streamBufferArray[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 };
575
576     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferCreateStatic( sizeof( streamBufferArray ), TEST_STREAM_BUFFER_SIZE + 2, streamBufferArray, &streamBufferStruct ) );
577
578     validate_and_clear_assertions();
579 }
580
581 /**
582  * @brief Validates a task is able to send upto buffer space available without blocking.
583  */
584 void test_xStreamBufferSend_success( void )
585 {
586     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0 };
587     size_t sent = 0;
588
589     vTaskSetTimeOutState_Ignore();
590     vTaskSuspendAll_Ignore();
591     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
592
593     /* Create a stream buffer of the default test sample size. */
594     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
595     TEST_ASSERT_NOT_NULL( xStreamBuffer );
596     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
597
598     /* Sending bytes of size upto to available size should succeed without blocking. */
599     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
600     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sent );
601     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
602     TEST_ASSERT_EQUAL( pdFALSE, xStreamBufferIsEmpty( xStreamBuffer ) );
603
604     vStreamBufferDelete( xStreamBuffer );
605 }
606
607 /**
608  * @brief Validates that sending data more than stream buffer size will cap it to the size of stream buffer without blocking.
609  */
610 void test_xStreamBufferSend_more_than_buffer_size( void )
611 {
612     uint8_t data[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 };
613     size_t sent = 0;
614
615     vTaskSetTimeOutState_Ignore();
616     vTaskSuspendAll_Ignore();
617     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
618
619     /* Create a stream buffer of the default test sample size. */
620     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
621     TEST_ASSERT_NOT_NULL( xStreamBuffer );
622     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
623
624     /* Sending bytes more than stream buffer size caps its to the size of stream buffer. */
625     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE + 1, TEST_STREAM_BUFFER_WAIT_TICKS );
626     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sent );
627     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
628
629     vStreamBufferDelete( xStreamBuffer );
630 }
631
632 /**
633  * @brief Sending zero bytes to stream buffer should return write nothing to the
634  * buffer and return 0.
635  */
636 void test_xStreamBufferSend_zero_bytes( void )
637 {
638     uint8_t data[ TEST_STREAM_BUFFER_SIZE + 1 ] = { 0 };
639
640     vTaskSetTimeOutState_Ignore();
641     vTaskSuspendAll_Ignore();
642     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
643
644     /* Create a stream buffer of the default test sample size. */
645     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
646     TEST_ASSERT_NOT_NULL( xStreamBuffer );
647     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
648
649     TEST_ASSERT_EQUAL( pdFALSE, xStreamBufferSend( xStreamBuffer, data, 0U, TEST_STREAM_BUFFER_WAIT_TICKS ) );
650
651     vStreamBufferDelete( xStreamBuffer );
652 }
653
654 /**
655  * @brief Validates cases where stream buffer has insufficient space to send the data and sender has to block.
656  */
657 void test_xStreamBufferSend_blocking( void )
658 {
659     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0 };
660     size_t sent = 0;
661
662     vTaskSetTimeOutState_Ignore();
663     xTaskGenericNotifyStateClear_IgnoreAndReturn( pdTRUE );
664     xTaskGetCurrentTaskHandle_IgnoreAndReturn( senderTask );
665     vTaskSuspendAll_Ignore();
666     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
667
668     /* Create a stream buffer of test sample size. */
669     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
670     TEST_ASSERT_NOT_NULL( xStreamBuffer );
671     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
672
673     /* Sending upto size of stream buffer should not block. */
674     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE - 1, TEST_STREAM_BUFFER_WAIT_TICKS );
675     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE - 1, sent );
676     TEST_ASSERT_EQUAL( 1, xStreamBufferSpacesAvailable( xStreamBuffer ) );
677
678     /*
679      * Sending beyond the stream buffer size should make task wait for upto TEST_STREAM_BUFFER_WAIT_TICKS. After the timeout
680      * elapses, task should return with partial bytes sent.
681      */
682     xTaskGenericNotifyWait_ExpectAndReturn( 0, 0, 0, NULL, TEST_STREAM_BUFFER_WAIT_TICKS, pdTRUE );
683     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
684     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
685     TEST_ASSERT_EQUAL( 1, sent );
686     TEST_ASSERT_EQUAL( pdTRUE, xStreamBufferIsFull( xStreamBuffer ) );
687
688     /*
689      * A task trying to send to a stream buffer without any space available should block for upto TEST_STREAM_BUFFER_WAIT_TICKS.
690      * Sender task should be notified and woken up when bytes are consumed by a receiver task during the wait period.
691      */
692     xTaskGenericNotifyWait_StubWithCallback( streamBufferReceiveCallback );
693     xTaskGenericNotify_StubWithCallback( senderTaskNotificationCallback );
694     xTaskCheckForTimeOut_IgnoreAndReturn( pdFALSE );
695     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
696     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sent );
697     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
698     TEST_ASSERT_EQUAL( 1, senderTaskWoken );
699
700     /*
701      * A task trying to send to a stream buffer without any space available should block for upto TEST_STREAM_BUFFER_WAIT_TICKS.
702      * Sender task should be notified and woken up when bytes are consumed by an ISR during the wait period.
703      */
704     xTaskGenericNotifyWait_StubWithCallback( streamBufferReceiveFromISRCallback );
705     xTaskGenericNotifyFromISR_StubWithCallback( senderTaskNotificationFromISRCallback );
706     xTaskCheckForTimeOut_IgnoreAndReturn( pdFALSE );
707     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
708     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sent );
709     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
710     TEST_ASSERT_EQUAL( 2, senderTaskWoken );
711
712     vStreamBufferDelete( xStreamBuffer );
713 }
714
715 /**
716  * @brief Validates that stream buffer does not block if zero wait time is passed.
717  */
718 void test_xStreamBufferSend_zero_wait_ticks( void )
719 {
720     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0 };
721     size_t dataSent = 0;
722
723     vTaskSetTimeOutState_Ignore();
724     vTaskSuspendAll_Ignore();
725     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
726
727     /* Create a stream buffer of sample size. */
728     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
729     TEST_ASSERT_NOT_NULL( xStreamBuffer );
730     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
731
732     /* Sending data of upto stream buffer size should not block. */
733     dataSent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
734     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, dataSent );
735     TEST_ASSERT_EQUAL( 0, xStreamBufferSpacesAvailable( xStreamBuffer ) );
736
737     /*  Sending data beyond stream buffer size but with zero wait ticks should not block and return zero bytes sent. */
738     dataSent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, 0 );
739     TEST_ASSERT_EQUAL( 0, dataSent );
740
741     vStreamBufferDelete( xStreamBuffer );
742 }
743
744 /**
745  * @brief Validates that a task is able to receive from a non empty stream buffer without blocking.
746  */
747 void test_xStreamBufferReceive_success( void )
748 {
749     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
750     uint8_t dataReceived[ TEST_STREAM_BUFFER_SIZE ] = { 0x00 };
751     size_t sentBytes = 0, receivedBytes = 0;
752
753     vTaskSetTimeOutState_Ignore();
754     vTaskSuspendAll_Ignore();
755     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
756
757     /* Create a stream buffer of sample size. */
758     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
759     TEST_ASSERT_NOT_NULL( xStreamBuffer );
760     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
761
762     /* Send TEST_STREAM_BUFFER_SIZE data to the stream buffer. */
763     sentBytes = xStreamBufferSend( xStreamBuffer, &data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
764     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sentBytes );
765
766     /* Receive the partial data from stream Buffer without blocking. */
767     receivedBytes = xStreamBufferReceive( xStreamBuffer, &dataReceived, TEST_STREAM_BUFFER_SIZE - 1, TEST_STREAM_BUFFER_WAIT_TICKS );
768     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE - 1, receivedBytes );
769     TEST_ASSERT_EQUAL_HEX8_ARRAY( data, dataReceived, receivedBytes );
770     TEST_ASSERT_EQUAL( 1, xStreamBufferBytesAvailable( xStreamBuffer ) );
771
772     /* Request for full TEST_STREAM_BUFFER_SIZE bytes, but only receive what's available without blocking.  */
773     receivedBytes = xStreamBufferReceive( xStreamBuffer, &dataReceived, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
774     TEST_ASSERT_EQUAL( 1, receivedBytes );
775     TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xStreamBuffer ) );
776
777     vStreamBufferDelete( xStreamBuffer );
778 }
779
780 /**
781  * @brief Validates receiving from an empty stream buffer will block until at least trigger level bytes are
782  * sent to the buffer.
783  */
784 void test_xStreamBufferReceive_blocking( void )
785 {
786     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
787     size_t receivedBytes = 0;
788
789     vTaskSetTimeOutState_Ignore();
790     xTaskGenericNotifyStateClear_IgnoreAndReturn( pdTRUE );
791     xTaskGetCurrentTaskHandle_IgnoreAndReturn( receiverTask );
792     xTaskGenericNotify_StubWithCallback( receiverTaskNotificationCallback );
793
794     xTaskCheckForTimeOut_IgnoreAndReturn( pdFALSE );
795     vTaskSuspendAll_Ignore();
796     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
797
798     /* Create a stream buffer of sample size. */
799     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
800     TEST_ASSERT_NOT_NULL( xStreamBuffer );
801     TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xStreamBuffer ) );
802
803     /*
804      * Receiving from an empty buffer causes the task to wait for TEST_STREAM_BUFFER_WAIT_TICKS period.
805      * After the timeout elapses, task returns with zero bytes received.
806      */
807     xTaskGenericNotifyWait_ExpectAndReturn( 0, 0, 0, NULL, TEST_STREAM_BUFFER_WAIT_TICKS, pdTRUE );
808     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
809     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
810     TEST_ASSERT_EQUAL( 0, receivedBytes );
811
812     /*
813      * Sending at least trigger level bytes, should notify and wake up the receiver task.
814      */
815     xTaskGenericNotifyWait_StubWithCallback( streamBufferSendCallback );
816     xTaskCheckForTimeOut_IgnoreAndReturn( pdFALSE );
817     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
818     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_TRIGGER_LEVEL, receivedBytes );
819     TEST_ASSERT_EQUAL( 1, receiverTaskWoken );
820     TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xStreamBuffer ) );
821
822     /*
823      * Sending at least trigger level bytes from ISR, should notify and wake up the receiver task.
824      */
825     xTaskGenericNotifyWait_StubWithCallback( streamBufferSendFromISRCallback );
826     xTaskGenericNotifyFromISR_StubWithCallback( receiverTaskNotificationFromISRCallback );
827     xTaskCheckForTimeOut_IgnoreAndReturn( pdFALSE );
828     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
829     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_TRIGGER_LEVEL, receivedBytes );
830     TEST_ASSERT_EQUAL( 2, receiverTaskWoken );
831     TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xStreamBuffer ) );
832
833
834     /* Sending less than trigger level bytes should not wake up the task. */
835     xTaskGenericNotifyWait_StubWithCallback( sendLessThanTriggerLevelBytesCallback );
836     xTaskGenericNotifyFromISR_StubWithCallback( receiverTaskNotificationFromISRCallback );
837     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
838     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
839     TEST_ASSERT_EQUAL( 2, receiverTaskWoken );
840
841     /* Sending less than trigger level bytes from ISR should not wake up the task. */
842     xTaskGenericNotifyWait_StubWithCallback( sendLessThanTriggerLevelBytesFromISRCallback );
843     xTaskGenericNotifyFromISR_StubWithCallback( receiverTaskNotificationFromISRCallback );
844     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
845     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
846     TEST_ASSERT_EQUAL( 2, receiverTaskWoken );
847
848     vStreamBufferDelete( xStreamBuffer );
849 }
850
851 /**
852  * @brief Validates that receiver task does not block if zero wait ticks are passed.
853  */
854 void test_xStreamBufferReceive_zero_wait_ticks( void )
855 {
856     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
857     size_t receivedBytes = 0;
858
859     vTaskSetTimeOutState_Ignore();
860     vTaskSuspendAll_Ignore();
861     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
862
863     /* Create a stream buffer of sample size. */
864     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
865     TEST_ASSERT_NOT_NULL( xStreamBuffer );
866     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
867
868     /* Task does not block on an empty stream buffer if zero wait ticks are passed. */
869     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, 0 );
870     TEST_ASSERT_EQUAL( 0, receivedBytes );
871
872     vStreamBufferDelete( xStreamBuffer );
873 }
874
875 /**
876  * @brief Validates that an interrupt service routine is able to read data from stream
877  * buffer without blocking.
878  */
879 void test_xStreamBufferReceiveFromISR_success( void )
880 {
881     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
882     uint8_t dataReceived[ TEST_STREAM_BUFFER_SIZE ] = { 0x00 };
883     size_t receivedBytes = 0, sentBytes = 0;
884     BaseType_t xHighPriorityTaskWoken = pdFALSE;
885
886     vTaskSetTimeOutState_Ignore();
887     vTaskSuspendAll_Ignore();
888     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
889
890     /* Create a stream buffer of sample size. */
891     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
892     TEST_ASSERT_NOT_NULL( xStreamBuffer );
893     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
894
895     /* Send data to fill the stream buffer to maximum capacity. */
896     sentBytes = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
897     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sentBytes );
898     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
899
900     /* Receive partial data from stream buffer without blocking. */
901     receivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer, &dataReceived, ( TEST_STREAM_BUFFER_SIZE - 1U ), &xHighPriorityTaskWoken );
902     TEST_ASSERT_EQUAL( ( TEST_STREAM_BUFFER_SIZE - 1U ), receivedBytes );
903     TEST_ASSERT_EQUAL_HEX8_ARRAY( data, dataReceived, receivedBytes );
904     TEST_ASSERT_EQUAL( pdFALSE, xHighPriorityTaskWoken );
905
906     /* Try to receive full capacity from stream buffer but only remaining data is received. */
907     receivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer, &dataReceived, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken );
908     TEST_ASSERT_EQUAL( 1U, receivedBytes );
909     TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xStreamBuffer ) );
910
911     vStreamBufferDelete( xStreamBuffer );
912 }
913
914 /**
915  * @brief Assertion fails if a null stream buffer is passed.
916  */
917 void test_xStreamBufferReceiveFromISR_null_stream_buffer( void )
918 {
919     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
920     BaseType_t xHighPriorityTaskWoken = pdFALSE;
921
922     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferReceiveFromISR( NULL, data, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken ) );
923
924     validate_and_clear_assertions();
925 }
926
927 /**
928  * @brief Assertion fails if a null message is passed.
929  */
930 void test_xStreamBufferReceiveFromISR_null_buffer( void )
931 {
932     BaseType_t xHighPriorityTaskWoken = pdFALSE;
933
934     /* Create a stream buffer of sample size. */
935     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
936     TEST_ASSERT_NOT_NULL( xStreamBuffer );
937     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
938
939     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferReceiveFromISR( xStreamBuffer, NULL, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken ) );
940
941     validate_and_clear_assertions();
942
943     vStreamBufferDelete( xStreamBuffer );
944 }
945
946 /**
947  * @brief Validates that an interrupt service routine is able to send data upto the size of stream buffer without blocking.
948  */
949 void test_xStreamBufferSendFromISR_success( void )
950 {
951     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
952     size_t sentBytes = 0;
953     BaseType_t xHighPriorityTaskWoken = pdFALSE;
954
955     vTaskSetTimeOutState_Ignore();
956     vTaskSuspendAll_Ignore();
957     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
958
959     /* Create a stream buffer of sample size. */
960     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
961     TEST_ASSERT_NOT_NULL( xStreamBuffer );
962     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
963
964     /* Send data to an empty buffer should succeed without blocking. */
965     sentBytes = xStreamBufferSendFromISR( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE - 1U, &xHighPriorityTaskWoken );
966     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE - 1U, sentBytes );
967     TEST_ASSERT_EQUAL( 1U, xStreamBufferSpacesAvailable( xStreamBuffer ) );
968     TEST_ASSERT_EQUAL( pdFALSE, xHighPriorityTaskWoken );
969
970     /* Send full capacity from ISR again should send partial bytes without blocking. */
971     sentBytes = xStreamBufferSendFromISR( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken );
972     TEST_ASSERT_EQUAL( 1U, sentBytes );
973     TEST_ASSERT_EQUAL( 0, xStreamBufferSpacesAvailable( xStreamBuffer ) );
974     TEST_ASSERT_EQUAL( pdFALSE, xHighPriorityTaskWoken );
975
976     /* Send to full stream buffer should return 0 bytes sent without blocking. */
977     sentBytes = xStreamBufferSendFromISR( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken );
978     TEST_ASSERT_EQUAL( 0U, sentBytes );
979     TEST_ASSERT_EQUAL( pdFALSE, xHighPriorityTaskWoken );
980
981     vStreamBufferDelete( xStreamBuffer );
982 }
983
984 /**
985  * @brief Assertion fails if a null stream buffer is passed.
986  */
987 void test_xStreamBufferSendFromISR_null_stream_buffer( void )
988 {
989     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
990     BaseType_t xHighPriorityTaskWoken = pdFALSE;
991
992     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferSendFromISR( NULL, data, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken ) );
993
994     validate_and_clear_assertions();
995 }
996
997 /**
998  * @brief Assertion fails if a null message is passed.
999  */
1000 void test_xStreamBufferSendFromISR_null_message( void )
1001 {
1002     BaseType_t xHighPriorityTaskWoken = pdFALSE;
1003
1004     /* Create a stream buffer of sample size. */
1005     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1006     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1007     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1008
1009     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferSendFromISR( xStreamBuffer, NULL, TEST_STREAM_BUFFER_SIZE, &xHighPriorityTaskWoken ) );
1010
1011     validate_and_clear_assertions();
1012
1013     vStreamBufferDelete( xStreamBuffer );
1014 }
1015
1016 /**
1017  * @brief Validates user is able to reset the stream buffer back to empty state.
1018  */
1019 void test_xStreamBufferReset_success( void )
1020 {
1021     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0 };
1022     size_t dataSent = 0;
1023     BaseType_t status = pdFALSE;
1024
1025     vTaskSetTimeOutState_Ignore();
1026     vTaskSuspendAll_Ignore();
1027     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1028
1029     /* Create a stream buffer of the default test sample size. */
1030     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1031     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1032
1033     /* Validate stream buffer is empty initially. */
1034     validate_stream_buffer_init_state( xStreamBuffer, TEST_STREAM_BUFFER_SIZE );
1035
1036     /* Send full capacity to stream buffer. */
1037     dataSent = xStreamBufferSend( xStreamBuffer, data, sizeof( data ), TEST_STREAM_BUFFER_WAIT_TICKS );
1038     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, dataSent );
1039
1040     /* Verify that all bytes are available. */
1041     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
1042
1043     /* Reset the stream buffer back to empty state. */
1044     status = xStreamBufferReset( xStreamBuffer );
1045     TEST_ASSERT_EQUAL( pdTRUE, status );
1046
1047     /* Validate that stream buffer is empty. */
1048     validate_stream_buffer_init_state( xStreamBuffer, TEST_STREAM_BUFFER_SIZE );
1049
1050     vStreamBufferDelete( xStreamBuffer );
1051 }
1052
1053 /**
1054  * @brief Resetting a null stream buffer should fail assertion.
1055  */
1056 void test_xStreamBufferReset_null_stream_buffer( void )
1057 {
1058     vTaskSetTimeOutState_Ignore();
1059     vTaskSuspendAll_Ignore();
1060     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1061
1062     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferReset( NULL ) );
1063     validate_and_clear_assertions();
1064 }
1065
1066 /**
1067  * @brief Validates that stream buffer reset fails if a sender or receiver task is blocked on it.
1068  */
1069 void test_xStreamBufferReset_while_blocked( void )
1070 {
1071     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
1072     size_t sentBytes = 0, receivedBytes = 0;
1073
1074     vTaskSetTimeOutState_Ignore();
1075     xTaskGenericNotifyStateClear_IgnoreAndReturn( pdTRUE );
1076     xTaskGetCurrentTaskHandle_IgnoreAndReturn( senderTask );
1077     xTaskGenericNotifyWait_StubWithCallback( resetWhenBlockedCallback );
1078     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
1079     vTaskSuspendAll_Ignore();
1080     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1081
1082     /* Create a stream buffer of sample size. */
1083     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1084     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1085     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1086
1087     /*
1088      * Perform a blocking operation to receive from an empty stream buffer. Reset stream buffer within receiver task notify
1089      * wait callback should fail.
1090      */
1091     receivedBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1092     TEST_ASSERT_EQUAL( 0, receivedBytes );
1093
1094     /*
1095      * Send full size data to stream buffer.
1096      */
1097     sentBytes = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1098     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sentBytes );
1099     TEST_ASSERT_EQUAL( pdTRUE, xStreamBufferIsFull( xStreamBuffer ) );
1100
1101     /*
1102      * Sending data to full stream buffer causes task to be blocked. Reset stream buffer within sender task notify
1103      * wait callback should fail.
1104      */
1105     sentBytes = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1106     TEST_ASSERT_EQUAL( 0, sentBytes );
1107
1108     vStreamBufferDelete( xStreamBuffer );
1109 }
1110
1111 /**
1112  * @brief Validates that a receiver task is able to receive data after lowering the stream buffer trigger level.
1113  */
1114 void test_xStreamBufferSetTrigerLevel_success( void )
1115 {
1116     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
1117     BaseType_t status;
1118
1119     vTaskSetTimeOutState_Ignore();
1120     xTaskGenericNotifyStateClear_IgnoreAndReturn( pdTRUE );
1121     xTaskGetCurrentTaskHandle_IgnoreAndReturn( receiverTask );
1122     xTaskGenericNotify_StubWithCallback( receiverTaskNotificationCallback );
1123     xTaskGenericNotifyWait_StubWithCallback( streamBufferSendCallback );
1124     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
1125     vTaskSuspendAll_Ignore();
1126     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1127
1128     /* Create stream buffer with trigger level equal to maximum stream buffer size. */
1129     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_SIZE );
1130     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1131
1132     /* Set the trigger level to TEST_STREAM_BUFFER_TRIGGER_LEVEL. */
1133     status = xStreamBufferSetTriggerLevel( xStreamBuffer, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1134     TEST_ASSERT_EQUAL( pdTRUE, status );
1135
1136     /*
1137      * Receive data from empty stream buffer, with trigger level bytes send from the callback. This should unblock
1138      * the task.
1139      */
1140     ( void ) xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1141     TEST_ASSERT_EQUAL( 1, receiverTaskWoken );
1142
1143     vStreamBufferDelete( xStreamBuffer );
1144 }
1145
1146 /**
1147  * @brief Validate setting trigger level with invalid parameters fails.
1148  */
1149 void test_xStreamBufferSetTrigerLevel_larger_than_buffer_size( void )
1150 {
1151     BaseType_t status;
1152
1153     /* Create stream buffer. */
1154     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1155     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1156
1157     /* Set the trigger level to ( TEST_STREAM_BUFFER_SIZE + 1) should fail. */
1158     status = xStreamBufferSetTriggerLevel( xStreamBuffer, ( TEST_STREAM_BUFFER_SIZE + 1 ) );
1159     TEST_ASSERT_EQUAL( pdFALSE, status );
1160
1161     vStreamBufferDelete( xStreamBuffer );
1162 }
1163
1164 /**
1165  * @brief Set the trigger level to 0 should pass but internally set trigger level to 1.
1166  */
1167 void test_xStreamBufferSetTrigerLevel_zero( void )
1168 {
1169     BaseType_t status;
1170
1171     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1172     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1173
1174     status = xStreamBufferSetTriggerLevel( xStreamBuffer, 0 );
1175     TEST_ASSERT_EQUAL( pdTRUE, status );
1176
1177     vStreamBufferDelete( xStreamBuffer );
1178 }
1179
1180 /**
1181  * @brief Setting trigger level for  a null stream buffer should fail assertion.
1182  */
1183 void test_xStreamBufferSetTriggerLevel_null_stream_buffer( void )
1184 {
1185     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferSetTriggerLevel( NULL, TEST_STREAM_BUFFER_TRIGGER_LEVEL ) );
1186     validate_and_clear_assertions();
1187 }
1188
1189 /**
1190  * @brief Checking bytes available for a null stream buffer should fail assertion.
1191  */
1192 void test_xStreamBufferBytesAvailable_null_stream_buffer( void )
1193 {
1194     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferBytesAvailable( NULL ) );
1195     validate_and_clear_assertions();
1196 }
1197
1198 /**
1199  * @brief Checking if stream buffer is full for a null stream buffer should fail assertion.
1200  */
1201 void test_xStreamBufferIsFull_null_stream_buffer( void )
1202 {
1203     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferIsFull( NULL ) );
1204     validate_and_clear_assertions();
1205 }
1206
1207 /**
1208  * @brief Checking if stream buffer is empty for a null stream buffer should fail assertion.
1209  */
1210 void test_xStreamBufferIsEmpty_null_stream_buffer( void )
1211 {
1212     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferIsEmpty( NULL ) );
1213     validate_and_clear_assertions();
1214 }
1215
1216 /**
1217  * @brief Checking buffer size for a null stream buffer should fail assertion.
1218  */
1219 void test_xStreamBufferSpacesAvailable_null_stream_buffer( void )
1220 {
1221     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferSpacesAvailable( NULL ) );
1222     validate_and_clear_assertions();
1223 }
1224
1225 /**
1226  * @brief Checking Next message length available for a null stream buffer fails
1227  * assertion.
1228  */
1229 void test_xStreamBufferNextMessageLengthBytes_null_stream_buffer( void )
1230 {
1231     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferNextMessageLengthBytes( NULL ) );
1232     validate_and_clear_assertions();
1233 }
1234
1235 /**
1236  * @brief Validates xStreamBufferSendCompletedFromISR function unblocks a receive task.
1237  */
1238 void test_xStreamBufferSendCompletedFromISR_success( void )
1239 {
1240     BaseType_t status = pdFALSE;
1241     BaseType_t highPriorityTaskWoken = pdFALSE;
1242     uint8_t data[ TEST_STREAM_BUFFER_SIZE ];
1243     size_t receiveBytes = 0;
1244
1245     /* Create a stream buffer of the default test sample size. */
1246     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1247     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1248
1249     /* Send completed from ISR without receiver task waiting. */
1250     status = xStreamBufferSendCompletedFromISR( xStreamBuffer, &highPriorityTaskWoken );
1251     TEST_ASSERT_EQUAL( pdFALSE, status );
1252     TEST_ASSERT_EQUAL( pdFALSE, highPriorityTaskWoken );
1253
1254     /* Send completed from ISR with receiver task waiting. */
1255     vTaskSetTimeOutState_Ignore();
1256     xTaskGenericNotifyStateClear_IgnoreAndReturn( pdTRUE );
1257     xTaskGetCurrentTaskHandle_IgnoreAndReturn( receiverTask );
1258     xTaskGenericNotifyWait_StubWithCallback( sendCompletedFromISRCallback );
1259     xTaskGenericNotifyFromISR_StubWithCallback( receiverTaskNotificationFromISRCallback );
1260     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
1261     vTaskSuspendAll_Ignore();
1262     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1263
1264     receiveBytes = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1265     TEST_ASSERT_EQUAL( 0, receiveBytes );
1266     TEST_ASSERT_EQUAL( 1, receiverTaskWoken );
1267
1268     vStreamBufferDelete( xStreamBuffer );
1269 }
1270
1271 /**
1272  * @brief Validates xStreamBufferSendCompletedFromISR fails assertion if a null stream buffer is passed.
1273  */
1274 void test_xStreamBufferSendCompletedFromISR_null_stream_buffer( void )
1275 {
1276     BaseType_t highPriorityTaskWoken = pdFALSE;
1277
1278     /* Send completed from ISR without receiver task waiting. */
1279     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferSendCompletedFromISR( NULL, &highPriorityTaskWoken ) );
1280     validate_and_clear_assertions();
1281 }
1282
1283 /**
1284  * @brief Validates xStreamBufferReceiveCompletedFromISR unblocks a sender task.
1285  */
1286 void test_xStreamBufferReceiveCompletedFromISR_success( void )
1287 {
1288     BaseType_t status = pdFALSE;
1289     BaseType_t highPriorityTaskWoken = pdFALSE;
1290
1291     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
1292     size_t sentBytes = 0;
1293
1294     vTaskSetTimeOutState_Ignore();
1295     vTaskSuspendAll_Ignore();
1296     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1297
1298     /* Create a stream buffer of the default test sample size. */
1299     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1300     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1301
1302     /* Receive completed from ISR without a sender task waiting. */
1303     status = xStreamBufferReceiveCompletedFromISR( xStreamBuffer, &highPriorityTaskWoken );
1304     TEST_ASSERT_EQUAL( pdFALSE, status );
1305     TEST_ASSERT_EQUAL( pdFALSE, highPriorityTaskWoken );
1306
1307
1308     /* Send full capacity to a stream buffer so that sender task blocks. */
1309     sentBytes = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1310     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sentBytes );
1311     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
1312
1313     /* Receive completed from ISR when a sender task is waiting. */
1314     xTaskGenericNotifyStateClear_IgnoreAndReturn( pdTRUE );
1315     xTaskGetCurrentTaskHandle_IgnoreAndReturn( senderTask );
1316     xTaskGenericNotifyWait_StubWithCallback( receiveCompletedFromISRCallback );
1317     xTaskGenericNotifyFromISR_StubWithCallback( senderTaskNotificationFromISRCallback );
1318     xTaskCheckForTimeOut_IgnoreAndReturn( pdTRUE );
1319     sentBytes = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1320     TEST_ASSERT_EQUAL( 0, sentBytes );
1321     TEST_ASSERT_EQUAL( 1, senderTaskWoken );
1322
1323     vStreamBufferDelete( xStreamBuffer );
1324 }
1325
1326 /**
1327  * @brief Validates xStreamBufferReceiveCompletedFromISR fails assertion if a null stream buffer is passed.
1328  */
1329 void test_xStreamBufferReceiveCompletedFromISR_null_stream_buffer( void )
1330 {
1331     BaseType_t highPriorityTaskWoken = pdFALSE;
1332
1333     /* Send completed from ISR without receiver task waiting. */
1334     EXPECT_ASSERT_BREAK( ( void ) xStreamBufferReceiveCompletedFromISR( NULL, &highPriorityTaskWoken ) );
1335     validate_and_clear_assertions();
1336 }
1337
1338 /**
1339  * @brief Validates scenario where stream buffer head and tail pointer wraps over.
1340  */
1341 void test_xStreamBufferSend_WrapOver( void )
1342 {
1343     uint8_t data[ TEST_STREAM_BUFFER_SIZE ] = { 0xAA };
1344     size_t sent = 0, received = 0;
1345
1346     vTaskSetTimeOutState_Ignore();
1347     vTaskSuspendAll_Ignore();
1348     xTaskResumeAll_IgnoreAndReturn( pdTRUE );
1349
1350     /* Create a stream buffer of the default test sample size. */
1351     xStreamBuffer = xStreamBufferCreate( TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_TRIGGER_LEVEL );
1352     TEST_ASSERT_NOT_NULL( xStreamBuffer );
1353     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1354
1355     /* Sending bytes upto max stream buffer size. */
1356     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1357     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, sent );
1358     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
1359     TEST_ASSERT_EQUAL( 0, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1360
1361     /* Read upto max buffer size - 1 */
1362     received = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE - 1, TEST_STREAM_BUFFER_WAIT_TICKS );
1363     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE - 1, received );
1364     TEST_ASSERT_EQUAL( 1, xStreamBufferBytesAvailable( xStreamBuffer ) );
1365     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE - 1, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1366
1367     /* Send upto max buffer size - 1 so that head wraps over. */
1368     sent = xStreamBufferSend( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE - 1, TEST_STREAM_BUFFER_WAIT_TICKS );
1369     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE - 1, sent );
1370     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferBytesAvailable( xStreamBuffer ) );
1371     TEST_ASSERT_EQUAL( 0U, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1372
1373
1374     /* Read all bytes and verify that tail wraps over as well. */
1375     received = xStreamBufferReceive( xStreamBuffer, data, TEST_STREAM_BUFFER_SIZE, TEST_STREAM_BUFFER_WAIT_TICKS );
1376     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, received );
1377     TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xStreamBuffer ) );
1378     TEST_ASSERT_EQUAL( TEST_STREAM_BUFFER_SIZE, xStreamBufferSpacesAvailable( xStreamBuffer ) );
1379
1380     vStreamBufferDelete( xStreamBuffer );
1381 }