File: rabin_karp_test.go

package info (click to toggle)
golang-github-ulikunitz-xz 0.5.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,496 kB
  • sloc: makefile: 7; sh: 5
file content (42 lines) | stat: -rw-r--r-- 824 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
// Copyright 2014-2022 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package hash

import (
	"math/rand"
	"testing"
)

func TestRabinKarpSimple(t *testing.T) {
	p := []byte("abcde")
	r := NewRabinKarp(4)
	h2 := Hashes(r, p)
	for i, h := range h2 {
		w := Hashes(r, p[i:i+4])[0]
		t.Logf("%d h=%#016x w=%#016x", i, h, w)
		if h != w {
			t.Errorf("rolling hash %d: %#016x; want %#016x",
				i, h, w)
		}
	}
}

func makeBenchmarkBytes(n int) []byte {
	rnd := rand.New(rand.NewSource(42))
	p := make([]byte, n)
	for i := range p {
		p[i] = byte(rnd.Uint32())
	}
	return p
}

func BenchmarkRabinKarp(b *testing.B) {
	p := makeBenchmarkBytes(4096)
	r := NewRabinKarp(4)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		Hashes(r, p)
	}
}