Skip to content

Commit 9e30cd8

Browse files
committed
This reverts commit a344a13.
1 parent 6fdf0b7 commit 9e30cd8

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

test/test_socketcan.py

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
"""
44
Test functions in `can.interfaces.socketcan.socketcan`.
55
"""
6-
import ctypes
7-
import struct
86
import unittest
7+
8+
from unittest.mock import Mock
99
from unittest.mock import patch
10+
from unittest.mock import call
11+
12+
import ctypes
1013

1114
from can.interfaces.socketcan.socketcan import (
1215
bcm_header_factory,
@@ -237,25 +240,51 @@ def side_effect_ctypes_alignment(value):
237240
]
238241
self.assertEqual(expected_fields, BcmMsgHead._fields_)
239242

240-
def test_build_bcm_header(self):
241-
def _find_u32_fmt_char() -> str:
242-
for _fmt in ("H", "I", "L", "Q"):
243-
if struct.calcsize(_fmt) == 4:
244-
return _fmt
243+
@unittest.skipIf(
244+
not (
245+
ctypes.sizeof(ctypes.c_long) == 4 and ctypes.alignment(ctypes.c_long) == 4
246+
),
247+
"Should only run on platforms where sizeof(long) == 4 and alignof(long) == 4",
248+
)
249+
def test_build_bcm_header_sizeof_long_4_alignof_long_4(self):
250+
expected_result = (
251+
b"\x02\x00\x00\x00\x00\x00\x00\x00"
252+
b"\x00\x00\x00\x00\x00\x00\x00\x00"
253+
b"\x00\x00\x00\x00\x00\x00\x00\x00"
254+
b"\x00\x00\x00\x00\x01\x04\x00\x00"
255+
b"\x01\x00\x00\x00\x00\x00\x00\x00"
256+
)
245257

246-
def _standard_size_little_endian_to_native(data: bytes) -> bytes:
247-
std_le_fmt = "<IIIllllII"
248-
native_fmt = "@" + std_le_fmt[1:].replace("I", _find_u32_fmt_char())
249-
aligned_data = struct.pack(native_fmt, *struct.unpack(std_le_fmt, data))
250-
padded_data = aligned_data + b"\x00" * ((8 - len(aligned_data) % 8) % 8)
251-
return padded_data
258+
self.assertEqual(
259+
expected_result,
260+
build_bcm_header(
261+
opcode=CAN_BCM_TX_DELETE,
262+
flags=0,
263+
count=0,
264+
ival1_seconds=0,
265+
ival1_usec=0,
266+
ival2_seconds=0,
267+
ival2_usec=0,
268+
can_id=0x401,
269+
nframes=1,
270+
),
271+
)
252272

253-
expected_result = _standard_size_little_endian_to_native(
273+
@unittest.skipIf(
274+
not (
275+
ctypes.sizeof(ctypes.c_long) == 8 and ctypes.alignment(ctypes.c_long) == 8
276+
),
277+
"Should only run on platforms where sizeof(long) == 8 and alignof(long) == 8",
278+
)
279+
def test_build_bcm_header_sizeof_long_8_alignof_long_8(self):
280+
expected_result = (
254281
b"\x02\x00\x00\x00\x00\x00\x00\x00"
255282
b"\x00\x00\x00\x00\x00\x00\x00\x00"
256283
b"\x00\x00\x00\x00\x00\x00\x00\x00"
257-
b"\x00\x00\x00\x00\x01\x04\x00\x00"
258-
b"\x01\x00\x00\x00"
284+
b"\x00\x00\x00\x00\x00\x00\x00\x00"
285+
b"\x00\x00\x00\x00\x00\x00\x00\x00"
286+
b"\x00\x00\x00\x00\x00\x00\x00\x00"
287+
b"\x01\x04\x00\x00\x01\x00\x00\x00"
259288
)
260289

261290
self.assertEqual(

0 commit comments

Comments
 (0)