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