1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 const N = 10000 // make this bigger for a larger (and slower) test
16 var data string // test data for write tests
17 var testBytes []byte // test data; same as data but as a slice.
20 testBytes = make([]byte, N)
21 for i := 0; i < N; i++ {
22 testBytes[i] = 'a' + byte(i%26)
24 data = string(testBytes)
27 // Verify that contents of buf match the string s.
28 func check(t *testing.T, testname string, buf *Buffer, s string) {
31 if buf.Len() != len(bytes) {
32 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes))
35 if buf.Len() != len(str) {
36 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str))
39 if buf.Len() != len(s) {
40 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s))
43 if string(bytes) != s {
44 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s)
48 // Fill buf through n writes of byte slice fub.
49 // The initial contents of buf corresponds to the string s;
50 // the result is the final contents of buf returned as a string.
51 func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
52 check(t, testname+" (fill 1)", buf, s)
54 m, err := buf.Write(fub)
56 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub))
59 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
62 check(t, testname+" (fill 4)", buf, s)
67 func TestNewBuffer(t *testing.T) {
68 buf := NewBuffer(testBytes)
69 check(t, "NewBuffer", buf, data)
72 // Empty buf through repeated reads into fub.
73 // The initial contents of buf corresponds to the string s.
74 func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
75 check(t, testname+" (empty 1)", buf, s)
78 n, err := buf.Read(fub)
83 t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err)
86 check(t, testname+" (empty 3)", buf, s)
89 check(t, testname+" (empty 4)", buf, "")
92 func TestBasicOperations(t *testing.T) {
95 for i := 0; i < 5; i++ {
96 check(t, "TestBasicOperations (1)", &buf, "")
99 check(t, "TestBasicOperations (2)", &buf, "")
102 check(t, "TestBasicOperations (3)", &buf, "")
104 n, err := buf.Write([]byte(data[0:1]))
106 t.Errorf("wrote 1 byte, but n == %d", n)
109 t.Errorf("err should always be nil, but err == %s", err)
111 check(t, "TestBasicOperations (4)", &buf, "a")
113 buf.WriteByte(data[1])
114 check(t, "TestBasicOperations (5)", &buf, "ab")
116 n, err = buf.Write([]byte(data[2:26]))
118 t.Errorf("wrote 25 bytes, but n == %d", n)
120 check(t, "TestBasicOperations (6)", &buf, string(data[0:26]))
123 check(t, "TestBasicOperations (7)", &buf, string(data[0:26]))
126 check(t, "TestBasicOperations (8)", &buf, string(data[0:20]))
128 empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5))
129 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
131 buf.WriteByte(data[1])
132 c, err := buf.ReadByte()
134 t.Error("ReadByte unexpected eof")
137 t.Errorf("ReadByte wrong value c=%v", c)
139 c, err = buf.ReadByte()
141 t.Error("ReadByte unexpected not eof")
146 func TestLargeByteWrites(t *testing.T) {
152 for i := 3; i < limit; i += 3 {
153 s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes)
154 empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i))
156 check(t, "TestLargeByteWrites (3)", &buf, "")
159 func TestLargeByteReads(t *testing.T) {
161 for i := 3; i < 30; i += 3 {
162 s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
163 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
165 check(t, "TestLargeByteReads (3)", &buf, "")
168 func TestMixedReadsAndWrites(t *testing.T) {
171 for i := 0; i < 50; i++ {
172 wlen := rand.Intn(len(data))
173 s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen])
174 rlen := rand.Intn(len(data))
175 fub := make([]byte, rlen)
176 n, _ := buf.Read(fub)
179 empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
182 func TestNil(t *testing.T) {
184 if b.String() != "<nil>" {
185 t.Errorf("expected <nil>; got %q", b.String())
189 func TestReadFrom(t *testing.T) {
191 for i := 3; i < 30; i += 3 {
192 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
195 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
199 func TestWriteTo(t *testing.T) {
201 for i := 3; i < 30; i += 3 {
202 s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
205 empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data)))
209 func TestNext(t *testing.T) {
210 b := []byte{0, 1, 2, 3, 4}
211 tmp := make([]byte, 5)
212 for i := 0; i <= 5; i++ {
213 for j := i; j <= 5; j++ {
214 for k := 0; k <= 6; k++ {
215 // 0 <= i <= j <= 5; 0 <= k <= 6
216 // Check that if we start with a buffer
217 // of length j at offset i and ask for
218 // Next(k), we get the right bytes.
219 buf := NewBuffer(b[0:j])
220 n, _ := buf.Read(tmp[0:i])
222 t.Fatalf("Read %d returned %d", i, n)
230 t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb))
232 for l, v := range bb {
234 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i)
242 var readBytesTests = []struct {
248 {"", 0, []string{""}, io.EOF},
249 {"a\x00", 0, []string{"a\x00"}, nil},
250 {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
251 {"hello\x01world", 1, []string{"hello\x01"}, nil},
252 {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF},
253 {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
254 {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF},
257 func TestReadBytes(t *testing.T) {
258 for _, test := range readBytesTests {
259 buf := NewBuffer([]byte(test.buffer))
261 for _, expected := range test.expected {
263 bytes, err = buf.ReadBytes(test.delim)
264 if string(bytes) != expected {
265 t.Errorf("expected %q, got %q", expected, bytes)
272 t.Errorf("expected error %v, got %v", test.err, err)
277 func TestGrow(t *testing.T) {
280 tmp := make([]byte, 72)
281 for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
282 xBytes := bytes.Repeat(x, startLen)
283 for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
284 buf := NewBuffer(xBytes)
285 // If we read, this affects buf.off, which is good to test.
286 readBytes, _ := buf.Read(tmp)
288 yBytes := bytes.Repeat(y, growLen)
289 // Check no allocation occurs in write, as long as we're single-threaded.
290 var m1, m2 runtime.MemStats
291 runtime.ReadMemStats(&m1)
293 runtime.ReadMemStats(&m2)
294 if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
295 t.Errorf("allocation occurred during write")
297 // Check that buffer has correct data.
298 if !bytes.Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) {
299 t.Errorf("bad initial data at %d %d", startLen, growLen)
301 if !bytes.Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
302 t.Errorf("bad written data at %d %d", startLen, growLen)
308 // Was a bug: used to give EOF reading empty slice at EOF.
309 func TestReadEmptyAtEOF(t *testing.T) {
311 slice := make([]byte, 0)
312 n, err := b.Read(slice)
314 t.Errorf("read error: %v", err)
317 t.Errorf("wrong count; got %d want 0", n)
321 // Tests that we occasionally compact. Issue 5154.
322 func TestBufferGrowth(t *testing.T) {
324 buf := make([]byte, 1024)
327 for i := 0; i < 5<<10; i++ {
335 // (*Buffer).grow allows for 2x capacity slop before sliding,
336 // so set our error threshold at 3x.
338 t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0)
343 func BenchmarkBufferNotEmptyWriteRead(b *testing.B) {
344 buf := make([]byte, 1024)
345 for i := 0; i < b.N; i++ {
348 for i := 0; i < 5<<10; i++ {
355 // Check that we don't compact too often. From Issue 5154.
356 func BenchmarkBufferFullSmallReads(b *testing.B) {
357 buf := make([]byte, 1024)
358 for i := 0; i < b.N; i++ {
361 for b.Len()+20 < cap(b.buf) {
364 for i := 0; i < 5<<10; i++ {