Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unstable return value of digest #153

Merged
merged 4 commits into from
Apr 14, 2024
Merged
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
7 changes: 7 additions & 0 deletions docs/docs/changelog/unreleased.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Unreleased

## 🐞 Bug Fixes
* Fixed unstable return values of `digest` function. For more details, see issue [#151](https://github.com/Nicoretti/crc/issues/151).

!!! bug
This issue specifically affected scenarios where the CRC register was manually manipulated. Standard usages of the `Calculator` class were not impacted.
Furthermore, this issue primarily occurred in configurations that required reverse output.

## 📚 Documentation
* Add overview of crc configurations

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "crc"
version = "6.1.1"
version = "6.1.2"
description = "Library and CLI to calculate and verify all kinds of CRC checksums"
packages = [
{ include = "crc", from = "src" },
5 changes: 2 additions & 3 deletions src/crc/_crc.py
Original file line number Diff line number Diff line change
@@ -253,9 +253,8 @@ def digest(self) -> int:
"""
See `AbstractRegister.digest`
"""
if self._config.reverse_output:
self.register = self.reverse()
return self.register ^ self._config.final_xor_value
value = self.reverse() if self._config.reverse_output else self.register
return value ^ self._config.final_xor_value

def reverse(self) -> int:
"""
42 changes: 42 additions & 0 deletions test/unit/regression/test_unstable_digest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
This module contains regression tests regarding the issue of unstable return values from the `digest` function.
The tests ensure that the `digest` function consistently returns the expected output for given inputs.
For more context and a detailed discussion of the problem, refer to the GitHub issue:
https://github.com/Nicoretti/crc/issues/151
"""

import itertools

import pytest

import crc


def test_original_regression():
reg = crc.Register(crc.Crc8.BLUETOOTH)
reg.init()
reg.update(b"Hello World!")

times = 10
expected = [81 for _ in range(0, times)]
actual = [reg.digest() for _ in range(0, times)]

assert actual == expected


@pytest.mark.parametrize(
"configuration,times,expected",
[
(config, 10, crc.Calculator(config).checksum(b"Hello World!"))
for config in itertools.chain(crc.Crc8, crc.Crc16, crc.Crc32, crc.Crc64)
],
)
def test_digest_is_stable(configuration, times, expected):
expected = [expected for _ in range(times)]

reg = crc.Register(configuration)
reg.init()
reg.update(b"Hello World!")
actual = [reg.digest() for _ in range(times)]

assert actual == expected