-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcontains_test.go
101 lines (94 loc) · 2.29 KB
/
contains_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package mem
import (
"bytes"
"testing"
)
func TestContainsByte(t *testing.T) {
for _, test := range []struct {
name string
haystack []byte
needle byte
expect bool
}{
{
name: "nil",
haystack: nil,
needle: 'x',
expect: false,
},
{
name: "empty",
haystack: []byte{},
needle: 'x',
expect: false,
},
{
name: "one equal byte",
haystack: []byte{'x'},
needle: 'x',
expect: true,
},
{
name: "one unequal byte",
haystack: []byte{'a'},
needle: 'x',
expect: false,
},
{
name: "run without byte",
haystack: []byte("aaaaaaaaaaaaaaaaaa"),
needle: 'x',
expect: false,
},
{
name: "run with byte prefix",
haystack: []byte("xaaaaaaaaaaaaaaaaaa"),
needle: 'x',
expect: true,
},
{
name: "run with byte suffix",
haystack: []byte("aaaaaaaaaaaaaaaaaax"),
needle: 'x',
expect: true,
},
} {
t.Run(test.name, func(t *testing.T) {
if actual := ContainsByte(test.haystack, test.needle); actual != test.expect {
t.Fatalf("ContainsByte(%v, %v) => %v", test.haystack, test.needle, actual)
}
})
}
// Test specific lengths up to 1KB.
var src []byte
for i := 0; i < 1000; i++ {
if ContainsByte(src, 'x') {
t.Fatalf("ContainsByte(%v, 'x') => true", src)
}
src = append(src, 'x')
if !ContainsByte(src, 'x') {
t.Fatalf("ContainsByte(%v, 'x') => false", src)
}
src[i] = '0'
}
}
func BenchmarkContainsByte(b *testing.B) {
benchmarkContainsByte(b, ContainsByte)
}
func benchmarkContainsByte(b *testing.B, contains func([]byte, byte) bool) {
large := bytes.Repeat([]byte{'a'}, 8*1024)
b.Run("empty", benchmarkContainsByteCase(contains, nil, 'x'))
b.Run("small-not-found", benchmarkContainsByteCase(contains, []byte("abcdef"), 'x'))
b.Run("small-found-at-end", benchmarkContainsByteCase(contains, []byte("abcdefx"), 'x'))
b.Run("large-not-found", benchmarkContainsByteCase(contains, large, 'x'))
b.Run("large-found-at-end", benchmarkContainsByteCase(contains, append(large, 'x'), 'x'))
}
func benchmarkContainsByteCase(contains func([]byte, byte) bool, haystack []byte, needle byte) func(*testing.B) {
return func(b *testing.B) {
b.SetBytes(int64(len(haystack)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
contains(haystack, needle)
}
}
}