Skip to content

Improve Adler32 NMax test#124838

Closed
saucecontrol wants to merge 4 commits intodotnet:mainfrom
saucecontrol:adler-test
Closed

Improve Adler32 NMax test#124838
saucecontrol wants to merge 4 commits intodotnet:mainfrom
saucecontrol:adler-test

Conversation

@saucecontrol
Copy link
Member

@saucecontrol saucecontrol commented Feb 25, 2026

The existing test doesn't check the actual boundary condition. For example, if NMax is changed to 8192 in the Adler32 implementation, the tests still pass.

This change correctly breaks if NMax is set as small as 5553.

Copilot AI review requested due to automatic review settings February 25, 2026 01:05
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Feb 25, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates Adler32Tests to validate the actual overflow boundary condition tied to Adler32’s delayed-modulo optimization (NMax), ensuring the test fails if NMax is reduced below the safe threshold.

Changes:

  • Reworks LargeInput_ExceedsNMax to prime the hasher into a maximal checksum state, then append max-value bytes to stress 32-bit intermediate overflow behavior.
  • Updates expected checksum values to match the new (stronger) test construction.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-hashing, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

Copilot AI review requested due to automatic review settings February 28, 2026 17:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/libraries/System.IO.Hashing/tests/Adler32Tests.cs:241

  • This change removes AllMaxBytes_MatchesReference, which was the only place in this file that validated one-shot Adler32.HashToUInt32 against the reference implementation for an all-0xFF input across a range of lengths (including small vector-friendly sizes). If the intent isn’t to drop that coverage, consider keeping an all-0xFF reference comparison (either by restoring the test or folding a few representative 0xFF cases into an existing theory).
        /// <summary>
        /// Tests incremental appending with various chunk sizes to verify that the
        /// vectorized paths produce the same result regardless of how data is fed in.
        /// </summary>

Comment on lines +174 to +175
// Starting from an already-maxed checksum, a stream of 5553 max value
// bytes will overflow if not reduced by mod 65521 before the last byte.
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

The comment refers specifically to a stream of "5553" max-value bytes overflowing, but this theory also runs with length=11104. To keep the test documentation accurate, update the wording to refer to the parameter (e.g., "a stream of length max-value bytes" / explain why 5553 is the critical boundary case).

Suggested change
// Starting from an already-maxed checksum, a stream of 5553 max value
// bytes will overflow if not reduced by mod 65521 before the last byte.
// Starting from an already-maxed checksum, a stream of length max-value
// bytes will overflow if not reduced by mod 65521 before the last byte.
// 5553 is the smallest such length; 11104 is a larger stress case.

Copilot uses AI. Check for mistakes.
Comment on lines 157 to 162
public void LargeInput_ExceedsNMax(int length, uint expected)
{
byte[] data = new byte[length];
for (int i = 0; i < data.Length; i++)
{
data[i] = (byte)('a' + (i % 26));
}

Assert.Equal(expected, Adler32.HashToUInt32(data));
// This test ensures that Adler32 optimizations involving delayed modulo
// do not overflow a 32-bit intermediate at any point.

var alg = new Adler32();
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

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

LargeInput_ExceedsNMax now relies on first priming the instance to a maxed checksum state before appending the length bytes. The current name suggests the test is only about input size, but the setup (maxed starting state + 0xFF data) is the key condition being validated; consider renaming to reflect that so future readers don’t accidentally “simplify” the setup and lose the boundary coverage.

Copilot uses AI. Check for mistakes.
@saucecontrol
Copy link
Member Author

I'm going to put up a PR with a new vectorized Adler32 implementation. Will include the test changes in that one.

@saucecontrol saucecontrol deleted the adler-test branch March 5, 2026 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Hashing community-contribution Indicates that the PR has been added by a community member test-enhancement Improvements of test source code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants