Skip to content

Commit 07af271

Browse files
committed
Add The Test
Signed-off-by: adity1raut <araut7798@gmail.com>
1 parent 10f4cac commit 07af271

File tree

1 file changed

+177
-176
lines changed

1 file changed

+177
-176
lines changed

pkg/utils/hash/murmur3_test.go

Lines changed: 177 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -19,213 +19,214 @@ package hash
1919
import (
2020
"testing"
2121
)
22+
2223
func TestSum64(t *testing.T) {
23-
tests := []struct {
24-
name string
25-
input []byte
26-
expected uint64
27-
}{
28-
{
29-
name: "empty byte slice",
30-
input: []byte{},
31-
expected: 17241709254077376921,
32-
},
33-
{
34-
name: "single byte",
35-
input: []byte{0x42},
36-
expected: 7046029254386353131,
37-
},
38-
{
39-
name: "hello world",
40-
input: []byte("hello world"),
41-
expected: 5020219685658847592,
42-
},
43-
{
44-
name: "longer string",
45-
input: []byte("The quick brown fox jumps over the lazy dog"),
46-
expected: 0xa8a6e93b487c8ad4,
47-
},
48-
{
49-
name: "binary data",
50-
input: []byte{0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd},
51-
expected: 0x4a1b9e4a4c9a7a9c,
52-
},
53-
}
54-
55-
for _, tt := range tests {
56-
t.Run(tt.name, func(t *testing.T) {
57-
result := Sum64(tt.input)
58-
// Note: xxhash results are deterministic but platform-dependent
59-
// These expected values would need to be verified on your system
60-
if result == 0 && len(tt.input) > 0 {
61-
t.Errorf("Sum64(%v) returned 0 for non-empty input", tt.input)
62-
}
63-
// Test deterministic behavior
64-
result2 := Sum64(tt.input)
65-
if result != result2 {
66-
t.Errorf("Sum64(%v) is not deterministic: %d != %d", tt.input, result, result2)
67-
}
68-
})
69-
}
24+
tests := []struct {
25+
name string
26+
input []byte
27+
expected uint64
28+
}{
29+
{
30+
name: "empty byte slice",
31+
input: []byte{},
32+
expected: 17241709254077376921,
33+
},
34+
{
35+
name: "single byte",
36+
input: []byte{0x42},
37+
expected: 7046029254386353131,
38+
},
39+
{
40+
name: "hello world",
41+
input: []byte("hello world"),
42+
expected: 5020219685658847592,
43+
},
44+
{
45+
name: "longer string",
46+
input: []byte("The quick brown fox jumps over the lazy dog"),
47+
expected: 0xa8a6e93b487c8ad4,
48+
},
49+
{
50+
name: "binary data",
51+
input: []byte{0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd},
52+
expected: 0x4a1b9e4a4c9a7a9c,
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
result := Sum64(tt.input)
59+
// Note: xxhash results are deterministic but platform-dependent
60+
// These expected values would need to be verified on your system
61+
if result == 0 && len(tt.input) > 0 {
62+
t.Errorf("Sum64(%v) returned 0 for non-empty input", tt.input)
63+
}
64+
// Test deterministic behavior
65+
result2 := Sum64(tt.input)
66+
if result != result2 {
67+
t.Errorf("Sum64(%v) is not deterministic: %d != %d", tt.input, result, result2)
68+
}
69+
})
70+
}
7071
}
7172

