foxBMS - Unit Tests  1.1.2
The foxBMS Unit Tests API Documentation
test_redundancy.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, 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 2020-07-31 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Test of the redundancy module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "unity.h"
56 #include "Mockbms.h"
57 #include "Mockdatabase.h"
58 #include "Mockdatabase_helper.h"
59 #include "Mockdiag.h"
60 #include "Mockos.h"
61 #include "Mockplausibility.h"
62 
63 #include "foxmath.h"
64 #include "redundancy.h"
65 #include "test_assert_helper.h"
66 
67 /*========== Definitions and Implementations for Unit Test ==================*/
68 
71 
75 
76 static inline void injectDatabaseEntries(void) {
77  DATA_Read_4_DataBlocks_ExpectAndReturn(
82  STD_OK);
83  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver0();
84  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver1();
85  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver2();
86  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver3();
87  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver0(&testCellvoltageBase);
88  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver1(&testCellvoltageRedundancy0);
89  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver2(&testCelltemperatureBase);
90  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver3(&testCelltemperatureRedundancy0);
91 }
92 
93 /*========== Setup and Teardown =============================================*/
94 void setUp(void) {
99 }
100 
101 void tearDown(void) {
102 }
103 
104 /*========== Test Cases =====================================================*/
105 /* test for null pointer */
106 
108  /* Measurement has been updated once, after 10ms */
109  uint32_t timestamp = 10u;
110  uint32_t previousTimestamp = 0u;
111  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
112 
113  /* Measurement has been updated twice, first after 10ms, then after 50ms */
114  timestamp = 60u;
115  previousTimestamp = 10u;
116  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
117 
118  /* Measurement has been updated three times, first after 10ms, then after 50ms, then after 10ms */
119  timestamp = 70u;
120  previousTimestamp = 60u;
121  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
122 
123  /* Measurement has never been updated */
124  timestamp = 0u;
125  previousTimestamp = 0u;
126  TEST_ASSERT_FALSE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
127 }
128 
130  /* Always check always if database entry has been updated within the last 100ms */
131  uint32_t timeDifference = 100u;
132 
133  /* Time difference: 50ms -> true */
134  uint32_t timestamp = 50u;
135  uint32_t previousTimestamp = 0u;
136  OS_GetTickCount_ExpectAndReturn(100u);
137  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
138 
139  /* Time difference: 100ms -> true, but never updated */
140  timestamp = 0u;
141  OS_GetTickCount_ExpectAndReturn(100u);
142  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
143 
144  /* Time difference: 101ms -> false */
145  timestamp = 0u;
146  OS_GetTickCount_ExpectAndReturn(101u);
147  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
148 
149  /* Time difference: 63ms -> true */
150  timestamp = 4937u;
151  OS_GetTickCount_ExpectAndReturn(5000u);
152  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
153 
154  /* Time difference: 50ms -> true */
155  timestamp = UINT32_MAX;
156  OS_GetTickCount_ExpectAndReturn(50u);
157  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
158 
159  /* Time difference: 100ms -> true */
160  timestamp = UINT32_MAX - 50u;
161  OS_GetTickCount_ExpectAndReturn(49u);
162  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
163 
164  /* Time difference: 101ms -> false */
165  timestamp = UINT32_MAX - 50u;
166  OS_GetTickCount_ExpectAndReturn(50u);
167  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
168 
169  /* Time difference: UINT32_MAX - 50 -> false */
170  timestamp = 50u;
171  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
172  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
173 }
174 
180 }
181 
187 }
188 
194 }
195 
200 }
201 
207 }
208 
213 }
214 
215 /* test main function */
217  /* inject database entries into function */
219 
220  /* database entries never written - */
221  DATA_EntryUpdatedWithinInterval_IgnoreAndReturn(false);
222  DATA_DatabaseEntryUpdatedAtLeastOnce_IgnoreAndReturn(false);
223 
224  /* tick zero, database starts also with zero --> nothing to do */
225  OS_GetTickCount_IgnoreAndReturn(0);
226 
227  DIAG_Handler_IgnoreAndReturn(STD_OK);
228 
230 }
@ DATA_BLOCK_ID_CELL_VOLTAGE_REDUNDANCY0
Definition: database_cfg.h:96
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:73
@ DATA_BLOCK_ID_CELL_TEMPERATURE_REDUNDANCY0
Definition: database_cfg.h:97
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:95
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:94
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:74
math library for often used math functions
@ STD_NOT_OK
Definition: fstd_types.h:73
@ STD_OK
Definition: fstd_types.h:72
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
STD_RETURN_TYPE_e TEST_MRC_CalculateCellTemperatureMinMaxAverage(DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperatures, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1185
STD_RETURN_TYPE_e TEST_MRC_CalculateCellVoltageMinMaxAverage(DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1180
STD_RETURN_TYPE_e TEST_MRC_UpdateCellTemperatureValidation(DATA_BLOCK_CELL_TEMPERATURE_s *pCellTemperature, DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperature)
Definition: redundancy.c:1207
bool TEST_MRC_MeasurementUpdatedAtLeastOnce(uint32_t timestamp, uint32_t previousTimestamp)
Definition: redundancy.c:1145
STD_RETURN_TYPE_e TEST_MRC_UpdateCellVoltageValidation(DATA_BLOCK_CELL_VOLTAGE_s *pCellvoltage, DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages)
Definition: redundancy.c:1196
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:1201
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:1190
STD_RETURN_TYPE_e TEST_MRC_MeasurementUpdatedRecently(uint32_t timestamp, uint32_t previousTimestamp, uint32_t timeInterval)
Definition: redundancy.c:1148
STD_RETURN_TYPE_e MRC_ValidateMicMeasurement(void)
Function to validate the measurement between redundant measurement values for cell voltage and cell t...
Definition: redundancy.c:1094
Header fileS for handling redundancy between redundant cell voltage and cell temperature measurements...
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:110
uint32_t timestamp
Definition: database_cfg.h:111
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:136
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:120
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:163
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_MicMeasurementValidationTickZeroNothingToDo(void)
DATA_BLOCK_CELL_VOLTAGE_s testCellvoltageBase
void testMRC_UpdateCellVoltageValidationNullPointer(void)
void testMRC_CalculateCellVoltageMinMaxAverageNullPointer(void)
DATA_BLOCK_CELL_VOLTAGE_s testCellvoltageRedundancy0
DATA_BLOCK_CELL_TEMPERATURE_s testCelltemperatureBase
void testMRC_CalculateCellTemperatureMinMaxAverageNullPointer(void)
DATA_BLOCK_CELL_TEMPERATURE_s testCelltemperatureRedundancy0
void testMRC_ValidateCellVoltageNullPointer(void)
void setUp(void)
void testMRC_ValidateCellTemperatureNullPointer(void)
void tearDown(void)
void testMRC_MeasurementUpdatedAtLeastOnce(void)
void testMRC_MeasurementUpdatedRecently(void)
void testMRC_UpdateCellTemperatureValidationNullPointer(void)
static void injectDatabaseEntries(void)