| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xxhash |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/binary" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
func TestIntegrity(t *testing.T) { |
| 14 |
data := []byte{'1', '2', 3, 4, 5, 6, 7, 8, 9, 10} |
| 15 |
h0 := New() |
| 16 |
want := h0.String(string(data)) |
| 17 |
|
| 18 |
h1 := New() |
| 19 |
got := h1.String(string(data[:2])) |
| 20 |
num := binary.LittleEndian.Uint64(data[2:]) |
| 21 |
got = got.Uint64(num) |
| 22 |
|
| 23 |
assert.Equal(t, want.Sum64(), got.Sum64()) |
| 24 |
} |
| 25 |
|
| 26 |
func TestNew(t *testing.T) { |
| 27 |
h1 := New() |
| 28 |
h2 := New() |
| 29 |
|
| 30 |
// Test that the underlying digest is properly initialized. |
| 31 |
if h1.Sum64() != h2.Sum64() { |
| 32 |
t.Errorf("New() should return consistent initial value: %d != %d", h1.Sum64(), h2.Sum64()) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func TestUint64(t *testing.T) { |
| 37 |
h1 := New().Uint64(42) |
| 38 |
h2 := New().Uint64(42) |
| 39 |
if h1.Sum64() != h2.Sum64() { |
| 40 |
t.Errorf("Uint64() should be deterministic: %d != %d", h1.Sum64(), h2.Sum64()) |
| 41 |
} |
| 42 |
|
| 43 |
h3 := New().Uint64(43) |
| 44 |
if h1.Sum64() == h3.Sum64() { |
| 45 |
t.Errorf("Different inputs should produce different hashes: %d == %d", h1.Sum64(), h3.Sum64()) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestBool(t *testing.T) { |
| 50 |
h1 := New().Bool(true) |
| 51 |
h2 := New().Bool(true) |
| 52 |
if h1.Sum64() != h2.Sum64() { |
| 53 |
t.Errorf("Bool() should be deterministic: %d != %d", h1.Sum64(), h2.Sum64()) |
| 54 |
} |
| 55 |
|
| 56 |
h3 := New().Bool(false) |
| 57 |
if h1.Sum64() == h3.Sum64() { |
| 58 |
t.Errorf("Different bool values should produce different hashes: %d == %d", h1.Sum64(), h3.Sum64()) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestFloat64(t *testing.T) { |
| 63 |
h1 := New().Float64(3.14) |
| 64 |
h2 := New().Float64(3.14) |
| 65 |
if h1.Sum64() != h2.Sum64() { |
| 66 |
t.Errorf("Float64() should be deterministic: %d != %d", h1.Sum64(), h2.Sum64()) |
| 67 |
} |
| 68 |
|
| 69 |
h3 := New().Float64(2.71) |
| 70 |
if h1.Sum64() == h3.Sum64() { |
| 71 |
t.Errorf("Different float values should produce different hashes: %d == %d", h1.Sum64(), h3.Sum64()) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestInt64(t *testing.T) { |
| 76 |
h1 := New().Int64(42) |
| 77 |
h2 := New().Int64(42) |
| 78 |
if h1.Sum64() != h2.Sum64() { |
| 79 |
t.Errorf("Int64() should be deterministic: %d != %d", h1.Sum64(), h2.Sum64()) |
| 80 |
} |
| 81 |
|
| 82 |
h3 := New().Int64(43) |
| 83 |
if h1.Sum64() == h3.Sum64() { |
| 84 |
t.Errorf("Different int64 values should produce different hashes: %d == %d", h1.Sum64(), h3.Sum64()) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func TestString(t *testing.T) { |
| 89 |
h1 := New().String("hello") |
| 90 |
h2 := New().String("hello") |
| 91 |
if h1.Sum64() != h2.Sum64() { |
| 92 |
t.Errorf("String() should be deterministic: %d != %d", h1.Sum64(), h2.Sum64()) |
| 93 |
} |
| 94 |
|
| 95 |
h3 := New().String("world") |
| 96 |
if h1.Sum64() == h3.Sum64() { |
| 97 |
t.Errorf("Different strings should produce different hashes: %d == %d", h1.Sum64(), h3.Sum64()) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func TestChaining(t *testing.T) { |
| 102 |
// Test that methods can be chained and produce different results |
| 103 |
h1 := New().String("key").Uint64(42).Bool(true) |
| 104 |
h2 := New().String("key").Uint64(42).Bool(true) |
| 105 |
h3 := New().String("key").Uint64(43).Bool(true) |
| 106 |
|
| 107 |
if h1.Sum64() != h2.Sum64() { |
| 108 |
t.Errorf("Chained operations should be deterministic: %d != %d", h1.Sum64(), h2.Sum64()) |
| 109 |
} |
| 110 |
|
| 111 |
if h1.Sum64() == h3.Sum64() { |
| 112 |
t.Errorf("Different chained operations should produce different hashes: %d == %d", h1.Sum64(), h3.Sum64()) |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
func BenchmarkStringKB(b *testing.B) { |
| 117 |
b.SetBytes(1024) |
| 118 |
data := make([]byte, 1024) |
| 119 |
for i := range data { |
| 120 |
data[i] = byte(i) |
| 121 |
} |
| 122 |
s := string(data) |
| 123 |
h := New() |
| 124 |
|
| 125 |
b.ReportAllocs() |
| 126 |
b.ResetTimer() |
| 127 |
for b.Loop() { |
| 128 |
h.String(s) |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func BenchmarkUint64KB(b *testing.B) { |
| 133 |
b.SetBytes(8) |
| 134 |
i := uint64(192386739218721) |
| 135 |
h := New() |
| 136 |
|
| 137 |
b.ReportAllocs() |
| 138 |
b.ResetTimer() |
| 139 |
for b.Loop() { |
| 140 |
h.Uint64(i) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func BenchmarkUint64(b *testing.B) { |
| 145 |
h := New() |
| 146 |
|
| 147 |
b.ReportAllocs() |
| 148 |
for i := 0; b.Loop(); i++ { |
| 149 |
h = h.Uint64(uint64(i)) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
func BenchmarkString(b *testing.B) { |
| 154 |
h := New() |
| 155 |
str := "benchmark_string_value" |
| 156 |
|
| 157 |
b.ReportAllocs() |
| 158 |
for b.Loop() { |
| 159 |
h = h.String(str) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func BenchmarkBool(b *testing.B) { |
| 164 |
h := New() |
| 165 |
|
| 166 |
b.ReportAllocs() |
| 167 |
for i := 0; b.Loop(); i++ { |
| 168 |
h = h.Bool(i%2 == 0) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func BenchmarkFloat64(b *testing.B) { |
| 173 |
h := New() |
| 174 |
|
| 175 |
b.ReportAllocs() |
| 176 |
for i := 0; b.Loop(); i++ { |
| 177 |
h = h.Float64(float64(i) * 3.14159) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func BenchmarkInt64(b *testing.B) { |
| 182 |
h := New() |
| 183 |
|
| 184 |
b.ReportAllocs() |
| 185 |
for i := 0; b.Loop(); i++ { |
| 186 |
h = h.Int64(int64(i)) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func BenchmarkSum64(b *testing.B) { |
| 191 |
h := New().String("key").Uint64(42).Bool(true) |
| 192 |
|
| 193 |
b.ReportAllocs() |
| 194 |
for b.Loop() { |
| 195 |
_ = h.Sum64() |
| 196 |
} |
| 197 |
} |