forked from philhofer/fwd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
452 lines (390 loc) · 9.39 KB
/
reader_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package fwd
import (
"bytes"
"io"
"io/ioutil"
"math/rand"
"testing"
"unsafe"
)
// partialReader reads into only
// part of the supplied byte slice
// to the underlying reader
type partialReader struct {
r io.Reader
}
func (p partialReader) Read(b []byte) (int, error) {
n := max(1, rand.Intn(len(b)))
return p.r.Read(b[:n])
}
func randomBts(sz int) []byte {
o := make([]byte, sz)
for i := 0; i < len(o); i += 8 {
j := (*int64)(unsafe.Pointer(&o[i]))
*j = rand.Int63()
}
return o
}
func TestRead(t *testing.T) {
bts := randomBts(512)
// make the buffer much
// smaller than the underlying
// bytes to incur multiple fills
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 128)
if rd.BufferSize() != cap(rd.data) {
t.Errorf("BufferSize() returned %d; should return %d", rd.BufferSize(), cap(rd.data))
}
// starting Buffered() should be 0
if rd.Buffered() != 0 {
t.Errorf("Buffered() should return 0 at initialization; got %d", rd.Buffered())
}
some := make([]byte, 32)
n, err := rd.Read(some)
if err != nil {
t.Fatal(err)
}
if n == 0 {
t.Fatal("read 0 bytes w/ a non-nil error!")
}
some = some[:n]
more := make([]byte, 64)
j, err := rd.Read(more)
if err != nil {
t.Fatal(err)
}
if j == 0 {
t.Fatal("read 0 bytes w/ a non-nil error")
}
more = more[:j]
out, err := ioutil.ReadAll(rd)
if err != nil {
t.Fatal(err)
}
all := append(some, more...)
all = append(all, out...)
if !bytes.Equal(bts, all) {
t.Errorf("bytes not equal; %d bytes in and %d bytes out", len(bts), len(out))
}
// test filling out of the underlying reader
big := randomBts(1 << 21)
rd = NewReaderSize(partialReader{bytes.NewReader(big)}, 2048)
buf := make([]byte, 3100)
n, err = rd.ReadFull(buf)
if err != nil {
t.Fatal(err)
}
if n != 3100 {
t.Errorf("expected 3100 bytes read by ReadFull; got %d", n)
}
if !bytes.Equal(buf[:n], big[:n]) {
t.Error("data parity")
}
rest := make([]byte, (1<<21)-3100)
n, err = io.ReadFull(rd, rest)
if err != nil {
t.Fatal(err)
}
if n != len(rest) {
t.Errorf("expected %d bytes read by io.ReadFull; got %d", len(rest), n)
}
if !bytes.Equal(append(buf, rest...), big) {
t.Fatal("data parity")
}
}
func TestReadByte(t *testing.T) {
bts := randomBts(512)
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 98)
var (
err error
i int
b byte
)
// scan through the whole
// array byte-by-byte
for err != io.EOF {
b, err = rd.ReadByte()
if err == nil {
if b != bts[i] {
t.Fatalf("offset %d: %d in; %d out", i, b, bts[i])
}
}
i++
}
if err != io.EOF {
t.Fatal(err)
}
}
func remaining(r *Reader) int {
return r.Buffered() + r.r.(partialReader).r.(*bytes.Reader).Len()
}
func TestSkipNoSeek(t *testing.T) {
bts := randomBts(1024)
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 200)
n, err := rd.Skip(512)
if err != nil {
t.Fatal(err)
}
if n != 512 {
t.Fatalf("Skip() returned a nil error, but skipped %d bytes instead of %d", n, 512)
}
if remaining(rd) != 512 {
t.Errorf("expected 512 remaining; got %d", remaining(rd))
}
var b byte
b, err = rd.ReadByte()
if err != nil {
t.Fatal(err)
}
if b != bts[512] {
t.Errorf("at index %d: %d in; %d out", 512, bts[512], b)
}
n, err = rd.Skip(10)
if err != nil {
t.Fatal(err)
}
if n != 10 {
t.Fatalf("Skip() returned a nil error, but skipped %d bytes instead of %d", n, 10)
}
// the number of bytes remaining in the buffer needs
// to comport with the number of bytes we expect to have skipped
if want := 1024 - 512 - 10 - 1; remaining(rd) != want {
t.Errorf("only %d bytes remaining (want %d)?", remaining(rd), want)
}
n, err = rd.Skip(10)
if err != nil {
t.Fatal(err)
}
if n != 10 {
t.Fatalf("Skip(10) a second time returned %d", n)
}
if want := 1024 - 512 - 10 - 10 - 1; remaining(rd) != want {
t.Errorf("only %d bytes remaining (want %d)?", remaining(rd), want)
}
b, err = rd.ReadByte()
if err != nil {
t.Fatalf("second ReadByte(): %s", err)
}
if b != bts[512+10+10+1] {
t.Errorf("expected %d but got %d", bts[512+10+10], b)
}
// now try to skip past the end; we expect
// only to skip the number of bytes remaining
want := remaining(rd)
n, err = rd.Skip(2000)
if err != io.ErrUnexpectedEOF {
t.Fatalf("expected error %q; got %q", io.EOF, err)
}
if n != want {
t.Fatalf("expected to skip only %d bytes; skipped %d", want, n)
}
}
func TestSkipSeek(t *testing.T) {
bts := randomBts(1024)
// bytes.Reader implements io.Seeker
rd := NewReaderSize(bytes.NewReader(bts), 200)
n, err := rd.Skip(512)
if err != nil {
t.Fatal(err)
}
if n != 512 {
t.Fatalf("Skip() returned a nil error, but skipped %d bytes instead of %d", n, 512)
}
var b byte
b, err = rd.ReadByte()
if err != nil {
t.Fatal(err)
}
if b != bts[512] {
t.Fatalf("at index %d: %d in; %d out", 512, bts[512], b)
}
n, err = rd.Skip(10)
if err != nil {
t.Fatal(err)
}
if n != 10 {
t.Fatalf("Skip() returned a nil error, but skipped %d bytes instead of %d", n, 10)
}
// now try to skip past the end
rd.Reset(bytes.NewReader(bts))
// because of how bytes.Reader
// implements Seek, this should
// return (2000, nil)
n, err = rd.Skip(2000)
if err != nil {
t.Fatal(err)
}
if n != 2000 {
t.Fatalf("should have returned %d bytes; returned %d", 2000, n)
}
// the next call to Read()
// should return io.EOF
n, err = rd.Read([]byte{0, 0, 0})
if err != io.EOF {
t.Errorf("expected %q; got %q", io.EOF, err)
}
if n != 0 {
t.Errorf("expected 0 bytes read; got %d", n)
}
}
func TestPeek(t *testing.T) {
bts := randomBts(1024)
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 200)
// first, a peek < buffer size
var (
peek []byte
err error
)
peek, err = rd.Peek(100)
if err != nil {
t.Fatal(err)
}
if len(peek) != 100 {
t.Fatalf("asked for %d bytes; got %d", 100, len(peek))
}
if !bytes.Equal(peek, bts[:100]) {
t.Fatal("peeked bytes not equal")
}
// now, a peek > buffer size
peek, err = rd.Peek(256)
if err != nil {
t.Fatal(err)
}
if len(peek) != 256 {
t.Fatalf("asked for %d bytes; got %d", 100, len(peek))
}
if !bytes.Equal(peek, bts[:256]) {
t.Fatal("peeked bytes not equal")
}
// now try to peek past EOF
peek, err = rd.Peek(2048)
if err != io.EOF {
t.Fatalf("expected error %q; got %q", io.EOF, err)
}
if len(peek) != 1024 {
t.Fatalf("expected %d bytes peek-able; got %d", 1024, len(peek))
}
}
func TestNext(t *testing.T) {
size := 1024
bts := randomBts(size)
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 200)
chunksize := 256
chunks := size / chunksize
for i := 0; i < chunks; i++ {
out, err := rd.Next(chunksize)
if err != nil {
t.Fatal(err)
}
start := chunksize * i
if !bytes.Equal(bts[start:start+chunksize], out) {
t.Fatalf("chunk %d: chunks not equal", i+1)
}
}
}
func TestWriteTo(t *testing.T) {
bts := randomBts(2048)
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 200)
// cause the buffer
// to fill a little, just
// to complicate things
rd.Peek(25)
var out bytes.Buffer
n, err := rd.WriteTo(&out)
if err != nil {
t.Fatal(err)
}
if n != 2048 {
t.Fatalf("should have written %d bytes; wrote %d", 2048, n)
}
if !bytes.Equal(out.Bytes(), bts) {
t.Fatal("bytes not equal")
}
}
func TestReadFull(t *testing.T) {
bts := randomBts(1024)
rd := NewReaderSize(partialReader{bytes.NewReader(bts)}, 256)
// try to ReadFull() the whole thing
out := make([]byte, 1024)
n, err := rd.ReadFull(out)
if err != nil {
t.Fatal(err)
}
if n != 1024 {
t.Fatalf("expected to read %d bytes; read %d", 1024, n)
}
if !bytes.Equal(bts, out) {
t.Fatal("bytes not equal")
}
// we've read everything; this should EOF
n, err = rd.Read(out)
if err != io.EOF {
t.Fatalf("expected %q; got %q", io.EOF, err)
}
rd.Reset(partialReader{bytes.NewReader(bts)})
// now try to read *past* EOF
out = make([]byte, 1500)
n, err = rd.ReadFull(out)
if err != io.ErrUnexpectedEOF {
t.Fatalf("expected error %q; got %q", io.EOF, err)
}
if n != 1024 {
t.Fatalf("expected to read %d bytes; read %d", 1024, n)
}
}
type readCounter struct {
r io.Reader
count int
}
func (r *readCounter) Read(p []byte) (int, error) {
r.count++
return r.r.Read(p)
}
func TestReadFullPerf(t *testing.T) {
const size = 1 << 22
data := randomBts(size)
c := readCounter{
r: &partialReader{
r: bytes.NewReader(data),
},
}
r := NewReader(&c)
const segments = 4
out := make([]byte, size/segments)
for i := 0; i < segments; i++ {
// force an unaligned read
_, err := r.Peek(5)
if err != nil {
t.Fatal(err)
}
n, err := r.ReadFull(out)
if err != nil {
t.Fatal(err)
}
if n != size/segments {
t.Fatalf("read %d bytes, not %d", n, size/segments)
}
}
t.Logf("called Read() on the underlying reader %d times to fill %d buffers", c.count, size/r.BufferSize())
}
func TestReaderBufCreation(t *testing.T) {
tests := []struct {
name string
buffer []byte
size int
}{
{name: "nil", buffer: nil, size: minReaderSize},
{name: "empty", buffer: []byte{}, size: minReaderSize},
{name: "allocated", buffer: make([]byte, 0, 200), size: 200},
{name: "filled", buffer: make([]byte, 200), size: 200},
}
for _, test := range tests {
var b bytes.Buffer
r := NewReaderBuf(&b, test.buffer)
if r.BufferSize() != test.size {
t.Errorf("%s: unequal buffer size (got: %d, expected: %d)", test.name, r.BufferSize(), test.size)
}
if r.Buffered() != 0 {
t.Errorf("%s: unequal buffered bytes (got: %d, expected: 0)", test.name, r.Buffered())
}
}
}