Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/codext/checksums/crc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

CRC = {
'': {
'': (0x1021, 0xc6c6, True, True, 0, 0xbf05),
'a': (0x1021, 0xc6c6, True, True, 0, 0xbf05),
},
8: {
Expand Down Expand Up @@ -78,6 +79,7 @@
'mpt1327': (0x6815, 0, False, False, 1, 0x2566),
},
16: {
'': (0x8005, 0, True, True, 0, 0xbb3d),
'acorn': (0x1021, 0, False, False, 0, 0x31c3),
'arc': (0x8005, 0, True, True, 0, 0xbb3d),
'atom': (0x002d, 0, True, True, 0, 0x4287),
Expand Down
19 changes: 19 additions & 0 deletions tests/test_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ def test_codec_checksum_functions(self):
for s, r in [("", ""), ("0", "0"), ("1", "8"), ("7992739871", "3")]:
self.assertEqual(codecs.encode(s, "luhn"), r)
self.assertEqual(codecs.encode("-", "luhn", errors="ignore"), "")

def test_codec_crc_bare_defaults(self):
# a crc<width> used without a variant suffix must resolve to that family's default ;
# width 16 and the crc-a family had no default and crashed with KeyError instead
from codext.checksums.crc import CRC
for n in CRC.keys():
name = "crc%d" % n if isinstance(n, int) else "crc"
expected = "%0{}x".format(round((n or 16)/4+.5)) % CRC[n][""][5]
self.assertEqual(codecs.encode("123456789", name), expected)
self.assertEqual(codecs.encode(b"123456789", name), b(expected))
# bare crc-16 is CRC-16/ARC (poly 0x8005), the same as its arc/ibm/lha aliases
for name in ["crc16", "crc-16", "crc_16", "crc16-arc", "crc16-ibm", "crc16-lha"]:
self.assertEqual(codecs.encode("123456789", name), "bb3d")
# bare crc is CRC-A, the same as the crca alias
self.assertEqual(codecs.encode("123456789", "crc"), "bf05")
self.assertEqual(codecs.encode("123456789", "crca"), "bf05")
# adding a default must not make an unknown variant resolve
self.assertRaises(LookupError, codecs.encode, "123456789", "crc16-bad")
self.assertRaises(LookupError, codecs.encode, "123456789", "crc-bad")

def test_codec_dummy_str_manips(self):
STR = "this is a test"
Expand Down
Loading