Skip to content

Commit

Permalink
Add tests for different chunk sizes and offsets
Browse files Browse the repository at this point in the history
The latter in particular is useful to make sure that the different
memory alignment code paths in the C code are correct.

Signed-off-by: Rodrigo Tobar <[email protected]>
  • Loading branch information
rtobar committed Jul 23, 2024
1 parent ea59ae6 commit c8ce465
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Adding explicit fallthrough annotations
in several ``switch`` C statements
for clarity, and to avoid potential warnings (#46).
* Run test against different memory alignments.

## [2.4.1]

Expand Down
20 changes: 20 additions & 0 deletions test/test_crc32c.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def ushort_as_bytes(x):
def uchar_as_bytes(c):
return struct.pack('<B', c)

def batched(x, size):
length = len(x)
for i in range(0, length, size):
yield x[i: min(i + size, length)]

if sys.version_info[0] == 2:
def as_individual_bytes(x):
return x
Expand Down Expand Up @@ -120,6 +125,21 @@ def test_piece_by_piece(self):
c = crc32c.crc32c(x, c)
self.assertEqual(self.checksum, c)

def test_by_different_chunk_lenghts(self):
for chunk_size in range(1, 33):
c = 0
for chunk in batched(self.val, chunk_size):
c = crc32c.crc32c(bytes(chunk), c)
self.assertEqual(self.checksum, c)

def test_by_different_memory_offsets(self):
for offset in range(16):
val = memoryview(self.val)
c = crc32c.crc32c(val[0:offset])
c = crc32c.crc32c(val[offset:], c)
self.assertEqual(self.checksum, c, "Invalid checksum when splitting at offset %d" % offset)


# Generate the actual unittest classes for each of the testing values
if crc32c is not None:
for name, val, checksum in (numbers1, numbers2, numbers3, phrase, long_phrase):
Expand Down

0 comments on commit c8ce465

Please sign in to comment.