]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/generic/queue_status_utest.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Test / CMock / queue / generic / queue_status_utest.c
1 /*
2  * FreeRTOS V202107.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 queue_status_utest.c */
27
28 /* C runtime includes. */
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <string.h>
32
33 #include "../queue_utest_common.h"
34
35 /* Queue includes */
36 #include "FreeRTOS.h"
37 #include "FreeRTOSConfig.h"
38 #include "queue.h"
39
40 /* ============================  GLOBAL VARIABLES =========================== */
41
42 /* ==========================  CALLBACK FUNCTIONS =========================== */
43
44 /* ============================= Unity Fixtures ============================= */
45
46 void setUp( void )
47 {
48     commonSetUp();
49 }
50
51 void tearDown( void )
52 {
53     commonTearDown();
54 }
55
56 void suiteSetUp()
57 {
58     commonSuiteSetUp();
59 }
60
61 int suiteTearDown( int numFailures )
62 {
63     return commonSuiteTearDown( numFailures );
64 }
65
66 /* ==========================  Helper functions =========================== */
67
68 /* =============================  Test Cases ============================== */
69
70 /**
71  * @brief Test uxQueueMessagesWaiting with an invalid QueueHandle
72  * @coverage uxQueueMessagesWaiting
73  */
74 void test_uxQueueMessagesWaiting_invalid_handle( void )
75 {
76     EXPECT_ASSERT_BREAK( uxQueueMessagesWaiting( NULL ) );
77 }
78
79 /**
80  * @brief Test uxQueueMessagesWaiting with a queue of size 5 x 4 bytes
81  * @details Test uxQueueMessagesWaiting with a typical queue when empty and occupied.
82  * @coverage uxQueueMessagesWaiting
83  */
84 void test_uxQueueMessagesWaiting_empty_occupied( void )
85 {
86     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
87
88     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
89
90     for( uint32_t i = 0; i < 5; i++ )
91     {
92         xQueueSend( xQueue, &i, 0 );
93         TEST_ASSERT_EQUAL( i + 1, uxQueueMessagesWaiting( xQueue ) );
94     }
95
96     vQueueDelete( xQueue );
97 }
98
99 /**
100  * @brief Test uxQueueMessagesWaitingFromISR with an invalid Queue Handle
101  * @coverage uxQueueMessagesWaitingFromISR
102  */
103 void test_uxQueueMessagesWaitingFromISR_invalid_handle( void )
104 {
105     EXPECT_ASSERT_BREAK( uxQueueMessagesWaitingFromISR( NULL ) );
106 }
107
108 /**
109  * @brief Test uxQueueMessagesWaitingFromISR with a queue of size 5 x 4 bytes
110  * @details Test uxQueueMessagesWaitingFromISR with a typical queue when empty and occupied.
111  * @coverage uxQueueMessagesWaitingFromISR
112  */
113 void test_uxQueueMessagesWaitingFromISR_empty_occupied( void )
114 {
115     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
116
117     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaitingFromISR( xQueue ) );
118
119     for( uint32_t i = 0; i < 5; i++ )
120     {
121         xQueueSend( xQueue, &i, 0 );
122         TEST_ASSERT_EQUAL( i + 1, uxQueueMessagesWaitingFromISR( xQueue ) );
123     }
124
125     vQueueDelete( xQueue );
126 }
127
128 /**
129  * @brief Test uxQueueSpacesAvailable with an invalid QueueHandle
130  * @coverage uxQueueSpacesAvailable
131  */
132 void test_uxQueueSpacesAvailable_invalid_handle( void )
133 {
134     EXPECT_ASSERT_BREAK( uxQueueSpacesAvailable( NULL ) );
135 }
136
137 /**
138  * @brief Test uxQueueSpacesAvailable with a queue of size 5 x 4 bytes
139  * @details Test uxQueueSpacesAvailable with a typical queue when empty and occupied.
140  * @coverage uxQueueSpacesAvailable
141  */
142 void test_uxQueueSpacesAvailable_empty_occupied( void )
143 {
144     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
145
146     TEST_ASSERT_EQUAL( 5, uxQueueSpacesAvailable( xQueue ) );
147
148     for( uint32_t i = 0; i < 5; i++ )
149     {
150         xQueueSend( xQueue, &i, 0 );
151         TEST_ASSERT_EQUAL( 4 - i, uxQueueSpacesAvailable( xQueue ) );
152     }
153
154     vQueueDelete( xQueue );
155 }
156
157
158 /**
159  * @brief Test xQueueIsQueueEmptyFromISR with an invalid QueueHandle
160  * @coverage xQueueIsQueueEmptyFromISR
161  */
162 void test_xQueueIsQueueEmptyFromISR_invalid_handle( void )
163 {
164     EXPECT_ASSERT_BREAK( xQueueIsQueueEmptyFromISR( NULL ) );
165 }
166
167 /**
168  * @brief Test xQueueIsQueueEmptyFromISR with a queue of size 5 x 4 bytes
169  * @details Test xQueueIsQueueEmptyFromISR with a typical queue when empty and occupied.
170  * @coverage xQueueIsQueueEmptyFromISR
171  */
172 void test_xQueueIsQueueEmptyFromISR_empty_occupied( void )
173 {
174     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
175
176     TEST_ASSERT_EQUAL( pdTRUE, xQueueIsQueueEmptyFromISR( xQueue ) );
177
178     for( uint32_t i = 0; i < 5; i++ )
179     {
180         xQueueSend( xQueue, &i, 0 );
181         TEST_ASSERT_EQUAL( pdFALSE, xQueueIsQueueEmptyFromISR( xQueue ) );
182     }
183
184     vQueueDelete( xQueue );
185 }
186
187 /**
188  * @brief Test xQueueIsQueueFullFromISR with an invalid QueueHandle
189  * @coverage xQueueIsQueueFullFromISR
190  */
191 void test_xQueueIsQueueFullFromISR_invalid_handle( void )
192 {
193     EXPECT_ASSERT_BREAK( xQueueIsQueueFullFromISR( NULL ) );
194 }
195
196 /**
197  * @brief Test xQueueIsQueueFullFromISR with a queue of size 5 x 4 bytes
198  * @details Test xQueueIsQueueFullFromISR with a typical queue when empty and occupied.
199  * @coverage xQueueIsQueueFullFromISR
200  */
201 void test_xQueueIsQueueFullFromISR_empty_occupied( void )
202 {
203     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
204
205     for( uint32_t i = 0; i < 5; i++ )
206     {
207         TEST_ASSERT_EQUAL( pdFALSE, xQueueIsQueueFullFromISR( xQueue ) );
208         xQueueSend( xQueue, &i, 0 );
209     }
210
211     TEST_ASSERT_EQUAL( pdTRUE, xQueueIsQueueFullFromISR( xQueue ) );
212
213     vQueueDelete( xQueue );
214 }
215
216 /**
217  * @brief Test vQueueWaitForMessageRestricted with an empty queue.
218  * @details Test vQueueWaitForMessageRestricted with various values for
219  *  xTicksToWait and xWaitIndefinitely.
220  * @coverage vQueueWaitForMessageRestricted
221  */
222 void test_vQueueWaitForMessageRestricted_empty( void )
223 {
224     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
225
226     struct
227     {
228         TickType_t xTicksToWait;
229         BaseType_t xWaitIndefinitely;
230     }
231     xTestCaseArray[] =
232     {
233         { 0,     pdFALSE },
234         { 0,     pdTRUE  },
235         { 1,     pdFALSE },
236         { 1,     pdTRUE  },
237         { 10,    pdFALSE },
238         { 10,    pdTRUE  },
239         { 65536, pdTRUE  }
240     };
241
242     List_t * pxTasksWaitingToReceive = pxGetTasksWaitingToReceiveFromQueue( xQueue );
243
244     for( uint32_t i = 0; i < ( sizeof( xTestCaseArray ) / sizeof( xTestCaseArray[ 0 ] ) ); i++ )
245     {
246         vTaskPlaceOnEventListRestricted_Expect( pxTasksWaitingToReceive,
247                                                 xTestCaseArray[ i ].xTicksToWait,
248                                                 xTestCaseArray[ i ].xWaitIndefinitely );
249         /* call api function */
250         vQueueWaitForMessageRestricted( xQueue,
251                                         xTestCaseArray[ i ].xTicksToWait,
252                                         xTestCaseArray[ i ].xWaitIndefinitely );
253     }
254
255     vQueueDelete( xQueue );
256 }
257
258 /**
259  * @brief Test vQueueWaitForMessageRestricted with an occupied queue.
260  * @details Test vQueueWaitForMessageRestricted with various values for
261  *  xTicksToWait and xWaitIndefinitely.
262  * @coverage vQueueWaitForMessageRestricted
263  */
264 void test_vQueueWaitForMessageRestricted_occupied( void )
265 {
266     QueueHandle_t xQueue = xQueueCreate( 5, sizeof( uint32_t ) );
267
268     uint32_t testVal = getNextMonotonicTestValue();
269
270     xQueueSend( xQueue, &testVal, 0 );
271
272     const struct
273     {
274         TickType_t xTicksToWait;
275         BaseType_t xWaitIndefinitely;
276     }
277     xTestCaseArray[] =
278     {
279         { 0,     pdFALSE },
280         { 0,     pdTRUE  },
281         { 1,     pdFALSE },
282         { 1,     pdTRUE  },
283         { 10,    pdFALSE },
284         { 10,    pdTRUE  },
285         { 65536, pdTRUE  }
286     };
287
288     for( uint32_t i = 0; i < ( sizeof( xTestCaseArray ) / sizeof( xTestCaseArray[ 0 ] ) ); i++ )
289     {
290         /* call api function */
291         vQueueWaitForMessageRestricted( xQueue,
292                                         xTestCaseArray[ i ].xTicksToWait,
293                                         xTestCaseArray[ i ].xWaitIndefinitely );
294         /* Do not expect a call to vTaskPlaceOnEventListRestricted */
295     }
296
297     vQueueDelete( xQueue );
298 }
299
300 /**
301  * @brief Test vQueueWaitForMessageRestricted on an empty locked queue.
302  * @details This test case verifies a situation that should never occur ( vQueueWaitForMessageRestricted called on a locked queue ).
303  * @coverage vQueueWaitForMessageRestricted
304  */
305 void test_vQueueWaitForMessageRestricted_locked( void )
306 {
307     /* Create a new binary Queue */
308     QueueHandle_t xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
309
310     /* Set private lock counters */
311     vSetQueueRxLock( xQueue, queueLOCKED_UNMODIFIED );
312     vSetQueueTxLock( xQueue, queueLOCKED_UNMODIFIED );
313
314     List_t * pxTasksWaitingToReceive = pxGetTasksWaitingToReceiveFromQueue( xQueue );
315
316     vTaskPlaceOnEventListRestricted_Expect( pxTasksWaitingToReceive,
317                                             10,
318                                             pdFALSE );
319
320     /* Run vQueueWaitForMessageRestricted with the queue locked */
321     vQueueWaitForMessageRestricted( xQueue, 10, pdFALSE );
322
323     /* Verify that the queue is now unlocked */
324     TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueRxLock( xQueue ) );
325     TEST_ASSERT_EQUAL( queueUNLOCKED, cGetQueueTxLock( xQueue ) );
326
327     vQueueDelete( xQueue );
328 }