summaryrefslogtreecommitdiff
path: root/FreeRTOS/Test/CMock/queue/td_task.c
blob: 7fd4f27c3cf8042aeed4223a723abfc9316ad2c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * FreeRTOS V202012.00
 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * https://www.FreeRTOS.org
 * https://github.com/FreeRTOS
 *
 */
/*! @file td_task.c */

#include "queue_utest_common.h"

/* Test includes. */
#include "unity.h"

/* Mock includes. */
#include "mock_task.h"
#include "mock_fake_port.h"


/* ============================  GLOBAL VARIABLES =========================== */
static BaseType_t xSchedulerState = taskSCHEDULER_RUNNING;
static ListItem_t taskListItem;
static ListItem_t fakeTaskListItem;
static TickType_t xTickCount = 0;
static BaseType_t xYieldPending = pdFALSE;
static BaseType_t xYieldCount = 0;
static BaseType_t xPortYieldCount = 0;
static BaseType_t xPortYieldFromISRCount = 0;
static BaseType_t xPortYieldWithinAPICount = 0;
static BaseType_t xTaskMissedYieldCount = 0;
static BaseType_t xYieldFromTaskResumeAllCount = 0;

/* ==========================  CALLBACK FUNCTIONS =========================== */

static BaseType_t xTaskGetSchedulerStateStub( int num_calls )
{
    return xSchedulerState;
}

static void vTaskSuspendAllStub( int cmock_num_calls )
{
    TEST_ASSERT_EQUAL_MESSAGE( taskSCHEDULER_RUNNING, xSchedulerState, "vTaskSuspendAll called with scheduler suspended." );
    xSchedulerState = taskSCHEDULER_SUSPENDED;
}

void td_task_vTaskSuspendAllStubNoCheck( int cmock_num_calls )
{
    xSchedulerState = taskSCHEDULER_SUSPENDED;
}

static void vTaskMissedYieldStub( int cmock_num_calls )
{
    TEST_ASSERT_TRUE_MESSAGE( ( td_task_getFakeTaskPriority() >= DEFAULT_PRIORITY ), "A Missed Yield should only occur when a higher priority task is pending." );
    xTaskMissedYieldCount++;
    xYieldPending = pdTRUE;
}

BaseType_t td_task_xTaskResumeAllStub( int cmock_num_calls )
{
    BaseType_t xDidYield = pdFALSE;

    TEST_ASSERT_EQUAL_MESSAGE( taskSCHEDULER_SUSPENDED, xSchedulerState, "xTaskResumeAll called with scheduler running." );

    xSchedulerState = taskSCHEDULER_RUNNING;

    if( ( td_task_getFakeTaskPriority() >= DEFAULT_PRIORITY ) &&
        ( listLIST_ITEM_CONTAINER( &fakeTaskListItem ) != NULL ) )
    {
        xYieldPending = pdTRUE;
    }

    if( xYieldPending )
    {
        #if ( configUSE_PREEMPTION == 1 )
            xDidYield = pdTRUE;
            xYieldCount++;
            xYieldFromTaskResumeAllCount++;
            xYieldPending = pdFALSE;
        #endif
    }

    /* Remove task from blocked list */
    if( listLIST_ITEM_CONTAINER( &taskListItem ) )
    {
        uxListRemove( &taskListItem );
    }

    return xDidYield;
}

static void vPortYieldStub( int cmock_num_calls )
{
    xYieldCount++;
    xPortYieldCount++;
    xYieldPending = pdFALSE;
}

static void vPortYieldFromISRStub( int cmock_num_calls )
{
    xYieldCount++;
    xPortYieldFromISRCount++;
    xYieldPending = pdFALSE;
}

void td_task_vPortYieldWithinAPIStub( int cmock_num_calls )
{
    xYieldCount++;
    xPortYieldWithinAPICount++;
    xYieldPending = pdFALSE;
}

/* Timeout handling callbacks */
static void vTaskInternalSetTimeOutStateStub( TimeOut_t * const pxTimeOut,
                                              int cmock_num_calls )
{
    pxTimeOut->xOverflowCount = 0;
    pxTimeOut->xTimeOnEntering = xTickCount;
}

