-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path06-00_searching_words_test.go
85 lines (71 loc) · 1.63 KB
/
06-00_searching_words_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package hd_test
import (
"fmt"
"testing"
hd "github.com/nikolaydubina/go-hackers-delight"
)
func ExampleZByteL() {
fmt.Println(hd.ZByteL(0x12_00_FF_00))
// Output: 1
}
func FuzzZByteL(f *testing.F) {
for _, x := range fuzzUint32 {
f.Add(x)
}
f.Fuzz(func(t *testing.T, x uint32) {
exp := hd.ZByteL1(x)
vs := []int{
hd.ZByteL(x),
}
for i, got := range vs {
if exp != got {
t.Error(i, "x", fmt.Sprintf("%032b", x), "exp", exp, "got", got)
}
}
})
}
func ExampleZByteL64() {
fmt.Println(hd.ZByteL64(0x11_12_00_00_FF_FF_00_11))
// Output: 2
}
func ExampleFindInByte() {
fmt.Println(hd.FindInByte(0x12_00_FF_00, 0xFF))
// Output: 2
}
func ExampleFindInByteEq() {
fmt.Println(hd.FindInByteEq(0x12_00_F9_00, 0x23_01_F9_00))
// Output: 2
}
func ExampleFindFirstStringOnes() {
fmt.Println(hd.FindFirstStringOnes(0b0000_0000_0000_0000_1100_1110_1111_1000, 5))
// Output: 24
}
func FuzzFindFirstStringOnes(f *testing.F) {
for _, x := range fuzzUint32 {
f.Add(x, uint8(3))
}
f.Fuzz(func(t *testing.T, x uint32, n uint8) {
n = n % 32
exp := hd.FindFirstStringOnes(x, int(n))
vs := []int{
hd.FindFirstStringOnes1(x, int(n)),
}
for i, got := range vs {
if exp != got {
t.Error(i, "x", fmt.Sprintf("%032b", x), "exp", exp, "got", got)
}
}
})
}
func ExampleLenLongestStringOnes() {
fmt.Println(hd.LenLongestStringOnes(uint64(0b0000_0000_0000_0000_1100_1110_1111_1000)))
// Output: 5
}
func ExampleLenShortestStringOnes() {
fmt.Println(hd.LenShortestStringOnes(0xFF0FF0))
// Output: 8 8
}
func ExampleLenShortestStringOnes_zero() {
fmt.Println(hd.LenShortestStringOnes(0))
// Output: 32 0
}