Skip to content

Conversation

@manmathbh
Copy link
Contributor

Add unit test file for hash package

This pull request adds unit and benchmark tests for the hash package. These tests cover Hash128, rotl64, and fmix64 functions, aiming to improve reliability and test coverage.

Fixes #1529

Let me know if there's anything I should improve or adjust!

Copilot AI review requested due to automatic review settings November 25, 2025 15:21
@kmesh-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign okabe-rintarou-0 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot
Copy link
Collaborator

Welcome @manmathbh! It looks like this is your first PR to kmesh-net/kmesh 🎉

@gemini-code-assist
Copy link

Summary of Changes

Hello @manmathbh, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the quality and reliability of the hash package by introducing a dedicated test file. It includes a wide array of unit tests for the Hash128 function and its underlying helper functions (rotl64, fmix64), ensuring their correctness and robustness across various inputs. Additionally, benchmark tests have been added to evaluate the performance characteristics of the Hash128 function, contributing to a more stable and performant codebase.

Highlights

  • New Test File: A new file, pkg/utils/hash/hash_test.go, has been added to introduce comprehensive testing for the hash package.
  • Hash128 Unit Tests: Extensive unit tests for the Hash128 function have been implemented, covering various scenarios such as empty inputs, single bytes, different string lengths, determinism, distinct inputs, and all possible tail lengths (0-15 bytes).
  • Rotl64 and Fmix64 Unit Tests: Unit tests for the rotl64 (rotate left 64-bit) and fmix64 (finalization mix 64-bit) helper functions have been added to ensure their correct bitwise operations and mixing properties, including an avalanche test for fmix64.
  • Hash128 Benchmarks: Benchmark tests for Hash128 have been included to measure its performance with small, medium, and large input data sizes, providing insights into its efficiency.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Bits dance, a hash is born, Tests ensure no logic's torn, Fast and true, its path is worn, Reliability, new morn.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copilot finished reviewing on behalf of manmathbh November 25, 2025 15:23
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a comprehensive set of unit and benchmark tests for the hash package, which is a great improvement for code reliability and maintainability. The tests are well-structured, covering various inputs, edge cases like tail lengths, and properties like determinism and avalanche effect.

I've found one critical area where the test coverage could be improved. The underlying Hash128 implementation uses unsafe.Pointer, which can lead to panics on certain CPU architectures if the input data is not memory-aligned. I've added a comment with a suggestion to add a test case that specifically checks for this potential crash. Addressing this will make the hash function more robust and prevent runtime panics on different platforms.

"testing"
)

func TestHash128(t *testing.T) {

Choose a reason for hiding this comment

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

critical

The Hash128 implementation in murmur3.go uses unsafe.Pointer to cast a byte slice to *[2]uint64. This operation can cause a panic on CPU architectures that require memory access to be aligned (e.g., some ARM or MIPS processors) if the input data slice does not start on an 8-byte boundary.

This is a critical correctness issue that can lead to unexpected crashes. The current test suite does not cover this scenario. Please add a test case to verify that Hash128 can handle unaligned data without panicking. While the ideal solution is to fix Hash128 to use encoding/binary for safe unaligned reads, adding a test is a crucial first step to highlight and prevent this issue.

Here is a suggested test function to add:

func TestHash128_UnalignedInput(t *testing.T) {
	// Create a buffer and then a sub-slice that is not 8-byte aligned.
	buf := make([]byte, 33)
	for i := range buf {
		buf[i] = byte(i)
	}
	unalignedData := buf[1:] // len=32, but potentially unaligned for uint64 reads.

	// The test should verify that hashing unaligned data does not cause a panic.
	defer func() {
		if r := recover(); r != nil {
			t.Errorf("Hash128 panicked on unaligned data: %v", r)
		}
	}()

	// This call should not panic, even on architectures that enforce alignment.
	Hash128(unalignedData, 0)
}

Copy link

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 pull request adds comprehensive unit and benchmark tests for the hash package's MurmurHash3 implementation. The tests provide coverage for the Hash128 function and its helper functions (rotl64 and fmix64), improving code reliability and maintainability.

  • Adds parametrized tests for Hash128 with various input scenarios including edge cases
  • Includes tests for unexported helper functions rotl64 and fmix64
  • Adds benchmark tests for Hash128 with small, medium, and large data sizes

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +111 to +112
h1_1, h2_1 := Hash128(testData, seed)
h1_2, h2_2 := Hash128(testData, seed)
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

[nitpick] Variable names use inconsistent formatting with underscores (h1_1, h2_1, h1_2, h2_2). In Go, camelCase is preferred. Consider renaming to h1First, h2First, h1Second, h2Second or similar.

Copilot uses AI. Check for mistakes.
Comment on lines +125 to +126
h1_1, h2_1 := Hash128(data1, seed)
h1_2, h2_2 := Hash128(data2, seed)
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

[nitpick] Variable names use inconsistent formatting with underscores (h1_1, h2_1, h1_2, h2_2). In Go, camelCase is preferred. Consider renaming to h1Data1, h2Data1, h1Data2, h2Data2 or similar.

Copilot uses AI. Check for mistakes.
}
}

func TestRotl64(t *testing.T) {
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Test function name should be Test_rotl64 (with lowercase 'r') to follow Go conventions for testing unexported functions, as seen in other tests in the codebase (e.g., Test_ConvertIpToUint32, Test_buildAccesslog).

Suggested change
func TestRotl64(t *testing.T) {
func Test_rotl64(t *testing.T) {

Copilot uses AI. Check for mistakes.
}
}

func TestFmix64(t *testing.T) {
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Test function name should be Test_fmix64 (with lowercase 'f') to follow Go conventions for testing unexported functions, as seen in other tests in the codebase (e.g., Test_ConvertIpToUint32, Test_buildAccesslog).

Suggested change
func TestFmix64(t *testing.T) {
func Test_fmix64(t *testing.T) {

Copilot uses AI. Check for mistakes.
}
}

func TestFmix64_Avalanche(t *testing.T) {
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

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

Test function name should be Test_fmix64_Avalanche (with lowercase 'f') to follow Go conventions for testing unexported functions, as seen in other tests in the codebase.

Suggested change
func TestFmix64_Avalanche(t *testing.T) {
func Test_fmix64_Avalanche(t *testing.T) {

Copilot uses AI. Check for mistakes.
@manmathbh
Copy link
Contributor Author

Closing this PR as the changes have been moved to a feature branch to follow best practices.

Continued in #1532

Related to issue #1529

@manmathbh manmathbh closed this Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add unit tests for hash package

2 participants