File: hash_test.go

package info (click to toggle)
golang-github-segmentio-fasthash 1.0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,211 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
package jody

import (
	"fmt"
	"testing"

	"github.com/segmentio/fasthash/fasthashtest"
)

func TestHash64(t *testing.T) {
	// I couldn't find a reference implementation in Go, so this is a hacky
	// test that checks for values generated from the jodyhash command line
	// utility.

	referenceString64 := func(s string) uint64 {
		switch s {
		case "":
			return 0x0000000000000000
		case "A":
			return 0x000000002e8208ba
		case "Hello World!":
			return 0x60be57b5a53eb1c7
		case "DAB45194-42CC-4106-AB9F-2447FA4D35C2":
			return 0x587861d2b41e1997
		case "你好吗":
			return 0x944cd5c16171eca3
		default:
			panic("test not implemented: " + s)
		}
	}

	referenceByte64 := func(b []byte) uint64 {
		return referenceString64(string(b))
	}

	referenceUint64 := func(u uint64) uint64 {
		if u != 42 {
			panic(fmt.Sprint("test not implemented:", u))
		}
		return 0x0007cf56f7fc0ba3
	}

	fasthashtest.TestHashString64(t, "jody", referenceString64, HashString64)
	fasthashtest.TestHashBytes64(t, "jody", referenceByte64, HashBytes64)
	fasthashtest.TestHashUint64(t, "jody", referenceUint64, HashUint64)
}

func BenchmarkHash64(b *testing.B) {
	fasthashtest.BenchmarkHashString64(b, "jody", nil, HashString64)
}