foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
test_database_helper.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_database_helper.c
44  * @author foxBMS Team
45  * @date 2021-05-05 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the database helper functions
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "general.h"
57 
58 #include "unity.h"
59 #include "Mockfassert.h"
60 #include "Mockmpu_prototypes.h"
61 #include "Mockos.h"
62 
63 #include "database_cfg.h"
64 
65 #include "database_helper.h"
66 #include "test_assert_helper.h"
67 
68 /*========== Definitions and Implementations for Unit Test ==================*/
69 
70 /*========== Setup and Teardown =============================================*/
71 void setUp(void) {
72 }
73 
74 void tearDown(void) {
75 }
76 
77 /*========== Test Cases =====================================================*/
78 /** This function tests various inputs for helper function
79  * #DATA_DatabaseEntryUpdatedAtLeastOnce */
82 
83  /* Database entry has been updated once, after 10ms */
84  databaseEntry.header.timestamp = 10u;
85  databaseEntry.header.previousTimestamp = 0u;
86  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
87 
88  /* Database entry has been updated twice, first after 10ms, then after 50ms */
89  databaseEntry.header.timestamp = 60u;
90  databaseEntry.header.previousTimestamp = 10u;
91  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
92 
93  /* Database entry has been updated three times, first after 10ms, then after 50ms, then after 10ms */
94  databaseEntry.header.timestamp = 70u;
95  databaseEntry.header.previousTimestamp = 60u;
96  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
97 
98  /* Database entry has never been updated */
99  databaseEntry.header.timestamp = 0u;
100  databaseEntry.header.previousTimestamp = 0u;
101  TEST_ASSERT_FALSE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
102 }
103 
104 /** This function tests various inputs for database helper function
105  * #DATA_EntryUpdatedWithinInterval */
108 
109  /* Always check always if database entry has been updated within the last 100ms */
110  uint32_t timeDifference = 100u;
111 
112  /* Time difference: 50ms -> true */
113  databaseEntry.header.timestamp = 50u;
114  OS_GetTickCount_ExpectAndReturn(100u);
115  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
116 
117  /* Time difference: 100ms -> true, but never updated */
118  databaseEntry.header.timestamp = 0u;
119  OS_GetTickCount_ExpectAndReturn(100u);
120  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
121 
122  /* Time difference: 101ms -> false */
123  databaseEntry.header.timestamp = 0u;
124  OS_GetTickCount_ExpectAndReturn(101u);
125  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
126 
127  /* Time difference: 63ms -> true */
128  databaseEntry.header.timestamp = 4937u;
129  OS_GetTickCount_ExpectAndReturn(5000u);
130  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
131 
132  /* Time difference: 50ms -> true */
133  databaseEntry.header.timestamp = UINT32_MAX;
134  OS_GetTickCount_ExpectAndReturn(50u);
135  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
136 
137  /* Time difference: 100ms -> true */
138  databaseEntry.header.timestamp = UINT32_MAX - 50u;
139  OS_GetTickCount_ExpectAndReturn(49u);
140  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
141 
142  /* Time difference: 101ms -> false */
143  databaseEntry.header.timestamp = UINT32_MAX - 50u;
144  OS_GetTickCount_ExpectAndReturn(50u);
145  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
146 
147  /* Time difference: UINT32_MAX - 50 -> false */
148  databaseEntry.header.timestamp = 50u;
149  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
150  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
151 }
152 
153 /** This function tests various inputs for database helper function
154  * #DATA_EntryUpdatedPeriodicallyWithinInterval */
157 
158  /* Always check always if database entry has been periodically updated within the last 100ms */
159  uint32_t timeDifference = 100u;
160 
161  /* Time difference timestamp - systick: 40ms -> true
162  * Time difference timestamp - previous timestamp: 50ms -> true */
163  databaseEntry.header.timestamp = 60u;
164  databaseEntry.header.previousTimestamp = 10u;
165  OS_GetTickCount_ExpectAndReturn(100u);
166  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
167 
168  /* Within time interval but never updated -> false */
169  databaseEntry.header.timestamp = 0u;
170  databaseEntry.header.previousTimestamp = 0u;
171  OS_GetTickCount_ExpectAndReturn(50u);
172  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
173 
174  /* Time difference timestamp - systick: 100ms -> true
175  * Time difference timestamp - previous timestamp: 40ms -> true */
176  databaseEntry.header.timestamp = 80u;
177  databaseEntry.header.previousTimestamp = 40u;
178  OS_GetTickCount_ExpectAndReturn(180u);
179  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
180 
181  /* Time difference timestamp - systick: 101ms -> false
182  * Time difference timestamp - previous timestamp: 40ms -> true */
183  databaseEntry.header.timestamp = 80u;
184  databaseEntry.header.previousTimestamp = 40u;
185  OS_GetTickCount_ExpectAndReturn(181);
186  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
187 
188  /* Time difference timestamp - systick: 50ms -> true
189  * Time difference timestamp - previous timestamp: 110ms -> false */
190  databaseEntry.header.timestamp = 150u;
191  databaseEntry.header.previousTimestamp = 40u;
192  OS_GetTickCount_ExpectAndReturn(200u);
193  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
194 
195  /* Time difference timestamp - systick: 100ms -> true
196  * Time difference timestamp - previous timestamp: 100ms -> true */
197  databaseEntry.header.timestamp = 150u;
198  databaseEntry.header.previousTimestamp = 50u;
199  OS_GetTickCount_ExpectAndReturn(250u);
200  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
201 
202  /* Time difference timestamp - systick: 100ms -> true
203  * Time difference timestamp - previous timestamp: 100ms -> true */
204  databaseEntry.header.timestamp = UINT32_MAX - 50u;
205  databaseEntry.header.previousTimestamp = UINT32_MAX - 150u;
206  OS_GetTickCount_ExpectAndReturn(49u);
207  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
208 
209  /* Time difference timestamp - systick: 101ms -> false
210  * Time difference timestamp - previous timestamp: 100ms -> true */
211  databaseEntry.header.timestamp = UINT32_MAX - 50u;
212  databaseEntry.header.previousTimestamp = UINT32_MAX - 150u;
213  OS_GetTickCount_ExpectAndReturn(50u);
214  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
215 
216  /* Time difference timestamp - systick: 100ms -> true
217  * Time difference timestamp - previous timestamp: 100ms -> true */
218  databaseEntry.header.timestamp = 49u;
219  databaseEntry.header.previousTimestamp = UINT32_MAX - 50u;
220  OS_GetTickCount_ExpectAndReturn(150u);
221  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
222 
223  /* Time difference timestamp - systick: 100ms -> true
224  * Time difference timestamp - previous timestamp: 101ms -> false */
225  databaseEntry.header.timestamp = UINT32_MAX - 50u;
226  databaseEntry.header.previousTimestamp = UINT32_MAX - 151u;
227  OS_GetTickCount_ExpectAndReturn(49u);
228  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
229 
230  /* Time difference timestamp - systick: 100ms -> true
231  * Time difference timestamp - previous timestamp: 101ms -> false */
232  databaseEntry.header.timestamp = 50u;
233  databaseEntry.header.previousTimestamp = UINT32_MAX - 50u;
234  OS_GetTickCount_ExpectAndReturn(150u);
235  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
236 
237  /* Time difference: UINT32_MAX - 50 -> false */
238  databaseEntry.header.timestamp = 50u;
239  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
240  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
241 }
242 
243 /** This function tests various inputs for database helper function
244  * #DATA_GetStringNumberFromVoltageIndex */
246  /* Last cell in string 0 */
247  uint16_t voltageIndex = (BS_NR_OF_CELL_BLOCKS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
248  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromVoltageIndex(voltageIndex), 0u);
249 
250  /* Last cell in last string */
252  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromVoltageIndex(voltageIndex), BS_NR_OF_STRINGS - 1u);
253 
254  /* Test is function passes if index 0 is passed */
256 
257  /* Test is function asserts if invalid index is passed */
261 }
262 
263 /** This function tests various inputs for database helper function
264  * #DATA_GetModuleNumberFromVoltageIndex */
266  uint16_t voltageIndex = (BS_NR_OF_CELL_BLOCKS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
267  TEST_ASSERT_EQUAL(DATA_GetModuleNumberFromVoltageIndex(voltageIndex), BS_NR_OF_MODULES_PER_STRING - 1u);
268 
269  /* Test is function passes if index 0 is passed */
271 
272  /* Test is function asserts if invalid index is passed */
276 }
277 
278 /** This function tests various inputs for database helper function
279  * #testDATA_GetCellNumberFromVoltageIndex */
281  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
282  for (uint8_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_MODULE; c++) {
283  uint16_t voltageIndex = (m * BS_NR_OF_CELL_BLOCKS_PER_MODULE) + c;
284  TEST_ASSERT_EQUAL(DATA_GetCellNumberFromVoltageIndex(voltageIndex), c);
285  }
286  }
287 
288  /* Test is function passes if index 0 is passed */
290 
291  /* Test is function asserts if invalid index is passed */
295 }
296 
297 /** This function tests various inputs for database helper function
298  * #DATA_GetStringNumberFromTemperatureIndex */
300  /* last sensor in string 0 */
301  uint16_t sensorIndex = (BS_NR_OF_TEMP_SENSORS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
302  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromTemperatureIndex(sensorIndex), 0u);
303 
304  /* last sensor in last string */
306  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromTemperatureIndex(sensorIndex), BS_NR_OF_STRINGS - 1u);
307 
308  /* Test is function passes if index 0 is passed */
310 
311  /* Test is function asserts if invalid index is passed */
315 }
316 
317 /** This function tests various inputs for database helper function
318  * #DATA_GetModuleNumberFromTemperatureIndex */
320  uint16_t sensorIndex = (BS_NR_OF_TEMP_SENSORS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
321  TEST_ASSERT_EQUAL(DATA_GetModuleNumberFromTemperatureIndex(sensorIndex), BS_NR_OF_MODULES_PER_STRING - 1u);
322 
323  /* Test is function passes if index 0 is passed */
325 
326  /* Test is function asserts if invalid index is passed */
330 }
331 
332 /** This function tests various inputs for database helper function
333  * #DATA_GetSensorNumberFromTemperatureIndex */
335  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
336  for (uint8_t sensor = 0u; sensor < BS_NR_OF_TEMP_SENSORS_PER_MODULE; sensor++) {
337  uint16_t sensorIndex = (m * BS_NR_OF_TEMP_SENSORS_PER_MODULE) + sensor;
338  TEST_ASSERT_EQUAL(DATA_GetSensorNumberFromTemperatureIndex(sensorIndex), sensor);
339  }
340  }
341 
342  /* Test is function passes if index 0 is passed */
344 
345  /* Test is function asserts if invalid index is passed */
349 }
#define BS_NR_OF_CELL_BLOCKS_PER_MODULE
number of cells per module
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define BS_NR_OF_TEMP_SENSORS_PER_MODULE
number of temperature sensors per battery module
#define BS_NR_OF_MODULES_PER_STRING
number of modules in a string
Database configuration header.
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:76
uint8_t DATA_GetSensorNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns sensor number of passed temperature sensor index.
uint8_t DATA_GetModuleNumberFromVoltageIndex(uint16_t cellIndex)
Returns module number of passed cell index.
uint8_t DATA_GetModuleNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns module number of passed temperature sensor index.
uint8_t DATA_GetStringNumberFromVoltageIndex(uint16_t cellIndex)
Returns string number of passed cell index.
uint8_t DATA_GetStringNumberFromTemperatureIndex(uint16_t sensorIndex)
Returns string number of passed temperature sensor index.
bool DATA_DatabaseEntryUpdatedAtLeastOnce(DATA_BLOCK_HEADER_s dataBlockHeader)
Checks if passed database entry has been updated at least once.
bool DATA_EntryUpdatedPeriodicallyWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval)
Checks if passed database entry has been periodically updated within the time interval.
uint8_t DATA_GetCellNumberFromVoltageIndex(uint16_t cellIndex)
Returns cell number of passed cell index.
bool DATA_EntryUpdatedWithinInterval(DATA_BLOCK_HEADER_s dataBlockHeader, uint32_t timeInterval)
Checks if passed database entry has been updated within the last time interval.
Database module header.
General macros and definitions for the whole platform.
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:129
uint32_t previousTimestamp
Definition: database_cfg.h:121
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
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 testDATA_GetCellNumberFromVoltageIndex(void)
void testDATA_GetSensorNumberFromTemperatureIndex(void)
void testDATA_GetStringNumberFromTemperatureIndex(void)
void testDATA_GetStringNumberFromVoltageIndex(void)
void testDATA_EntryUpdatedPeriodicallyWithinInterval(void)
void setUp(void)
void tearDown(void)
void testDATA_DatabaseEntryUpdatedAtLeastOnce(void)
void testDATA_GetModuleNumberFromTemperatureIndex(void)
void testDATA_GetModuleNumberFromVoltageIndex(void)
void testDATA_EntryUpdatedWithinInterval(void)