1// Copyright 2014-2017 Ulrich Kunitz. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package hash
6
7import (
8	"math/rand"
9	"testing"
10)
11
12func TestRabinKarpSimple(t *testing.T) {
13	p := []byte("abcde")
14	r := NewRabinKarp(4)
15	h2 := Hashes(r, p)
16	for i, h := range h2 {
17		w := Hashes(r, p[i:i+4])[0]
18		t.Logf("%d h=%#016x w=%#016x", i, h, w)
19		if h != w {
20			t.Errorf("rolling hash %d: %#016x; want %#016x",
21				i, h, w)
22		}
23	}
24}
25
26func makeBenchmarkBytes(n int) []byte {
27	rnd := rand.New(rand.NewSource(42))
28	p := make([]byte, n)
29	for i := range p {
30		p[i] = byte(rnd.Uint32())
31	}
32	return p
33}
34
35func BenchmarkRabinKarp(b *testing.B) {
36	p := makeBenchmarkBytes(4096)
37	r := NewRabinKarp(4)
38	b.ResetTimer()
39	for i := 0; i < b.N; i++ {
40		Hashes(r, p)
41	}
42}
43