-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer_test.go
153 lines (123 loc) · 3.04 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
package ioutils // import "github.com/docker/docker/pkg/ioutils"
import (
"bytes"
"testing"
)
func TestFixedBufferCap(t *testing.T) {
buf := &fixedBuffer{buf: make([]byte, 0, 5)}
n := buf.Cap()
if n != 5 {
t.Fatalf("expected buffer capacity to be 5 bytes, got %d", n)
}
}
func TestFixedBufferLen(t *testing.T) {
buf := &fixedBuffer{buf: make([]byte, 0, 10)}
buf.Write([]byte("hello"))
l := buf.Len()
if l != 5 {
t.Fatalf("expected buffer length to be 5 bytes, got %d", l)
}
buf.Write([]byte("world"))
l = buf.Len()
if l != 10 {
t.Fatalf("expected buffer length to be 10 bytes, got %d", l)
}
// read 5 bytes
b := make([]byte, 5)
buf.Read(b)
l = buf.Len()
if l != 5 {
t.Fatalf("expected buffer length to be 5 bytes, got %d", l)
}
n, err := buf.Write([]byte("i-wont-fit"))
if n != 0 {
t.Fatalf("expected no bytes to be written to buffer, got %d", n)
}
if err != errBufferFull {
t.Fatalf("expected errBufferFull, got %v", err)
}
l = buf.Len()
if l != 5 {
t.Fatalf("expected buffer length to still be 5 bytes, got %d", l)
}
buf.Reset()
l = buf.Len()
if l != 0 {
t.Fatalf("expected buffer length to still be 0 bytes, got %d", l)
}
}
func TestFixedBufferString(t *testing.T) {
buf := &fixedBuffer{buf: make([]byte, 0, 10)}
buf.Write([]byte("hello"))
buf.Write([]byte("world"))
out := buf.String()
if out != "helloworld" {
t.Fatalf("expected output to be \"helloworld\", got %q", out)
}
// read 5 bytes
b := make([]byte, 5)
buf.Read(b)
// test that fixedBuffer.String() only returns the part that hasn't been read
out = buf.String()
if out != "world" {
t.Fatalf("expected output to be \"world\", got %q", out)
}
}
func TestFixedBufferWrite(t *testing.T) {
buf := &fixedBuffer{buf: make([]byte, 0, 64)}
n, err := buf.Write([]byte("hello"))
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatalf("expected 5 bytes written, got %d", n)
}
if string(buf.buf[:5]) != "hello" {
t.Fatalf("expected \"hello\", got %q", string(buf.buf[:5]))
}
n, err = buf.Write(bytes.Repeat([]byte{1}, 64))
if n != 59 {
t.Fatalf("expected 59 bytes written before buffer is full, got %d", n)
}
if err != errBufferFull {
t.Fatalf("expected errBufferFull, got %v - %v", err, buf.buf[:64])
}
}
func TestFixedBufferRead(t *testing.T) {
buf := &fixedBuffer{buf: make([]byte, 0, 64)}
if _, err := buf.Write([]byte("hello world")); err != nil {
t.Fatal(err)
}
b := make([]byte, 5)
n, err := buf.Read(b)
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatalf("expected 5 bytes read, got %d - %s", n, buf.String())
}
if string(b) != "hello" {
t.Fatalf("expected \"hello\", got %q", string(b))
}
n, err = buf.Read(b)
if err != nil {
t.Fatal(err)
}
if n != 5 {
t.Fatalf("expected 5 bytes read, got %d", n)
}
if string(b) != " worl" {
t.Fatalf("expected \" worl\", got %s", string(b))
}
b = b[:1]
n, err = buf.Read(b)
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatalf("expected 1 byte read, got %d - %s", n, buf.String())
}
if string(b) != "d" {
t.Fatalf("expected \"d\", got %s", string(b))
}
}