foxBMS - Unit Tests  1.5.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-02-03 (date of last update)
47  * @version v1.5.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 TEST_FILE("redundancy.c")
72 
73 /*========== Definitions and Implementations for Unit Test ==================*/
74 
77 
81 
82 static inline void injectDatabaseEntries(void) {
83  DATA_Read4DataBlocks_ExpectAndReturn(
88  STD_OK);
89  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver0();
90  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver1();
91  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver2();
92  DATA_Read4DataBlocks_IgnoreArg_pDataToReceiver3();
93  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver0(&testCellVoltageBase);
94  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver1(&testCellVoltageRedundancy0);
95  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver2(&testCellTemperatureBase);
96  DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver3(&testCellTemperatureRedundancy0);
97 }
98 
99 /*========== Setup and Teardown =============================================*/
100 void setUp(void) {
105 }
106 
107 void tearDown(void) {
108 }
109 
110 /*========== Test Cases =====================================================*/
111 /* test for null pointer */
112 
114  /* Measurement has been updated once, after 10ms */
115  uint32_t timestamp = 10u;
116  uint32_t previousTimestamp = 0u;
117  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
118 
119  /* Measurement has been updated twice, first after 10ms, then after 50ms */
120  timestamp = 60u;
121  previousTimestamp = 10u;
122  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
123 
124  /* Measurement has been updated three times, first after 10ms, then after 50ms, then after 10ms */
125  timestamp = 70u;
126  previousTimestamp = 60u;
127  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
128 
129  /* Measurement has never been updated */
130  timestamp = 0u;
131  previousTimestamp = 0u;
132  TEST_ASSERT_FALSE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
133 }
134 
136  /* Always check always if database entry has been updated within the last 100ms */
137  uint32_t timeDifference = 100u;
138 
139  /* Time difference: 50ms -> true */
140  uint32_t timestamp = 50u;
141  uint32_t previousTimestamp = 0u;
142  OS_GetTickCount_ExpectAndReturn(100u);
143  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
144 
145  /* Time difference: 100ms -> true, but never updated */
146  timestamp = 0u;
147  OS_GetTickCount_ExpectAndReturn(100u);
148  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
149 
150  /* Time difference: 101ms -> false */
151  timestamp = 0u;
152  OS_GetTickCount_ExpectAndReturn(101u);
153  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
154 
155  /* Time difference: 63ms -> true */
156  timestamp = 4937u;
157  OS_GetTickCount_ExpectAndReturn(5000u);
158  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
159 
160  /* Time difference: 50ms -> true */
161  timestamp = UINT32_MAX;
162  OS_GetTickCount_ExpectAndReturn(50u);
163  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
164 
165  /* Time difference: 100ms -> true */
166  timestamp = UINT32_MAX - 50u;
167  OS_GetTickCount_ExpectAndReturn(49u);
168  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
169 
170  /* Time difference: 101ms -> false */
171  timestamp = UINT32_MAX - 50u;
172  OS_GetTickCount_ExpectAndReturn(50u);
173  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
174 
175  /* Time difference: UINT32_MAX - 50 -> false */
176  timestamp = 50u;
177  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
178  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
179 }
180 
186 }
187 
193 }
194 
200 }
201 
206 }
207 
213 }
214 
219 }
220 
221 /* test main function */
223  /* inject database entries into function */
225 
226  /* database entries never written - */
227  DATA_EntryUpdatedWithinInterval_IgnoreAndReturn(false);
228  DATA_DatabaseEntryUpdatedAtLeastOnce_IgnoreAndReturn(false);
229 
230  /* tick zero, database starts also with zero --> nothing to do */
231  OS_GetTickCount_IgnoreAndReturn(0);
232 
233  DIAG_Handler_IgnoreAndReturn(STD_OK);
234 
236 }
@ DATA_BLOCK_ID_CELL_VOLTAGE_REDUNDANCY0
Definition: database_cfg.h:102
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:81
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:79
@ DATA_BLOCK_ID_CELL_TEMPERATURE_REDUNDANCY0
Definition: database_cfg.h:103
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:101
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:100
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:80
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:1220
STD_RETURN_TYPE_e TEST_MRC_CalculateCellVoltageMinMaxAverage(DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1215
STD_RETURN_TYPE_e TEST_MRC_UpdateCellTemperatureValidation(DATA_BLOCK_CELL_TEMPERATURE_s *pCellTemperature, DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperature)
Definition: redundancy.c:1242
bool TEST_MRC_MeasurementUpdatedAtLeastOnce(uint32_t timestamp, uint32_t previousTimestamp)
Definition: redundancy.c:1177
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:1117
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:1236
STD_RETURN_TYPE_e TEST_MRC_UpdateCellVoltageValidation(DATA_BLOCK_CELL_VOLTAGE_s *pCellVoltage, DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages)
Definition: redundancy.c:1231
STD_RETURN_TYPE_e TEST_MRC_MeasurementUpdatedRecently(uint32_t timestamp, uint32_t previousTimestamp, uint32_t timeInterval)
Definition: redundancy.c:1180
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:1225
Header fileS for handling redundancy between redundant cell voltage and cell temperature measurements...
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:149
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:132
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:122
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:162
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)