File: fasthashtest.go

package info (click to toggle)
golang-github-segmentio-fasthash 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 132 kB
  • sloc: makefile: 2
file content (97 lines) | stat: -rw-r--r-- 2,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package fasthashtest

import (
	"fmt"
	"strings"
	"testing"
)

// TestHashString64 is the implementation of a test suite to verify the
// behavior of a hashing algorithm.
func TestHashString64(t *testing.T, name string, reference func(string) uint64, algorithm func(string) uint64) {
	t.Run(name, func(t *testing.T) {
		for _, s := range [...]string{"", "A", "Hello World!", "DAB45194-42CC-4106-AB9F-2447FA4D35C2", "你好吗"} {
			t.Run(s, func(t *testing.T) {
				if reference == nil {
					algorithm(s)
				} else {
					sum1 := reference(s)
					sum2 := algorithm(s)

					if sum1 != sum2 {
						t.Errorf("invalid hash, expected %x but got %x", sum1, sum2)
					}
				}
			})
		}
	})
}

// TestHashBytes64 is the implementation of a test suite to verify the
// behavior of a hashing algorithm.
func TestHashBytes64(t *testing.T, name string, reference func([]byte) uint64, algorithm func([]byte) uint64) {
	t.Run(name, func(t *testing.T) {
		for _, s := range [...]string{"", "A", "Hello World!", "DAB45194-42CC-4106-AB9F-2447FA4D35C2"} {
			t.Run(s, func(t *testing.T) {
				b := []byte(s)
				if reference == nil {
					algorithm(b)
				} else {
					sum1 := reference(b)
					sum2 := algorithm(b)

					if sum1 != sum2 {
						t.Errorf("invalid hash, expected %x but got %x", sum1, sum2)
					}
				}
			})
		}
	})
}

// TestHashUint64 is the implementation of a test suite to verify the
// behavior of a hashing algorithm.
func TestHashUint64(t *testing.T, name string, reference func(uint64) uint64, algorithm func(uint64) uint64) {
	t.Run(name, func(t *testing.T) {
		if reference == nil {
			algorithm(42)
		} else {
			sum1 := reference(42)
			sum2 := algorithm(42)

			if sum1 != sum2 {
				t.Errorf("invalid hash, expected %x but got %x", sum1, sum2)
			}
		}
	})
}

// BenchmarkHashString64 is the implementation of a benchmark suite to compare
// the CPU and memory efficiency of a hashing algorithm against a reference
// implementation.
func BenchmarkHashString64(b *testing.B, name string, reference func(string) uint64, algorithm func(string) uint64) {
	b.Run(name, func(b *testing.B) {
		if reference != nil {
			b.Run("reference", func(b *testing.B) { benchmark(b, reference) })
		}
		b.Run("optimized", func(b *testing.B) { benchmark(b, algorithm) })
	})
}

var benchmarkStrings = [...]string{
	"asdf",
	"hello world",
	"DAB45194-42CC-4106-AB9F-2447FA4D35C2",
	strings.Repeat("1234567890", 100),
}

func benchmark(b *testing.B, hash func(string) uint64) {
	for _, s := range benchmarkStrings {
		b.Run(fmt.Sprintf("strlen=%d", len(s)), func(b *testing.B) {
			for i := 0; i != b.N; i++ {
				hash(s)
			}
			b.SetBytes(int64(len(s)))
		})
	}
}