mirror of
https://github.com/romanz/amodem.git
synced 2026-04-20 04:56:25 +08:00
framing: handle bitstream & replace ECC by CRC-32
This commit is contained in:
@@ -1,32 +1,35 @@
|
||||
from amodem import framing
|
||||
import random
|
||||
import itertools
|
||||
import reedsolo
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def concat(chunks):
|
||||
return bytearray(itertools.chain.from_iterable(chunks))
|
||||
def concat(iterable):
|
||||
return bytearray(itertools.chain.from_iterable(iterable))
|
||||
|
||||
r = random.Random(0)
|
||||
blob = bytearray(r.randrange(0, 256) for i in range(64 * 1024))
|
||||
|
||||
|
||||
def test_random():
|
||||
r = random.Random(0)
|
||||
x = bytearray(r.randrange(0, 256) for i in range(64 * 1024))
|
||||
y = framing.encode(x)
|
||||
x_ = concat(framing.decode(y))
|
||||
assert x_ == x
|
||||
@pytest.fixture(params=[b'', b'abc', b'1234567890', blob, blob[:12345]])
|
||||
def data(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def test_errors():
|
||||
data = bytearray(range(244))
|
||||
blocks = list(framing.encode(data))
|
||||
assert len(blocks) == 2
|
||||
for i in range(framing.DEFAULT_NSYM // 2):
|
||||
blocks[0][i] = blocks[0][i] ^ 0xFF
|
||||
def test_checksum(data):
|
||||
c = framing.Checksum()
|
||||
assert c.decode(c.encode(data)) == data
|
||||
|
||||
i = framing.DEFAULT_NSYM // 2
|
||||
try:
|
||||
blocks[0][i] = blocks[0][i] ^ 0xFF
|
||||
concat(framing.decode(blocks))
|
||||
assert False
|
||||
except reedsolo.ReedSolomonError as e:
|
||||
assert e.args == ('Too many errors to correct',)
|
||||
|
||||
def test_framer(data):
|
||||
f = framing.Framer()
|
||||
encoded = concat(f.encode(data))
|
||||
decoded = concat(f.decode(encoded))
|
||||
assert decoded == data
|
||||
|
||||
|
||||
def test_main(data):
|
||||
encoded = framing.encode(data)
|
||||
decoded = framing.decode(encoded)
|
||||
assert bytearray(decoded) == data
|
||||
|
||||
Reference in New Issue
Block a user