55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from pathlib import Path
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from udp_teleop_bridge.protocol import ( # noqa: E402
|
|
PACKET_SIZE,
|
|
default_server_addr_for_transport,
|
|
normalize_command,
|
|
normalize_transport,
|
|
pack_command,
|
|
unpack_command,
|
|
)
|
|
|
|
|
|
def test_pack_unpack_round_trip() -> None:
|
|
command = (0.1, -0.2, 0.3, -0.4, 0.5, -0.6)
|
|
|
|
payload = pack_command(command)
|
|
|
|
assert len(payload) == PACKET_SIZE
|
|
assert unpack_command(payload) == pytest.approx(command)
|
|
|
|
|
|
@pytest.mark.parametrize('value', [float('nan'), float('inf'), float('-inf')])
|
|
def test_normalize_command_rejects_non_finite_values(value: float) -> None:
|
|
with pytest.raises(ValueError, match='non-finite'):
|
|
normalize_command((0.0, 0.0, value, 0.0, 0.0, 0.0))
|
|
|
|
|
|
def test_unpack_command_rejects_wrong_length() -> None:
|
|
with pytest.raises(ValueError, match='Expected'):
|
|
unpack_command(b'\x00' * (PACKET_SIZE - 1))
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
('transport', 'expected'),
|
|
[
|
|
('udp', '127.0.0.1:9001'),
|
|
('kcp', '127.0.0.1:9002'),
|
|
],
|
|
)
|
|
def test_default_server_addr_for_transport(transport: str, expected: str) -> None:
|
|
assert default_server_addr_for_transport(transport) == expected
|
|
|
|
|
|
def test_normalize_transport_rejects_unknown_value() -> None:
|
|
with pytest.raises(ValueError, match='Unsupported transport'):
|
|
normalize_transport('sctp')
|