foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
test_algorithm.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, 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_algorithm.c
44  * @author foxBMS Team
45  * @date 2020-06-30 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Test of the algorithm module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockalgorithm_cfg.h"
58 #include "Mockos.h"
59 #include "Mocktest_algorithm_stubs.h"
60 
61 #include "algorithm.h"
62 #include "test_assert_helper.h"
63 
64 /*========== Definitions and Implementations for Unit Test ==================*/
68 };
69 
70 const uint16_t algo_length = sizeof(algo_algorithms) / sizeof(algo_algorithms[0]);
71 
72 /*========== Setup and Teardown =============================================*/
73 void setUp(void) {
74  /* uninitialize everything */
75  for (uint16_t i = 0u; i < algo_length; i++) {
77  }
78 
79  /* relock initialization */
81 }
82 
83 void tearDown(void) {
84 }
85 
86 /*========== Test Cases =====================================================*/
87 /* TODO: check behavior when timer flows over for the runtime analysis */
88 
90  /* when no algorithm is initialized and init is not unlocked then nothing should be called */
91  OS_EnterTaskCritical_Ignore();
92  OS_ExitTaskCritical_Ignore();
94  TEST_ASSERT_EQUAL(ALGO_UNINITIALIZED, algo_algorithms[0].state);
95  TEST_ASSERT_EQUAL(ALGO_UNINITIALIZED, algo_algorithms[1].state);
96 }
97 
99  /* when no algorithm is initialized and init is not unlocked then nothing should be called */
100  OS_EnterTaskCritical_Ignore();
101  OS_ExitTaskCritical_Ignore();
103  TEST_ASSERT_EQUAL(ALGO_UNINITIALIZED, algo_algorithms[0].state);
104  TEST_ASSERT_EQUAL(ALGO_UNINITIALIZED, algo_algorithms[1].state);
105 
106  /* after that if we unlock, then the init process will be handled and the
107  first calculation computed */
109 
110  /* call to the init function of the second entry (first one has no init) */
111  TEST_AlgorithmInitializationFunction_ExpectAndReturn(STD_OK);
112 
113  /* this is the retrieval of the start time for both algorithms and
114  after that both algorithms should be called */
115  OS_GetTickCount_ExpectAndReturn(0u);
116  TEST_AlgorithmComputeFunction_Expect();
117  ALGO_MarkAsDone_Expect(0u);
118  OS_GetTickCount_ExpectAndReturn(0u);
119  TEST_AlgorithmComputeFunction_Expect();
120  ALGO_MarkAsDone_Expect(1u);
121 
123 
124  /* afterwards the algorithms should have switched to running as we are
125  mocking ALGO_MarkAsDone without function
126  (normally the algorithm would then switch back its state upon completion) */
127  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[0].state);
128  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[1].state);
129 }
130 
132  /* after that if we unlock, then the init process will be handled and the
133  first calculation computed */
134  OS_EnterTaskCritical_Ignore();
135  OS_ExitTaskCritical_Ignore();
137 
138  const uint32_t storeCycleTime = algo_algorithms[0].cycleTime_ms;
139  /* set to an invalid cycle time */
141 
143 
144  TEST_ASSERT_EQUAL(ALGO_UNINITIALIZED, algo_algorithms[0].state);
145  TEST_ASSERT_EQUAL(ALGO_UNINITIALIZED, algo_algorithms[1].state);
146 
147  /* restore cycle time for other tests */
148  algo_algorithms[0].cycleTime_ms = storeCycleTime;
149 }
150 
152  /* unlock so that we can continue */
153  OS_EnterTaskCritical_Ignore();
154  OS_ExitTaskCritical_Ignore();
156 
157  /* call to the init function of the second entry (first one has no init)
158  this time we indicate a failure */
159  TEST_AlgorithmInitializationFunction_ExpectAndReturn(STD_NOT_OK);
160 
161  /* now the algorithm without init should be running and the other one in
162  error state */
163  OS_GetTickCount_ExpectAndReturn(0u);
164  TEST_AlgorithmComputeFunction_Expect();
165  ALGO_MarkAsDone_Expect(0u);
166 
168 
169  /* afterwards the algorithms should have switched to running and failure state */
170  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[0].state);
171  TEST_ASSERT_EQUAL(ALGO_FAILED_INIT, algo_algorithms[1].state);
172 
173  /* subsequent calls after that should not change anything */
175 
176  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[0].state);
177  TEST_ASSERT_EQUAL(ALGO_FAILED_INIT, algo_algorithms[1].state);
178 }
179 
181  OS_EnterTaskCritical_Ignore();
182  OS_ExitTaskCritical_Ignore();
183  OS_GetTickCount_IgnoreAndReturn(0u);
184  TEST_AlgorithmComputeFunction_Ignore();
185  ALGO_MarkAsDone_Ignore();
186 
187  /* unlock so that we can continue */
189 
190  /* call to the init function of the second entry (first one has no init)
191  this time we indicate a failure */
192  TEST_AlgorithmInitializationFunction_ExpectAndReturn(STD_OK);
193 
195 
196  /* afterwards the algorithms should have switched to running and failure state */
197  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[0].state);
198  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[1].state);
199 
200  /* unlock so that we can continue */
202 
203  /* subsequent calls after that should not change anything */
205 
206  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[0].state);
207  TEST_ASSERT_EQUAL(ALGO_RUNNING, algo_algorithms[1].state);
208 }
209 
211  /* unlock so that we can continue */
212  OS_EnterTaskCritical_Ignore();
213  OS_ExitTaskCritical_Ignore();
215 
216  /* call to the init function of the second entry (first one has no init)
217  this time we send something that cannot be returned normally */
218  TEST_AlgorithmInitializationFunction_ExpectAndReturn(42u);
219 
220  /* as a result the system will fail on init and assert */
222 }
223 
224 void testCycleTimeZero(void) {
225  /* this test aims to test what a cycle time of zero does */
226 
227  /* unlock so that we can continue */
228  OS_EnterTaskCritical_Ignore();
229  OS_ExitTaskCritical_Ignore();
231 
232  /* inject a cycle time of zero */
234 
235  /* call the main function;
236  if this crashes we ran probably into a division by zero;*/
237  TEST_AlgorithmInitializationFunction_ExpectAndReturn(STD_OK);
238  OS_GetTickCount_ExpectAndReturn(0u);
239  TEST_AlgorithmComputeFunction_Expect();
240  ALGO_MarkAsDone_Expect(0u);
241  OS_GetTickCount_ExpectAndReturn(0u);
242  TEST_AlgorithmComputeFunction_Expect();
243  ALGO_MarkAsDone_Expect(1u);
245 }
246 
248  const uint32_t startTime = 500u;
249  const uint32_t currentTime = 0u;
250  const ALGO_STATE_e state = ALGO_READY;
251 
252  algo_algorithms[0].startTime = startTime;
253  algo_algorithms[0].state = state;
254  OS_GetTickCount_ExpectAndReturn(currentTime);
256  TEST_ASSERT_EQUAL(state, algo_algorithms[0].state);
257 }
258 
260  const uint32_t startTime = 500u;
261  const uint32_t currentTime = 500u;
262  const ALGO_STATE_e state = ALGO_RUNNING;
263 
264  algo_algorithms[0].startTime = startTime;
265  algo_algorithms[0].state = state;
266  OS_GetTickCount_ExpectAndReturn(currentTime);
268  TEST_ASSERT_EQUAL(state, algo_algorithms[0].state);
269 }
270 
272  const uint32_t startTime = 500u;
273  const uint32_t currentTime = 50000u;
274  const ALGO_STATE_e state = ALGO_RUNNING;
275 
276  algo_algorithms[0].startTime = startTime;
277  algo_algorithms[0].state = state;
278  OS_GetTickCount_ExpectAndReturn(currentTime);
280  TEST_ASSERT_EQUAL(ALGO_BLOCKED, algo_algorithms[0].state);
281 }
void ALGO_UnlockInitialization(void)
Calling this function sets a signal that lets ALGO_Initialization() know that the initialization has ...
Definition: algorithm.c:106
void TEST_ALGO_ResetInitializationRequest()
Definition: algorithm.c:167
void ALGO_MainFunction(void)
handles the call of different algorithm functions when cycle time has expired
Definition: algorithm.c:112
void ALGO_MonitorExecutionTime(void)
monitors the calculation duration of the different algorithms
Definition: algorithm.c:151
Headers for the driver for the storage in the EEPROM memory.
ALGO_STATE_e
Definition: algorithm_cfg.h:81
@ ALGO_UNINITIALIZED
Definition: algorithm_cfg.h:82
@ ALGO_RUNNING
Definition: algorithm_cfg.h:84
@ ALGO_FAILED_INIT
Definition: algorithm_cfg.h:87
@ ALGO_READY
Definition: algorithm_cfg.h:83
@ ALGO_BLOCKED
Definition: algorithm_cfg.h:86
#define ALGO_TICK_ms
Definition: algorithm_cfg.h:67
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
uint32_t cycleTime_ms
Definition: algorithm_cfg.h:94
uint32_t startTime
Definition: algorithm_cfg.h:96
ALGO_STATE_e state
Definition: algorithm_cfg.h:93
void testUninitializedCallsNothing(void)
const uint16_t algo_length
void testUnlockInitializationInvalidAlgorithmConfiguration(void)
void testUnlockInitialization(void)
void testMonitorFunctionPassBecauseInTime(void)
void testCycleTimeZero(void)
void testTwoTimesInitialization(void)
void testMonitorFunctionStopBecauseOutOfTime(void)
void testUnsuccessfulInitialization(void)
void setUp(void)
void tearDown(void)
ALGO_TASKS_s algo_algorithms[]
void testWrongInitializationImplementation(void)
void testMonitorFunctionPassBecauseNotRunning(void)
STD_RETURN_TYPE_e TEST_AlgorithmInitializationFunction(void)
void TEST_AlgorithmComputeFunction(void)
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed