| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hashing |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"math" |
| 19 |
"strings" |
| 20 |
"sync" |
| 21 |
"testing" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
) |
| 25 |
|
| 26 |
func TestXxHashFromReader(t *testing.T) { |
| 27 |
c := qt.New(t) |
| 28 |
s := "Hello World" |
| 29 |
r := strings.NewReader(s) |
| 30 |
got, size, err := XXHashFromReader(r) |
| 31 |
c.Assert(err, qt.IsNil) |
| 32 |
c.Assert(size, qt.Equals, int64(len(s))) |
| 33 |
c.Assert(got, qt.Equals, uint64(7148569436472236994)) |
| 34 |
} |
| 35 |
|
| 36 |
func TestXxHashFromReaderPara(t *testing.T) { |
| 37 |
c := qt.New(t) |
| 38 |
|
| 39 |
var wg sync.WaitGroup |
| 40 |
for i := range 10 { |
| 41 |
wg.Go(func() { |
| 42 |
for j := range 100 { |
| 43 |
s := strings.Repeat("Hello ", i+j+1*42) |
| 44 |
r := strings.NewReader(s) |
| 45 |
got, size, err := XXHashFromReader(r) |
| 46 |
c.Assert(size, qt.Equals, int64(len(s))) |
| 47 |
c.Assert(err, qt.IsNil) |
| 48 |
expect, _ := XXHashFromString(s) |
| 49 |
c.Assert(got, qt.Equals, expect) |
| 50 |
} |
| 51 |
}) |
| 52 |
} |
| 53 |
|
| 54 |
wg.Wait() |
| 55 |
} |
| 56 |
|
| 57 |
func TestXxHashFromString(t *testing.T) { |
| 58 |
c := qt.New(t) |
| 59 |
s := "Hello World" |
| 60 |
got, err := XXHashFromString(s) |
| 61 |
c.Assert(err, qt.IsNil) |
| 62 |
c.Assert(got, qt.Equals, uint64(7148569436472236994)) |
| 63 |
} |
| 64 |
|
| 65 |
func TestXxHashFromStringHexEncoded(t *testing.T) { |
| 66 |
c := qt.New(t) |
| 67 |
s := "The quick brown fox jumps over the lazy dog" |
| 68 |
got := XxHashFromStringHexEncoded(s) |
| 69 |
// Facit: https://asecuritysite.com/encryption/xxhash?val=The%20quick%20brown%20fox%20jumps%20over%20the%20lazy%20dog |
| 70 |
c.Assert(got, qt.Equals, "0b242d361fda71bc") |
| 71 |
} |
| 72 |
|
| 73 |
func BenchmarkXXHashFromReader(b *testing.B) { |
| 74 |
r := strings.NewReader("Hello World") |
| 75 |
|
| 76 |
for b.Loop() { |
| 77 |
XXHashFromReader(r) |
| 78 |
r.Seek(0, 0) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func BenchmarkXXHashFromString(b *testing.B) { |
| 83 |
s := "Hello World" |
| 84 |
|
| 85 |
for b.Loop() { |
| 86 |
XXHashFromString(s) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func BenchmarkXXHashFromStringHexEncoded(b *testing.B) { |
| 91 |
s := "The quick brown fox jumps over the lazy dog" |
| 92 |
|
| 93 |
for b.Loop() { |
| 94 |
XxHashFromStringHexEncoded(s) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func TestHashString(t *testing.T) { |
| 99 |
c := qt.New(t) |
| 100 |
|
| 101 |
c.Assert(HashString("a", "b"), qt.Equals, "3176555414984061461") |
| 102 |
c.Assert(HashString("ab"), qt.Equals, "7347350983217793633") |
| 103 |
|
| 104 |
var vals []any = []any{"a", "b", tstKeyer{"c"}} |
| 105 |
|
| 106 |
c.Assert(HashString(vals...), qt.Equals, "4438730547989914315") |
| 107 |
c.Assert(vals[2], qt.Equals, tstKeyer{"c"}) |
| 108 |
} |
| 109 |
|
| 110 |
type tstKeyer struct { |
| 111 |
key string |
| 112 |
} |
| 113 |
|
| 114 |
func (t tstKeyer) Key() string { |
| 115 |
return t.key |
| 116 |
} |
| 117 |
|
| 118 |
func (t tstKeyer) String() string { |
| 119 |
return "key: " + t.key |
| 120 |
} |
| 121 |
|
| 122 |
func BenchmarkHashString(b *testing.B) { |
| 123 |
word := " hello " |
| 124 |
|
| 125 |
var tests []string |
| 126 |
|
| 127 |
for i := 1; i <= 5; i++ { |
| 128 |
sentence := strings.Repeat(word, int(math.Pow(4, float64(i)))) |
| 129 |
tests = append(tests, sentence) |
| 130 |
} |
| 131 |
|
| 132 |
b.ResetTimer() |
| 133 |
|
| 134 |
for _, test := range tests { |
| 135 |
b.Run(fmt.Sprintf("n%d", len(test)), func(b *testing.B) { |
| 136 |
for b.Loop() { |
| 137 |
HashString(test) |
| 138 |
} |
| 139 |
}) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func BenchmarkHashMap(b *testing.B) { |
| 144 |
m := map[string]any{} |
| 145 |
for i := range 1000 { |
| 146 |
m[fmt.Sprintf("key%d", i)] = i |
| 147 |
} |
| 148 |
|
| 149 |
for b.Loop() { |
| 150 |
HashString(m) |
| 151 |
} |
| 152 |
} |