-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
14-00_cyclic_redundancy_check_test.go
69 lines (59 loc) · 1.3 KB
/
14-00_cyclic_redundancy_check_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package hd_test
import (
"hash/crc32"
"math/rand/v2"
"testing"
hd "github.com/nikolaydubina/go-hackers-delight"
)
func TestCRC32GeneratorConst(t *testing.T) {
r := hd.ReverseBits(hd.CRC32Generator)
if r != hd.CRC32GeneratorReversed {
t.Errorf("expected %032b, got %032b", hd.CRC32GeneratorReversed, r)
}
}
func FuzzCRC32(f *testing.F) {
f.Fuzz(func(t *testing.T, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10 byte) {
data := []byte{x1, x2, x3, x4, x5, x6, x7, x8, x9, x10}
exp := crc32.ChecksumIEEE(data)
got := []uint32{
hd.CRC32Basic(data),
hd.CRC32TableLookup(data),
}
for i, got := range got {
if exp != got {
t.Error(i, "exp", exp, "got", got)
}
}
})
}
func BenchmarkCRC32(b *testing.B) {
var out uint32
var vals [][100]byte
for i := 0; i < 1000; i++ {
var v [100]byte
for j := range 100 {
v[j] = byte(rand.Int32())
}
vals = append(vals, v)
}
vs := []struct {
name string
f func(data []byte) uint32
}{
{"basic", crc32.ChecksumIEEE},
{"CRC32Basic", hd.CRC32Basic},
{"CRC32TableLookup", hd.CRC32TableLookup},
}
for _, v := range vs {
b.Run(v.name, func(b *testing.B) {
for i := 0; i < b.N; i += len(vals) {
for j := 0; j < len(vals)-1; j++ {
out = v.f(vals[j][:])
}
}
})
}
if (out*2 - out - out) != 0 {
b.Fatal("never")
}
}