-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbufLazy_test.go
108 lines (85 loc) · 2.55 KB
/
bufLazy_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
package tls
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func eval(t *testing.T, buf *LazyBuffer, data []byte, n int, isLazy bool) {
assert.EqualValues(t, buf.Bytes(), data)
assert.EqualValues(t, buf.Len(), n)
assert.EqualValues(t, buf.IsLazy(), isLazy)
}
func TestBufLazyEmpty(t *testing.T) {
var buf LazyBuffer
eval(t, &buf, []byte(nil), 0, true)
buf.Write(nil)
eval(t, &buf, []byte{}, 0, false)
}
func TestBufLazyLazyMode(t *testing.T) {
var buf LazyBuffer
var data []byte = []byte("Hello World!")
buf.Set(data)
eval(t, &buf, data, len(data), true)
// Next 1 byte
assert.EqualValues(t, buf.Next(1), data[:1])
eval(t, &buf, data[1:], len(data)-1, true)
// Next remaining byte
assert.EqualValues(t, buf.Next(len(data)-1), data[1:])
eval(t, &buf, make([]byte, 0), 0, true)
// Next 1 more byte
assert.EqualValues(t, buf.Next(1), data[:0])
eval(t, &buf, make([]byte, 0), 0, true)
// Reset the data, must be in lazy mode as the previous data is drained
buf.Set(data)
eval(t, &buf, data, len(data), true)
// Next all byte + 1
assert.EqualValues(t, buf.Next(len(data)+1), data)
eval(t, &buf, make([]byte, 0), 0, true)
// Done
buf.Done()
eval(t, &buf, []byte(nil), 0, true)
}
func TestBufLazyLazyToWriteMode(t *testing.T) {
var buf LazyBuffer
var data []byte = []byte("Hello World!")
buf.Set(data)
eval(t, &buf, data, len(data), true)
// switch to write
buf.Set(data)
doubleData := append(data, data...)
eval(t, &buf, doubleData, len(doubleData), false)
// append new data
doubleData = append(doubleData, data...)
buf.Set(data)
eval(t, &buf, doubleData, len(doubleData), false)
buf.Done()
eval(t, &buf, []byte(nil), 0, true)
}
func TestBufLazyWriteMode(t *testing.T) {
var buf LazyBuffer
var data []byte = []byte(strings.Repeat("A", defaultSize))
buf.Grow(defaultSize)
// fill up the default buffer
buf.Write(data)
eval(t, &buf, data, len(data), false)
// grow the buffer
buf.Write(data[:1])
doubleData := append(data, data...)
eval(t, &buf, doubleData[:len(data)+1], len(data)+1, false)
// fill up the remaining buffer
buf.Write(data[1:])
eval(t, &buf, doubleData, len(doubleData), false)
// consume half of the data
assert.EqualValues(t, buf.Next(len(data)), data)
eval(t, &buf, data, len(data), false)
// consume 1 byte
assert.EqualValues(t, buf.Next(1), data[:1])
eval(t, &buf, data[1:], len(data)-1, false)
// grow 1 byte, the data is copied to the beginning
// slide things down
buf.Grow(1)
eval(t, &buf, data[1:], len(data)-1, false)
// Done
buf.Done()
eval(t, &buf, []byte(nil), 0, true)
}