]> begriffs open source - cmsis-freertos/blob - Test/CMock/queue/td_port.c
Updated pack to FreeRTOS 10.4.4
[cmsis-freertos] / Test / CMock / queue / td_port.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 td_port.c */
27
28 #include "queue_utest_common.h"
29
30 /* C runtime includes. */
31 #include <stdlib.h>
32 #include <stdbool.h>
33 #include <string.h>
34
35 /* Test includes. */
36 #include "unity.h"
37 #include "unity_memory.h"
38
39 /* Mock includes. */
40 #include "mock_task.h"
41 #include "mock_fake_port.h"
42
43 /* ============================  GLOBAL VARIABLES =========================== */
44
45 bool xInCriticalSection = false;
46 uint32_t ulNumEnterCriticalSection = 0;
47 uint32_t ulNumExitCriticalSection = 0;
48
49 UBaseType_t uxSavedInterruptStatusGlobal = 0;
50 uint32_t ulNumCallsSetInterruptMaskFromISR = 0;
51 uint32_t ulNumCallsClearInterruptMaskFromISR = 0;
52
53 /* ==========================  CALLBACK FUNCTIONS =========================== */
54
55 static void enterCriticalSectionStub( int cmock_num_calls )
56 {
57     /* check that enterCriticalSectionStub was not called twice in a row */
58     TEST_ASSERT_FALSE_MESSAGE( xInCriticalSection, "vFakePortEnterCriticalSection was called twice in a row." );
59     xInCriticalSection = true;
60     ulNumEnterCriticalSection++;
61 }
62
63 static void exitCriticalSectionStub( int cmock_num_calls )
64 {
65     /* check that exitCriticalSectionStub was not called twice in a row */
66     TEST_ASSERT_TRUE_MESSAGE( xInCriticalSection, "vFakePortExitCriticalSection was called twice in a row." );
67     xInCriticalSection = false;
68     ulNumExitCriticalSection++;
69 }
70
71 static UBaseType_t portSetInterruptMaskFromISRStub( int cmock_num_calls )
72 {
73     ulNumCallsSetInterruptMaskFromISR++;
74     uxSavedInterruptStatusGlobal = getLastMonotonicTestValue() + 241235;
75     xInCriticalSection = true;
76     return uxSavedInterruptStatusGlobal;
77 }
78
79 static void portClearInterruptMaskFromISRStub( UBaseType_t uxSavedInterruptStatus,
80                                                int cmock_num_calls )
81 {
82     ulNumCallsClearInterruptMaskFromISR++;
83     TEST_ASSERT_EQUAL_MESSAGE( uxSavedInterruptStatusGlobal, uxSavedInterruptStatus,
84                                "Saved interrupt state from call to portClearInterruptMaskFromISR does not match last call to portSetInterruptMaskFromISR." );
85     xInCriticalSection = false;
86     uxSavedInterruptStatusGlobal = 0;
87 }
88
89 /* ============================= Unity Fixtures ============================= */
90
91 /* ==========================  Helper functions =========================== */
92
93 void td_port_register_stubs( void )
94 {
95     /* Track critical section state */
96     xInCriticalSection = false;
97     ulNumEnterCriticalSection = 0;
98     ulNumExitCriticalSection = 0;
99
100     vFakePortEnterCriticalSection_Stub( &enterCriticalSectionStub );
101     vFakePortExitCriticalSection_Stub( &exitCriticalSectionStub );
102
103     uxSavedInterruptStatusGlobal = 0;
104     vFakePortClearInterruptMaskFromISR_Stub( &portClearInterruptMaskFromISRStub );
105     ulFakePortSetInterruptMaskFromISR_Stub( &portSetInterruptMaskFromISRStub );
106 }
107
108 BaseType_t td_port_isInCriticalSection( void )
109 {
110     return xInCriticalSection;
111 }
112
113 void td_port_teardown_check( void )
114 {
115     TEST_ASSERT_EQUAL_MESSAGE( ulNumEnterCriticalSection,
116                                ulNumExitCriticalSection,
117                                "Number of calls to vFakePortEnterCriticalSection does not match the number of calls to vFakePortExitCriticalSection." );
118
119     TEST_ASSERT_EQUAL_MESSAGE( 0,
120                                uxSavedInterruptStatusGlobal,
121                                "uxSavedInterruptStatus was non-zero at the end of the preceeding test case." );
122 }