-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_test.go
170 lines (148 loc) · 3.96 KB
/
buffer_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
package buffer
import (
"testing"
)
func TestBuffer_Byte(t *testing.T) {
var buffer Buffer
buffer.Init(1)
_, err := buffer.ReadByte()
if err != ErrEmpty {
t.Errorf("ReadByte returned wrong error:\n\texpect: [%v]\n\tactual: [%v]", ErrEmpty, err)
}
err = buffer.WriteByte('a')
if err != nil {
t.Errorf("WriteByte unexpectedly returned non-nil error: %v", err)
}
err = buffer.WriteByte('b')
if err != nil {
t.Errorf("WriteByte unexpectedly returned non-nil error: %v", err)
}
err = buffer.WriteByte('c')
if err != ErrFull {
t.Errorf("WriteByte returned wrong error:\n\texpect: [%v]\n\tactual: [%v]", ErrFull, err)
}
ch, err := buffer.ReadByte()
if err != nil {
t.Errorf("ReadByte unexpectedly returned non-nil error: %v", err)
}
if ch != 'a' {
t.Errorf("ReadByte unexpectedly returned ch=%q, expected %q", rune(ch), rune('a'))
}
err = buffer.WriteByte('c')
if err != nil {
t.Errorf("WriteByte unexpectedly returned non-nil error: %v", err)
}
ch, err = buffer.ReadByte()
if err != nil {
t.Errorf("ReadByte unexpectedly returned non-nil error: %v", err)
}
if ch != 'b' {
t.Errorf("ReadByte unexpectedly returned ch=%q, expected %q", ch, 'b')
}
ch, err = buffer.ReadByte()
if err != nil {
t.Errorf("ReadByte unexpectedly returned non-nil error: %v", err)
}
if ch != 'c' {
t.Errorf("ReadByte unexpectedly returned ch=%q, expected %q", ch, 'c')
}
_, err = buffer.ReadByte()
if err != ErrEmpty {
t.Errorf("ReadByte returned wrong error:\n\texpect: [%v]\n\tactual: [%v]", ErrEmpty, err)
}
}
func TestBuffer_Bytes(t *testing.T) {
var buffer Buffer
buffer.Init(1)
err := buffer.WriteByte('0')
if err != nil {
t.Errorf("WriteByte unexpectedly returned non-nil error: %v", err)
}
nn, err := buffer.Write([]byte{'a'})
if err != nil {
t.Errorf("Write unexpectedly returned non-nil error: %v", err)
}
if nn != 1 {
t.Errorf("Write unexpectedly returned nn=%d, expected %d", nn, 1)
}
ch, err := buffer.ReadByte()
if err != nil {
t.Errorf("ReadByte unexpectedly returned non-nil error: %v", err)
}
if ch != '0' {
t.Errorf("ReadByte unexpectedly returned ch=%q, expected %q", ch, '0')
}
nn, err = buffer.Write([]byte{'b'})
if err != nil {
t.Errorf("Write unexpectedly returned non-nil error: %v", err)
}
if nn != 1 {
t.Errorf("Write unexpectedly returned nn=%d, expected %d", nn, 1)
}
var tmp [4]byte
nn, err = buffer.Read(tmp[:])
if err != nil {
t.Errorf("Read unexpectedly returned non-nil error: %v", err)
}
if nn != 2 {
t.Errorf("Read unexpectedly returned nn=%d, expected %d", nn, 2)
}
if expect := [4]byte{'a', 'b', 0, 0}; tmp != expect {
t.Errorf("Read unexpectedly filled buffer with wrong data:\n\texpect: %#v\n\tactual: %#v", expect, tmp)
}
}
func BenchmarkBuffer_WriteByte_2(b *testing.B) {
var buffer Buffer
buffer.Init(2)
for n := 0; n < b.N; n++ {
err := buffer.WriteByte('a')
if err == ErrFull {
tmp := buffer.PrepareBulkRead(1 << 2)
buffer.CommitBulkRead(uint(len(tmp)))
}
}
}
func BenchmarkBuffer_WriteByte_8(b *testing.B) {
var buffer Buffer
buffer.Init(8)
for n := 0; n < b.N; n++ {
err := buffer.WriteByte('a')
if err == ErrFull {
tmp := buffer.PrepareBulkRead(1 << 8)
buffer.CommitBulkRead(uint(len(tmp)))
}
}
}
func BenchmarkBuffer_WriteByte_15(b *testing.B) {
var buffer Buffer
buffer.Init(15)
for n := 0; n < b.N; n++ {
err := buffer.WriteByte('a')
if err == ErrFull {
tmp := buffer.PrepareBulkRead(1 << 15)
buffer.CommitBulkRead(uint(len(tmp)))
}
}
}
func BenchmarkBuffer_WriteByte_16(b *testing.B) {
var buffer Buffer
buffer.Init(16)
for n := 0; n < b.N; n++ {
err := buffer.WriteByte('a')
if err == ErrFull {
tmp := buffer.PrepareBulkRead(1 << 16)
buffer.CommitBulkRead(uint(len(tmp)))
}
}
}
func BenchmarkBuffer_WriteByte_24(b *testing.B) {
var buffer Buffer
buffer.Init(24)
for n := 0; n < b.N; n++ {
err := buffer.WriteByte('a')
if err == ErrFull {
tmp := buffer.PrepareBulkRead(1 << 24)
buffer.CommitBulkRead(uint(len(tmp)))
}
}
}