-
Notifications
You must be signed in to change notification settings - Fork 137
Add unit test file for hash package #1530
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
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
|
Welcome @manmathbh! It looks like this is your first PR to kmesh-net/kmesh 🎉 |
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
}There was a problem hiding this 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.
| h1_1, h2_1 := Hash128(testData, seed) | ||
| h1_2, h2_2 := Hash128(testData, seed) |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
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.
| h1_1, h2_1 := Hash128(data1, seed) | ||
| h1_2, h2_2 := Hash128(data2, seed) |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
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.
| } | ||
| } | ||
|
|
||
| func TestRotl64(t *testing.T) { |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
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).
| func TestRotl64(t *testing.T) { | |
| func Test_rotl64(t *testing.T) { |
| } | ||
| } | ||
|
|
||
| func TestFmix64(t *testing.T) { |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
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).
| func TestFmix64(t *testing.T) { | |
| func Test_fmix64(t *testing.T) { |
| } | ||
| } | ||
|
|
||
| func TestFmix64_Avalanche(t *testing.T) { |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
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.
| func TestFmix64_Avalanche(t *testing.T) { | |
| func Test_fmix64_Avalanche(t *testing.T) { |
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!