BaseType_t td_task_xTaskCheckForTimeOutStub( TimeOut_t * const pxTimeOut,
                                             TickType_t * const pxTicksToWait,
                                             int cmock_num_calls )
{
    BaseType_t xReturnValue = pdFALSE;

    xTickCount++;

    if( ( xTickCount - pxTimeOut->xTimeOnEntering ) > *pxTicksToWait )
    {
        xReturnValue = pdTRUE;
    }

    return xReturnValue;
}

/* Sorted Event list related */
static BaseType_t xTaskRemoveFromEventListStub( const List_t * const pxEventList,
                                                int cmock_num_calls )
{
    BaseType_t xReturnValue = pdFALSE;

    /* check that xTaskRemoveFromEventList was called from within a critical section */
    TEST_ASSERT_TRUE_MESSAGE( td_port_isInCriticalSection(), "xTaskRemoveFromEventList was called outside of a critical section." );

    ListItem_t * pxItem = listGET_HEAD_ENTRY( pxEventList );

    TickType_t xItemPriority = ( configMAX_PRIORITIES - listGET_LIST_ITEM_VALUE( pxItem ) );

    ( void ) uxListRemove( pxItem );

    xReturnValue = ( xItemPriority > DEFAULT_PRIORITY );

    xYieldPending |= xReturnValue;

    return( xReturnValue );
}



static void vTaskPlaceOnEventListStub( List_t * const pxEventList,
                                       const TickType_t xTicksToWait,
                                       int cmock_num_calls )
{
    if( listLIST_ITEM_CONTAINER( &taskListItem ) )
    {
        uxListRemove( &taskListItem );
    }

    listSET_LIST_ITEM_VALUE( &taskListItem, ( configMAX_PRIORITIES - DEFAULT_PRIORITY ) );

    vListInsert( pxEventList, &taskListItem );
}

/* ============================= Unity Fixtures ============================= */


/* ==========================  Helper functions ============================= */


void td_task_register_stubs( void )
{
    /* Initialize local static variables */
    xSchedulerState = taskSCHEDULER_RUNNING;
    xTickCount = 0;
    vListInitialiseItem( &taskListItem );
    listSET_LIST_ITEM_VALUE( &taskListItem, configMAX_PRIORITIES - DEFAULT_PRIORITY );
    vListInitialiseItem( &fakeTaskListItem );
    listSET_LIST_ITEM_VALUE( &fakeTaskListItem, configMAX_PRIORITIES - DEFAULT_PRIORITY );
    xYieldPending = pdFALSE;
    xYieldCount = 0;
    xPortYieldCount = 0;
    xPortYieldFromISRCount = 0;
    xPortYieldWithinAPICount = 0;
    xTaskMissedYieldCount = 0;
    xYieldFromTaskResumeAllCount = 0;

    /* Setup stubs */
    vFakePortYield_Stub( &vPortYieldStub );
    vFakePortYieldFromISR_Stub( &vPortYieldFromISRStub );
    vFakePortYieldWithinAPI_Stub( &td_task_vPortYieldWithinAPIStub );

    xTaskGetSchedulerState_Stub( &xTaskGetSchedulerStateStub );
    vTaskSuspendAll_Stub( &vTaskSuspendAllStub );
    vTaskMissedYield_Stub( &vTaskMissedYieldStub );
    xTaskResumeAll_Stub( &td_task_xTaskResumeAllStub );

    vTaskInternalSetTimeOutState_Stub( &vTaskInternalSetTimeOutStateStub );
    xTaskCheckForTimeOut_Stub( &td_task_xTaskCheckForTimeOutStub );

    xTaskRemoveFromEventList_Stub( &xTaskRemoveFromEventListStub );
    vTaskPlaceOnEventList_Stub( &vTaskPlaceOnEventListStub );
}

void td_task_setSchedulerState( BaseType_t state )
{
    xSchedulerState = state;
}

