foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
test_bal.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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_bal.c
44  * @author foxBMS Team
45  * @date 2020-08-05 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Test of the API of the balancing module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockdatabase.h"
58 #include "Mockos.h"
59 
60 #include "database_cfg.h"
61 
62 #include "bal.h"
63 
64 #include <stdbool.h>
65 #include <stdio.h>
66 
67 /*========== Definitions and Implementations for Unit Test ==================*/
69 
70 /*========== Setup and Teardown =============================================*/
71 void setUp(void) {
72  initState.timer = 0;
81  initState.active = false;
85 }
86 
87 void tearDown(void) {
88 }
89 
90 /*========== Test Cases =====================================================*/
91 void testNoStateChanged(void) {
94  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_UNINITIALIZED);
95  TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_ENTRY);
96  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
97 }
98 
99 void testStateChanged(void) {
104  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZATION);
105  TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_ENTRY);
106  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
107 }
108 
113  TEST_ASSERT_EQUAL(initState.lastState, BAL_STATEMACH_UNINITIALIZED);
114  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_UNINITIALIZED);
115  TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_CHECK_IMBALANCES);
116  TEST_ASSERT_EQUAL(initState.substate, BAL_CHECK_IMBALANCES);
117 }
118 
120  OS_EnterTaskCritical_Expect();
121  OS_ExitTaskCritical_Expect();
122  uint8_t returnValue = 0;
123  returnValue = BAL_CheckReEntrance(&initState);
124  TEST_ASSERT_EQUAL(returnValue, 0);
125  OS_EnterTaskCritical_Expect();
126  OS_ExitTaskCritical_Expect();
127  returnValue = BAL_CheckReEntrance(&initState);
128  TEST_ASSERT_EQUAL(returnValue, 0xFF);
129 }
130 
132  OS_EnterTaskCritical_Expect();
133  OS_ExitTaskCritical_Expect();
136  returnValue = BAL_TransferStateRequest(&initState);
137  TEST_ASSERT_EQUAL(returnValue, BAL_STATE_ERROR_REQUEST);
138  TEST_ASSERT_EQUAL(initState.stateRequest, BAL_STATE_NO_REQUEST);
139 }
140 
142  BAL_RETURN_TYPE_e returnValue = BAL_OK;
143 
145  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
146 }
148  BAL_RETURN_TYPE_e returnValue = BAL_OK;
151  TEST_ASSERT_EQUAL(initState.balancingGlobalAllowed, true);
152  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
153 }
155  BAL_RETURN_TYPE_e returnValue = BAL_OK;
158  TEST_ASSERT_EQUAL(initState.balancingGlobalAllowed, false);
159  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
160 }
162  BAL_RETURN_TYPE_e returnValue = BAL_OK;
164  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
165 }
167  BAL_RETURN_TYPE_e returnValue = BAL_OK;
169  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
170 }
172  BAL_RETURN_TYPE_e returnValue = BAL_OK;
175  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
176 }
177 
179  BAL_RETURN_TYPE_e returnValue = BAL_OK;
182  TEST_ASSERT_EQUAL(returnValue, BAL_ALREADY_INITIALIZED);
183 }
184 
186  /* illegal request */
187  BAL_RETURN_TYPE_e returnValue = BAL_OK;
190  TEST_ASSERT_EQUAL(returnValue, BAL_ILLEGAL_REQUEST);
191 }
192 
195  TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
196  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZATION);
197  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
198 }
199 
202 }
203 
207  TEST_ASSERT_EQUAL(initState.errorRequestCounter, 0xFF);
208 }
209 
212  TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
213  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZED);
214  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
215 }
216 
219  TEST_ASSERT_EQUAL(initState.initializationFinished, STD_OK);
220  TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
221  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_CHECK_BALANCING);
222  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
223 }
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:73
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:174
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:160
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:103
void BAL_ProcessStateInitialized(BAL_STATE_s *currentState)
State machine subfunction to transfer from an initalized state to "running" states of th state machin...
Definition: bal.c:182
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:87
BAL_RETURN_TYPE_e BAL_CheckStateRequest(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
checks the state requests that are made.
Definition: bal.c:116
Header for the driver for balancing.
BAL_RETURN_TYPE_e
Definition: bal.h:115
@ BAL_ALREADY_INITIALIZED
Definition: bal.h:123
@ BAL_OK
Definition: bal.h:116
@ BAL_ILLEGAL_REQUEST
Definition: bal.h:119
BAL_STATE_REQUEST_e
Definition: bal.h:102
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:108
@ BAL_STATE_ERROR_REQUEST
Definition: bal.h:104
@ BAL_STATE_NO_REQUEST
Definition: bal.h:109
@ BAL_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:106
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:105
@ BAL_STATE_INIT_REQUEST
Definition: bal.h:103
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:107
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:73
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:74
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:75
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:76
@ BAL_CHECK_IMBALANCES
Definition: bal.h:92
@ BAL_ENTRY
Definition: bal.h:91
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:65
Database configuration header.
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
uint32_t errorRequestCounter
Definition: bal.h:139
int32_t balancingThreshold
Definition: bal.h:142
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:133
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:140
uint8_t lastSubstate
Definition: bal.h:137
uint8_t triggerEntry
Definition: bal.h:138
BAL_STATEMACH_e lastState
Definition: bal.h:136
bool active
Definition: bal.h:141
bool balancingGlobalAllowed
Definition: bal.h:144
BAL_STATEMACH_SUB_e substate
Definition: bal.h:135
bool balancingAllowed
Definition: bal.h:143
uint16_t timer
Definition: bal.h:132
BAL_STATEMACH_e state
Definition: bal.h:134
void testFiniteStateMachineCheckStateRequestFunction6(void)
Definition: test_bal.c:178
void testProcessStateInitialized(void)
Definition: test_bal.c:217
void testProcessStateUninitalized1(void)
Definition: test_bal.c:200
void testProcessStateUninitalized2(void)
Definition: test_bal.c:204
void testStateChanged(void)
Definition: test_bal.c:99
void testFiniteStateMachineCheckStateRequestFunction3(void)
Definition: test_bal.c:161
void testFiniteStateMachineCheckStateRequestFunction4(void)
Definition: test_bal.c:166
void testFiniteStateMachineCheckStateRequestFunction7(void)
Definition: test_bal.c:185
void setUp(void)
Definition: test_bal.c:71
void testProcessStateInitialization(void)
Definition: test_bal.c:210
void tearDown(void)
Definition: test_bal.c:87
void testFiniteStateMachineCheckStateRequestFunction5(void)
Definition: test_bal.c:171
void testFiniteStateMachineCheckStateRequestFunction0(void)
Definition: test_bal.c:141
void testProcessStateUninitalized0(void)
Definition: test_bal.c:193
void testSubstateChanged(void)
Definition: test_bal.c:109
void testNoStateChanged(void)
Definition: test_bal.c:91
void testFiniteStateMachineTransferStateRequestFunction(void)
Definition: test_bal.c:131
void testFiniteStateMachineCheckStateRequestFunction1(void)
Definition: test_bal.c:147
void testFiniteStateMachineCheckReEntranceFunction(void)
Definition: test_bal.c:119
BAL_STATE_s initState
Definition: test_bal.c:68
void testFiniteStateMachineCheckStateRequestFunction2(void)
Definition: test_bal.c:154