foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
test_can_helper.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_can_helper.c
44  * @author foxBMS Team
45  * @date 2021-04-22 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the CAN helper functions
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockcan.h"
58 #include "Mockdatabase.h"
59 #include "Mockdiag.h"
60 #include "Mockfoxmath.h"
61 #include "Mockimd.h"
62 #include "Mockmpu_prototypes.h"
63 #include "Mockos.h"
64 
65 #include "database_cfg.h"
66 
67 #include "can_cbs_tx.h"
68 #include "can_helper.h"
69 #include "test_assert_helper.h"
70 
71 /*========== Definitions and Implementations for Unit Test ==================*/
72 
73 /*========== Setup and Teardown =============================================*/
74 void setUp(void) {
75 }
76 
77 void tearDown(void) {
78 }
79 
80 /*========== Test Cases =====================================================*/
81 
82 /** test the interface of CAN_TxSetMessageDataWithSignalData for null pointer and invalid parameters */
84  uint64_t message = 0;
85  uint64_t bitStart = 0;
86  uint8_t bitLength = 0;
87  uint64_t canSignal = 0;
89 
90  TEST_ASSERT_FAIL_ASSERT(CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness));
91 
93 
94  TEST_ASSERT_FAIL_ASSERT(CAN_TxSetMessageDataWithSignalData(&message, 64u, 0u, canSignal, endianness));
95  TEST_ASSERT_FAIL_ASSERT(CAN_TxSetMessageDataWithSignalData(&message, 1u, 65u, canSignal, endianness));
96  TEST_ASSERT_FAIL_ASSERT(CAN_TxSetMessageDataWithSignalData(&message, 65u, 64u, canSignal, endianness));
97  TEST_ASSERT_FAIL_ASSERT(CAN_TxSetMessageDataWithSignalData(&message, 10u, 64u, canSignal, endianness));
98 
101  TEST_ASSERT_FAIL_ASSERT(CAN_TxSetMessageDataWithSignalData(&message, 1u, 1u, canSignal, INT8_MAX));
102 }
103 
104 /** test CAN_TxSetMessageDataWithSignalData with various signals */
106  uint64_t message = 0;
107  uint64_t bitStart = 0;
108  uint8_t bitLength = 63;
109  uint64_t canSignal = UINT64_MAX >> 1u;
110  CAN_ENDIANNESS_e endianness = CAN_LITTLE_ENDIAN;
111 
112  CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness);
113 
114  TEST_ASSERT_EQUAL_UINT64(canSignal, message);
115 
116  message = 0;
117  bitStart = 0;
118  bitLength = 64;
119  canSignal = UINT64_MAX;
120  endianness = CAN_LITTLE_ENDIAN;
121 
122  CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness);
123 
124  TEST_ASSERT_EQUAL_UINT64(canSignal, message);
125 
126  message = 0;
127  bitStart = 0;
128  bitLength = 1;
129  canSignal = 0x1u;
130  endianness = CAN_LITTLE_ENDIAN;
131 
132  CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness);
133 
134  TEST_ASSERT_EQUAL_UINT64(canSignal, message);
135 
136  message = 0;
137  bitStart = 0;
138  bitLength = 3;
139  canSignal = 0x7u;
140  endianness = CAN_LITTLE_ENDIAN;
141 
142  CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness);
143 
144  TEST_ASSERT_EQUAL_UINT64(canSignal, message);
145 
146  message = 0;
147  bitStart = 0;
148  bitLength = 3;
149  canSignal = UINT64_MAX;
150  endianness = CAN_LITTLE_ENDIAN;
151 
152  CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness);
153  /* This behavior is a by-product of implementation and you should not necessarily rely on it */
154  TEST_ASSERT_EQUAL_UINT64(0x7u, message);
155 
156  message = 0;
157  bitStart = 5;
158  bitLength = 2;
159  canSignal = 0x3u;
160  endianness = CAN_LITTLE_ENDIAN;
161 
162  CAN_TxSetMessageDataWithSignalData(&message, bitStart, bitLength, canSignal, endianness);
163 
164  TEST_ASSERT_EQUAL_UINT64(0x60u, message);
165 }
166 
167 /** test the interface of CAN_RxGetSignalDataFromMessageData for null pointer and invalid parameters */
169  uint64_t message = 0;
170  uint64_t bitStart = 0;
171  uint8_t bitLength = 0;
172  uint64_t pCanSignal = 0;
173  CAN_ENDIANNESS_e endianness = CAN_LITTLE_ENDIAN;
174 
175  TEST_ASSERT_FAIL_ASSERT(CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &pCanSignal, endianness));
176 
178 
179  TEST_ASSERT_FAIL_ASSERT(CAN_RxGetSignalDataFromMessageData(message, 64u, 0u, &pCanSignal, endianness));
180  TEST_ASSERT_FAIL_ASSERT(CAN_RxGetSignalDataFromMessageData(message, 1u, 65u, &pCanSignal, endianness));
181  TEST_ASSERT_FAIL_ASSERT(CAN_RxGetSignalDataFromMessageData(message, 65u, 64u, &pCanSignal, endianness));
182  TEST_ASSERT_FAIL_ASSERT(CAN_RxGetSignalDataFromMessageData(message, 10u, 64u, &pCanSignal, endianness));
183 
186  TEST_ASSERT_FAIL_ASSERT(CAN_RxGetSignalDataFromMessageData(message, 1u, 1u, &pCanSignal, INT8_MAX));
187 }
188 
189 /** test CAN_RxGetSignalDataFromMessageData with various signals */
191  uint64_t message = UINT64_MAX >> 1u;
192  uint64_t bitStart = 0;
193  uint8_t bitLength = 63;
194  uint64_t canSignal = 0;
195  CAN_ENDIANNESS_e endianness = CAN_LITTLE_ENDIAN;
196 
197  CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &canSignal, endianness);
198 
199  TEST_ASSERT_EQUAL_UINT64(message, canSignal);
200 
201  message = UINT64_MAX;
202  bitStart = 0;
203  bitLength = 64;
204  canSignal = 0;
205  endianness = CAN_LITTLE_ENDIAN;
206 
207  CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &canSignal, endianness);
208 
209  TEST_ASSERT_EQUAL_UINT64(message, canSignal);
210 
211  message = 0x1u;
212  bitStart = 0;
213  bitLength = 1;
214  canSignal = 0;
215  endianness = CAN_LITTLE_ENDIAN;
216 
217  CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &canSignal, endianness);
218 
219  TEST_ASSERT_EQUAL_UINT64(message, canSignal);
220 
221  message = 0x7u;
222  bitStart = 0;
223  bitLength = 3;
224  canSignal = 0;
225  endianness = CAN_LITTLE_ENDIAN;
226 
227  CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &canSignal, endianness);
228 
229  TEST_ASSERT_EQUAL_UINT64(message, canSignal);
230 
231  message = UINT64_MAX;
232  bitStart = 0;
233  bitLength = 3;
234  canSignal = 0;
235  endianness = CAN_LITTLE_ENDIAN;
236 
237  CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &canSignal, endianness);
238  /* This behavior is a by-product of implementation and you should not necessarily rely on it */
239  TEST_ASSERT_EQUAL_UINT64(0x7u, canSignal);
240 
241  message = 0x60u;
242  bitStart = 5;
243  bitLength = 2;
244  canSignal = 0u;
245  endianness = CAN_LITTLE_ENDIAN;
246 
247  CAN_RxGetSignalDataFromMessageData(message, bitStart, bitLength, &canSignal, endianness);
248 
249  TEST_ASSERT_EQUAL_UINT64(0x3u, canSignal);
250 }
251 
253  TEST_ASSERT_EQUAL(0u, CAN_ConvertBooleanToInteger(false));
254  TEST_ASSERT_EQUAL(1u, CAN_ConvertBooleanToInteger(true));
255 }
CAN callbacks header.
CAN_ENDIANNESS_e
Definition: can_cfg.h:146
@ CAN_LITTLE_ENDIAN
Definition: can_cfg.h:147
@ CAN_BIG_ENDIAN
Definition: can_cfg.h:148
void CAN_TxSetMessageDataWithSignalData(uint64_t *pMessage, uint64_t bitStart, uint8_t bitLength, uint64_t canSignal, CAN_ENDIANNESS_e endianness)
Puts CAN signal data in a 64-bit variable. This function is used to compose a 64-bit CAN message....
Definition: can_helper.c:167
uint8_t CAN_ConvertBooleanToInteger(bool input)
Transform a bool to a bit (set if true)
Definition: can_helper.c:336
void CAN_RxGetSignalDataFromMessageData(uint64_t message, uint64_t bitStart, uint8_t bitLength, uint64_t *pCanSignal, CAN_ENDIANNESS_e endianness)
Gets CAN signal data from a 64-bit variable. This function is used to get signal data from a 64-bit C...
Definition: can_helper.c:249
Headers for the helper functions for the CAN module.
Database configuration header.
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
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 testCAN_RxGetSignalDataFromMessageData64BitMessage(void)
void testCAN_RxGetSignalDataFromMessageDataInterfaceNullPointer(void)
void testCAN_TxSetMessageDataWithSignalDataInterfaceNullPointer(void)
void setUp(void)
void tearDown(void)
void testCAN_TxSetMessageDataWithSignalData64BitMessage(void)
void testCAN_ConvertBooleanToInteger(void)