3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
26 /*! @file semaphore_in_set_utest.c */
28 /* C runtime includes. */
33 #include "../queue_utest_common.h"
37 #include "FreeRTOSConfig.h"
39 #include "mock_fake_port.h"
41 /* ============================ GLOBAL VARIABLES =========================== */
43 /* ========================== CALLBACK FUNCTIONS =========================== */
45 /* ============================= Unity Fixtures ============================= */
62 int suiteTearDown( int numFailures )
64 return commonSuiteTearDown( numFailures );
67 /* ========================== Helper functions =========================== */
69 /* ========================== Test Cases =========================== */
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
77 void test_macro_xSemaphoreGiveFromISR_in_set_high_priority_pending_null_ptr( void )
79 SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
81 QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
83 xQueueAddToSet( xSemaphore, xQueueSet );
85 vFakePortAssertIfInterruptPriorityInvalid_Expect();
87 /* Insert an item into the event list */
88 td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
89 td_task_addFakeTaskWaitingToReceiveFromQueue( xQueueSet );
91 /* Give the semaphore */
92 TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, NULL ) );
94 TEST_ASSERT_EQUAL( pdTRUE, td_task_getYieldPending() );
96 SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
98 TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
100 vSemaphoreDelete( xSemaphore );
101 vQueueDelete( xQueueSet );
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
110 void test_macro_xSemaphoreGiveFromISR_in_set_high_priority_pending( void )
112 SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
114 QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
116 xQueueAddToSet( xSemaphore, xQueueSet );
118 vFakePortAssertIfInterruptPriorityInvalid_Expect();
120 /* Insert an item into the event list */
121 td_task_setFakeTaskPriority( DEFAULT_PRIORITY + 1 );
122 td_task_addFakeTaskWaitingToReceiveFromQueue( xQueueSet );
124 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
126 /* Give the semaphore */
127 TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
129 TEST_ASSERT_EQUAL( pdTRUE, xHigherPriorityTaskWoken );
131 TEST_ASSERT_EQUAL( pdTRUE, td_task_getYieldPending() );
133 SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
135 TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
137 vSemaphoreDelete( xSemaphore );
138 vQueueDelete( xQueueSet );
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
147 void test_macro_xSemaphoreGiveFromISR_in_set_low_priority_pending( void )
149 SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
151 QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
153 xQueueAddToSet( xSemaphore, xQueueSet );
155 vFakePortAssertIfInterruptPriorityInvalid_Expect();
157 /* Insert an item into the event list */
158 td_task_setFakeTaskPriority( DEFAULT_PRIORITY - 1 );
159 td_task_addFakeTaskWaitingToReceiveFromQueue( xQueueSet );
161 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
163 /* Give the semaphore */
164 TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
166 TEST_ASSERT_EQUAL( pdFALSE, xHigherPriorityTaskWoken );
168 TEST_ASSERT_EQUAL( pdFALSE, td_task_getYieldPending() );
170 SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
172 TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
174 vSemaphoreDelete( xSemaphore );
175 vQueueDelete( xQueueSet );
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
183 void test_macro_xSemaphoreGiveFromISR_in_set_no_pending( void )
185 SemaphoreHandle_t xSemaphore = xSemaphoreCreateBinary();
187 QueueSetHandle_t xQueueSet = xQueueCreateSet( 1 );
189 xQueueAddToSet( xSemaphore, xQueueSet );
191 vFakePortAssertIfInterruptPriorityInvalid_Expect();
193 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
195 /* Give the semaphore */
196 TEST_ASSERT_EQUAL( pdTRUE, xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken ) );
198 TEST_ASSERT_EQUAL( pdFALSE, xHigherPriorityTaskWoken );
200 SemaphoreHandle_t xSemaphoreTemp = xQueueSelectFromSet( xQueueSet, 0 );
202 TEST_ASSERT_EQUAL( B_SEMPHR_AVAILABLE, uxSemaphoreGetCount( xSemaphoreTemp ) );
204 vSemaphoreDelete( xSemaphore );
205 vQueueDelete( xQueueSet );