Skip to content

Verify Kraft's inequality is not violated - #804

Open
fkjellberg wants to merge 1 commit into
apache:masterfrom
fkjellberg:verify-krafts-inequality
Open

Verify Kraft's inequality is not violated#804
fkjellberg wants to merge 1 commit into
apache:masterfrom
fkjellberg:verify-krafts-inequality

Conversation

@fkjellberg

Copy link
Copy Markdown
Contributor

Generated-by: Claude Opus 5 noreply@anthropic.com

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

The documentation for HuffmanDecoder states that the code lengths must not violate Kraft's inequality but this is never checked. It turns out that some of the existing test cases violate this prerequisite.

HuffmanDecoder will now throw CompressorException if the code lengths violate Kraft's inequality. Without this early check before actually start decoding, the decoder may output corrupt data that hopefully will be detected by the CRC checksum or it may throw other unexpected exceptions while decoding the input stream.

I used Claude Opus 5 to generate some of the code and to help investigate and fix the failing test cases.

Generated-by: Claude Opus 5 noreply@anthropic.com

Generated-by: Claude Opus 5 <noreply@anthropic.com>

@garydgregory garydgregory left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @fkjellberg

Thank you for the PR.

A few comments:

  • There are no explicit test for a Kraft violation where minCodeLength > 1 or for a violation at a deeper length > 2. The current test only hits length 2.
  • The class-level comment could mention “Kraft’s inequality”.
  • The exception message could add the offending count and available nodes to aid debugging, but is not required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants