-
Notifications
You must be signed in to change notification settings - Fork 5
/
byte_reader_test.go
89 lines (70 loc) · 2.5 KB
/
byte_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
package rdb
import (
"bytes"
"errors"
"io"
"math/rand"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func makeRandBuffer(n int) []byte {
buf := make([]byte, n)
// nolint: gosec
_, err := rand.Read(buf)
Expect(err).NotTo(HaveOccurred())
return buf
}
func mustReadBytes(data []byte, err error) []byte {
Expect(err).NotTo(HaveOccurred())
return data
}
var _ = Describe("sliceReader", func() {
It("read data", func() {
buf := makeRandBuffer(10)
reader := newSliceReader(buf)
Expect(mustReadBytes(reader.ReadBytes(4))).To(Equal(buf[0:4]))
Expect(mustReadBytes(reader.ReadBytes(8))).To(Equal(buf[4:10]))
_, err := reader.ReadBytes(1)
Expect(errors.Is(err, io.EOF)).To(BeTrue())
})
})
var _ = Describe("bufferReader", func() {
It("small data", func() {
buf := makeRandBuffer(4)
reader := newBufferReader(bytes.NewReader(buf))
Expect(mustReadBytes(reader.ReadBytes(1))).To(Equal(buf[0:1]))
Expect(mustReadBytes(reader.ReadBytes(2))).To(Equal(buf[1:3]))
})
It("unexpected EOF", func() {
buf := makeRandBuffer(4)
reader := newBufferReader(bytes.NewReader(buf))
actual, err := reader.ReadBytes(5)
Expect(actual).To(BeNil())
Expect(errors.Is(err, io.ErrUnexpectedEOF)).To(BeTrue())
})
It("larger than max buffer size", func() {
buf := makeRandBuffer(4100)
reader := newBufferReader(bytes.NewReader(buf))
Expect(mustReadBytes(reader.ReadBytes(1))).To(Equal(buf[0:1]))
Expect(mustReadBytes(reader.ReadBytes(4097))).To(Equal(buf[1:4098]))
Expect(mustReadBytes(reader.ReadBytes(2))).To(Equal(buf[4098:4100]))
})
It("gradually extend the capacity", func() {
buf := makeRandBuffer(8000)
reader := newBufferReader(bytes.NewReader(buf))
Expect(mustReadBytes(reader.ReadBytes(4))).To(Equal(buf[0:4]))
Expect(mustReadBytes(reader.ReadBytes(513))).To(Equal(buf[4:517]))
Expect(mustReadBytes(reader.ReadBytes(1025))).To(Equal(buf[517:1542]))
Expect(mustReadBytes(reader.ReadBytes(2049))).To(Equal(buf[1542:3591]))
Expect(mustReadBytes(reader.ReadBytes(4097))).To(Equal(buf[3591:7688]))
Expect(mustReadBytes(reader.ReadBytes(200))).To(Equal(buf[7688:7888]))
Expect(mustReadBytes(reader.ReadBytes(100))).To(Equal(buf[7888:7988]))
})
It("move remaining data to the front", func() {
buf := makeRandBuffer(1200)
reader := newBufferReader(bytes.NewReader(buf))
Expect(mustReadBytes(reader.ReadBytes(200))).To(Equal(buf[0:200]))
Expect(mustReadBytes(reader.ReadBytes(400))).To(Equal(buf[200:600]))
Expect(mustReadBytes(reader.ReadBytes(600))).To(Equal(buf[600:1200]))
})
})