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