foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
test_redundancy.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_redundancy.c
44  * @author foxBMS Team
45  * @date 2020-07-31 (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 Test of the redundancy module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockbms.h"
58 #include "Mockdatabase.h"
59 #include "Mockdatabase_helper.h"
60 #include "Mockdiag.h"
61 #include "Mockos.h"
62 #include "Mockplausibility.h"
63 
64 #include "foxmath.h"
65 #include "redundancy.h"
66 #include "test_assert_helper.h"
67 
68 #include <stdbool.h>
69 #include <stdint.h>
70 
71 /*========== Unit Testing Framework Directives ==============================*/
72 TEST_SOURCE_FILE("redundancy.c")
73 
74 TEST_INCLUDE_PATH("../../src/app/application/bms")
75 TEST_INCLUDE_PATH("../../src/app/application/plausibility")
76 TEST_INCLUDE_PATH("../../src/app/application/redundancy")
77 TEST_INCLUDE_PATH("../../src/app/driver/config")
78 TEST_INCLUDE_PATH("../../src/app/driver/contactor")
79 TEST_INCLUDE_PATH("../../src/app/driver/foxmath")
80 TEST_INCLUDE_PATH("../../src/app/driver/sps")
81 TEST_INCLUDE_PATH("../../src/app/engine/database")
82 TEST_INCLUDE_PATH("../../src/app/engine/diag")
83 TEST_INCLUDE_PATH("../../src/app/task/config")
84 
85 /*========== Definitions and Implementations for Unit Test ==================*/
86 
89 
93 
94 static inline void injectDatabaseEntries(void) {
95  DATA_Read4DataBlocks_ExpectAndReturn(
100  STD_OK);
101  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver0();
102  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver1();
103  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver2();
104  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver3();
105  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver0(&testCellVoltageBase);
106  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver1(&testCellVoltageRedundancy0);
107  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver2(&testCellTemperatureBase);
108  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver3(&testCellTemperatureRedundancy0);
109 }
110 
111 /*========== Setup and Teardown =============================================*/
112 void setUp(void) {
117 }
118 
119 void tearDown(void) {
120 }
121 
122 /*========== Test Cases =====================================================*/
123 /* test for null pointer */
124 
126  /* Measurement has been updated once, after 10ms */
127  uint32_t timestamp = 10u;
128  uint32_t previousTimestamp = 0u;
129  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
130 
131  /* Measurement has been updated twice, first after 10ms, then after 50ms */
132  timestamp = 60u;
133  previousTimestamp = 10u;
134  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
135 
136  /* Measurement has been updated three times, first after 10ms, then after 50ms, then after 10ms */
137  timestamp = 70u;
138  previousTimestamp = 60u;
139  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
140 
141  /* Measurement has never been updated */
142  timestamp = 0u;
143  previousTimestamp = 0u;
144  TEST_ASSERT_FALSE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
145 }
146 
148  /* Always check always if database entry has been updated within the last 100ms */
149  uint32_t timeDifference = 100u;
150 
151  /* Time difference: 50ms -> true */
152  uint32_t timestamp = 50u;
153  uint32_t previousTimestamp = 0u;
154  OS_GetTickCount_ExpectAndReturn(100u);
155  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
156 
157  /* Time difference: 100ms -> true, but never updated */
158  timestamp = 0u;
159  OS_GetTickCount_ExpectAndReturn(100u);
160  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
161 
162  /* Time difference: 101ms -> false */
163  timestamp = 0u;
164  OS_GetTickCount_ExpectAndReturn(101u);
165  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
166 
167  /* Time difference: 63ms -> true */
168  timestamp = 4937u;
169  OS_GetTickCount_ExpectAndReturn(5000u);
170  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
171 
172  /* Time difference: 50ms -> true */
173  timestamp = UINT32_MAX;
174  OS_GetTickCount_ExpectAndReturn(50u);
175  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
176 
177  /* Time difference: 100ms -> true */
178  timestamp = UINT32_MAX - 50u;
179  OS_GetTickCount_ExpectAndReturn(49u);
180  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
181 
182  /* Time difference: 101ms -> false */
183  timestamp = UINT32_MAX - 50u;
184  OS_GetTickCount_ExpectAndReturn(50u);
185  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
186 
187  /* Time difference: UINT32_MAX - 50 -> false */
188  timestamp = 50u;
189  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
190  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
191 }
192 
198 }
199 
205 }
206 
212 }
213 
218 }
219 
225 }
226 
231 }
232 
233 /* test main function */
235  /* inject database entries into function */
237 
238  /* database entries never written - */
239  DATA_EntryUpdatedWithinInterval_IgnoreAndReturn(false);
240  DATA_DatabaseEntryUpdatedAtLeastOnce_IgnoreAndReturn(false);
241 
242  /* tick zero, database starts also with zero --> nothing to do */
243  OS_GetTickCount_IgnoreAndReturn(0);
244 
245  DIAG_Handler_IgnoreAndReturn(STD_OK);
246 
248 }
@ DATA_BLOCK_ID_CELL_VOLTAGE_REDUNDANCY0
Definition: database_cfg.h:91
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:99
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:89
@ DATA_BLOCK_ID_CELL_TEMPERATURE_REDUNDANCY0
Definition: database_cfg.h:88
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:87
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:90
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:86
static DIAG_DIAGNOSIS_STATE_s diag
Definition: diag.c:71
math library for often used math functions
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
STD_RETURN_TYPE_e TEST_MRC_CalculateCellTemperatureMinMaxAverage(DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperatures, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1206
STD_RETURN_TYPE_e TEST_MRC_CalculateCellVoltageMinMaxAverage(DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1201
STD_RETURN_TYPE_e TEST_MRC_UpdateCellTemperatureValidation(DATA_BLOCK_CELL_TEMPERATURE_s *pCellTemperature, DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperature)
Definition: redundancy.c:1228
bool TEST_MRC_MeasurementUpdatedAtLeastOnce(uint32_t timestamp, uint32_t previousTimestamp)
Definition: redundancy.c:1163
STD_RETURN_TYPE_e MRC_ValidateAfeMeasurement(void)
Function to validate the measurement between redundant measurement values for cell voltage and cell t...
Definition: redundancy.c:1103
STD_RETURN_TYPE_e TEST_MRC_ValidateCellTemperature(DATA_BLOCK_CELL_TEMPERATURE_s *pCelltemperatureBase, DATA_BLOCK_CELL_TEMPERATURE_s *pCelltemperatureRedundancy0, DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperatures)
Definition: redundancy.c:1222
STD_RETURN_TYPE_e TEST_MRC_UpdateCellVoltageValidation(DATA_BLOCK_CELL_VOLTAGE_s *pCellVoltage, DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages)
Definition: redundancy.c:1217
STD_RETURN_TYPE_e TEST_MRC_MeasurementUpdatedRecently(uint32_t timestamp, uint32_t previousTimestamp, uint32_t timeInterval)
Definition: redundancy.c:1166
STD_RETURN_TYPE_e TEST_MRC_ValidateCellVoltage(DATA_BLOCK_CELL_VOLTAGE_s *pCellVoltageBase, DATA_BLOCK_CELL_VOLTAGE_s *pCellVoltageRedundancy0, DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages)
Definition: redundancy.c:1211
Header fileS for handling redundancy between redundant cell voltage and cell temperature measurements...
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:153
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:135
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:125
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:167
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 testMRC_UpdateCellVoltageValidationNullPointer(void)
DATA_BLOCK_CELL_VOLTAGE_s testCellVoltageRedundancy0
void testMRC_CalculateCellVoltageMinMaxAverageNullPointer(void)
DATA_BLOCK_CELL_VOLTAGE_s testCellVoltageBase
void testMRC_CalculateCellTemperatureMinMaxAverageNullPointer(void)
void testMRC_ValidateCellVoltageNullPointer(void)
DATA_BLOCK_CELL_TEMPERATURE_s testCellTemperatureBase
void setUp(void)
void testMRC_ValidateCellTemperatureNullPointer(void)
void tearDown(void)
void testMRC_MeasurementUpdatedAtLeastOnce(void)
void testMRC_MeasurementUpdatedRecently(void)
void testMRC_AfeMeasurementValidationTickZeroNothingToDo(void)
void testMRC_UpdateCellTemperatureValidationNullPointer(void)
DATA_BLOCK_CELL_TEMPERATURE_s testCellTemperatureRedundancy0
static void injectDatabaseEntries(void)