]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/generic/queue_create_static_utest.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Test / CMock / queue / generic / queue_create_static_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 queue_create_static_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
69 static void test_long_queue( QueueHandle_t xQueue,
70                              uint32_t maxItems )
71 {
72     /* Veify that queue is empty */
73     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
74
75     queue_common_add_sequential_to_queue( xQueue, maxItems );
76
77     /* Veify that queue is full */
78     TEST_ASSERT_EQUAL( 0, uxQueueSpacesAvailable( xQueue ) );
79
80     queue_common_receive_sequential_from_queue( xQueue, maxItems, maxItems, 0 );
81
82     /* Veify that queue is empty */
83     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
84 }
85
86
87 /* ==========================  Test Cases =========================== */
88
89 /**
90  * @brief xQueueCreateStatic with a NULL pointer for queueStorage
91  * @coverage xQueueGenericCreateStatic
92  */
93 void test_macro_xQueueCreateStatic_null_QueueStorage_fail( void )
94 {
95     /* Expect that xQueueCreate will assert */
96     fakeAssertExpectFail();
97
98     StaticQueue_t queueBuffer;
99     QueueHandle_t xQueue = xQueueCreateStatic( MAX_QUEUE_ITEMS, sizeof( uint32_t ), NULL, &queueBuffer );
100
101     /* Validate the queue handle */
102     TEST_ASSERT_EQUAL( NULL, xQueue );
103
104     TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
105 }
106
107 /**
108  * @brief xQueueCreateStatic with a NULL pointer for queueBuffer
109  * @coverage xQueueGenericCreateStatic
110  */
111 void test_macro_xQueueCreateStatic_null_queueBuffer_fail( void )
112 {
113     /* Expect that xQueueCreate will assert */
114     fakeAssertExpectFail();
115
116     uint32_t queueStorage[ MAX_QUEUE_ITEMS ];
117     QueueHandle_t xQueue = INVALID_PTR;
118
119     xQueue = xQueueCreateStatic( MAX_QUEUE_ITEMS, sizeof( uint32_t ), ( void * ) queueStorage, NULL );
120
121     /* Validate that the queue handle is NULL */
122     TEST_ASSERT_EQUAL( NULL, xQueue );
123
124     /* Check that configASSERT was called twice */
125     fakeAssertVerifyNumAssertsAndClear( 2 );
126 }
127
128 /**
129  * @brief Test xQueueCreateStatic with a NULL buffer, uxQueueLength=1, uxItemSize=0
130  * @details This configuration is equivalent to a binary semaphore.
131  * @coverage xQueueGenericCreateStatic
132  */
133 void test_macro_xQueueCreateStatic_nullQueueStorage_oneItem_zeroLength( void )
134 {
135     StaticQueue_t queueBuffer;
136     QueueHandle_t xQueue = xQueueCreateStatic( 1, 0, NULL, &queueBuffer );
137
138     /* validate returned queue handle */
139     TEST_ASSERT_NOT_EQUAL( NULL, xQueue );
140
141     /* Veify that new queue is empty */
142     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
143
144     /* Valdiate that the queue has 1 space remaining */
145     TEST_ASSERT_EQUAL( 1, uxQueueSpacesAvailable( xQueue ) );
146
147     /* Send a test value */
148     TEST_ASSERT_EQUAL( pdTRUE, xQueueSend( xQueue, NULL, 0 ) );
149
150     /* Test receive */
151     TEST_ASSERT_EQUAL( pdTRUE, xQueueReceive( xQueue, NULL, 0 ) );
152
153     vQueueDelete( xQueue );
154 }
155
156 /**
157  * @brief Test xQueueCreateStatic with uxQueueLength=0, uxItemSize=0
158  * @details This configuration is invalid and causes a configASSERT.
159  * @coverage xQueueGenericCreateStatic
160  */
161 void test_macro_xQueueCreateStatic_validQueueStorage_zeroItem_zeroLength( void )
162 {
163     StaticQueue_t queueBuffer;
164
165     /* Expect that xQueueCreateStatic will assert because a zero length queue is invalid */
166     fakeAssertExpectFail();
167     QueueHandle_t xQueue = xQueueCreateStatic( 0, 0, NULL, &queueBuffer );
168
169     /* verify that configASSERT was called */
170     TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
171
172     /* validate returned queue handle */
173     TEST_ASSERT_EQUAL( NULL, xQueue );
174 }
175
176 /**
177  * @brief Test xQueueCreateStatic with a valid buffer, uxQueueLength=1, uxItemSize=0
178  * @details This configuration is invalid and causes a configASSERT.
179  * @coverage xQueueGenericCreateStatic
180  */
181 void test_macro_xQueueCreateStatic_validQueueStorage_oneItem_zeroLength( void )
182 {
183     StaticQueue_t queueBuffer;
184     uint32_t queueData;
185
186     /* Expect that xQueueCreateStatic will assert because data storage is
187      *   prohibited for a zero itemLength queue */
188     fakeAssertExpectFail();
189     QueueHandle_t xQueue = xQueueCreateStatic( 1, 0, ( void * ) &queueData, &queueBuffer );
190
191     /* verify that configASSERT was called */
192     TEST_ASSERT_EQUAL( true, fakeAssertGetFlagAndClear() );
193
194     /* validate returned queue handle */
195     TEST_ASSERT_EQUAL( NULL, xQueue );
196 }
197
198 /**
199  * @brief xQueueCreateStatic with a large queue.
200  * @coverage xQueueGenericCreateStatic
201  */
202 void test_macro_xQueueCreateStatic_large( void )
203 {
204     uint32_t queueStorage[ MAX_QUEUE_ITEMS ];
205     StaticQueue_t queueBuffer;
206     QueueHandle_t xQueue = xQueueCreateStatic( MAX_QUEUE_ITEMS, sizeof( uint32_t ), ( void * ) queueStorage, &queueBuffer );
207
208     test_long_queue( xQueue, MAX_QUEUE_ITEMS );
209     vQueueDelete( xQueue );
210 }