]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/generic/queue_reset_utest.c
Updated pack to FreeRTOS 10.4.6
[cmsis-freertos] / Test / CMock / queue / generic / queue_reset_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_reset_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 #include "mock_fake_port.h"
41
42 /* ============================  GLOBAL VARIABLES =========================== */
43
44 /* ==========================  CALLBACK FUNCTIONS =========================== */
45
46 /* ============================= Unity Fixtures ============================= */
47
48 void setUp( void )
49 {
50     commonSetUp();
51 }
52
53 void tearDown( void )
54 {
55     commonTearDown();
56 }
57
58 void suiteSetUp()
59 {
60     commonSuiteSetUp();
61 }
62
63 int suiteTearDown( int numFailures )
64 {
65     return commonSuiteTearDown( numFailures );
66 }
67
68 /* ==========================  Helper functions =========================== */
69
70 /* ==========================  Test Cases =========================== */
71
72 /**
73  * @brief Test xQueueReset with an invalid queue handle
74  * @coverage xQueueGenericReset
75  */
76 void test_macro_xQueueReset_invalid_handle( void )
77 {
78     EXPECT_ASSERT_BREAK( xQueueReset( NULL ) );
79 }
80
81 /**
82  * @brief Test xQueueReset with an empty queue of size 6 x 4 bytes
83  * @coverage xQueueGenericReset
84  */
85 void test_macro_xQueueReset_empty( void )
86 {
87     QueueHandle_t xQueue = xQueueCreate( 6, sizeof( uint32_t ) );
88
89     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
90
91     TEST_ASSERT_EQUAL( pdPASS, xQueueReset( xQueue ) );
92
93     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
94
95     vQueueDelete( xQueue );
96 }
97
98 /**
99  * @brief Test xQueueReset with a half-full queue of size 6 x 4 bytes
100  * @coverage xQueueGenericReset
101  */
102 void test_macro_xQueueReset_half_full( void )
103 {
104     QueueHandle_t xQueue = xQueueCreate( 6, sizeof( uint32_t ) );
105
106     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
107
108     for( uint32_t i = 0; i < 3; i++ )
109     {
110         xQueueSend( xQueue, &i, 0 );
111     }
112
113     TEST_ASSERT_EQUAL( 3, uxQueueMessagesWaiting( xQueue ) );
114
115     TEST_ASSERT_EQUAL( pdPASS, xQueueReset( xQueue ) );
116
117     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
118
119     vQueueDelete( xQueue );
120 }
121
122 /**
123  * @brief Test xQueueReset with a full queue of size 6 x 4 bytes
124  * @coverage xQueueGenericReset
125  */
126 void test_macro_xQueueReset_full( void )
127 {
128     QueueHandle_t xQueue = xQueueCreate( 6, sizeof( uint32_t ) );
129
130     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
131
132     for( uint32_t i = 0; i < 6; i++ )
133     {
134         xQueueSend( xQueue, &i, 0 );
135     }
136
137     TEST_ASSERT_EQUAL( 6, uxQueueMessagesWaiting( xQueue ) );
138
139     TEST_ASSERT_EQUAL( pdPASS, xQueueReset( xQueue ) );
140
141     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
142
143     vQueueDelete( xQueue );
144 }
145
146 /**
147  * @brief Test xQueueReset with a queue of size 6 x 4 bytes
148  * and a simulated higher priority task that is blocked waiting send to the queue.
149  * @coverage xQueueGenericReset
150  */
151 void test_macro_xQueueReset_tasks_waiting_higher_priority( void )
152 {
153     QueueHandle_t xQueue = xQueueCreate( 6, sizeof( uint32_t ) );
154
155     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
156
157     /* Fill the queue */
158     for( uint32_t i = 0; i < 6; i++ )
159     {
160         xQueueSend( xQueue, &i, 0 );
161     }
162
163     /* Insert an item into the event list */
164     td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
165     td_task_addFakeTaskWaitingToSendToQueue( xQueue );
166
167     TEST_ASSERT_EQUAL( pdTRUE, xQueueReset( xQueue ) );
168
169     TEST_ASSERT_EQUAL( 1, td_task_getYieldCount() );
170
171     TEST_ASSERT_EQUAL( 1, td_task_getCount_vPortYieldWithinAPI() );
172
173     vQueueDelete( xQueue );
174 }
175
176 /**
177  * @brief Test xQueueReset with a queue of size 6 x 4 bytes
178  * and a simulated lower priority task that is blocked waiting to send to the queue.
179  * xTasksWaitingToSend
180  * @coverage xQueueGenericReset
181  */
182 void test_macro_xQueueReset_tasks_waiting_lower_priority( void )
183 {
184     QueueHandle_t xQueue = xQueueCreate( 6, sizeof( uint32_t ) );
185
186     TEST_ASSERT_EQUAL( 0, uxQueueMessagesWaiting( xQueue ) );
187
188     /* Fill the queue */
189     for( uint32_t i = 0; i < 6; i++ )
190     {
191         xQueueSend( xQueue, &i, 0 );
192     }
193
194     /* Insert an item into the event list */
195     td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
196     td_task_addFakeTaskWaitingToSendToQueue( xQueue );
197
198     TEST_ASSERT_EQUAL( pdTRUE, xQueueReset( xQueue ) );
199
200     vQueueDelete( xQueue );
201 }