summaryrefslogtreecommitdiff
path: root/FreeRTOS/Test/CMock/queue/queue_utest_common.h
blob: 36b768532b988ec8e9f0018be42a385d2ff27c2e (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * FreeRTOS V202111.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 queue_utest_common.h */

#ifndef QUEUE_UTEST_COMMON_H
#define QUEUE_UTEST_COMMON_H

/* C runtime includes. */
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

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

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "queue.h"

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

/* ================================  CONSTANTS ===============================*/

#define MAX_MULTI_LEN             16
#define MAX_QUEUE_ITEMS           500
#define QUEUE_T_SIZE              sizeof( StaticQueue_t )

#define B_SEMPHR_AVAILABLE        1
#define B_SEMPHR_TAKEN            0

#define INVALID_UINT32            0xFFFFFFFF
#define INVALID_PTR               ( ( void * ) INVALID_UINT32 )

#define configASSERT_E            0xAA101

#define queueUNLOCKED             ( ( int8_t ) -1 )
#define queueLOCKED_UNMODIFIED    ( ( int8_t ) 0 )

#define DEFAULT_PRIORITY          5

#define TICKS_TO_WAIT             10
#define NUM_CALLS_TO_INTERCEPT    TICKS_TO_WAIT / 2

/* ===========================  FUNCTION PROTOTYPES  ======================== */
void setxMaskAssertAndAbort( bool mask );
bool getxMaskAssertAndAbort();
/* ============================  GLOBAL VARIABLES =========================== */

/* =================================  MACROS ================================ */

/**
 * @brief Expect a configASSERT from the function called.
 *  Break out of the called function when this occurs.
 * @details Use this macro when the call passsed in as a parameter is expected
 * to cause invalid memory access.
 */
#define EXPECT_ASSERT_BREAK( call )             \
    do                                          \
    {                                           \
        setxMaskAssertAndAbort( true );         \
        CEXCEPTION_T e = CEXCEPTION_NONE;       \
        Try                                     \
        {                                       \
            call;                               \
            TEST_FAIL();                        \
        }                                       \
        Catch( e )                              \
        TEST_ASSERT_EQUAL( configASSERT_E, e ); \
    } while( 0 )

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

/**
 * @brief defines malloc() for this test and redirects it to unity_malloc
 * @param xSize size of memory block to be allocated
 * @return pointer to the allocated memory on success.
 * @return NULL if the memory could not be allocated.
 */
void * pvPortMalloc( size_t xSize );

/**
 * @brief defines free() for this test and redirects it to unity_free
 * @param pv pointer to the block to be freed
 */
void vPortFree( void * pv );

/**
 * @brief Callback function for calls to configASSERT
 * @details This callback function will cause a unit test to fail if the
 *          provided assertion is false and the fakeAssertExpectFail()
 *          function was not called prior to the assertion.
 * @param assertion Boolean assertion passed into the configASSERT() macro
 * @param file Name of the file in which the assert occurred
 * @param line Line number of the assertion
 * @param num_calls Number of times configASSERT() was called
 */
void fakeAssertCallback( bool assertion,
                         char * file,
                         int line,
                         int num_calls );

/* ==========================  Unity fixtures =========================== */

/**
 * @brief Setup function called before each test case.
 */
void setUp( void );

/**
 * @brief Teardown function called after each test case.
 */
void tearDown( void );

/**
 * @brief Setup function called before this test suite (file).
 */
void suiteSetUp();

/**
 * @brief Setup function called afer this test suite (file) has completed.
 */
int suiteTearDown( int numFailures );

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

/**
 * @brief Common test case setup function for queue tests.
 */
void commonSetUp( void );

/**
 * @brief Common test case teardown function for queue tests.
 */
void commonTearDown( void );

/**
 * @brief Common test suite setup function for queue test suites.
 */
void commonSuiteSetUp();

/**
 * @brief Common test suite teardown function for queue test suites.
 */
int commonSuiteTearDown( int numFailures );

/**
 * @brief Get a monotonically increasing test value (somewhat random).
 */
uint32_t getNextMonotonicTestValue();

/**
 * @brief Get the test value provided by the last call to getNextMonotonicTestValue().
 */
uint32_t getLastMonotonicTestValue();

/**
 * @brief Mask subsquent failing assertions until next test case.
 */
void fakeAssertExpectFail( void );

/**
 * @brief Determine if a configASSERT occurred and clear the assertion flag.
 * @return true if an assert occurred since the start of the test suite or
 *  the last call to fakeAssertGetFlagAndClear.
 * @return false if no assert was triggered.
 */
bool fakeAssertGetFlagAndClear( void );

/**
 * @brief get the size of the last heap memory allocation via pvPortMalloc()
 * @return The parameter given during the last call to pvPortMalloc()
 */
size_t getLastMallocSize( void );

/**
 * @brief get the address of the last heap memory deallocation via pvPortFree()
 * @return The parameter given during the last call to pvPortFree()
 */
void * getLastFreedAddress( void );

/**
 * @brief get the number of malloc calls made in the current test case.
 * @return number of malloc calls made since the start of the test case.
 */
size_t getNumberMallocCalls( void );

/**
 * @return A pointer to the given queue's xTasksWaitingToSend event list.
 */
List_t * pxGetTasksWaitingToSendToQueue( QueueHandle_t xQueue );

/**
 * @return A pointer to the given queue's xTasksWaitingToReceive event list.
 */
List_t * pxGetTasksWaitingToReceiveFromQueue( QueueHandle_t xQueue );

/**
 * @return The value of a given queue's cRxLock.
 */
int8_t cGetQueueRxLock( QueueHandle_t xQueue );

/**
 * @return The value of a given queue's cTxLock.
 */
int8_t cGetQueueTxLock( QueueHandle_t xQueue );

/**
 * @brief Set the value of a given queue's cRxLock to the specified value.
 */
void vSetQueueRxLock( QueueHandle_t xQueue,
                      int8_t value );

/**
 * @brief Set the value of a given queue's cTxLock to the specified value.
 */
void vSetQueueTxLock( QueueHandle_t xQueue,
                      int8_t value );

/**
 * @brief Get the number of asserts that have occurred since the last call to this function in a given test case.
 */
BaseType_t fakeAssertGetNumAssertsAndClear( void );

/**
 * @brief Check that the number of failed configASSERTs that have occurred in this test case equals the given number.
 */
void fakeAssertVerifyNumAssertsAndClear( uint32_t ulNumAssertsExpected );

/**
 * @brief Receives a given number of items from the given queue and verifies that the items contain sequential values.
 */
void queue_common_receive_sequential_from_queue( QueueHandle_t xQueue,
                                                 uint32_t maxItems,
                                                 uint32_t numberOfItems,
                                                 uint32_t expectedFirstValue );

/**
 * @brief Adds a given number of itesm to the given queue with sequential values in each item.
 */
void queue_common_add_sequential_to_queue( QueueHandle_t xQueue,
                                           uint32_t numberOfItems );


/**
 * @brief Register the stubs contained in td_port.c using the relevant CMock _Stub calls.
 * @details This function should be called before every test case.
 */
void td_port_register_stubs( void );

/**
 * @brief Validate ending state of td_port related variables.
 * @details This function should be called after every test case.
 * It verifies the state of the variables used by td_port functions and
 * frees resources used by CMock.
 */
void td_port_teardown_check( void );

/**
 * @return pdTRUE if the simulated "port" a critical section/.
 */
BaseType_t td_port_isInCriticalSection( void );

/**
 * @brief Register the stubs contained in td_task.c using the relevant CMock _Stub calls.
 * @details This function should be called before every test case.
 */
void td_task_register_stubs( void );

/**
 * @brief Validate ending state of td_task related variables.
 * @details This function should be called after every test case.
 * It verifies the state of the variables used by td_task functions and
 * frees resources used by CMock.
 */
void td_task_teardown_check( void );

/**
 * @brief Sets the scheduler state used by the task test double.
 */
void td_task_setSchedulerState( BaseType_t state );

/**
 * @brief Sets the priority of the fake / simulated task used by td_task.c.
 */
void td_task_setFakeTaskPriority( TickType_t priority );

/**
 * @brief Sets the priority of the fake / simulated task used by td_task.c.
 */
TickType_t td_task_getFakeTaskPriority( void );

/**
 * @brief Adds the td_task.c fake task to the given queue's WaitingToSend event list.
 */
void td_task_addFakeTaskWaitingToSendToQueue( QueueHandle_t xQueue );

/**
 * @brief Adds the td_task.c fake task to the given queue's WaitingToReceive event list.
 */
void td_task_addFakeTaskWaitingToReceiveFromQueue( QueueHandle_t xQueue );

/**
 * @brief Test double for xTaskCheckForTimeOut
 */
BaseType_t td_task_xTaskCheckForTimeOutStub( TimeOut_t * const pxTimeOut,
                                             TickType_t * const pxTicksToWait,
                                             int cmock_num_calls );

/**
 * @brief Test double for xTaskResumeAll
 */
BaseType_t td_task_xTaskResumeAllStub( int cmock_num_calls );

/**
 * @brief Test double for vTaskSuspendAll which does not check the scheduler state when called.
 */
void td_task_vTaskSuspendAllStubNoCheck( int cmock_num_calls );

/**
 * @brief Test double for vPortYieldWithinAPI
 */
void td_task_vPortYieldWithinAPIStub( int cmock_num_calls );

/**
 * @brief Get the number of calls to all yield related functions.
 */
BaseType_t td_task_getYieldCount( void );

/**
 * @brief Get the number of times vPortYield was called and clear the counter.
 * @return number of times vPortYield was called during the current test case.
 */
BaseType_t td_task_getCount_vPortYield( void );

/**
 * @brief Get the number of times vPortYieldFromISR was called and clear the counter.
 * @return number of times vPortYieldFromISR was called during the current test case.
 */
BaseType_t td_task_getCount_vPortYieldFromISR( void );

/**
 * @brief Get the number of times vPortYieldWithinAPI was called and clear the counter.
 * @return number of times vPortYieldWithinAPI was called during the current test case.
 */
BaseType_t td_task_getCount_vPortYieldWithinAPI( void );

/**
 * @brief Get the number of times vTaskMissedYield was called and clear the counter.
 * @return number of times vTaskMissedYield was called during the current test case.
 */
BaseType_t td_task_getCount_vTaskMissedYield( void );

/**
 * @brief Get the number of times xTaskResumeAll and resulted in a yield.
 * @return number of times xTaskResumeAll was called and resulted in a yield
 * during the current test case. Also clears the counter before returning
 * the previous value.
 */
BaseType_t td_task_getCount_YieldFromTaskResumeAll( void );

/**
 * @brief Get the current state of the xYieldPending variable.
 * @return pdTRUE when a yield is pending due to a call to vTaskMissedYield
 * or xTaskRemoveFromEventList
 */
BaseType_t td_task_getYieldPending( void );


#endif /* QUEUE_UTEST_COMMON_H */