Files
templates/python/tests/test_tms_terminal_shared.py

44 lines
1.6 KiB
Python

"""Wire vectors shared with Android and the native TMS2812 host test."""
import pytest
from protocan import tms_terminal as tms
def test_ping_and_poke_vectors():
assert tms.ping(10).hex() == "0a3800010000f0b4"
raw = tms.request(10, 0x12345678, 0xABCD)
assert raw[:10].hex() == "0a3978563412cdab0000"
assert tms.crc16_modbus(raw) == 0
def test_test_all_vector():
digits = bytes.fromhex("810204080920102040800304")
raw = tms.build_test_request(7, 25, 18, digits)
assert raw[:18].hex() == "074219001200810204080920102040800304"
assert len(raw) == 20 and tms.crc16_modbus(raw) == 0
def test_response_accepts_only_missing_reserved_bytes():
raw = bytes.fromhex("0a3865a50000af9b0000")
for size in (8, 9, 10):
assert tms.decode_reply(raw[:size], 10, 56) == 0xA565
for invalid in (raw[:7], raw + b"\0", raw[:2] + b"\0" + raw[3:]):
with pytest.raises(ValueError):
tms.decode_reply(invalid, 10, 56)
with pytest.raises(ValueError):
tms.decode_reply(raw, 11, 56)
@pytest.mark.parametrize("args", [(256, 0, None), (1, -1, None), (1, 2**32, None), (1, 0, -1), (1, 0, 65536)])
def test_rejects_out_of_range_before_native_conversion(args):
with pytest.raises(ValueError):
tms.request(*args)
def test_uart_response_ignores_noise_and_preliminary_ack():
response = bytes.fromhex("0a3865a50000af9b0000")
ack = tms.build_fixed_request(10, 56, b"")
for prefix in (b"", ack, ack + b"\0\0"):
assert tms.memory_response(b"noise" + prefix + response, 10, 56) == response
assert tms.memory_response(response, 11, 56) is None
assert tms.memory_response(response[:7], 10, 56) is None