Skip to content

Commit

Permalink
fix(): add unit test for new configuration RTC[] reviewer()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shi Hui Yu committed Oct 18, 2024
1 parent d6e7732 commit 89d4a11
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/crc/_crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ class Crc8(enum.Enum):
reverse_input=False,
reverse_output=False,
)
'''
:Alias: ATM
'''

ROHC = Configuration(
width=8,
Expand All @@ -496,14 +499,6 @@ class Crc8(enum.Enum):
reverse_output=True,
)

MAXIM = Configuration(
width=8,
polynomial=0x31,
init_value=0x00,
final_xor_value=0x00,
reverse_input=True,
reverse_output=True,
)


@enum.unique
Expand All @@ -516,6 +511,9 @@ class Crc16(enum.Enum):
reverse_input=False,
reverse_output=False,
)
'''
:Alias: ZMODEM,ACORN
'''

GSM = Configuration(
width=16,
Expand Down Expand Up @@ -552,6 +550,9 @@ class Crc16(enum.Enum):
reverse_input=False,
reverse_output=False,
)
'''
:Alias: CCITT_FALSE
'''

KERMIT = Configuration(
width=16,
Expand All @@ -561,25 +562,10 @@ class Crc16(enum.Enum):
reverse_input=True,
reverse_output=True,
)

CCITT_FALSE = Configuration(
width=16,
polynomial=0x1021,
init_value=0xffff,
final_xor_value=0,
reverse_input=False,
reverse_output=False,
)

CCITT = Configuration(
width=16,
polynomial=0x1021,
init_value=0x0000,
final_xor_value=0,
reverse_input=True,
reverse_output=True,
)

'''
:Alias: CCITT
'''

IBM = Configuration(
width=16,
polynomial=0x8005,
Expand All @@ -588,6 +574,9 @@ class Crc16(enum.Enum):
reverse_input=True,
reverse_output=True,
)
'''
:Alias: ARC,LHA
'''

MAXIM = Configuration(
width=16,
Expand Down
112 changes: 112 additions & 0 deletions test/unit/test_crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,38 @@ def test_crc8_saej1850_zero(self):
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc8_itu(self):
config = Crc8.ITU
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x55),
Fixture(data=string.digits[1:], checksum=0xA1),
Fixture(data=string.digits[1:][::-1], checksum=0xC4),
Fixture(data=string.digits, checksum=0x10),
Fixture(data=string.digits[::-1], checksum=0x3B),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc8_rohc(self):
config = Crc8.ROHC
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0xFF),
Fixture(data=string.digits[1:], checksum=0xD0),
Fixture(data=string.digits[1:][::-1], checksum=0x68),
Fixture(data=string.digits, checksum=0xE3),
Fixture(data=string.digits[::-1], checksum=0x62),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc16_ccitt(self):
config = Crc16.XMODEM
for register_type in self._register_types:
Expand Down Expand Up @@ -300,6 +332,86 @@ def test_cr16_kermit(self):
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_ibm(self):
config = Crc16.IBM
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x0000),
Fixture(data=string.digits[1:], checksum=0xBB3D),
Fixture(data=string.digits[1:][::-1], checksum=0x39D9),
Fixture(data=string.digits, checksum=0x443D),
Fixture(data=string.digits[::-1], checksum=0x8EF8),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_maxim(self):
config = Crc16.MAXIM
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0xFFFF),
Fixture(data=string.digits[1:], checksum=0x44C2),
Fixture(data=string.digits[1:][::-1], checksum=0xC626),
Fixture(data=string.digits, checksum=0xBBC2),
Fixture(data=string.digits[::-1], checksum=0x7107),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_usb(self):
config = Crc16.USB
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x0000),
Fixture(data=string.digits[1:], checksum=0xB4C8),
Fixture(data=string.digits[1:][::-1], checksum=0x362C),
Fixture(data=string.digits, checksum=0xBCB2),
Fixture(data=string.digits[::-1], checksum=0x7677),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_x25(self):
config = Crc16.X25
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0x0000),
Fixture(data=string.digits[1:], checksum=0x906E),
Fixture(data=string.digits[1:][::-1], checksum=0x8578),
Fixture(data=string.digits, checksum=0x3C16),
Fixture(data=string.digits[::-1], checksum=0x3EB1),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_cr16_dnp(self):
config = Crc16.DNP
for register_type in self._register_types:
crc_register = register_type(config)
test_suit = [
Fixture(data="", checksum=0xFFFF),
Fixture(data=string.digits[1:], checksum=0xEA82),
Fixture(data=string.digits[1:][::-1], checksum=0x51A6),
Fixture(data=string.digits, checksum=0x6772),
Fixture(data=string.digits[::-1], checksum=0x4C77),
]
for test in test_suit:
crc_register.init()
crc_register.update(test.data.encode("utf-8"))
self.assertEqual(test.checksum, crc_register.digest())

def test_crc16_with_reflected_input(self):
config = Configuration(16, 0x1021, 0, 0, True, False)
for register_type in self._register_types:
Expand Down

0 comments on commit 89d4a11

Please sign in to comment.