-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgobls_test.go
208 lines (174 loc) · 5.06 KB
/
gobls_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gobls
import (
"bufio"
"bytes"
"testing"
)
const excessivelyLongLineLength = bufio.MaxScanTokenSize - 2
func makeBufioScanner(buf []byte) Scanner {
return bufio.NewScanner(bytes.NewReader(buf))
}
func makeScanner(buf []byte) Scanner {
return NewScanner(bytes.NewReader(buf))
}
func makeBufferScanner(buf []byte) Scanner {
return NewBufferScanner(buf)
}
func makeBuffer(lineCount, lineLength int) []byte {
buf := make([]byte, 0, lineCount*lineLength)
for line := 0; line < lineCount; line++ {
// Each line terminated by CRLF
for i := 0; i < lineLength-2; i++ {
buf = append(buf, 'a')
}
buf = append(buf, '\r', '\n')
}
return buf
}
func testSequenceBufio(t *testing.T, buf []byte, seq []string) {
t.Helper()
ensureSequence(t, "bufio", makeBufioScanner(buf), seq)
}
func testSequenceScanner(t *testing.T, buf []byte, seq []string) {
t.Helper()
ensureSequence(t, "scanner", makeScanner(buf), seq)
}
func testSequenceBuffer(t *testing.T, buf []byte, seq []string) {
t.Helper()
ensureSequence(t, "buffer", makeBufferScanner(buf), seq)
}
func TestEmpty(t *testing.T) {
var corpus []byte
var want []string
testSequenceBufio(t, corpus, want)
testSequenceScanner(t, corpus, want)
testSequenceBuffer(t, corpus, want)
}
func TestSequencesThroughEntireBuffer(t *testing.T) {
corpus := []byte("flubber\nblubber\nfoo")
want := []string{"flubber", "blubber", "foo"}
testSequenceBufio(t, corpus, want)
testSequenceScanner(t, corpus, want)
testSequenceBuffer(t, corpus, want)
}
func TestLongLinesRequireSingleInvocation(t *testing.T) {
buf := makeBuffer(1, 4096)
line := string(buf)
line = line[:len(line)-2] // trim final CRLF
want := []string{line}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
}
func TestHandlesLinesLongerThanBuffer(t *testing.T) {
if testing.Short() {
t.Skip("skipping long buffer")
}
buf := makeBuffer(1, 8192)
line := string(buf)
line = line[:len(line)-2] // trim final CRLF
want := []string{line}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
}
func TestExcessivelyLongLinesRequireSingleInvocation(t *testing.T) {
if testing.Short() {
t.Skip("skipping long buffer")
}
buf := makeBuffer(1, bufio.MaxScanTokenSize+5)
line := string(buf)
line = line[:len(line)-2] // trim final CRLF
want := []string{line}
if false {
// Test skipped because bufio will return err = token too long
testSequenceBufio(t, buf, want)
}
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
}
func TestScannerEmpty(t *testing.T) {
var buf []byte
var want []string
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
}
func TestScannerSingleByte(t *testing.T) {
t.Run("newline", func(t *testing.T) {
buf := []byte{'\n'}
want := []string{""}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("carriage return", func(t *testing.T) {
buf := []byte{'\r'}
want := []string{""}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("other", func(t *testing.T) {
buf := []byte{'a'}
want := []string{"a"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
}
func TestScannerSingleLine(t *testing.T) {
t.Run("with newline", func(t *testing.T) {
buf := []byte("line1\n")
want := []string{"line1"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("with carriage return", func(t *testing.T) {
buf := []byte("line1\r")
want := []string{"line1"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("with both", func(t *testing.T) {
buf := []byte("line1\r\n")
want := []string{"line1"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("with neither", func(t *testing.T) {
buf := []byte("line1")
want := []string{"line1"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
}
func TestScannerDoubleLine(t *testing.T) {
t.Run("with trailing newline", func(t *testing.T) {
buf := []byte("line1\nline2\n")
want := []string{"line1", "line2"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("with trailing carriage return", func(t *testing.T) {
// NOTE: Because carriage returns are ignored rather than marking
// the end of a line, this source buffer returns a single line.
buf := []byte("line1\rline2\r")
want := []string{"line1\rline2"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
t.Run("with trailing both", func(t *testing.T) {
buf := []byte("line1\r\nline2\r\n")
want := []string{"line1", "line2"}
testSequenceBufio(t, buf, want)
testSequenceScanner(t, buf, want)
testSequenceBuffer(t, buf, want)
})
}