foxBMS - Unit Tests  1.5.0
The foxBMS Unit Tests API Documentation
test_database_helper.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_database_helper.c
44  * @author foxBMS Team
45  * @date 2021-05-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 Tests for the database helper functions
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 
57 #include "unity.h"
58 #include "Mockfassert.h"
59 #include "Mockmpu_prototypes.h"
60 #include "Mockos.h"
61 
62 #include "database_cfg.h"
63 
64 #include "database_helper.h"
65 #include "test_assert_helper.h"
66 
67 /*========== Definitions and Implementations for Unit Test ==================*/
68 
69 /*========== Setup and Teardown =============================================*/
70 void setUp(void) {
71 }
72 
73 void tearDown(void) {
74 }
75 
76 /*========== Test Cases =====================================================*/
77 /** This function tests various inputs for helper function
78  * #DATA_DatabaseEntryUpdatedAtLeastOnce */
81 
82  /* Database entry has been updated once, after 10ms */
83  databaseEntry.header.timestamp = 10u;
84  databaseEntry.header.previousTimestamp = 0u;
85  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
86 
87  /* Database entry has been updated twice, first after 10ms, then after 50ms */
88  databaseEntry.header.timestamp = 60u;
89  databaseEntry.header.previousTimestamp = 10u;
90  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
91 
92  /* Database entry has been updated three times, first after 10ms, then after 50ms, then after 10ms */
93  databaseEntry.header.timestamp = 70u;
94  databaseEntry.header.previousTimestamp = 60u;
95  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
96 
97  /* Database entry has never been updated */
98  databaseEntry.header.timestamp = 0u;
99  databaseEntry.header.previousTimestamp = 0u;
100  TEST_ASSERT_FALSE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
101 }
102 
103 /** This function tests various inputs for database helper function
104  * #DATA_EntryUpdatedWithinInterval */
107 
108  /* Always check always if database entry has been updated within the last 100ms */
109  uint32_t timeDifference = 100u;
110 
111  /* Time difference: 50ms -> true */
112  databaseEntry.header.timestamp = 50u;
113  OS_GetTickCount_ExpectAndReturn(100u);
114  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
115 
116  /* Time difference: 100ms -> true, but never updated */
117  databaseEntry.header.timestamp = 0u;
118  OS_GetTickCount_ExpectAndReturn(100u);
119  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
120 
121  /* Time difference: 101ms -> false */
122  databaseEntry.header.timestamp = 0u;
123  OS_GetTickCount_ExpectAndReturn(101u);
124  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
125 
126  /* Time difference: 63ms -> true */
127  databaseEntry.header.timestamp = 4937u;
128  OS_GetTickCount_ExpectAndReturn(5000u);
129  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
130 
131  /* Time difference: 50ms -> true */
132  databaseEntry.header.timestamp = UINT32_MAX;
133  OS_GetTickCount_ExpectAndReturn(50u);
134  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
135 
136  /* Time difference: 100ms -> true */
137  databaseEntry.header.timestamp = UINT32_MAX - 50u;
138  OS_GetTickCount_ExpectAndReturn(49u);
139  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
140 
141  /* Time difference: 101ms -> false */
142  databaseEntry.header.timestamp = UINT32_MAX - 50u;
143  OS_GetTickCount_ExpectAndReturn(50u);
144  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
145 
146  /* Time difference: UINT32_MAX - 50 -> false */
147  databaseEntry.header.timestamp = 50u;
148  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
149  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
150 }
151 
152 /** This function tests various inputs for database helper function
153  * #DATA_EntryUpdatedPeriodicallyWithinInterval */
156 
157  /* Always check always if database entry has been periodically updated within the last 100ms */
158  uint32_t timeDifference = 100u;
159 
160  /* Time difference timestamp - systick: 40ms -> true
161  * Time difference timestamp - previous timestamp: 50ms -> true */
162  databaseEntry.header.timestamp = 60u;
163  databaseEntry.header.previousTimestamp = 10u;
164  OS_GetTickCount_ExpectAndReturn(100u);
165  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
166 
167  /* Within time interval but never updated -> false */
168  databaseEntry.header.timestamp = 0u;
169  databaseEntry.header.previousTimestamp = 0u;
170  OS_GetTickCount_ExpectAndReturn(50u);
171  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
172 
173  /* Time difference timestamp - systick: 100ms -> true
174  * Time difference timestamp - previous timestamp: 40ms -> true */
175  databaseEntry.header.timestamp = 80u;
176  databaseEntry.header.previousTimestamp = 40u;
177  OS_GetTickCount_ExpectAndReturn(180u);
178  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
179 
180  /* Time difference timestamp - systick: 101ms -> false
181  * Time difference timestamp - previous timestamp: 40ms -> true */
182  databaseEntry.header.timestamp = 80u;
183  databaseEntry.header.previousTimestamp = 40u;
184  OS_GetTickCount_ExpectAndReturn(181);
185  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
186 
187  /* Time difference timestamp - systick: 50ms -> true
188  * Time difference timestamp - previous timestamp: 110ms -> false */
189  databaseEntry.header.timestamp = 150u;
190  databaseEntry.header.previousTimestamp = 40u;
191  OS_GetTickCount_ExpectAndReturn(200u);
192  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
193 
194  /* Time difference timestamp - systick: 100ms -> true
195  * Time difference timestamp - previous timestamp: 100ms -> true */
196  databaseEntry.header.timestamp = 150u;
197  databaseEntry.header.previousTimestamp = 50u;
198  OS_GetTickCount_ExpectAndReturn(250u);
199  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
200 
201  /* Time difference timestamp - systick: 100ms -> true
202  * Time difference timestamp - previous timestamp: 100ms -> true */
203  databaseEntry.header.timestamp = UINT32_MAX - 50u;
204  databaseEntry.header.previousTimestamp = UINT32_MAX - 150u;
205  OS_GetTickCount_ExpectAndReturn(49u);
206  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
207 
208  /* Time difference timestamp - systick: 101ms -> false
209  * Time difference timestamp - previous timestamp: 100ms -> true */
210  databaseEntry.header.timestamp = UINT32_MAX - 50u;
211  databaseEntry.header.previousTimestamp = UINT32_MAX - 150u;
212  OS_GetTickCount_ExpectAndReturn(50u);
213  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
214 
215  /* Time difference timestamp - systick: 100ms -> true
216  * Time difference timestamp - previous timestamp: 100ms -> true */
217  databaseEntry.header.timestamp = 49u;
218  databaseEntry.header.previousTimestamp = UINT32_MAX - 50u;
219  OS_GetTickCount_ExpectAndReturn(150u);
220  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
221 
222  /* Time difference timestamp - systick: 100ms -> true
223  * Time difference timestamp - previous timestamp: 101ms -> false */
224  databaseEntry.header.timestamp = UINT32_MAX - 50u;
225  databaseEntry.header.previousTimestamp = UINT32_MAX - 151u;
226  OS_GetTickCount_ExpectAndReturn(49u);
227  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
228 
229  /* Time difference timestamp - systick: 100ms -> true
230  * Time difference timestamp - previous timestamp: 101ms -> false */
231  databaseEntry.header.timestamp = 50u;
232  databaseEntry.header.previousTimestamp = UINT32_MAX - 50u;
233  OS_GetTickCount_ExpectAndReturn(150u);
234  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
235 
236  /* Time difference: UINT32_MAX - 50 -> false */
237  databaseEntry.header.timestamp = 50u;
238  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
239  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
240 }
241 
242 /** This function tests various inputs for database helper function
243  * #DATA_GetStringNumberFromVoltageIndex */
245  /* Last cell in string 0 */
246  uint16_t voltageIndex = (BS_NR_OF_CELL_BLOCKS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
247  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromVoltageIndex(voltageIndex), 0u);
248 
249  /* Last cell in last string */
251  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromVoltageIndex(voltageIndex), BS_NR_OF_STRINGS - 1u);
252 
253  /* Test is function passes if index 0 is passed */
255 
256  /* Test is function asserts if invalid index is passed */
260 }
261 
262 /** This function tests various inputs for database helper function
263  * #DATA_GetModuleNumberFromVoltageIndex */
265  uint16_t voltageIndex = (BS_NR_OF_CELL_BLOCKS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
266  TEST_ASSERT_EQUAL(DATA_GetModuleNumberFromVoltageIndex(voltageIndex), BS_NR_OF_MODULES_PER_STRING - 1u);
267 
268  /* Test is function passes if index 0 is passed */
270 
271  /* Test is function asserts if invalid index is passed */
275 }
276 
277 /** This function tests various inputs for database helper function
278  * #testDATA_GetCellNumberFromVoltageIndex */
280  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
281  for (uint8_t c = 0u; c < BS_NR_OF_CELL_BLOCKS_PER_MODULE; c++) {
282  uint16_t voltageIndex = (m * BS_NR_OF_CELL_BLOCKS_PER_MODULE) + c;
283  TEST_ASSERT_EQUAL(DATA_GetCellNumberFromVoltageIndex(voltageIndex), c);
284  }
285  }
286 
287  /* Test is function passes if index 0 is passed */
289 
290  /* Test is function asserts if invalid index is passed */
294 }
295 
296 /** This function tests various inputs for database helper function
297  * #DATA_GetStringNumberFromTemperatureIndex */
299  /* last sensor in string 0 */
300  uint16_t sensorIndex = (BS_NR_OF_TEMP_SENSORS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
301  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromTemperatureIndex(sensorIndex), 0u);
302 
303  /* last sensor in last string */
305  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromTemperatureIndex(sensorIndex), BS_NR_OF_STRINGS - 1u);
306 
307  /* Test is function passes if index 0 is passed */
309 
310  /* Test is function asserts if invalid index is passed */
314 }
315 
316 /** This function tests various inputs for database helper function
317  * #DATA_GetModuleNumberFromTemperatureIndex */
319  uint16_t sensorIndex = (BS_NR_OF_TEMP_SENSORS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
320  TEST_ASSERT_EQUAL(DATA_GetModuleNumberFromTemperatureIndex(sensorIndex), BS_NR_OF_MODULES_PER_STRING - 1u);
321 
322  /* Test is function passes if index 0 is passed */
324 
325  /* Test is function asserts if invalid index is passed */
329 }
330 
331 /** This function tests various inputs for database helper function
332  * #DATA_GetSensorNumberFromTemperatureIndex */
334  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
335  for (uint8_t sensor = 0u; sensor < BS_NR_OF_TEMP_SENSORS_PER_MODULE; sensor++) {
336  uint16_t sensorIndex = (m * BS_NR_OF_TEMP_SENSORS_PER_MODULE) + sensor;
337  TEST_ASSERT_EQUAL(DATA_GetSensorNumberFromTemperatureIndex(sensorIndex), sensor);
338  }
339  }
340 
341  /* Test is function passes if index 0 is passed */
343 
344  /* Test is function asserts if invalid index is passed */
348 }
#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:79
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.
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:132
uint32_t previousTimestamp
Definition: database_cfg.h:124
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:122
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)