Skip to content

Commit 3f8748d

Browse files
committed
test: add unit tests for stream buffer extension API
Signed-off-by: Srikanth Patchava <spatchava@meta.com>
1 parent 83ca23b commit 3f8748d

1 file changed

Lines changed: 322 additions & 0 deletions

File tree

test/stream_buffer_ext_utest.c

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,322 @@
1+
/*
2+
* FreeRTOS Kernel V11.1.0
3+
* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*
25+
* https://www.FreeRTOS.org
26+
* https://github.com/FreeRTOS
27+
*/
28+
29+
/**
30+
* @file stream_buffer_ext_utest.c
31+
* @brief Unit tests for the stream buffer extension API (peek, snapshot).
32+
*/
33+
34+
#include "unity.h"
35+
#include "stream_buffer_ext.h"
36+
#include "FreeRTOS.h"
37+
#include "stream_buffer.h"
38+
39+
#include <string.h>
40+
41+
/* ========================== DEFINES ============================== */
42+
43+
#define TEST_BUFFER_SIZE ( ( size_t ) 64 )
44+
#define TEST_TRIGGER_LEVEL ( ( size_t ) 1 )
45+
#define TEST_DATA_SIZE ( ( size_t ) 16 )
46+
47+
/* ========================== GLOBALS ============================== */
48+
49+
static StreamBufferHandle_t xTestBuffer = NULL;
50+
static uint8_t ucTestData[ TEST_BUFFER_SIZE ];
51+
static uint8_t ucReceiveBuf[ TEST_BUFFER_SIZE ];
52+
53+
/* ======================== Unity Fixtures ========================= */
54+
55+
void setUp( void )
56+
{
57+
size_t i;
58+
59+
xTestBuffer = xStreamBufferCreate( TEST_BUFFER_SIZE, TEST_TRIGGER_LEVEL );
60+
TEST_ASSERT_NOT_NULL( xTestBuffer );
61+
62+
for( i = 0; i < TEST_BUFFER_SIZE; i++ )
63+
{
64+
ucTestData[ i ] = ( uint8_t ) ( i & 0xFF );
65+
}
66+
67+
memset( ucReceiveBuf, 0, sizeof( ucReceiveBuf ) );
68+
}
69+
70+
void tearDown( void )
71+
{
72+
if( xTestBuffer != NULL )
73+
{
74+
vStreamBufferDelete( xTestBuffer );
75+
xTestBuffer = NULL;
76+
}
77+
}
78+
79+
/* ========================== TESTS ================================ */
80+
81+
/**
82+
* @brief Peek returns data without consuming it.
83+
*/
84+
void test_xStreamBufferPeek_DataNotConsumed( void )
85+
{
86+
size_t xSent;
87+
size_t xPeeked;
88+
size_t xAvailable;
89+
90+
xSent = xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
91+
TEST_ASSERT_EQUAL( TEST_DATA_SIZE, xSent );
92+
93+
xPeeked = xStreamBufferPeek( xTestBuffer, ucReceiveBuf, TEST_DATA_SIZE );
94+
TEST_ASSERT_EQUAL( TEST_DATA_SIZE, xPeeked );
95+
TEST_ASSERT_EQUAL_UINT8_ARRAY( ucTestData, ucReceiveBuf, TEST_DATA_SIZE );
96+
97+
/* Verify data is still in the buffer. */
98+
xAvailable = xStreamBufferBytesAvailable( xTestBuffer );
99+
TEST_ASSERT_EQUAL( TEST_DATA_SIZE, xAvailable );
100+
}
101+
102+
/**
103+
* @brief Peek on empty buffer returns 0.
104+
*/
105+
void test_xStreamBufferPeek_EmptyBuffer( void )
106+
{
107+
size_t xPeeked;
108+
109+
xPeeked = xStreamBufferPeek( xTestBuffer, ucReceiveBuf, TEST_DATA_SIZE );
110+
TEST_ASSERT_EQUAL( 0, xPeeked );
111+
}
112+
113+
/**
114+
* @brief Peek with request larger than available returns only available.
115+
*/
116+
void test_xStreamBufferPeek_RequestMoreThanAvailable( void )
117+
{
118+
size_t xSent;
119+
size_t xPeeked;
120+
121+
xSent = xStreamBufferSend( xTestBuffer, ucTestData, 4, 0 );
122+
TEST_ASSERT_EQUAL( 4, xSent );
123+
124+
xPeeked = xStreamBufferPeek( xTestBuffer, ucReceiveBuf, TEST_BUFFER_SIZE );
125+
TEST_ASSERT_EQUAL( 4, xPeeked );
126+
TEST_ASSERT_EQUAL_UINT8_ARRAY( ucTestData, ucReceiveBuf, 4 );
127+
}
128+
129+
/**
130+
* @brief Peek with zero length returns 0.
131+
*/
132+
void test_xStreamBufferPeek_ZeroLength( void )
133+
{
134+
size_t xPeeked;
135+
136+
xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
137+
xPeeked = xStreamBufferPeek( xTestBuffer, ucReceiveBuf, 0 );
138+
TEST_ASSERT_EQUAL( 0, xPeeked );
139+
}
140+
141+
/**
142+
* @brief Multiple peeks return the same data.
143+
*/
144+
void test_xStreamBufferPeek_MultiplePeeksConsistent( void )
145+
{
146+
uint8_t ucBuf1[ TEST_DATA_SIZE ];
147+
uint8_t ucBuf2[ TEST_DATA_SIZE ];
148+
size_t xPeeked1, xPeeked2;
149+
150+
xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
151+
152+
xPeeked1 = xStreamBufferPeek( xTestBuffer, ucBuf1, TEST_DATA_SIZE );
153+
xPeeked2 = xStreamBufferPeek( xTestBuffer, ucBuf2, TEST_DATA_SIZE );
154+
155+
TEST_ASSERT_EQUAL( xPeeked1, xPeeked2 );
156+
TEST_ASSERT_EQUAL_UINT8_ARRAY( ucBuf1, ucBuf2, xPeeked1 );
157+
}
158+
159+
/**
160+
* @brief PeekAt with offset skips initial bytes.
161+
*/
162+
void test_xStreamBufferPeekAt_WithOffset( void )
163+
{
164+
size_t xSent;
165+
size_t xPeeked;
166+
167+
xSent = xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
168+
TEST_ASSERT_EQUAL( TEST_DATA_SIZE, xSent );
169+
170+
/* Peek starting at offset 4 — should get bytes 4..15. */
171+
xPeeked = xStreamBufferPeekAt( xTestBuffer, 4, ucReceiveBuf, TEST_DATA_SIZE );
172+
TEST_ASSERT_EQUAL( TEST_DATA_SIZE - 4, xPeeked );
173+
TEST_ASSERT_EQUAL_UINT8_ARRAY( &ucTestData[ 4 ], ucReceiveBuf, xPeeked );
174+
}
175+
176+
/**
177+
* @brief PeekAt with offset >= available returns 0.
178+
*/
179+
void test_xStreamBufferPeekAt_OffsetBeyondAvailable( void )
180+
{
181+
size_t xPeeked;
182+
183+
xStreamBufferSend( xTestBuffer, ucTestData, 4, 0 );
184+
185+
xPeeked = xStreamBufferPeekAt( xTestBuffer, 10, ucReceiveBuf, TEST_DATA_SIZE );
186+
TEST_ASSERT_EQUAL( 0, xPeeked );
187+
}
188+
189+
/**
190+
* @brief PeekAt with zero offset behaves like Peek.
191+
*/
192+
void test_xStreamBufferPeekAt_ZeroOffsetLikePeek( void )
193+
{
194+
uint8_t ucBufPeek[ TEST_DATA_SIZE ];
195+
uint8_t ucBufPeekAt[ TEST_DATA_SIZE ];
196+
size_t xPeekResult, xPeekAtResult;
197+
198+
xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
199+
200+
xPeekResult = xStreamBufferPeek( xTestBuffer, ucBufPeek, TEST_DATA_SIZE );
201+
xPeekAtResult = xStreamBufferPeekAt( xTestBuffer, 0, ucBufPeekAt, TEST_DATA_SIZE );
202+
203+
TEST_ASSERT_EQUAL( xPeekResult, xPeekAtResult );
204+
TEST_ASSERT_EQUAL_UINT8_ARRAY( ucBufPeek, ucBufPeekAt, xPeekResult );
205+
}
206+
207+
/**
208+
* @brief Snapshot captures correct state for empty buffer.
209+
*/
210+
void test_xStreamBufferSnapshot_EmptyBuffer( void )
211+
{
212+
StreamBufferSnapshot_t xSnap;
213+
BaseType_t xResult;
214+
215+
xResult = xStreamBufferSnapshot( xTestBuffer, &xSnap );
216+
TEST_ASSERT_EQUAL( pdTRUE, xResult );
217+
TEST_ASSERT_EQUAL( 0, xSnap.xReadableBytes );
218+
TEST_ASSERT_EQUAL( 1, xSnap.ucIsEmpty );
219+
TEST_ASSERT_EQUAL( 0, xSnap.ucIsFull );
220+
TEST_ASSERT_TRUE( xSnap.xWritableBytes > 0 );
221+
}
222+
223+
/**
224+
* @brief Snapshot captures correct state for partially filled buffer.
225+
*/
226+
void test_xStreamBufferSnapshot_PartiallyFilled( void )
227+
{
228+
StreamBufferSnapshot_t xSnap;
229+
230+
xStreamBufferSend( xTestBuffer, ucTestData, 10, 0 );
231+
232+
xStreamBufferSnapshot( xTestBuffer, &xSnap );
233+
TEST_ASSERT_EQUAL( 10, xSnap.xReadableBytes );
234+
TEST_ASSERT_EQUAL( 0, xSnap.ucIsEmpty );
235+
TEST_ASSERT_EQUAL( 0, xSnap.ucIsFull );
236+
}
237+
238+
/**
239+
* @brief Snapshot captures full buffer correctly.
240+
*/
241+
void test_xStreamBufferSnapshot_FullBuffer( void )
242+
{
243+
StreamBufferSnapshot_t xSnap;
244+
size_t xCapacity;
245+
246+
/* Fill the buffer to capacity. */
247+
xCapacity = xStreamBufferSpacesAvailable( xTestBuffer );
248+
xStreamBufferSend( xTestBuffer, ucTestData, xCapacity, 0 );
249+
250+
xStreamBufferSnapshot( xTestBuffer, &xSnap );
251+
TEST_ASSERT_EQUAL( 1, xSnap.ucIsFull );
252+
TEST_ASSERT_EQUAL( 0, xSnap.ucIsEmpty );
253+
TEST_ASSERT_EQUAL( 0, xSnap.xWritableBytes );
254+
}
255+
256+
/**
257+
* @brief Snapshot with NULL parameters returns pdFALSE.
258+
*/
259+
void test_xStreamBufferSnapshot_NullParams( void )
260+
{
261+
StreamBufferSnapshot_t xSnap;
262+
263+
TEST_ASSERT_EQUAL( pdFALSE, xStreamBufferSnapshot( NULL, &xSnap ) );
264+
TEST_ASSERT_EQUAL( pdFALSE, xStreamBufferSnapshot( xTestBuffer, NULL ) );
265+
}
266+
267+
/**
268+
* @brief GetReadableLength returns correct count.
269+
*/
270+
void test_xStreamBufferGetReadableLength( void )
271+
{
272+
size_t xLen;
273+
274+
xLen = xStreamBufferGetReadableLength( xTestBuffer );
275+
TEST_ASSERT_EQUAL( 0, xLen );
276+
277+
xStreamBufferSend( xTestBuffer, ucTestData, 8, 0 );
278+
xLen = xStreamBufferGetReadableLength( xTestBuffer );
279+
TEST_ASSERT_EQUAL( 8, xLen );
280+
}
281+
282+
/**
283+
* @brief GetReadableLength with NULL returns 0.
284+
*/
285+
void test_xStreamBufferGetReadableLength_Null( void )
286+
{
287+
TEST_ASSERT_EQUAL( 0, xStreamBufferGetReadableLength( NULL ) );
288+
}
289+
290+
/**
291+
* @brief Peek followed by receive gets the same data.
292+
*/
293+
void test_xStreamBufferPeek_ThenReceive_SameData( void )
294+
{
295+
uint8_t ucPeekBuf[ TEST_DATA_SIZE ];
296+
uint8_t ucRecvBuf[ TEST_DATA_SIZE ];
297+
size_t xPeeked, xReceived;
298+
299+
xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
300+
301+
xPeeked = xStreamBufferPeek( xTestBuffer, ucPeekBuf, TEST_DATA_SIZE );
302+
xReceived = xStreamBufferReceive( xTestBuffer, ucRecvBuf, TEST_DATA_SIZE, 0 );
303+
304+
TEST_ASSERT_EQUAL( xPeeked, xReceived );
305+
TEST_ASSERT_EQUAL_UINT8_ARRAY( ucPeekBuf, ucRecvBuf, xPeeked );
306+
307+
/* Buffer should now be empty. */
308+
TEST_ASSERT_EQUAL( 0, xStreamBufferBytesAvailable( xTestBuffer ) );
309+
}
310+
311+
/**
312+
* @brief Peek single byte from the buffer.
313+
*/
314+
void test_xStreamBufferPeek_SingleByte( void )
315+
{
316+
uint8_t ucByte = 0;
317+
318+
xStreamBufferSend( xTestBuffer, ucTestData, TEST_DATA_SIZE, 0 );
319+
320+
TEST_ASSERT_EQUAL( 1, xStreamBufferPeek( xTestBuffer, &ucByte, 1 ) );
321+
TEST_ASSERT_EQUAL( ucTestData[ 0 ], ucByte );
322+
}

0 commit comments

Comments
 (0)