Add tests for ByteUtil, flesh out other half of the split/join API.

This commit is contained in:
Jennifer Taylor 2021-10-20 15:12:33 +00:00
parent b837742d6b
commit 80c301ff85
2 changed files with 56 additions and 0 deletions

View File

@ -303,7 +303,22 @@ class ByteUtil:
]
return b''.join(chunks)
@staticmethod
def split16bithalves(data: bytes) -> Tuple[bytes, bytes]:
length = len(data)
return(
b''.join(data[x:(x + 2)] for x in range(0, length, 4)),
b''.join(data[(x + 2):(x + 4)] for x in range(0, length, 4)),
)
@staticmethod
def combine8bithalves(upper: bytes, lower: bytes) -> bytes:
chunks = [bytes([upper[i], lower[i]]) for i in range(len(upper))]
return b''.join(chunks)
@staticmethod
def split8bithalves(data: bytes) -> Tuple[bytes, bytes]:
return (
bytes([d for d in data[::2]]),
bytes([d for d in data[1::2]]),
)

41
tests/test_ByteUtil.py Normal file
View File

@ -0,0 +1,41 @@
from arcadeutils import ByteUtil
import unittest
class TestBinaryDiff(unittest.TestCase):
def test_byteswap(self) -> None:
self.assertEqual(
ByteUtil.byteswap(b"abcd1234"),
b'badc2143',
)
def test_wordswap(self) -> None:
self.assertEqual(
ByteUtil.wordswap(b"abcd1234"),
b'dcba4321',
)
def test_combine8bithalves(self) -> None:
self.assertEqual(
ByteUtil.combine8bithalves(b"1234", b"abcd"),
b"1a2b3c4d",
)
def test_split8bithalves(self) -> None:
self.assertEqual(
ByteUtil.split8bithalves(b"1a2b3c4d"),
(b"1234", b"abcd"),
)
def test_combine16bithalves(self) -> None:
self.assertEqual(
ByteUtil.combine16bithalves(b"1234", b"abcd"),
b"12ab34cd",
)
def test_split16bithalves(self) -> None:
self.assertEqual(
ByteUtil.split16bithalves(b"1a2b3c4d"),
(b"1a3c", b"2b4d"),
)