foxBMS-UnitTests  1.0.0
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
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
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 "Mockdiag.h"
59 #include "Mockos.h"
60 #include "Mockplausibility.h"
61 
62 #include "foxmath.h"
63 #include "redundancy.h"
64 #include "test_assert_helper.h"
65 
66 /*========== Definitions and Implementations for Unit Test ==================*/
67 
70 
74 
75 static inline void injectDatabaseEntries(void) {
76  DATA_Read_4_DataBlocks_ExpectAndReturn(
81  STD_OK);
82  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver0();
83  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver1();
84  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver2();
85  DATA_Read_4_DataBlocks_IgnoreArg_pDataToReceiver3();
86  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver0(&testCellvoltageBase);
87  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver1(&testCellvoltageRedundancy0);
88  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver2(&testCelltemperatureBase);
89  DATA_Read_4_DataBlocks_ReturnThruPtr_pDataToReceiver3(&testCelltemperatureRedundancy0);
90 }
91 
92 /*========== Setup and Teardown =============================================*/
93 void setUp(void) {
98 }
99 
100 void tearDown(void) {
101 }
102 
103 /*========== Test Cases =====================================================*/
104 /* test for null pointer */
105 
107  /* Measurement has been updated once, after 10ms */
108  uint32_t timestamp = 10u;
109  uint32_t previousTimestamp = 0u;
110  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
111 
112  /* Measurement has been updated twice, first after 10ms, then after 50ms */
113  timestamp = 60u;
114  previousTimestamp = 10u;
115  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
116 
117  /* Measurement has been updated three times, first after 10ms, then after 50ms, then after 10ms */
118  timestamp = 70u;
119  previousTimestamp = 60u;
120  TEST_ASSERT_TRUE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
121 
122  /* Measurement has never been updated */
123  timestamp = 0u;
124  previousTimestamp = 0u;
125  TEST_ASSERT_FALSE(TEST_MRC_MeasurementUpdatedAtLeastOnce(timestamp, previousTimestamp));
126 }
127 
129  /* Always check always if database entry has been updated within the last 100ms */
130  uint32_t timeDifference = 100u;
131 
132  /* Time difference: 50ms -> true */
133  uint32_t timestamp = 50u;
134  uint32_t previousTimestamp = 0u;
135  OS_GetTickCount_ExpectAndReturn(100u);
136  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
137 
138  /* Time difference: 100ms -> true, but never updated */
139  timestamp = 0u;
140  OS_GetTickCount_ExpectAndReturn(100u);
141  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
142 
143  /* Time difference: 101ms -> false */
144  timestamp = 0u;
145  OS_GetTickCount_ExpectAndReturn(101u);
146  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
147 
148  /* Time difference: 63ms -> true */
149  timestamp = 4937u;
150  OS_GetTickCount_ExpectAndReturn(5000u);
151  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
152 
153  /* Time difference: 50ms -> true */
154  timestamp = UINT32_MAX;
155  OS_GetTickCount_ExpectAndReturn(50u);
156  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
157 
158  /* Time difference: 100ms -> true */
159  timestamp = UINT32_MAX - 50u;
160  OS_GetTickCount_ExpectAndReturn(49u);
161  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_OK);
162 
163  /* Time difference: 101ms -> false */
164  timestamp = UINT32_MAX - 50u;
165  OS_GetTickCount_ExpectAndReturn(50u);
166  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
167 
168  /* Time difference: UINT32_MAX - 50 -> false */
169  timestamp = 50u;
170  OS_GetTickCount_ExpectAndReturn(UINT32_MAX);
171  TEST_ASSERT_EQUAL(TEST_MRC_MeasurementUpdatedRecently(timestamp, previousTimestamp, timeDifference), STD_NOT_OK);
172 }
173 
179 }
180 
186 }
187 
193 }
194 
199 }
200 
206 }
207 
212 }
213 
214 /* test main function */
216  /* inject database entries into function */
218 
219  /* database entries never written - */
220  DATA_DatabaseEntryUpdatedRecently_IgnoreAndReturn(false);
221  DATA_DatabaseEntryUpdatedAtLeastOnce_IgnoreAndReturn(false);
222 
223  /* tick zero, database starts also with zero --> nothing to do */
224  OS_GetTickCount_IgnoreAndReturn(0);
225 
226  DIAG_Handler_IgnoreAndReturn(STD_OK);
227 
229 }
tearDown
void tearDown(void)
Definition: test_redundancy.c:100
testMRC_CalculateCellVoltageMinMaxAverageNullPointer
void testMRC_CalculateCellVoltageMinMaxAverageNullPointer(void)
Definition: test_redundancy.c:174
TEST_MRC_CalculateCellVoltageMinMaxAverage
STD_RETURN_TYPE_e TEST_MRC_CalculateCellVoltageMinMaxAverage(DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1159
TEST_MRC_ValidateCellTemperature
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:1180
DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:95
DATA_BLOCK_MIN_MAX::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:162
testMRC_UpdateCellTemperatureValidationNullPointer
void testMRC_UpdateCellTemperatureValidationNullPointer(void)
Definition: test_redundancy.c:208
testMRC_UpdateCellVoltageValidationNullPointer
void testMRC_UpdateCellVoltageValidationNullPointer(void)
Definition: test_redundancy.c:195
TEST_MRC_MeasurementUpdatedRecently
STD_RETURN_TYPE_e TEST_MRC_MeasurementUpdatedRecently(uint32_t timestamp, uint32_t previousTimestamp, uint32_t timeInterval)
Definition: redundancy.c:1127
test_assert_helper.h
Helper for unit tests.
testCelltemperatureRedundancy0
DATA_BLOCK_CELL_TEMPERATURE_s testCelltemperatureRedundancy0
Definition: test_redundancy.c:72
TEST_MRC_UpdateCellTemperatureValidation
STD_RETURN_TYPE_e TEST_MRC_UpdateCellTemperatureValidation(DATA_BLOCK_CELL_TEMPERATURE_s *pCellTemperature, DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperature)
Definition: redundancy.c:1186
MRC_ValidateMicMeasurement
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:1073
TEST_MRC_MeasurementUpdatedAtLeastOnce
bool TEST_MRC_MeasurementUpdatedAtLeastOnce(uint32_t timestamp, uint32_t previousTimestamp)
Definition: redundancy.c:1124
testMRC_CalculateCellTemperatureMinMaxAverageNullPointer
void testMRC_CalculateCellTemperatureMinMaxAverageNullPointer(void)
Definition: test_redundancy.c:181
testMRC_MeasurementUpdatedRecently
void testMRC_MeasurementUpdatedRecently(void)
Definition: test_redundancy.c:128
DATA_BLOCK_CELL_TEMPERATURE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:135
DATA_BLOCK_ID_MIN_MAX
@ DATA_BLOCK_ID_MIN_MAX
Definition: database_cfg.h:75
DATA_BLOCK_ID_CELL_VOLTAGE_REDUNDANCY0
@ DATA_BLOCK_ID_CELL_VOLTAGE_REDUNDANCY0
Definition: database_cfg.h:96
DATA_BLOCK_ID_CELL_VOLTAGE_BASE
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:94
TEST_MRC_UpdateCellVoltageValidation
STD_RETURN_TYPE_e TEST_MRC_UpdateCellVoltageValidation(DATA_BLOCK_CELL_VOLTAGE_s *pCellvoltage, DATA_BLOCK_CELL_VOLTAGE_s *pValidatedVoltages)
Definition: redundancy.c:1175
foxmath.h
math library for often used math functions
STD_OK
@ STD_OK
Definition: fstd_types.h:72
testMRC_MeasurementUpdatedAtLeastOnce
void testMRC_MeasurementUpdatedAtLeastOnce(void)
Definition: test_redundancy.c:106
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
redundancy.h
Header fileS for handling redundancy between redundant cell voltage and cell temperature measurements...
setUp
void setUp(void)
Definition: test_redundancy.c:93
DATA_BLOCK_ID_CELL_TEMPERATURE_REDUNDANCY0
@ DATA_BLOCK_ID_CELL_TEMPERATURE_REDUNDANCY0
Definition: database_cfg.h:97
DATA_BLOCK_CELL_TEMPERATURE
Definition: database_cfg.h:131
DATA_BLOCK_CELL_VOLTAGE
Definition: database_cfg.h:115
DATA_BLOCK_MIN_MAX
Definition: database_cfg.h:158
testCellvoltageRedundancy0
DATA_BLOCK_CELL_VOLTAGE_s testCellvoltageRedundancy0
Definition: test_redundancy.c:69
NULL_PTR
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
TEST_MRC_CalculateCellTemperatureMinMaxAverage
STD_RETURN_TYPE_e TEST_MRC_CalculateCellTemperatureMinMaxAverage(DATA_BLOCK_CELL_TEMPERATURE_s *pValidatedTemperatures, DATA_BLOCK_MIN_MAX_s *pMinMaxAverageValues)
Definition: redundancy.c:1164
DATA_BLOCK_ID_CELL_VOLTAGE
@ DATA_BLOCK_ID_CELL_VOLTAGE
Definition: database_cfg.h:73
injectDatabaseEntries
static void injectDatabaseEntries(void)
Definition: test_redundancy.c:75
testMRC_ValidateCellTemperatureNullPointer
void testMRC_ValidateCellTemperatureNullPointer(void)
Definition: test_redundancy.c:201
DATA_BLOCK_ID_CELL_TEMPERATURE
@ DATA_BLOCK_ID_CELL_TEMPERATURE
Definition: database_cfg.h:74
TEST_MRC_ValidateCellVoltage
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:1169
DATA_BLOCK_CELL_VOLTAGE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:119
testCellvoltageBase
DATA_BLOCK_CELL_VOLTAGE_s testCellvoltageBase
Definition: test_redundancy.c:68
testCelltemperatureBase
DATA_BLOCK_CELL_TEMPERATURE_s testCelltemperatureBase
Definition: test_redundancy.c:71
testMRC_MicMeasurementValidationTickZeroNothingToDo
void testMRC_MicMeasurementValidationTickZeroNothingToDo(void)
Definition: test_redundancy.c:215
TEST_ASSERT_FAIL_ASSERT
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
Definition: test_assert_helper.h:70
testMRC_ValidateCellVoltageNullPointer
void testMRC_ValidateCellVoltageNullPointer(void)
Definition: test_redundancy.c:188
DATA_BLOCKHEADER::timestamp
uint32_t timestamp
Definition: database_cfg.h:110
TEST_ASSERT_PASS_ASSERT
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
Definition: test_assert_helper.h:88
DATA_BLOCKHEADER::uniqueId
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:109