Files
go1_pro_sdk/tests/test_common.py
2026-07-26 20:51:57 +08:00

28 lines
707 B
Python

"""Common codec utility tests."""
import struct
from go1_pro_sdk.utils.common import gen_crc
def _gen_crc_bitwise(data: bytes) -> bytes:
crc = 0xFFFFFFFF
for (word,) in struct.iter_unpack("<I", data):
for bit in range(32):
x = (crc >> 31) & 1
crc <<= 1
crc &= 0xFFFFFFFF
if x ^ (1 & (word >> (31 - bit))):
crc ^= 0x04c11db7
return struct.pack("<I", crc)
def test_gen_crc_matches_reference_bitwise():
samples = [
b"",
bytes(4),
bytes(range(64)),
b"\xfe\xef\xff\x00" + bytes(range(252)) * 2,
]
for sample in samples:
assert gen_crc(sample) == _gen_crc_bitwise(sample)