7273
func TestSum64String(t *testing.T) {
73-
tests := []struct {
74-
name string
75-
input string
76-
}{
77-
{
78-
name: "empty string",
79-
input: "",
80-
},
81-
{
82-
name: "single character",
83-
input: "a",
84-
},
85-
{
86-
name: "hello world",
87-
input: "hello world",
88-
},
89-
{
90-
name: "longer string",
91-
input: "The quick brown fox jumps over the lazy dog",
92-
},
93-
{
94-
name: "unicode string",
95-
input: "Hello, 世界! 🌍",
96-
},
97-
{
98-
name: "special characters",
99-
input: "!@#$%^&*()_+-=[]{}|;:,.<>?",
100-
},
101-
}
102-
103-
for _, tt := range tests {
104-
t.Run(tt.name, func(t *testing.T) {
105-
result := Sum64String(tt.input)
106-
107-
// Test deterministic behavior
108-
result2 := Sum64String(tt.input)
109-
if result != result2 {
110-
t.Errorf("Sum64String(%q) is not deterministic: %d != %d", tt.input, result, result2)
111-
}
112-
113-
// Test that string and byte slice produce same result
114-
byteResult := Sum64([]byte(tt.input))
115-
if result != byteResult {
116-
t.Errorf("Sum64String(%q) = %d, Sum64([]byte(%q)) = %d, want same result",
117-
tt.input, result, tt.input, byteResult)
118-
}
119-
})
120-
}
74+
tests := []struct {
75+
name string
76+
input string
77+
}{
78+
{
79+
name: "empty string",
80+
input: "",
81+
},
82+
{
83+
name: "single character",
84+
input: "a",
85+
},
86+
{
87+
name: "hello world",
88+
input: "hello world",
89+
},
90+
{
91+
name: "longer string",
92+
input: "The quick brown fox jumps over the lazy dog",
93+
},
94+
{
95+
name: "unicode string",
96+
input: "Hello, 世界! 🌍",
97+
},
98+
{
99+
name: "special characters",
100+
input: "!@#$%^&*()_+-=[]{}|;:,.<>?",
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
result := Sum64String(tt.input)
107+
108+
// Test deterministic behavior
109+
result2 := Sum64String(tt.input)
110+
if result != result2 {
111+
t.Errorf("Sum64String(%q) is not deterministic: %d != %d", tt.input, result, result2)
112+
}
113+
114+
// Test that string and byte slice produce same result
115+
byteResult := Sum64([]byte(tt.input))
116+
if result != byteResult {
117+
t.Errorf("Sum64String(%q) = %d, Sum64([]byte(%q)) = %d, want same result",
118+
tt.input, result, tt.input, byteResult)
119+
}
120+
})
121+
}
121122
}
122123

123124
func TestSum64_DifferentInputs(t *testing.T) {
124-
input1 := []byte("test input 1")
125-
input2 := []byte("test input 2")
126-
127-
hash1 := Sum64(input1)
128-
hash2 := Sum64(input2)
129-
130-
if hash1 == hash2 {
131-
t.Errorf("Different inputs produced same hash: %d", hash1)
132-
}
125+
input1 := []byte("test input 1")
126+
input2 := []byte("test input 2")
127+
128+
hash1 := Sum64(input1)
129+
hash2 := Sum64(input2)
130+
131+
if hash1 == hash2 {
132+
t.Errorf("Different inputs produced same hash: %d", hash1)
133+
}
133134
}
134135

135136
func TestSum64String_DifferentInputs(t *testing.T) {
136-
input1 := "test input 1"
137-
input2 := "test input 2"
138-
139-
hash1 := Sum64String(input1)
140-
hash2 := Sum64String(input2)
141-
142-
if hash1 == hash2 {
143-
t.Errorf("Different inputs produced same hash: %d", hash1)
144-
}
137+
input1 := "test input 1"
138+
input2 := "test input 2"
139+
140+
hash1 := Sum64String(input1)
141+
hash2 := Sum64String(input2)
142+
143+
if hash1 == hash2 {
144+
t.Errorf("Different inputs produced same hash: %d", hash1)
145+
}
145146
}
146147

147148
func TestSum64_LargeInput(t *testing.T) {
148-
// Test with large input to ensure no issues with memory
149-
largeInput := make([]byte, 10000)
150-
for i := range largeInput {
151-
largeInput[i] = byte(i % 256)
152-
}
153-
154-
result := Sum64(largeInput)
155-
if result == 0 {
156-
t.Errorf("Sum64 with large input returned 0")
157-
}
149+
// Test with large input to ensure no issues with memory
150+
largeInput := make([]byte, 10000)
151+
for i := range largeInput {
152+
largeInput[i] = byte(i % 256)
153+
}
154+
155+
result := Sum64(largeInput)
156+
if result == 0 {
157+
t.Errorf("Sum64 with large input returned 0")
158+
}
158159
}
159160

