-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdoc_test.go
43 lines (32 loc) · 979 Bytes
/
doc_test.go
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
package rollinghash_test
import (
"hash"
"log"
_adler32 "github.com/chmduquesne/rollinghash/adler32"
)
func Example() {
s := []byte("The quick brown fox jumps over the lazy dog")
// This example works with adler32, but the api is identical for all
// other rolling checksums. Consult the documentation of the checksum
// of interest.
classic := hash.Hash32(_adler32.New())
rolling := _adler32.New()
// Window len
n := 16
// You MUST load an initial window into the rolling hash before being
// able to roll bytes
rolling.Write(s[:n])
// Roll it and compare the result with full re-calculus every time
for i := n; i < len(s); i++ {
// Reset and write the window in classic
classic.Reset()
classic.Write(s[i-n+1 : i+1])
// Roll the incoming byte in rolling
rolling.Roll(s[i])
// Compare the hashes
if classic.Sum32() != rolling.Sum32() {
log.Fatalf("%v: expected %x, got %x",
s[i-n+1:i+1], classic.Sum32(), rolling.Sum32())
}
}
}