Skip to content

Commit

Permalink
Merge pull request briansmith#4 from xnum/master
Browse files Browse the repository at this point in the history
Fix generateMultiHash write data to source data slice
  • Loading branch information
Tanner Ryan authored Oct 8, 2019
2 parents 6bf7b14 + a46c755 commit 7b27005
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ func bytesToUint64(b []byte) uint64 {
// generateMultihash returns 4 64-bit (2 x 128-bit) MurmurHash3 hashes.
func generateMultiHash(data []byte) [4]uint64 {
h1, h2 := murmur128(data)
h3, h4 := murmur128(append(data, single))
buff := make([]byte, len(data)+1)
copy(buff, data)
buff[len(data)] = single
h3, h4 := murmur128(buff)
return [4]uint64{h1, h2, h3, h4}
}

Expand Down
37 changes: 37 additions & 0 deletions hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ring

import "testing"

func BenchmarkGenerateMultiHash(b *testing.B) {
data := []byte{0x00, 0x12, 0x34, 0x56, 0x78, 0x00}
buff := make([]byte, len(data))
for i := 0; i < b.N; i++ {
copy(buff, data)
generateMultiHash(buff[1:5])
}
}

func TestGenerateMultiHash(t *testing.T) {
data := []byte{
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
0x00, 0x12, 0x34, 0x56, 0x78, 0x00,
}
buff := make([]byte, len(data))
copy(buff, data)
generateMultiHash(buff[1:20])

for i := range data {
if data[i] != buff[i] {
t.Fatalf("data not match at index: %v", i)
}
}
}

0 comments on commit 7b27005

Please sign in to comment.