160161
func TestSum64String_LargeInput(t *testing.T) {
161-
// Test with large string input
162-
largeString := ""
163-
for i := 0; i < 10000; i++ {
164-
largeString += "a"
165-
}
166-
167-
result := Sum64String(largeString)
168-
if result == 0 {
169-
t.Errorf("Sum64String with large input returned 0")
170-
}
162+
// Test with large string input
163+
largeString := ""
164+
for i := 0; i < 10000; i++ {
165+
largeString += "a"
166+
}
167+
168+
result := Sum64String(largeString)
169+
if result == 0 {
170+
t.Errorf("Sum64String with large input returned 0")
171+
}
171172
}
172173

173174
func BenchmarkSum64_Small(b *testing.B) {
174-
data := []byte("hello")
175-
b.ResetTimer()
176-
for i := 0; i < b.N; i++ {
177-
Sum64(data)
178-
}
175+
data := []byte("hello")
176+
b.ResetTimer()
177+
for i := 0; i < b.N; i++ {
178+
Sum64(data)
179+
}
179180
}
180181

181182
func BenchmarkSum64_Medium(b *testing.B) {
182-
data := make([]byte, 128)
183-
for i := range data {
184-
data[i] = byte(i)
185-
}
186-
b.ResetTimer()
187-
for i := 0; i < b.N; i++ {
188-
Sum64(data)
189-
}
183+
data := make([]byte, 128)
184+
for i := range data {
185+
data[i] = byte(i)
186+
}
187+
b.ResetTimer()
188+
for i := 0; i < b.N; i++ {
189+
Sum64(data)
190+
}
190191
}
191192

192193
func BenchmarkSum64_Large(b *testing.B) {
193-
data := make([]byte, 1024)
194-
for i := range data {
195-
data[i] = byte(i)
196-
}
197-
b.ResetTimer()
198-
for i := 0; i < b.N; i++ {
199-
Sum64(data)
200-
}
194+
data := make([]byte, 1024)
195+
for i := range data {
196+
data[i] = byte(i)
197+
}
198+
b.ResetTimer()
199+
for i := 0; i < b.N; i++ {
200+
Sum64(data)
201+
}
201202
}
202203

203204
func BenchmarkSum64String_Small(b *testing.B) {
204-
str := "hello"
205-
b.ResetTimer()
206-
for i := 0; i < b.N; i++ {
207-
Sum64String(str)
208-
}
205+
str := "hello"
206+
b.ResetTimer()
207+
for i := 0; i < b.N; i++ {
208+
Sum64String(str)
209+
}
209210
}
210211

211212
func BenchmarkSum64String_Medium(b *testing.B) {
212-
str := ""
213-
for i := 0; i < 128; i++ {
214-
str += "a"
215-
}
216-
b.ResetTimer()
217-
for i := 0; i < b.N; i++ {
218-
Sum64String(str)
219-
}
213+
str := ""
214+
for i := 0; i < 128; i++ {
215+
str += "a"
216+
}
217+
b.ResetTimer()
218+
for i := 0; i < b.N; i++ {
219+
Sum64String(str)
220+
}
220221
}
221222

222223
func BenchmarkSum64String_Large(b *testing.B) {
223-
str := ""
224-
for i := 0; i < 1024; i++ {
225-
str += "a"
226-
}
227-
b.ResetTimer()
228-
for i := 0; i < b.N; i++ {
229-
Sum64String(str)
230-
}
231-
}
224+
str := ""
225+
for i := 0; i < 1024; i++ {
226+
str += "a"
227+
}
228+
b.ResetTimer()
229+
for i := 0; i < b.N; i++ {
230+
Sum64String(str)
231+
}
232+
}

0 commit comments

Comments
 (0)