foxBMS - Unit Tests  1.6.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-10-12 (date of last update)
47  * @version v1.6.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 /*========== Unit Testing Framework Directives ==============================*/
68 
69 /*========== Definitions and Implementations for Unit Test ==================*/
70 
71 /*========== Setup and Teardown =============================================*/
72 void setUp(void) {
73 }
74 
75 void tearDown(void) {
76 }
77 
78 /*========== Test Cases =====================================================*/
79 /** This function tests various inputs for helper function
80  * #DATA_DatabaseEntryUpdatedAtLeastOnce */
83 
84  /* Database entry has been updated once, after 10ms */
85  databaseEntry.header.timestamp = 10u;
86  databaseEntry.header.previousTimestamp = 0u;
87  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
88 
89  /* Database entry has been updated twice, first after 10ms, then after 50ms */
90  databaseEntry.header.timestamp = 60u;
91  databaseEntry.header.previousTimestamp = 10u;
92  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
93 
94  /* Database entry has been updated three times, first after 10ms, then after 50ms, then after 10ms */
95  databaseEntry.header.timestamp = 70u;
96  databaseEntry.header.previousTimestamp = 60u;
97  TEST_ASSERT_TRUE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
98 
99  /* Database entry has never been updated */
100  databaseEntry.header.timestamp = 0u;
101  databaseEntry.header.previousTimestamp = 0u;
102  TEST_ASSERT_FALSE(DATA_DatabaseEntryUpdatedAtLeastOnce(databaseEntry.header));
103 }
104 
105 /** This function tests various inputs for database helper function
106  * #DATA_EntryUpdatedWithinInterval */
109 
110  /* Always check always if database entry has been updated within the last 100ms */
111  uint32_t timeDifference = 100u;
112 
113  /* Time difference: 50ms -> true */
114  databaseEntry.header.timestamp = 50u;
115  OS_GetTickCount_ExpectAndReturn(100u);
116  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
117 
118  /* Time difference: 100ms -> true, but never updated */
119  databaseEntry.header.timestamp = 0u;
120  OS_GetTickCount_ExpectAndReturn(100u);
121  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
122 
123  /* Time difference: 101ms -> false */
124  databaseEntry.header.timestamp = 0u;
125  OS_GetTickCount_ExpectAndReturn(101u);
126  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
127 
128  /* Time difference: 63ms -> true */
129  databaseEntry.header.timestamp = 4937u;
130  OS_GetTickCount_ExpectAndReturn(5000u);
131  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
132 
133  /* Time difference: 50ms -> true */
134  databaseEntry.header.timestamp = UINT32_MAX;
135  OS_GetTickCount_ExpectAndReturn(50u);
136  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
137 
138  /* Time difference: 100ms -> true */
139  databaseEntry.header.timestamp = UINT32_MAX - 50u;
140  OS_GetTickCount_ExpectAndReturn(49u);
141  TEST_ASSERT_TRUE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
142 
143  /* Time difference: 101ms -> false */
144  databaseEntry.header.timestamp = UINT32_MAX - 50u;
145  OS_GetTickCount_ExpectAndReturn(50u);
146  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
147 
148  /* Time difference: UINT32_MAX - 50 -> false */
149  databaseEntry.header.timestamp = 50u;
150  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
151  TEST_ASSERT_FALSE(DATA_EntryUpdatedWithinInterval(databaseEntry.header, timeDifference));
152 }
153 
154 /** This function tests various inputs for database helper function
155  * #DATA_EntryUpdatedPeriodicallyWithinInterval */
158 
159  /* Always check always if database entry has been periodically updated within the last 100ms */
160  uint32_t timeDifference = 100u;
161 
162  /* Time difference timestamp - systick: 40ms -> true
163  * Time difference timestamp - previous timestamp: 50ms -> true */
164  databaseEntry.header.timestamp = 60u;
165  databaseEntry.header.previousTimestamp = 10u;
166  OS_GetTickCount_ExpectAndReturn(100u);
167  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
168 
169  /* Within time interval but never updated -> false */
170  databaseEntry.header.timestamp = 0u;
171  databaseEntry.header.previousTimestamp = 0u;
172  OS_GetTickCount_ExpectAndReturn(50u);
173  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
174 
175  /* Time difference timestamp - systick: 100ms -> true
176  * Time difference timestamp - previous timestamp: 40ms -> true */
177  databaseEntry.header.timestamp = 80u;
178  databaseEntry.header.previousTimestamp = 40u;
179  OS_GetTickCount_ExpectAndReturn(180u);
180  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
181 
182  /* Time difference timestamp - systick: 101ms -> false
183  * Time difference timestamp - previous timestamp: 40ms -> true */
184  databaseEntry.header.timestamp = 80u;
185  databaseEntry.header.previousTimestamp = 40u;
186  OS_GetTickCount_ExpectAndReturn(181);
187  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
188 
189  /* Time difference timestamp - systick: 50ms -> true
190  * Time difference timestamp - previous timestamp: 110ms -> false */
191  databaseEntry.header.timestamp = 150u;
192  databaseEntry.header.previousTimestamp = 40u;
193  OS_GetTickCount_ExpectAndReturn(200u);
194  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
195 
196  /* Time difference timestamp - systick: 100ms -> true
197  * Time difference timestamp - previous timestamp: 100ms -> true */
198  databaseEntry.header.timestamp = 150u;
199  databaseEntry.header.previousTimestamp = 50u;
200  OS_GetTickCount_ExpectAndReturn(250u);
201  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
202 
203  /* Time difference timestamp - systick: 100ms -> true
204  * Time difference timestamp - previous timestamp: 100ms -> true */
205  databaseEntry.header.timestamp = UINT32_MAX - 50u;
206  databaseEntry.header.previousTimestamp = UINT32_MAX - 150u;
207  OS_GetTickCount_ExpectAndReturn(49u);
208  TEST_ASSERT_TRUE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
209 
210  /* Time difference timestamp - systick: 101ms -> false
211  * Time difference timestamp - previous timestamp: 100ms -> true */
212  databaseEntry.header.timestamp = UINT32_MAX - 50u;
213  databaseEntry.header.previousTimestamp = UINT32_MAX - 150u;
214  OS_GetTickCount_ExpectAndReturn(50u);
215  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
216 
217  /* Time difference timestamp - systick: 100ms -> true
218  * Time difference timestamp - previous timestamp: 100ms -> true */
219  databaseEntry.header.timestamp = 49u;
220  databaseEntry.header.previousTimestamp = UINT32_MAX - 50u;
221  OS_GetTickCount_ExpectAndReturn(150u);
222  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
223 
224  /* Time difference timestamp - systick: 100ms -> true
225  * Time difference timestamp - previous timestamp: 101ms -> false */
226  databaseEntry.header.timestamp = UINT32_MAX - 50u;
227  databaseEntry.header.previousTimestamp = UINT32_MAX - 151u;
228  OS_GetTickCount_ExpectAndReturn(49u);
229  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
230 
231  /* Time difference timestamp - systick: 100ms -> true
232  * Time difference timestamp - previous timestamp: 101ms -> false */
233  databaseEntry.header.timestamp = 50u;
234  databaseEntry.header.previousTimestamp = UINT32_MAX - 50u;
235  OS_GetTickCount_ExpectAndReturn(150u);
236  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
237 
238  /* Time difference: UINT32_MAX - 50 -> false */
239  databaseEntry.header.timestamp = 50u;
240  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
241  TEST_ASSERT_FALSE(DATA_EntryUpdatedPeriodicallyWithinInterval(databaseEntry.header, timeDifference));
242 }
243 
244 /** This function tests various inputs for database helper function
245  * #DATA_GetStringNumberFromVoltageIndex */
247  /* Last cell in string 0 */
248  uint16_t voltageIndex = (BS_NR_OF_CELL_BLOCKS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
249  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromVoltageIndex(voltageIndex), 0u);
250 
251  /* Last cell in last string */
253  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromVoltageIndex(voltageIndex), BS_NR_OF_STRINGS - 1u);
254 
255  /* Test is function passes if index 0 is passed */
257 
258  /* Test is function asserts if invalid index is passed */
262 }
263 
264 /** This function tests various inputs for database helper function
265  * #DATA_GetModuleNumberFromVoltageIndex */
267  uint16_t voltageIndex = (BS_NR_OF_CELL_BLOCKS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
268  TEST_ASSERT_EQUAL(DATA_GetModuleNumberFromVoltageIndex(voltageIndex), BS_NR_OF_MODULES_PER_STRING - 1u);
269 
270  /* Test is function passes if index 0 is passed */
272 
273  /* Test is function asserts if invalid index is passed */
277 }
278 
279 /** This function tests various inputs for database helper function
280  * #testDATA_GetCellNumberFromVoltageIndex */
282  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
283  for (uint8_t cb = 0u; cb < BS_NR_OF_CELL_BLOCKS_PER_MODULE; cb++) {
284  uint16_t voltageIndex = (m * BS_NR_OF_CELL_BLOCKS_PER_MODULE) + cb;
285  TEST_ASSERT_EQUAL(DATA_GetCellNumberFromVoltageIndex(voltageIndex), cb);
286  }
287  }
288 
289  /* Test is function passes if index 0 is passed */
291 
292  /* Test is function asserts if invalid index is passed */
296 }
297 
298 /** This function tests various inputs for database helper function
299  * #DATA_GetStringNumberFromTemperatureIndex */
301  /* last sensor in string 0 */
302  uint16_t sensorIndex = (BS_NR_OF_TEMP_SENSORS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
303  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromTemperatureIndex(sensorIndex), 0u);
304 
305  /* last sensor in last string */
307  TEST_ASSERT_EQUAL(DATA_GetStringNumberFromTemperatureIndex(sensorIndex), BS_NR_OF_STRINGS - 1u);
308 
309  /* Test is function passes if index 0 is passed */
311 
312  /* Test is function asserts if invalid index is passed */
316 }
317 
318 /** This function tests various inputs for database helper function
319  * #DATA_GetModuleNumberFromTemperatureIndex */
321  uint16_t sensorIndex = (BS_NR_OF_TEMP_SENSORS_PER_MODULE * BS_NR_OF_MODULES_PER_STRING) - 1u;
322  TEST_ASSERT_EQUAL(DATA_GetModuleNumberFromTemperatureIndex(sensorIndex), BS_NR_OF_MODULES_PER_STRING - 1u);
323 
324  /* Test is function passes if index 0 is passed */
326 
327  /* Test is function asserts if invalid index is passed */
331 }
332 
333 /** This function tests various inputs for database helper function
334  * #DATA_GetSensorNumberFromTemperatureIndex */
336  for (uint8_t m = 0u; m < BS_NR_OF_MODULES_PER_STRING; m++) {
337  for (uint8_t sensor = 0u; sensor < BS_NR_OF_TEMP_SENSORS_PER_MODULE; sensor++) {
338  uint16_t sensorIndex = (m * BS_NR_OF_TEMP_SENSORS_PER_MODULE) + sensor;
339  TEST_ASSERT_EQUAL(DATA_GetSensorNumberFromTemperatureIndex(sensorIndex), sensor);
340  }
341  }
342 
343  /* Test is function passes if index 0 is passed */
345 
346  /* Test is function asserts if invalid index is passed */
350 }
#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:89
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:135
uint32_t previousTimestamp
Definition: database_cfg.h:127
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:125
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)