foxBMS - Unit Tests  1.2.1
The foxBMS Unit Tests API Documentation
test_debug_default.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_debug_default.c
44  * @author foxBMS Team
45  * @date 2020-09-17 (date of creation)
46  * @updated 2021-06-09 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix TEST
49  *
50  * @brief Test of the afe.c module
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "general.h"
56 
57 #include "unity.h"
58 #include "Mockdatabase.h"
59 #include "Mockos.h"
60 
61 #include "battery_cell_cfg.h"
62 #include "debug_default_cfg.h"
63 
64 #include "afe.h"
65 #include "debug_default.h"
66 
67 /* it's important to mention the implementation in debug_default.c
68 here in order to test the correct implementation */
69 TEST_FILE("debug_default.c")
70 
71 /*========== Definitions and Implementations for Unit Test ==================*/
72 #define FAKE_CELL_VOLTAGE_mV (BC_VOLTAGE_NOMINAL_mV)
73 
74 #define FAKE_CELL_TEMPERATURE_ddegC ((BC_TEMPERATURE_MAX_CHARGE_MOL_ddegC + BC_TEMPERATURE_MIN_CHARGE_MOL_ddegC) / 2u)
75 
76 static void TEST_assertEqualFakeState(FAKE_STATE_s *pExpected, FAKE_STATE_s *pActual);
77 
78 /** assert that the values and values behind the pointers in #FAKE_STATE_s are the same */
79 static void TEST_assertEqualFakeState(FAKE_STATE_s *pExpected, FAKE_STATE_s *pActual) {
80  TEST_ASSERT_EQUAL_UINT16_MESSAGE(
81  88,
82  sizeof(*pExpected),
83  "The data structure changed, update tests: "
84  "1: Add tests for the added/changed data structure. "
85  "2: Update the data structure length.");
86 
87  /* compare memory contents of database entries */
88  TEST_ASSERT_EQUAL_MEMORY(
89  pExpected->data.cellVoltage, pActual->data.cellVoltage, sizeof(*pExpected->data.cellVoltage));
90  TEST_ASSERT_EQUAL_MEMORY(
91  pExpected->data.cellTemperature, pActual->data.cellTemperature, sizeof(*pExpected->data.cellTemperature));
92  TEST_ASSERT_EQUAL_MEMORY(
93  pExpected->data.balancingFeedback, pActual->data.balancingFeedback, sizeof(*pExpected->data.balancingFeedback));
94  TEST_ASSERT_EQUAL_MEMORY(
95  pExpected->data.balancingControl, pActual->data.balancingControl, sizeof(*pExpected->data.balancingControl));
96  TEST_ASSERT_EQUAL_MEMORY(
97  pExpected->data.slaveControl, pActual->data.slaveControl, sizeof(*pExpected->data.slaveControl));
98  TEST_ASSERT_EQUAL_MEMORY(
99  pExpected->data.allGpioVoltages, pActual->data.allGpioVoltages, sizeof(*pExpected->data.allGpioVoltages));
100  TEST_ASSERT_EQUAL_MEMORY(pExpected->data.openWire, pActual->data.openWire, sizeof(*pExpected->data.openWire));
101 
102  TEST_ASSERT_EQUAL_UINT16(pExpected->timer, pActual->timer);
103  TEST_ASSERT_EQUAL_UINT8(pExpected->triggerEntry, pActual->triggerEntry);
104  TEST_ASSERT_EQUAL_UINT8(pExpected->currentState, pActual->currentState);
105  TEST_ASSERT_EQUAL_UINT8(pExpected->previousState, pActual->previousState);
106  TEST_ASSERT_EQUAL_UINT8(pExpected->nextSubstate, pActual->nextSubstate);
107  TEST_ASSERT_EQUAL_UINT8(pExpected->currentSubstate, pActual->currentSubstate);
108  TEST_ASSERT_EQUAL_UINT8(pExpected->previousSubstate, pActual->previousSubstate);
109  TEST_ASSERT_EQUAL_UINT8(pExpected->firstMeasurementFinished, pActual->firstMeasurementFinished);
110 }
111 
112 /*========== Setup and Teardown =============================================*/
113 void setUp(void) {
114 }
115 
116 void tearDown(void) {
117 }
118 
119 /*========== Test Cases =====================================================*/
120 
122  OS_EnterTaskCritical_Expect();
123  OS_ExitTaskCritical_Expect();
124 
125  FAKE_STATE_s state = {0};
126  bool reentrance = 0;
127  reentrance = TEST_FAKE_CheckMultipleCalls(&state);
128  TEST_ASSERT_EQUAL_UINT8(false, reentrance);
129 
130  OS_EnterTaskCritical_Expect();
131  OS_ExitTaskCritical_Expect();
132  state.triggerEntry = 1;
133  reentrance = TEST_FAKE_CheckMultipleCalls(&state);
134  TEST_ASSERT_EQUAL_UINT8(true, reentrance);
135 }
136 
138  OS_EnterTaskCritical_Expect();
139  OS_ExitTaskCritical_Expect();
140  DATA_Write_2_DataBlocks_IgnoreAndReturn(STD_OK);
141  DATA_Write_4_DataBlocks_IgnoreAndReturn(STD_OK);
142 
143  static DATA_BLOCK_CELL_VOLTAGE_s test_fake_cellVoltage = {.header.uniqueId = DATA_BLOCK_ID_CELL_VOLTAGE_BASE};
144  static DATA_BLOCK_CELL_TEMPERATURE_s test_fake_cellTemperature = {
146  static DATA_BLOCK_BALANCING_FEEDBACK_s test_fake_balancingFeedback = {
148  static DATA_BLOCK_BALANCING_CONTROL_s test_fake_balancingControl = {
150  static DATA_BLOCK_SLAVE_CONTROL_s test_fake_slaveControl = {.header.uniqueId = DATA_BLOCK_ID_SLAVE_CONTROL};
151  static DATA_BLOCK_ALL_GPIO_VOLTAGES_s test_fake_allGpioVoltage = {
153  static DATA_BLOCK_OPEN_WIRE_s test_fake_openWire = {.header.uniqueId = DATA_BLOCK_ID_OPEN_WIRE_BASE};
154  FAKE_STATE_s test_fake_state = {
155  .timer = 0,
156  .firstMeasurementFinished = false,
157  .triggerEntry = 0,
158  .nextState = FAKE_FSM_STATE_HAS_NEVER_RUN,
159  .currentState = FAKE_FSM_STATE_HAS_NEVER_RUN,
160  .previousState = FAKE_FSM_STATE_HAS_NEVER_RUN,
161  .nextSubstate = FAKE_FSM_SUBSTATE_DUMMY,
162  .currentSubstate = FAKE_FSM_SUBSTATE_DUMMY,
163  .previousSubstate = FAKE_FSM_SUBSTATE_DUMMY,
164  .data.allGpioVoltages = &test_fake_allGpioVoltage,
165  .data.balancingControl = &test_fake_balancingControl,
166  .data.balancingFeedback = &test_fake_balancingFeedback,
167  .data.cellTemperature = &test_fake_cellTemperature,
168  .data.cellVoltage = &test_fake_cellVoltage,
169  .data.openWire = &test_fake_openWire,
170  .data.slaveControl = &test_fake_slaveControl,
171  };
172 
173  static DATA_BLOCK_CELL_VOLTAGE_s test_fake_cellVoltageCompare = {
175  static DATA_BLOCK_CELL_TEMPERATURE_s test_fake_cellTemperatureCompare = {
177  static DATA_BLOCK_BALANCING_FEEDBACK_s test_fake_balancingFeedbackCompare = {
179  static DATA_BLOCK_BALANCING_CONTROL_s test_fake_balancingControlCompare = {
181  static DATA_BLOCK_SLAVE_CONTROL_s test_fake_slaveControlCompare = {.header.uniqueId = DATA_BLOCK_ID_SLAVE_CONTROL};
182  static DATA_BLOCK_ALL_GPIO_VOLTAGES_s test_fake_allGpioVoltageCompare = {
184  static DATA_BLOCK_OPEN_WIRE_s test_fake_openWireCompare = {.header.uniqueId = DATA_BLOCK_ID_OPEN_WIRE_BASE};
185 
186  FAKE_STATE_s test_fake_stateCompare = {
187  .timer = 0,
188  .firstMeasurementFinished = true,
189  .triggerEntry = 0,
190  .nextState = FAKE_FSM_STATE_HAS_NEVER_RUN,
191  .currentState = FAKE_FSM_STATE_HAS_NEVER_RUN,
192  .previousState = FAKE_FSM_STATE_HAS_NEVER_RUN,
193  .nextSubstate = FAKE_FSM_SUBSTATE_DUMMY,
194  .currentSubstate = FAKE_FSM_SUBSTATE_DUMMY,
195  .previousSubstate = FAKE_FSM_SUBSTATE_DUMMY,
196  .data.allGpioVoltages = &test_fake_allGpioVoltageCompare,
197  .data.balancingControl = &test_fake_balancingControlCompare,
198  .data.balancingFeedback = &test_fake_balancingFeedbackCompare,
199  .data.cellTemperature = &test_fake_cellTemperatureCompare,
200  .data.cellVoltage = &test_fake_cellVoltageCompare,
201  .data.openWire = &test_fake_openWireCompare,
202  .data.slaveControl = &test_fake_slaveControlCompare,
203  };
204 
205  uint16_t i = 0;
206 
207  for (uint8_t stringNumber = 0u; stringNumber < BS_NR_OF_STRINGS; stringNumber++) {
208  for (i = 0; i < BS_NR_OF_BAT_CELLS; i++) {
209  test_fake_stateCompare.data.cellVoltage->cellVoltage_mV[stringNumber][i] = FAKE_CELL_VOLTAGE_mV;
210  }
211  test_fake_stateCompare.data.cellVoltage->packVoltage_mV[stringNumber] = FAKE_CELL_VOLTAGE_mV *
213 
214  for (i = 0; i < BS_NR_OF_TEMP_SENSORS_PER_STRING; i++) {
215  test_fake_stateCompare.data.cellTemperature->cellTemperature_ddegC[stringNumber][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
#define BS_NR_OF_TEMP_SENSORS_PER_STRING
#define BS_NR_OF_BAT_CELLS
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:77
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:95
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:81
@ DATA_BLOCK_ID_SLAVE_CONTROL
Definition: database_cfg.h:78
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:94
@ DATA_BLOCK_ID_ALL_GPIO_VOLTAGES_BASE
Definition: database_cfg.h:82
@ DATA_BLOCK_ID_BALANCING_FEEDBACK_BASE
Definition: database_cfg.h:79
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:76
@ FAKE_FSM_STATE_HAS_NEVER_RUN
Definition: debug_default.h:67
Header for the configuration for the fake AFE.
@ STD_OK
Definition: fstd_types.h:81
General macros and definitions for the whole platform.
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:111
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:302
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:238
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:270
int16_t cellTemperature_ddegC[BS_NR_OF_STRINGS][BS_NR_OF_TEMP_SENSORS_PER_STRING]
Definition: database_cfg.h:139
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:137
int16_t cellVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_BAT_CELLS]
Definition: database_cfg.h:124
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:121
int32_t packVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:123
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:290
uint32_t eepromWriteAddressLastUsed
Definition: database_cfg.h:257
uint32_t eepromReadAddressLastUsed
Definition: database_cfg.h:255
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:252
DATA_BLOCK_BALANCING_FEEDBACK_s * balancingFeedback
Definition: debug_default.h:89
DATA_BLOCK_OPEN_WIRE_s * openWire
Definition: debug_default.h:93
DATA_BLOCK_ALL_GPIO_VOLTAGES_s * allGpioVoltages
Definition: debug_default.h:92
DATA_BLOCK_CELL_VOLTAGE_s * cellVoltage
Definition: debug_default.h:87
DATA_BLOCK_BALANCING_CONTROL_s * balancingControl
Definition: debug_default.h:90
DATA_BLOCK_CELL_TEMPERATURE_s * cellTemperature
Definition: debug_default.h:88
DATA_BLOCK_SLAVE_CONTROL_s * slaveControl
Definition: debug_default.h:91
bool firstMeasurementFinished
uint16_t timer
Definition: debug_default.h:98
uint8_t triggerEntry
Definition: debug_default.h:99
FAKE_FSM_SUBSTATES_e nextSubstate
FAKE_FSM_STATES_e previousState
FAKE_DATABASE_ENTRIES_s data
FAKE_FSM_SUBSTATES_e previousSubstate
FAKE_FSM_STATES_e currentState
FAKE_FSM_SUBSTATES_e currentSubstate
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)