-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
194 lines (150 loc) · 3.31 KB
/
reader_test.go
File metadata and controls
194 lines (150 loc) · 3.31 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package searchreader
import (
"bytes"
"errors"
"io"
"strings"
"testing"
)
func TestReader(t *testing.T) {
// Source
src := bytes.NewReader([]byte(`hello, world!`))
// What to search
search1 := strings.NewReader(`l`)
search2 := strings.NewReader(`ll`)
sr := New(src,
WithCaseSensitive(search1),
WithCaseSensitive(search2),
)
buffer := make([]byte, 1024)
rb, results, err := sr.Read(buffer)
if err != nil {
if errors.Is(err, io.EOF) {
t.Fail()
}
panic(err)
}
if rb != 13 {
t.Errorf(`expected %d, got %d`, 13, rb)
t.Fail()
}
if results == nil {
t.Errorf(`expected results, got nil`)
t.Fail()
}
if len(results) != 4 {
// 3 x `l` 1 x `ll`
t.Errorf(`expected 4 results, got %d`, len(results))
t.Fail()
}
search1result1 := uint(2) // l
search1result1found := false
search1result2 := uint(3) // l
search1result2found := false
search1result3 := uint(10) // l
search1result3found := false
search2result1 := uint(2) // ll
search2resultfound := false
for _, res := range results {
if res.Index == 0 && res.StartPosition == search1result1 {
search1result1found = true
}
if res.Index == 0 && res.StartPosition == search1result2 {
search1result2found = true
}
if res.Index == 0 && res.StartPosition == search1result3 {
search1result3found = true
}
if res.Index == 1 && res.StartPosition == search2result1 {
search2resultfound = true
}
}
if !search1result1found {
t.Errorf(`search1 result 1 (first l) not found`)
t.Fail()
}
if !search1result2found {
t.Errorf(`search1 result 2 (second l) not found`)
t.Fail()
}
if !search1result3found {
t.Errorf(`search1 result 3 (third l) not found`)
t.Fail()
}
if !search2resultfound {
t.Errorf(`search2 result (ll) not found`)
t.Fail()
}
}
// TestReaderChunk test chunk search
func TestReaderChunk(t *testing.T) {
// Source
src := bytes.NewReader([]byte(`xAAx`))
// What to search
search1 := strings.NewReader(`x`)
search2 := strings.NewReader(`AA`)
sr := New(src,
WithCaseSensitive(search1),
WithCaseSensitive(search2),
)
// Only read 2 bytes
buffer := make([]byte, 2)
rb, results1, err := sr.Read(buffer)
if err != nil {
if errors.Is(err, io.EOF) {
t.Fail()
}
panic(err)
}
if rb != 2 {
t.Errorf(`expected %d, got %d`, 2, rb)
t.Fail()
}
if len(results1) != 2 {
t.Errorf(`expected %d results, got %d`, 2, len(results1))
t.Fail()
}
search1result1 := uint(0) // x
search1result1found := false
search2result1 := uint(1) // AA
search2result1found := false
for _, res := range results1 {
if res.Index == 0 && res.StartPosition == search1result1 {
search1result1found = true
}
if res.Index == 1 && res.StartPosition == search2result1 {
search2result1found = true
}
}
if !search1result1found {
t.Fail()
}
if !search2result1found {
t.Fail()
}
rb, results2, err := sr.Read(buffer)
if err != nil {
if errors.Is(err, io.EOF) {
t.Fail()
}
panic(err)
}
if rb != 2 {
t.Errorf(`expected %d, got %d`, 2, rb)
t.Fail()
}
if len(results2) != 1 {
t.Errorf(`expected %d results, got %d`, 1, len(results2))
t.Fail()
}
search1result1 = uint(1) // x
search1result1found = false
for _, res := range results2 {
if res.Index == 0 && res.StartPosition == search1result1 {
search1result1found = true
}
}
if !search1result1found {
t.Fail()
}
}