]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/sets/semaphore_in_set_utest.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Test / CMock / queue / sets / semaphore_in_set_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 semaphore_in_set_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 "semphr.h"
39 #include "mock_fake_port.h"
40
41 /* ============================  GLOBAL VARIABLES =========================== */
42
43 /* ==========================  CALLBACK FUNCTIONS =========================== */
44
45 /* ============================= Unity Fixtures ============================= */
46
47 void setUp( void )
48 {
49     commonSetUp();
50 }
51
52 void tearDown( void )
53 {
54     commonTearDown();
55 }
56
57 void suiteSetUp()
58 {
59     commonSuiteSetUp();
60 }
61
62 int suiteTearDown( int numFailures )
63 {
64     return commonSuiteTearDown( numFailures );
65 }
66
67 /* ==========================  Helper functions =========================== */
68
69 /* ==========================  Test Cases =========================== */
70
71 /**
72  * @brief Test xSemaphoreGiveFromISR with a higher priority task waiting and a null pointer for pxHigherPriorityTaskWoken
73  * @details Test xSemaphoreGiveFromISR on a queue that is in a Queue Set with a higher priority task waiting.
74  *  Verify that a null pxHigherPriorityTaskWoken is handled correctly.
75  * @coverage xQueueGiveFromISR
76  */
77 void test_macro_xSemaphoreGiveFromISR_in_set_high_priority_pending_null_ptr( void )
78 {
79     SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
80
81     QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
82
83     xQueueAddToSet( xSemaphore, xQueueSet );
84
85     vFakePortAssertIfInterruptPriorityInvalid_Expect();
86
87     /* Insert an item into the event list */
88     td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
89     td_task_addFakeTaskWaitingToReceiveFromQueue( xQueueSet );
90
91     /* Give the semaphore */
92     TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
93
94     TEST_ASSERT_EQUAL( pdTRUE, td_task_getYieldPending() );
95
96     SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
97
98     TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
99
100     vSemaphoreDelete( xSemaphore );
101     vQueueDelete( xQueueSet );
102 }
103
104 /**
105  * @brief Test xSemaphoreGiveFromISR with a higher priority task waiting on a queue in and Queue Set
106  * @details Test xSemaphoreGiveFromISR with a higher priority task waiting and
107  *  verifies that xHigherPriorityTaskWoken is set accordingly.
108  * @coverage xQueueGiveFromISR
109  */
110 void test_macro_xSemaphoreGiveFromISR_in_set_high_priority_pending( void )
111 {
112     SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
113
114     QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
115
116     xQueueAddToSet( xSemaphore, xQueueSet );
117
118     vFakePortAssertIfInterruptPriorityInvalid_Expect();
119
120     /* Insert an item into the event list */
121     td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
122     td_task_addFakeTaskWaitingToReceiveFromQueue( xQueueSet );
123
124     BaseType_t xHigherPriorityTaskWoken = pdFALSE;
125
126     /* Give the semaphore */
127     TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
128
129     TEST_ASSERT_EQUAL( pdTRUE, xHigherPriorityTaskWoken );
130
131     TEST_ASSERT_EQUAL( pdTRUE, td_task_getYieldPending() );
132
133     SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
134
135     TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
136
137     vSemaphoreDelete( xSemaphore );
138     vQueueDelete( xQueueSet );
139 }
140
141 /**
142  * @brief Test xSemaphoreGiveFromISR with a lower priority task waiting on a queue in a Queue Set
143  * @details Test xSemaphoreGiveFromISR on a Queeu in a Queue Set with a lower priority task waiting and
144  *  verify that xHigherPriorityTaskWoken is not modified.
145  * @coverage xQueueGiveFromISR
146  */
147 void test_macro_xSemaphoreGiveFromISR_in_set_low_priority_pending( void )
148 {
149     SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
150
151     QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
152
153     xQueueAddToSet( xSemaphore, xQueueSet );
154
155     vFakePortAssertIfInterruptPriorityInvalid_Expect();
156
157     /* Insert an item into the event list */
158     td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
159     td_task_addFakeTaskWaitingToReceiveFromQueue( xQueueSet );
160
161     BaseType_t xHigherPriorityTaskWoken = pdFALSE;
162
163     /* Give the semaphore */
164     TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
165
166     TEST_ASSERT_EQUAL( pdFALSE, xHigherPriorityTaskWoken );
167
168     TEST_ASSERT_EQUAL( pdFALSE, td_task_getYieldPending() );
169
170     SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
171
172     TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
173
174     vSemaphoreDelete( xSemaphore );
175     vQueueDelete( xQueueSet );
176 }
177
178 /**
179  * @brief Test xSemaphoreGiveFromISR on a queue in a Queue Set with no tasks waiting
180  * @details Test xSemaphoreGiveFromISR on a Queue in a Queue Set no tasks waiting and verify that xHigherPriorityTaskWoken is not modified.
181  * @coverage xQueueGiveFromISR
182  */
183 void test_macro_xSemaphoreGiveFromISR_in_set_no_pending( void )
184 {
185     SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
186
187     QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
188
189     xQueueAddToSet( xSemaphore, xQueueSet );
190
191     vFakePortAssertIfInterruptPriorityInvalid_Expect();
192
193     BaseType_t xHigherPriorityTaskWoken = pdFALSE;
194
195     /* Give the semaphore */
196     TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
197
198     TEST_ASSERT_EQUAL( pdFALSE, xHigherPriorityTaskWoken );
199
200     SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
201
202     TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
203
204     vSemaphoreDelete( xSemaphore );
205     vQueueDelete( xQueueSet );
206 }