| 1 | package wyhash |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "runtime" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestHash(t *testing.T) { |
| 10 | var data []byte |
| 11 | |
| 12 | for i, want := range vecs { |
| 13 | data = append(data, byte(i)) |
| 14 | got := Hash(data[:i], 0x0102030405060708) |
| 15 | if i != 0 && got != want { |
| 16 | t.Errorf("Hash(...%d)=%x, want %x", i, got, want) |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func BenchmarkCompare(b *testing.B) { |
| 22 | sizes := []int{ |
| 23 | 0, 1, 3, 4, 8, 9, 16, 17, 32, |
| 24 | 33, 64, 65, 96, 97, 128, 129, 240, 241, |
| 25 | 512, 1024, 100 * 1024, |
| 26 | } |
| 27 | |
| 28 | for _, size := range sizes { |
| 29 | b.Run(fmt.Sprintf("%d", size), func(b *testing.B) { |
| 30 | b.SetBytes(int64(size)) |
| 31 | var acc uint64 |
| 32 | d := string(make([]byte, size)) |
| 33 | b.ReportAllocs() |
| 34 | b.ResetTimer() |
| 35 | |
| 36 | for i := 0; i < b.N; i++ { |
| 37 | acc = HashString(d, 0) |
| 38 | } |
| 39 | runtime.KeepAlive(acc) |
| 40 | }) |
| 41 | } |
| 42 | } |