foxBMS-UnitTests  1.0.0
The foxBMS Unit Tests API Documentation
test_bal.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
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 2020-08-05 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Test of the API of the balancing module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 #include "Mockdatabase.h"
57 #include "Mockos.h"
58 
59 #include "database_cfg.h"
60 
61 #include "bal.h"
62 
63 #include <stdio.h>
64 
65 /*========== Definitions and Implementations for Unit Test ==================*/
67 
68 /*========== Setup and Teardown =============================================*/
69 void setUp(void) {
70  initState.timer = 0;
79  initState.active = false;
83 }
84 
85 void tearDown(void) {
86 }
87 
88 /*========== Test Cases =====================================================*/
89 void testNoStateChanged(void) {
92  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_UNINITIALIZED);
93  TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_ENTRY);
94  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
95 }
96 
97 void testStateChanged(void) {
102  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZATION);
103  TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_ENTRY);
104  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
105 }
106 
111  TEST_ASSERT_EQUAL(initState.lastState, BAL_STATEMACH_UNINITIALIZED);
112  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_UNINITIALIZED);
113  TEST_ASSERT_EQUAL(initState.lastSubstate, BAL_CHECK_IMBALANCES);
114  TEST_ASSERT_EQUAL(initState.substate, BAL_CHECK_IMBALANCES);
115 }
116 
118  OS_EnterTaskCritical_Expect();
119  OS_ExitTaskCritical_Expect();
120  uint8_t returnValue = 0;
121  returnValue = BAL_CheckReEntrance(&initState);
122  TEST_ASSERT_EQUAL(returnValue, 0);
123  OS_EnterTaskCritical_Expect();
124  OS_ExitTaskCritical_Expect();
125  returnValue = BAL_CheckReEntrance(&initState);
126  TEST_ASSERT_EQUAL(returnValue, 0xFF);
127 }
128 
130  OS_EnterTaskCritical_Expect();
131  OS_ExitTaskCritical_Expect();
134  returnValue = BAL_TransferStateRequest(&initState);
135  TEST_ASSERT_EQUAL(returnValue, BAL_STATE_ERROR_REQUEST);
136  TEST_ASSERT_EQUAL(initState.stateRequest, BAL_STATE_NO_REQUEST);
137 }
138 
140  BAL_RETURN_TYPE_e returnValue = BAL_OK;
141 
143  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
144 }
146  BAL_RETURN_TYPE_e returnValue = BAL_OK;
149  TEST_ASSERT_EQUAL(initState.balancingGlobalAllowed, true);
150  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
151 }
153  BAL_RETURN_TYPE_e returnValue = BAL_OK;
156  TEST_ASSERT_EQUAL(initState.balancingGlobalAllowed, false);
157  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
158 }
160  BAL_RETURN_TYPE_e returnValue = BAL_OK;
162  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
163 }
165  BAL_RETURN_TYPE_e returnValue = BAL_OK;
167  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
168 }
170  BAL_RETURN_TYPE_e returnValue = BAL_OK;
173  TEST_ASSERT_EQUAL(returnValue, BAL_OK);
174 }
175 
177  BAL_RETURN_TYPE_e returnValue = BAL_OK;
180  TEST_ASSERT_EQUAL(returnValue, BAL_ALREADY_INITIALIZED);
181 }
182 
184  /* illegal request */
185  BAL_RETURN_TYPE_e returnValue = BAL_OK;
188  TEST_ASSERT_EQUAL(returnValue, BAL_ILLEGAL_REQUEST);
189 }
190 
193  TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
194  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZATION);
195  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
196 }
197 
200 }
201 
205  TEST_ASSERT_EQUAL(initState.errorRequestCounter, 0xFF);
206 }
207 
210  TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
211  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_INITIALIZED);
212  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
213 }
214 
217  TEST_ASSERT_EQUAL(initState.initializationFinished, STD_OK);
218  TEST_ASSERT_EQUAL(initState.timer, BAL_STATEMACH_SHORTTIME_100ms);
219  TEST_ASSERT_EQUAL(initState.state, BAL_STATEMACH_CHECK_BALANCING);
220  TEST_ASSERT_EQUAL(initState.substate, BAL_ENTRY);
221 }
testStateChanged
void testStateChanged(void)
Definition: test_bal.c:97
testFiniteStateMachineCheckStateRequestFunction0
void testFiniteStateMachineCheckStateRequestFunction0(void)
Definition: test_bal.c:139
BAL_STATE::lastSubstate
uint8_t lastSubstate
Definition: bal.h:134
BAL_STATE::balancingThreshold
int32_t balancingThreshold
Definition: bal.h:139
BAL_STATE::balancingGlobalAllowed
bool balancingGlobalAllowed
Definition: bal.h:141
BAL_STATE::triggerEntry
uint8_t triggerEntry
Definition: bal.h:135
BAL_STATE_NO_REQUEST
@ BAL_STATE_NO_REQUEST
Definition: bal.h:106
BAL_ENTRY
@ BAL_ENTRY
Definition: bal.h:88
initState
BAL_STATE_s initState
Definition: test_bal.c:66
BAL_TransferStateRequest
BAL_STATE_REQUEST_e BAL_TransferStateRequest(BAL_STATE_s *currentState)
transfers the current state request to the state machine.
Definition: bal.c:97
testSubstateChanged
void testSubstateChanged(void)
Definition: test_bal.c:107
testProcessStateUninitalized2
void testProcessStateUninitalized2(void)
Definition: test_bal.c:202
testFiniteStateMachineCheckStateRequestFunction4
void testFiniteStateMachineCheckStateRequestFunction4(void)
Definition: test_bal.c:164
testFiniteStateMachineCheckStateRequestFunction2
void testFiniteStateMachineCheckStateRequestFunction2(void)
Definition: test_bal.c:152
BAL_STATE
Definition: bal.h:128
BAL_STATEMACH_CHECK_BALANCING
@ BAL_STATEMACH_CHECK_BALANCING
Definition: bal.h:73
bal.h
Header for the driver for balancing.
testProcessStateInitialized
void testProcessStateInitialized(void)
Definition: test_bal.c:215
BAL_CheckReEntrance
uint8_t BAL_CheckReEntrance(BAL_STATE_s *currentState)
re-entrance check of BAL state machine trigger function
Definition: bal.c:82
BAL_STATEMACH_INITIALIZATION
@ BAL_STATEMACH_INITIALIZATION
Definition: bal.h:71
BAL_STATE_NO_BALANCING_REQUEST
@ BAL_STATE_NO_BALANCING_REQUEST
Definition: bal.h:102
BAL_STATE::timer
uint16_t timer
Definition: bal.h:129
BAL_ProcessStateInitialization
void BAL_ProcessStateInitialization(BAL_STATE_s *currentState)
State machine subfunction to initialize the balancing state machine.
Definition: bal.c:164
testFiniteStateMachineCheckStateRequestFunction5
void testFiniteStateMachineCheckStateRequestFunction5(void)
Definition: test_bal.c:169
BAL_ProcessStateInitialized
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:171
testNoStateChanged
void testNoStateChanged(void)
Definition: test_bal.c:89
BAL_ALREADY_INITIALIZED
@ BAL_ALREADY_INITIALIZED
Definition: bal.h:120
BAL_STATE::state
BAL_STATEMACH_e state
Definition: bal.h:131
BAL_STATE::initializationFinished
STD_RETURN_TYPE_e initializationFinished
Definition: bal.h:137
STD_OK
@ STD_OK
Definition: fstd_types.h:72
BAL_STATEMACH_UNINITIALIZED
@ BAL_STATEMACH_UNINITIALIZED
Definition: bal.h:70
testProcessStateUninitalized1
void testProcessStateUninitalized1(void)
Definition: test_bal.c:198
tearDown
void tearDown(void)
Definition: test_bal.c:85
testFiniteStateMachineCheckStateRequestFunction1
void testFiniteStateMachineCheckStateRequestFunction1(void)
Definition: test_bal.c:145
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
BAL_STATE_GLOBAL_ENABLE_REQUEST
@ BAL_STATE_GLOBAL_ENABLE_REQUEST
Definition: bal.h:105
BAL_ILLEGAL_REQUEST
@ BAL_ILLEGAL_REQUEST
Definition: bal.h:116
setUp
void setUp(void)
Definition: test_bal.c:69
BAL_STATE_GLOBAL_DISABLE_REQUEST
@ BAL_STATE_GLOBAL_DISABLE_REQUEST
Definition: bal.h:104
BAL_STATE::lastState
BAL_STATEMACH_e lastState
Definition: bal.h:133
testFiniteStateMachineCheckStateRequestFunction3
void testFiniteStateMachineCheckStateRequestFunction3(void)
Definition: test_bal.c:159
testFiniteStateMachineCheckStateRequestFunction6
void testFiniteStateMachineCheckStateRequestFunction6(void)
Definition: test_bal.c:176
BAL_RETURN_TYPE_e
enum BAL_RETURN_TYPE BAL_RETURN_TYPE_e
BAL_SaveLastStates
void BAL_SaveLastStates(BAL_STATE_s *pBalancingState)
Saves the last state and the last substate.
Definition: bal.c:69
BAL_STATE::active
bool active
Definition: bal.h:138
BAL_STATEMACH_SHORTTIME_100ms
#define BAL_STATEMACH_SHORTTIME_100ms
Definition: bal_cfg.h:63
testFiniteStateMachineTransferStateRequestFunction
void testFiniteStateMachineTransferStateRequestFunction(void)
Definition: test_bal.c:129
BAL_STATEMACH_INITIALIZED
@ BAL_STATEMACH_INITIALIZED
Definition: bal.h:72
BAL_STATE_INIT_REQUEST
@ BAL_STATE_INIT_REQUEST
Definition: bal.h:100
testFiniteStateMachineCheckStateRequestFunction7
void testFiniteStateMachineCheckStateRequestFunction7(void)
Definition: test_bal.c:183
BAL_STATE::substate
BAL_STATEMACH_SUB_e substate
Definition: bal.h:132
BAL_CheckStateRequest
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:109
BAL_STATE::stateRequest
BAL_STATE_REQUEST_e stateRequest
Definition: bal.h:130
testFiniteStateMachineCheckReEntranceFunction
void testFiniteStateMachineCheckReEntranceFunction(void)
Definition: test_bal.c:117
BAL_STATE_REQUEST_e
enum BAL_STATE_REQUEST BAL_STATE_REQUEST_e
BAL_STATE::errorRequestCounter
uint32_t errorRequestCounter
Definition: bal.h:136
BAL_STATE_ERROR_REQUEST
@ BAL_STATE_ERROR_REQUEST
Definition: bal.h:101
testProcessStateInitialization
void testProcessStateInitialization(void)
Definition: test_bal.c:208
BAL_STATE::balancingAllowed
bool balancingAllowed
Definition: bal.h:140
BAL_CHECK_IMBALANCES
@ BAL_CHECK_IMBALANCES
Definition: bal.h:89
BAL_ProcessStateUninitalized
void BAL_ProcessStateUninitalized(BAL_STATE_s *pCurrentState, BAL_STATE_REQUEST_e stateRequest)
Substate handling function for BAL_Trigger()
Definition: bal.c:151
BAL_OK
@ BAL_OK
Definition: bal.h:113
database_cfg.h
Database configuration header.
BAL_STATE_ALLOWBALANCING_REQUEST
@ BAL_STATE_ALLOWBALANCING_REQUEST
Definition: bal.h:103
testProcessStateUninitalized0
void testProcessStateUninitalized0(void)
Definition: test_bal.c:191