void td_task_teardown_check( void )
{
    /* Assertions to run at the end of the test case */
    TEST_ASSERT_EQUAL_MESSAGE( taskSCHEDULER_RUNNING, xSchedulerState, "Test case ended with the scheduler suspended." );

    TEST_ASSERT_EQUAL_MESSAGE( 0, xYieldCount, "Test case ended with xYieldCount > 0" );
    TEST_ASSERT_EQUAL_MESSAGE( 0, xPortYieldCount, "Test case ended with xPortYieldCount > 0" );
    TEST_ASSERT_EQUAL_MESSAGE( 0, xPortYieldFromISRCount, "Test case ended with xPortYieldFromISRCount > 0" );
    TEST_ASSERT_EQUAL_MESSAGE( 0, xPortYieldWithinAPICount, "Test case ended with xPortYieldWithinAPICount > 0" );
    TEST_ASSERT_EQUAL_MESSAGE( 0, xYieldFromTaskResumeAllCount, "Test case ended with xYieldFromTaskResumeAllCount > 0" );
    TEST_ASSERT_EQUAL_MESSAGE( 0, xTaskMissedYieldCount, "Test case ended with xTaskMissedYieldCount > 0" );
    TEST_ASSERT_EQUAL_MESSAGE( pdFALSE, xYieldPending, "Test case ended with xYieldPending != pdFALSE" );
}

void td_task_setFakeTaskPriority( TickType_t priority )
{
    fakeTaskListItem.xItemValue = ( configMAX_PRIORITIES - priority );
    List_t * pxContainer = listLIST_ITEM_CONTAINER( &fakeTaskListItem );

    if( pxContainer != NULL )
    {
        uxListRemove( &fakeTaskListItem );
        vListInsert( pxContainer, &fakeTaskListItem );
    }
}

void td_task_addFakeTaskWaitingToSendToQueue( QueueHandle_t xQueue )
{
    StaticQueue_t * pxQueue = ( StaticQueue_t * ) xQueue;
    List_t * pxTasksWaitingToSend = ( List_t * ) &( pxQueue->xDummy3[ 0 ] );

    if( listLIST_ITEM_CONTAINER( &fakeTaskListItem ) )
    {
        uxListRemove( &fakeTaskListItem );
    }

    fakeTaskListItem.pvOwner = NULL;

    vListInsert( pxTasksWaitingToSend, &fakeTaskListItem );
}

void td_task_addFakeTaskWaitingToReceiveFromQueue( QueueHandle_t xQueue )
{
    StaticQueue_t * pxQueue = ( StaticQueue_t * ) xQueue;
    List_t * pxTasksWaitingToReceive = ( List_t * ) &( pxQueue->xDummy3[ 1 ] );

    if( listLIST_ITEM_CONTAINER( &fakeTaskListItem ) )
    {
        uxListRemove( &fakeTaskListItem );
    }

    fakeTaskListItem.pvOwner = NULL;

    vListInsert( pxTasksWaitingToReceive, &fakeTaskListItem );
}

TickType_t td_task_getFakeTaskPriority( void )
{
    return( configMAX_PRIORITIES - fakeTaskListItem.xItemValue );
}

BaseType_t td_task_getYieldCount( void )
{
    BaseType_t xReturnValue = xYieldCount;

    xYieldCount = 0;
    return xReturnValue;
}

BaseType_t td_task_getCount_vPortYield( void )
{
    BaseType_t xReturnValue = xPortYieldCount;

    xPortYieldCount = 0;
    return xReturnValue;
}

BaseType_t td_task_getCount_vPortYieldFromISR( void )
{
    BaseType_t xReturnValue = xPortYieldFromISRCount;

    xPortYieldFromISRCount = 0;
    return xReturnValue;
}

BaseType_t td_task_getCount_vPortYieldWithinAPI( void )
{
    BaseType_t xReturnValue = xPortYieldWithinAPICount;

    xPortYieldWithinAPICount = 0;
    return xReturnValue;
}

BaseType_t td_task_getCount_vTaskMissedYield( void )
{
    BaseType_t xReturnValue = xTaskMissedYieldCount;

    xTaskMissedYieldCount = 0;
    return xReturnValue;
}

BaseType_t td_task_getCount_YieldFromTaskResumeAll( void )
{
    BaseType_t xReturnValue = xYieldFromTaskResumeAllCount;

    xYieldFromTaskResumeAllCount = 0;
    return xReturnValue;
}

BaseType_t td_task_getYieldPending( void )
{
    BaseType_t xReturnValue = xYieldPending;

    xYieldPending = pdFALSE;
    return xReturnValue;
}