-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamy_test.go
134 lines (126 loc) · 2.91 KB
/
streamy_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Streamy
// For the full copyright and license information, please view the LICENSE.txt file.
package streamy_test
import (
"bytes"
"io"
"testing"
"github.com/devfacet/streamy"
)
func TestIndex(t *testing.T) {
table := []struct {
reader io.Reader
search []byte
readSize int
index int64
read int64
}{
{
reader: bytes.NewBufferString("tes"),
search: []byte("test"),
readSize: 4,
index: -1,
read: 3,
},
{
reader: bytes.NewBufferString("test"),
search: []byte("test"),
readSize: 4,
index: 0,
read: 4,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 0,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 1,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 2,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 3,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 4,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 5,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 14,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("test"),
readSize: 15,
index: 10,
read: 14,
},
{
reader: bytes.NewBufferString("this is a test"),
search: []byte("foo"),
readSize: 0,
index: -1,
read: 14,
},
}
for _, v := range table {
index, read, err := streamy.Index(v.reader, v.search, v.readSize)
if err != nil {
t.Errorf("got %v, want nil", err)
} else if index != v.index {
t.Errorf("got %v, want %v", index, v.index)
} else if read != v.read {
t.Errorf("got %v, want %v", read, v.read)
}
}
}
func BenchmarkIndexS2N1(b *testing.B) {
for i := 0; i < b.N; i++ {
streamy.Index(bytes.NewBuffer([]byte{0x86, 0xc8, 0x63, 0xbf, 0xd2, 0x02, 0x96, 0x49, 0x49, 0x96}), []byte{0x49, 0x49}, 1)
}
}
func BenchmarkIndexS2N2(b *testing.B) {
for i := 0; i < b.N; i++ {
streamy.Index(bytes.NewBuffer([]byte{0x86, 0xc8, 0x63, 0xbf, 0xd2, 0x02, 0x96, 0x49, 0x49, 0x96}), []byte{0x49, 0x49}, 2)
}
}
func BenchmarkIndexS2N10(b *testing.B) {
for i := 0; i < b.N; i++ {
streamy.Index(bytes.NewBuffer([]byte{0x86, 0xc8, 0x63, 0xbf, 0xd2, 0x02, 0x96, 0x49, 0x49, 0x96}), []byte{0x49, 0x49}, 10)
}
}
func BenchmarkIndexS2N0(b *testing.B) {
for i := 0; i < b.N; i++ {
streamy.Index(bytes.NewBuffer([]byte{0x86, 0xc8, 0x63, 0xbf, 0xd2, 0x02, 0x96, 0x49, 0x49, 0x96}), []byte{0x49, 0x49}, 0)
}
}