RAFlasher: add SCI (UART) method
[renesas-ra-flasher] / tests / test_parse.py
1 from raflash.RAPacker import calc_sum, unpack_pkt, pack_pkt
2 import pytest
3
4 def test_calc_sum():
5     assert calc_sum(0x12, ['0x00']) == (0, 0x2, 0xEC)
6     assert calc_sum(0x34, ['0x00']) == (0, 0x2, 0xCA)
7     assert calc_sum(0x00, ['0x00']) == (0, 0x02, 0xFE)
8
9 def test_unpack():
10     assert unpack_pkt(b'\x81\x00\x02\x00\x00\xFE\x03') == ['0x00']
11     assert unpack_pkt(b'\x81\x00\x02\x12\x00\xEC\x03') == ['0x00']
12     assert unpack_pkt(b'\x81\x00\x02\x13\x00\xEB\x03') == ['0x00']
13
14 def test_read_unpack():
15     assert unpack_pkt(b'\x81\x00\x04\x15\xAA\xBB\xCC\xB6\x03') == ['0xAA', '0xBB', '0xCC']
16
17 def test_pack_unpack():
18     assert unpack_pkt(pack_pkt(0x13, ['0x00','0x01','0x02'], ack=True)) == ['0x00', '0x01', '0x02']
19     assert unpack_pkt(pack_pkt(0x34, ['0x00'], ack=True)) == ['0x00']
20     assert unpack_pkt(pack_pkt(0x00, ['0x00'], ack=True)) == ['0x00']
21     assert unpack_pkt(pack_pkt(0x12, ['0x00'], ack=True)) == ['0x00']
22
23 def test_err_unpack():
24     with pytest.raises(ValueError, match=r".*0xC3.*") as excinfo:
25         unpack_pkt(b'\x81\x00\x02\x93\xC3\x38\x03')
26     assert str(excinfo.value) == 'MCU encountered error 0xC3'