优化crc速度
This commit is contained in:
27
tests/test_common.py
Normal file
27
tests/test_common.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""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)
|
||||
Reference in New Issue
Block a user