foxBMS - Unit Tests  1.1.2
The foxBMS Unit Tests API Documentation
test_sys_mon.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file test_sys_mon.c
44  * @author foxBMS Team
45  * @date 2020-04-02 (date of creation)
46  * @updated 2020-05-28 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Tests for the sys_mon module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 #include "Mockdiag.h"
57 #include "Mockos.h"
58 #include "Mocksys_mon_cfg.h"
59 
60 #include "fassert.h"
61 #include "sys_mon.h"
62 #include "test_assert_helper.h"
63 
64 /*========== Definitions and Implementations for Unit Test ==================*/
65 #define DUMMY_TSK_ID_0 0
66 #define DUMMY_TSK_ID_1 1
67 #define DUMMY_CYCLETIME 10
68 #define DUMMY_MAX_JITTER 1
69 
71  TEST_ASSERT_EQUAL(DUMMY_TSK_ID_0, tsk_id);
72 }
73 
75  TEST_ASSERT_EQUAL(DUMMY_TSK_ID_1, tsk_id);
76 }
77 
93 };
94 
95 /*========== Setup and Teardown =============================================*/
96 void setUp(void) {
98  notifications[DUMMY_TSK_ID_0].timestampEnter = 0;
99  notifications[DUMMY_TSK_ID_0].timestampExit = 0;
100  notifications[DUMMY_TSK_ID_0].duration = 0;
101 }
102 
103 void tearDown(void) {
104 }
105 
106 /*========== Test Cases =====================================================*/
108  /* Internal timestamp should be 0, expect an early return
109  (if not returning early, test would fail since other functions
110  are called; also make sure that the code path that would call the
111  DIAG_Handler would be hit if not returning early) */
113  notifications[DUMMY_TSK_ID_0].timestampEnter = 0;
114  notifications[DUMMY_TSK_ID_0].timestampExit = 100;
115  notifications[DUMMY_TSK_ID_0].duration = 100;
116  OS_GetTickCount_ExpectAndReturn(0u);
118 }
119 
121  /* call a task with Monitoring disabled */
122  OS_GetTickCount_ExpectAndReturn(0u);
124 
125  OS_GetTickCount_ExpectAndReturn(100u);
127 }
128 
130  /* provoke the violation of the task duration */
131  OS_GetTickCount_ExpectAndReturn(0u);
133 
135  notifications[DUMMY_TSK_ID_0].timestampEnter = 0;
136  notifications[DUMMY_TSK_ID_0].timestampExit = 100;
137  notifications[DUMMY_TSK_ID_0].duration = 100;
138 
139  OS_GetTickCount_ExpectAndReturn(100u);
140  DIAG_Handler_ExpectAndReturn(
143 }
144 
146  /* give an invalid Task ID to Notify */
149  TEST_ASSERT_NOT_EQUAL(424242, notifications[SYSM_TASK_ID_MAX + 1u].timestampEnter);
150 }
151 
153  /* check whether Notify properly sets the entry timestamp */
154  OS_EnterTaskCritical_Expect();
155  OS_ExitTaskCritical_Expect();
158  TEST_ASSERT_EQUAL(UINT32_MAX, notifications[DUMMY_TSK_ID_0].timestampEnter);
159 
160  OS_EnterTaskCritical_Expect();
161  OS_ExitTaskCritical_Expect();
163  notifications = TEST_SYSM_GetNotifications();
164  TEST_ASSERT_EQUAL(0, notifications[DUMMY_TSK_ID_0].timestampEnter);
165 }
166 
168  /* check whether Notify properly sets the exit timestamp and computes duration */
169  OS_EnterTaskCritical_Expect();
170  OS_ExitTaskCritical_Expect();
173  TEST_ASSERT_EQUAL(UINT32_MAX, notifications[DUMMY_TSK_ID_0].timestampEnter);
174 
175  const uint32 exitTime = 100;
176  OS_EnterTaskCritical_Expect();
177  OS_ExitTaskCritical_Expect();
179  notifications = TEST_SYSM_GetNotifications();
180  TEST_ASSERT_EQUAL(exitTime, notifications[DUMMY_TSK_ID_0].timestampExit);
181  /* we go over the overflow, so it should be exit time plus one */
182  TEST_ASSERT_EQUAL(exitTime + 1, notifications[DUMMY_TSK_ID_0].duration);
183 }
184 
186  /* This test hits the assert with an illegal notify type */
187  OS_EnterTaskCritical_Ignore();
188  /* use a notify type of INT8_MAX, as this is likely an illegal notify type */
189  TEST_ASSERT_FAIL_ASSERT(SYSM_Notify(DUMMY_TSK_ID_0, INT8_MAX, UINT32_MAX));
191  /* check that nothing has been written */
192  TEST_ASSERT_NOT_EQUAL(UINT32_MAX, notifications[DUMMY_TSK_ID_0].timestampEnter);
193  TEST_ASSERT_NOT_EQUAL(UINT32_MAX, notifications[DUMMY_TSK_ID_0].timestampExit);
194 }
@ DIAG_HANDLER_RETURN_OK
Definition: diag.h:64
@ DIAG_SYSTEM
Definition: diag_cfg.h:247
@ DIAG_EVENT_NOT_OK
Definition: diag_cfg.h:235
@ DIAG_ID_SYSTEMMONITORING
Definition: diag_cfg.h:158
Assert macro implementation.
uint32_t duration
Definition: sys_mon.h:71
uint32_t timestampEnter
Definition: sys_mon.h:69
uint32_t timestampExit
Definition: sys_mon.h:70
SYSM_NOTIFICATION_s * TEST_SYSM_GetNotifications(void)
Definition: sys_mon.c:123
void SYSM_CheckNotifications(void)
overall system monitoring
Definition: sys_mon.c:72
void SYSM_Notify(SYSM_TASK_ID_e tsk_id, SYSM_NOTIFY_TYPE_e state, uint32_t time)
Sets needed bits to indicate that a task is running.
Definition: sys_mon.c:104
@ SYSM_NOTIFY_ENTER
Definition: sys_mon.h:62
@ SYSM_NOTIFY_EXIT
Definition: sys_mon.h:63
@ SYSM_HANDLING_SWITCHOFFCONTACTOR
Definition: sys_mon_cfg.h:65
@ SYSM_ENABLED
Definition: sys_mon_cfg.h:89
@ SYSM_DISABLED
Definition: sys_mon_cfg.h:90
@ SYSM_TASK_ID_MAX
Definition: sys_mon_cfg.h:78
@ SYSM_RECORDING_ENABLED
Definition: sys_mon_cfg.h:83
enum SYSM_TASK_ID SYSM_TASK_ID_e
Helper for unit tests.
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
void testSYSM_NotifyHitAssertWithIllegalNotifyType(void)
Definition: test_sys_mon.c:185
void testSYSM_NotifyInvalidTaskID(void)
Definition: test_sys_mon.c:145
void testSYSM_CheckNotificationsSYSMDisabled(void)
Definition: test_sys_mon.c:120
#define DUMMY_TSK_ID_1
Definition: test_sys_mon.c:66
void testSYSM_CheckNotificationsProvokeDurationViolation(void)
Definition: test_sys_mon.c:129
#define DUMMY_MAX_JITTER
Definition: test_sys_mon.c:68
#define DUMMY_TSK_ID_0
Definition: test_sys_mon.c:65
#define DUMMY_CYCLETIME
Definition: test_sys_mon.c:67
void testSYSM_CheckNotificationsEarlyExitOnTimestampEquality(void)
Definition: test_sys_mon.c:107
void setUp(void)
Definition: test_sys_mon.c:96
void tearDown(void)
Definition: test_sys_mon.c:103
void TEST_SYSM_DummyCallback_0(SYSM_TASK_ID_e tsk_id)
Definition: test_sys_mon.c:70
void testSYSM_NotifyEnterTimestampProperlySet(void)
Definition: test_sys_mon.c:152
void TEST_SYSM_DummyCallback_1(SYSM_TASK_ID_e tsk_id)
Definition: test_sys_mon.c:74
void testSYSM_NotifyExitTimestampProperlySetAndDurationCalculated(void)
Definition: test_sys_mon.c:167
SYSM_MONITORING_CFG_s sysm_ch_cfg[2]
Definition: test_sys_mon.c:78