foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
test_redundancy.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_redundancy.c
44  * @author foxBMS Team
45  * @date 2020-07-31 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.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 /*========== Definitions and Implementations for Unit Test ==================*/
69 
72 
76 
77 static inline void injectDatabaseEntries(void) {
78  DATA_Read4DataBlocks_ExpectAndReturn(
83  STD_OK);
84  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver0();
85  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver1();
86  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver2();
87  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver3();
88  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver0(&testCellVoltageBase);
89  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver1(&testCellVoltageRedundancy0);
90  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver2(&testCellTemperatureBase);
91  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver3(&testCellTemperatureRedundancy0);
92 }
93 
94 /*========== Setup and Teardown =============================================*/
95 void setUp(void) {
100 }
101 
102 void tearDown(void) {
103 }
104 
105 /*========== Test Cases =====================================================*/
106 /* test for null pointer */
107 
109  /* Measurement has been updated once, after 10ms */
110  uint32_t timestamp = 10u;
111  uint32_t previousTimestamp = 0u;
112  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
113 
114  /* Measurement has been updated twice, first after 10ms, then after 50ms */
115  timestamp = 60u;
116  previousTimestamp = 10u;
117  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
118 
119  /* Measurement has been updated three times, first after 10ms, then after 50ms, then after 10ms */
120  timestamp = 70u;
121  previousTimestamp = 60u;
122  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
123 
124  /* Measurement has never been updated */
125  timestamp = 0u;
126  previousTimestamp = 0u;
127  TEST_ASSERT_FALSE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
128 }
129 
131  /* Always check always if database entry has been updated within the last 100ms */
132  uint32_t timeDifference = 100u;
133 
134  /* Time difference: 50ms -> true */
135  uint32_t timestamp = 50u;
136  uint32_t previousTimestamp = 0u;
137  OS_GetTickCount_ExpectAndReturn(100u);
138  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
139 
140  /* Time difference: 100ms -> true, but never updated */
141  timestamp = 0u;
142  OS_GetTickCount_ExpectAndReturn(100u);
143  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
144 
145  /* Time difference: 101ms -> false */
146  timestamp = 0u;
147  OS_GetTickCount_ExpectAndReturn(101u);
148  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
149 
150  /* Time difference: 63ms -> true */
151  timestamp = 4937u;
152  OS_GetTickCount_ExpectAndReturn(5000u);
153  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
154 
155  /* Time difference: 50ms -> true */
156  timestamp = UINT32_MAX;
157  OS_GetTickCount_ExpectAndReturn(50u);
158  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
159 
160  /* Time difference: 100ms -> true */
161  timestamp = UINT32_MAX - 50u;
162  OS_GetTickCount_ExpectAndReturn(49u);
163  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
164 
165  /* Time difference: 101ms -> false */
166  timestamp = UINT32_MAX - 50u;
167  OS_GetTickCount_ExpectAndReturn(50u);
168  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
169 
170  /* Time difference: UINT32_MAX - 50 -> false */
171  timestamp = 50u;
172  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
173  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
174 }
175 
181 }
182 
188 }
189 
195 }
196 
201 }
202 
208 }
209 
214 }
215 
216 /* test main function */
218  /* inject database entries into function */
220 
221  /* database entries never written - */
222  DATA_EntryUpdatedWithinInterval_IgnoreAndReturn(false);
223  DATA_DatabaseEntryUpdatedAtLeastOnce_IgnoreAndReturn(false);
224 
225  /* tick zero, database starts also with zero --> nothing to do */
226  OS_GetTickCount_IgnoreAndReturn(0);
227 
228  DIAG_Handler_IgnoreAndReturn(STD_OK);
229 
231 }
@ DATA_BLOCK_ID_CELL_VOLTAGE_REDUNDANCY0
Definition: database_cfg.h:99
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:78
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:76
@ DATA_BLOCK_ID_CELL_TEMPERATURE_REDUNDANCY0
Definition: database_cfg.h:100
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:98
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:97
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:77
math library for often used math functions
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
STD_RETURN_TYPE_e TEST_MRC_CalculateCellTemperatureMinMaxAverage(DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperatures, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1214
STD_RETURN_TYPE_e TEST_MRC_CalculateCellVoltageMinMaxAverage(DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1209
STD_RETURN_TYPE_e TEST_MRC_UpdateCellTemperatureValidation(DATA_BLOCK_CELL_TEMPERATURE_s *pCellTemperature, DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperature)
Definition: redundancy.c:1236
bool TEST_MRC_MeasurementUpdatedAtLeastOnce(uint32_t timestamp, uint32_t previousTimestamp)
Definition: redundancy.c:1171
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:1111
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:1230
STD_RETURN_TYPE_e TEST_MRC_UpdateCellVoltageValidation(DATA_BLOCK_CELL_VOLTAGE_s *pCellVoltage, DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages)
Definition: redundancy.c:1225
STD_RETURN_TYPE_e TEST_MRC_MeasurementUpdatedRecently(uint32_t timestamp, uint32_t previousTimestamp, uint32_t timeInterval)
Definition: redundancy.c:1174
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:1219
Header fileS for handling redundancy between redundant cell voltage and cell temperature measurements...
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:146
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:129
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:159
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)