foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
test_debug_default.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_debug_default.c
44  * @author foxBMS Team
45  * @date 2020-09-17 (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 afe.c module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "general.h"
57 
58 #include "unity.h"
59 #include "Mockdatabase.h"
60 #include "Mockos.h"
61 
62 #include "battery_cell_cfg.h"
63 #include "debug_default_cfg.h"
64 
65 #include "afe.h"
66 #include "debug_default.h"
67 
68 /* it's important to mention the implementation in debug_default.c
69 here in order to test the correct implementation */
70 TEST_FILE("debug_default.c")
71 
72 /*========== Definitions and Implementations for Unit Test ==================*/
73 #define FAKE_CELL_VOLTAGE_mV (BC_VOLTAGE_NOMINAL_mV)
74 
75 #define FAKE_CELL_TEMPERATURE_ddegC ((BC_TEMPERATURE_MAX_CHARGE_MOL_ddegC + BC_TEMPERATURE_MIN_CHARGE_MOL_ddegC) / 2u)
76 
77 static void TEST_assertEqualFakeState(FAKE_STATE_s *pExpected, FAKE_STATE_s *pActual);
78 
79 /** assert that the values and values behind the pointers in #FAKE_STATE_s are the same */
80 static void TEST_assertEqualFakeState(FAKE_STATE_s *pExpected, FAKE_STATE_s *pActual) {
81  TEST_ASSERT_EQUAL_UINT16_MESSAGE(
82  88,
83  sizeof(*pExpected),
84  "The data structure changed, update tests: "
85  "1: Add tests for the added/changed data structure. "
86  "2: Update the data structure length.");
87 
88  /* compare memory contents of database entries */
89  TEST_ASSERT_EQUAL_MEMORY(
90  pExpected->data.cellVoltage, pActual->data.cellVoltage, sizeof(*pExpected->data.cellVoltage));
91  TEST_ASSERT_EQUAL_MEMORY(
92  pExpected->data.cellTemperature, pActual->data.cellTemperature, sizeof(*pExpected->data.cellTemperature));
93  TEST_ASSERT_EQUAL_MEMORY(
94  pExpected->data.balancingFeedback, pActual->data.balancingFeedback, sizeof(*pExpected->data.balancingFeedback));
95  TEST_ASSERT_EQUAL_MEMORY(
96  pExpected->data.balancingControl, pActual->data.balancingControl, sizeof(*pExpected->data.balancingControl));
97  TEST_ASSERT_EQUAL_MEMORY(
98  pExpected->data.slaveControl, pActual->data.slaveControl, sizeof(*pExpected->data.slaveControl));
99  TEST_ASSERT_EQUAL_MEMORY(
100  pExpected->data.allGpioVoltages, pActual->data.allGpioVoltages, sizeof(*pExpected->data.allGpioVoltages));
101  TEST_ASSERT_EQUAL_MEMORY(pExpected->data.openWire, pActual->data.openWire, sizeof(*pExpected->data.openWire));
102 
103  TEST_ASSERT_EQUAL_UINT16(pExpected->timer, pActual->timer);
104  TEST_ASSERT_EQUAL_UINT8(pExpected->triggerEntry, pActual->triggerEntry);
105  TEST_ASSERT_EQUAL_UINT8(pExpected->currentState, pActual->currentState);
106  TEST_ASSERT_EQUAL_UINT8(pExpected->previousState, pActual->previousState);
107  TEST_ASSERT_EQUAL_UINT8(pExpected->nextSubstate, pActual->nextSubstate);
108  TEST_ASSERT_EQUAL_UINT8(pExpected->currentSubstate, pActual->currentSubstate);
109  TEST_ASSERT_EQUAL_UINT8(pExpected->previousSubstate, pActual->previousSubstate);
110  TEST_ASSERT_EQUAL_UINT8(pExpected->firstMeasurementFinished, pActual->firstMeasurementFinished);
111 }
112 
113 /*========== Setup and Teardown =============================================*/
114 void setUp(void) {
115 }
116 
117 void tearDown(void) {
118 }
119 
120 /*========== Test Cases =====================================================*/
121 
123  OS_EnterTaskCritical_Expect();
124  OS_ExitTaskCritical_Expect();
125 
126  FAKE_STATE_s state = {0};
127  bool reentrance = 0;
128  reentrance = TEST_FAKE_CheckMultipleCalls(&state);
129  TEST_ASSERT_EQUAL_UINT8(false, reentrance);
130 
131  OS_EnterTaskCritical_Expect();
132  OS_ExitTaskCritical_Expect();
133  state.triggerEntry = 1;
134  reentrance = TEST_FAKE_CheckMultipleCalls(&state);
135  TEST_ASSERT_EQUAL_UINT8(true, reentrance);
136 }
137 
139  OS_EnterTaskCritical_Expect();
140  OS_ExitTaskCritical_Expect();
141  DATA_Write2DataBlocks_IgnoreAndReturn(STD_OK);
142  DATA_Write4DataBlocks_IgnoreAndReturn(STD_OK);
143 
144  static DATA_BLOCK_CELL_VOLTAGE_s test_fake_cellVoltage = {.header.uniqueId = DATA_BLOCK_ID_CELL_VOLTAGE_BASE};
145  static DATA_BLOCK_CELL_TEMPERATURE_s test_fake_cellTemperature = {
147  static DATA_BLOCK_BALANCING_FEEDBACK_s test_fake_balancingFeedback = {
149  static DATA_BLOCK_BALANCING_CONTROL_s test_fake_balancingControl = {
151  static DATA_BLOCK_SLAVE_CONTROL_s test_fake_slaveControl = {.header.uniqueId = DATA_BLOCK_ID_SLAVE_CONTROL};
152  static DATA_BLOCK_ALL_GPIO_VOLTAGES_s test_fake_allGpioVoltage = {
154  static DATA_BLOCK_OPEN_WIRE_s test_fake_openWire = {.header.uniqueId = DATA_BLOCK_ID_OPEN_WIRE_BASE};
155  FAKE_STATE_s test_fake_state = {
156  .timer = 0,
157  .firstMeasurementFinished = false,
158  .triggerEntry = 0,
159  .nextState = FAKE_FSM_STATE_HAS_NEVER_RUN,
160  .currentState = FAKE_FSM_STATE_HAS_NEVER_RUN,
161  .previousState = FAKE_FSM_STATE_HAS_NEVER_RUN,
162  .nextSubstate = FAKE_FSM_SUBSTATE_DUMMY,
163  .currentSubstate = FAKE_FSM_SUBSTATE_DUMMY,
164  .previousSubstate = FAKE_FSM_SUBSTATE_DUMMY,
165  .data.allGpioVoltages = &test_fake_allGpioVoltage,
166  .data.balancingControl = &test_fake_balancingControl,
167  .data.balancingFeedback = &test_fake_balancingFeedback,
168  .data.cellTemperature = &test_fake_cellTemperature,
169  .data.cellVoltage = &test_fake_cellVoltage,
170  .data.openWire = &test_fake_openWire,
171  .data.slaveControl = &test_fake_slaveControl,
172  };
173 
174  static DATA_BLOCK_CELL_VOLTAGE_s test_fake_cellVoltageCompare = {
176  static DATA_BLOCK_CELL_TEMPERATURE_s test_fake_cellTemperatureCompare = {
178  static DATA_BLOCK_BALANCING_FEEDBACK_s test_fake_balancingFeedbackCompare = {
180  static DATA_BLOCK_BALANCING_CONTROL_s test_fake_balancingControlCompare = {
182  static DATA_BLOCK_SLAVE_CONTROL_s test_fake_slaveControlCompare = {.header.uniqueId = DATA_BLOCK_ID_SLAVE_CONTROL};
183  static DATA_BLOCK_ALL_GPIO_VOLTAGES_s test_fake_allGpioVoltageCompare = {
185  static DATA_BLOCK_OPEN_WIRE_s test_fake_openWireCompare = {.header.uniqueId = DATA_BLOCK_ID_OPEN_WIRE_BASE};
186 
187  FAKE_STATE_s test_fake_stateCompare = {
188  .timer = 0,
189  .firstMeasurementFinished = true,
190  .triggerEntry = 0,
191  .nextState = FAKE_FSM_STATE_HAS_NEVER_RUN,
192  .currentState = FAKE_FSM_STATE_HAS_NEVER_RUN,
193  .previousState = FAKE_FSM_STATE_HAS_NEVER_RUN,
194  .nextSubstate = FAKE_FSM_SUBSTATE_DUMMY,
195  .currentSubstate = FAKE_FSM_SUBSTATE_DUMMY,
196  .previousSubstate = FAKE_FSM_SUBSTATE_DUMMY,
197  .data.allGpioVoltages = &test_fake_allGpioVoltageCompare,
198  .data.balancingControl = &test_fake_balancingControlCompare,
199  .data.balancingFeedback = &test_fake_balancingFeedbackCompare,
200  .data.cellTemperature = &test_fake_cellTemperatureCompare,
201  .data.cellVoltage = &test_fake_cellVoltageCompare,
202  .data.openWire = &test_fake_openWireCompare,
203  .data.slaveControl = &test_fake_slaveControlCompare,
204  };
205 
206  uint16_t i = 0;
207 
208  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
209  for (i = 0; i < BS_NR_OF_CELL_BLOCKS_PER_STRING; i++) {
210  test_fake_stateCompare.data.cellVoltage->cellVoltage_mV[s][i] = FAKE_CELL_VOLTAGE_mV;
211  }
212  test_fake_stateCompare.data.cellVoltage->packVoltage_mV[s] = FAKE_CELL_VOLTAGE_mV *
214 
215  for (i = 0; i < BS_NR_OF_TEMP_SENSORS_PER_STRING; i++) {
217  }
218 
219  test_fake_stateCompare.data.slaveControl->eepromReadAddressLastUsed = 0xFFFFFFFF;
220  test_fake_stateCompare.data.slaveControl->eepromReadAddressToUse = 0xFFFFFFFF;
221  test_fake_stateCompare.data.slaveControl->eepromWriteAddressLastUsed = 0xFFFFFFFF;
222  test_fake_stateCompare.data.slaveControl->eepromWriteAddressToUse = 0xFFFFFFFF;
223  }
224  test_fake_stateCompare.firstMeasurementFinished = true;
225 
226  test_fake_state.firstMeasurementFinished = false;
228 
229  TEST_ASSERT_EQUAL_UINT8(true, test_fake_state.firstMeasurementFinished);
230  TEST_assertEqualFakeState(&test_fake_stateCompare, &test_fake_state);
231 }
232 
234  TEST_ASSERT_EQUAL(STD_OK, FAKE_Initialize());
235 }
236 
238 }
239 
241 }
AFE driver header.
Configuration of the battery cell (e.g., minimum and maximum cell voltage)
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
#define BS_NR_OF_CELL_BLOCKS_PER_STRING
#define BS_NR_OF_TEMP_SENSORS_PER_STRING
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:80
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:98
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:84
@ DATA_BLOCK_ID_SLAVE_CONTROL
Definition: database_cfg.h:81
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:97
@ DATA_BLOCK_ID_ALL_GPIO_VOLTAGES_BASE
Definition: database_cfg.h:85
@ DATA_BLOCK_ID_BALANCING_FEEDBACK_BASE
Definition: database_cfg.h:82
STD_RETURN_TYPE_e FAKE_Initialize(void)
initialize driver
void TEST_FAKE_SetFirstMeasurementCycleFinished(FAKE_STATE_s *pFakeState)
bool TEST_FAKE_CheckMultipleCalls(FAKE_STATE_s *pFakeState)
Header for the driver of the fake AFE driver.
@ FAKE_FSM_SUBSTATE_DUMMY
Definition: debug_default.h:77
@ FAKE_FSM_STATE_HAS_NEVER_RUN
Definition: debug_default.h:68
Header for the configuration for the fake AFE.
@ STD_OK
Definition: fstd_types.h:82
General macros and definitions for the whole platform.
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:315
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:247
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:282
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:146
int16_t cellTemperature_ddegC[BS_NR_OF_STRINGS][BS_NR_OF_TEMP_SENSORS_PER_STRING]
Definition: database_cfg.h:148
int32_t packVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:131
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:129
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_CELL_BLOCKS_PER_STRING]
Definition: database_cfg.h:132
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:302
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:263
DATA_BLOCK_BALANCING_CONTROL_s * balancingControl
Definition: debug_default.h:91
DATA_BLOCK_SLAVE_CONTROL_s * slaveControl
Definition: debug_default.h:92
DATA_BLOCK_OPEN_WIRE_s * openWire
Definition: debug_default.h:94
DATA_BLOCK_CELL_VOLTAGE_s * cellVoltage
Definition: debug_default.h:88
DATA_BLOCK_BALANCING_FEEDBACK_s * balancingFeedback
Definition: debug_default.h:90
DATA_BLOCK_CELL_TEMPERATURE_s * cellTemperature
Definition: debug_default.h:89
DATA_BLOCK_ALL_GPIO_VOLTAGES_s * allGpioVoltages
Definition: debug_default.h:93
FAKE_FSM_SUBSTATES_e nextSubstate
FAKE_FSM_STATES_e currentState
FAKE_FSM_SUBSTATES_e previousSubstate
uint16_t timer
Definition: debug_default.h:99
FAKE_FSM_STATES_e previousState
FAKE_DATABASE_ENTRIES_s data
uint8_t triggerEntry
FAKE_FSM_SUBSTATES_e currentSubstate
bool firstMeasurementFinished
void testFAKE_Initialize(void)
void testTEST_FAKE_CheckMultipleCalls(void)
#define FAKE_CELL_TEMPERATURE_ddegC
#define FAKE_CELL_VOLTAGE_mV
static void TEST_assertEqualFakeState(FAKE_STATE_s *pExpected, FAKE_STATE_s *pActual)
void testFAKE_SetFirstMeasurementCycleFinished(void)
void testFAKE_TriggerAfe(void)
void setUp(void)
void tearDown(void)
void testFAKE_IsFirstMeasurementCycleFinished(void)