Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 26, 2026

Description

Adds AppendingEmptyHasNoEffect() test to NonCryptoHashTestDriver base class, verifying that interleaving empty span appends with actual data produces identical hashes to appending data alone.

Changes

  • Added [Fact] test method to NonCryptoHashTestDriver.cs
  • Test pattern: reference hash with [1,2,3] vs. hash with Empty + [1,2,3] + Empty
  • Automatically applies to all implementations: CRC32, CRC64, XxHash32, XxHash64 (including seeded variants), and future additions
[Fact]
public void AppendingEmptyHasNoEffect()
{
    NonCryptographicHashAlgorithm reference = CreateInstance();
    reference.Append(new byte[] { 1, 2, 3 });
    byte[] expected = reference.GetCurrentHash();

    NonCryptographicHashAlgorithm hash = CreateInstance();
    hash.Append(ReadOnlySpan<byte>.Empty);
    hash.Append(new byte[] { 1, 2, 3 });
    hash.Append(ReadOnlySpan<byte>.Empty);
    byte[] actual = hash.GetCurrentHash();

    Assert.Equal(expected, actual);
}

Testing

All 1061 tests pass, including 8 instances of the new test across hash implementations.

Original prompt

Summary

Add a new test to the NonCryptoHashTestDriver base class that verifies appending empty data (empty spans/arrays) has no observable effect on the hash result.

Background

This was requested by @vcsjones in PR #123601 review comment: #123601 (comment)

I think we should add a test to the base test fixtures that basically do:

public void Test1()
{
    Hash reference = new();
    reference.Append([1, 2, 3]);
    uint expected = reference.GetCurrentHashAsUInt32();

    Hash h = new();
    h.Append([]);
    h.Append([1, 2, 3]);
    h.Append([]);
    uint actual = h.GetCurrentHashAsUInt32();
    
    Assert.Equal(expected, actual);
}

I am not aware of any hash function implementations where appending empty has an observable effect.

Implementation

Add a new [Fact] test method to src/libraries/System.IO.Hashing/tests/NonCryptoHashTestDriver.cs that:

  1. Creates a reference hash instance and appends some test data (e.g., [1, 2, 3])
  2. Gets the expected hash value
  3. Creates another hash instance
  4. Appends empty data, then the same test data, then empty data again
  5. Gets the actual hash value
  6. Asserts they are equal

Example implementation:

[Fact]
public void AppendingEmptyHasNoEffect()
{
    NonCryptographicHashAlgorithm reference = CreateInstance();
    reference.Append(new byte[] { 1, 2, 3 });
    byte[] expected = reference.GetCurrentHash();

    NonCryptographicHashAlgorithm hash = CreateInstance();
    hash.Append(ReadOnlySpan<byte>.Empty);
    hash.Append(new byte[] { 1, 2, 3 });
    hash.Append(ReadOnlySpan<byte>.Empty);
    byte[] actual = hash.GetCurrentHash();

    Assert.Equal(expected, actual);
}

This test will automatically run for all NonCryptographicHashAlgorithm implementations (CRC32, CRC64, XxHash32, XxHash64, XxHash3, XxHash128, and the new Adler32), ensuring consistent behavior across the board.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Add test for appending empty data in NonCryptoHashTestDriver Add test verifying empty data appends have no effect on hash results Jan 26, 2026
Copilot AI requested a review from stephentoub January 26, 2026 22:52
@stephentoub stephentoub marked this pull request as ready for review January 26, 2026 22:54
Copilot AI review requested due to automatic review settings January 26, 2026 22:54
@stephentoub stephentoub enabled auto-merge (squash) January 26, 2026 22:54
@dotnet-policy-service
Copy link
Contributor

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

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

This PR adds a new test to verify that appending empty data (empty spans) has no observable effect on hash computation results across all non-cryptographic hash algorithm implementations.

Changes:

  • Added AppendingEmptyHasNoEffect() test method to NonCryptoHashTestDriver base class
  • Test validates that interleaving empty span appends with actual data produces identical hashes to appending data alone
  • Automatically applies to all implementations: CRC32, CRC64, XxHash32, XxHash64 (including seeded variants)

@stephentoub
Copy link
Member

/ba-g "Failed to generate "CoreCLR component" build project!" unrelated to this test change

@stephentoub stephentoub merged commit 2a3cec1 into main Jan 29, 2026
85 of 88 checks passed
@stephentoub stephentoub deleted the copilot/add-test-empty-data-appending branch January 29, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants