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.
16 const N
= 10000 // make this bigger for a larger (and slower) test
17 var data
string // test data for write tests
18 var testBytes
[]byte // test data; same as data but as a slice.
21 testBytes
= make([]byte, N
)
22 for i
:= 0; i
< N
; i
++ {
23 testBytes
[i
] = 'a' + byte(i%26
)
25 data
= string(testBytes
)
28 // Verify that contents of buf match the string s.
29 func check(t
*testing
.T
, testname
string, buf
*Buffer
, s
string) {
32 if buf
.Len() != len(bytes
) {
33 t
.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname
, buf
.Len(), len(bytes
))
36 if buf
.Len() != len(str
) {
37 t
.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname
, buf
.Len(), len(str
))
40 if buf
.Len() != len(s
) {
41 t
.Errorf("%s: buf.Len() == %d, len(s) == %d", testname
, buf
.Len(), len(s
))
44 if string(bytes
) != s
{
45 t
.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname
, string(bytes
), s
)
49 // Fill buf through n writes of string fus.
50 // The initial contents of buf corresponds to the string s;
51 // the result is the final contents of buf returned as a string.
52 func fillString(t
*testing
.T
, testname
string, buf
*Buffer
, s
string, n
int, fus
string) string {
53 check(t
, testname
+" (fill 1)", buf
, s
)
55 m
, err
:= buf
.WriteString(fus
)
57 t
.Errorf(testname
+" (fill 2): m == %d, expected %d", m
, len(fus
))
60 t
.Errorf(testname
+" (fill 3): err should always be nil, found err == %s", err
)
63 check(t
, testname
+" (fill 4)", buf
, s
)
68 // Fill buf through n writes of byte slice fub.
69 // The initial contents of buf corresponds to the string s;
70 // the result is the final contents of buf returned as a string.
71 func fillBytes(t
*testing
.T
, testname
string, buf
*Buffer
, s
string, n
int, fub
[]byte) string {
72 check(t
, testname
+" (fill 1)", buf
, s
)
74 m
, err
:= buf
.Write(fub
)
76 t
.Errorf(testname
+" (fill 2): m == %d, expected %d", m
, len(fub
))
79 t
.Errorf(testname
+" (fill 3): err should always be nil, found err == %s", err
)
82 check(t
, testname
+" (fill 4)", buf
, s
)
87 func TestNewBuffer(t
*testing
.T
) {
88 buf
:= NewBuffer(testBytes
)
89 check(t
, "NewBuffer", buf
, data
)
92 func TestNewBufferString(t
*testing
.T
) {
93 buf
:= NewBufferString(data
)
94 check(t
, "NewBufferString", buf
, data
)
97 // Empty buf through repeated reads into fub.
98 // The initial contents of buf corresponds to the string s.
99 func empty(t
*testing
.T
, testname
string, buf
*Buffer
, s
string, fub
[]byte) {
100 check(t
, testname
+" (empty 1)", buf
, s
)
103 n
, err
:= buf
.Read(fub
)
108 t
.Errorf(testname
+" (empty 2): err should always be nil, found err == %s", err
)
111 check(t
, testname
+" (empty 3)", buf
, s
)
114 check(t
, testname
+" (empty 4)", buf
, "")
117 func TestBasicOperations(t
*testing
.T
) {
120 for i
:= 0; i
< 5; i
++ {
121 check(t
, "TestBasicOperations (1)", &buf
, "")
124 check(t
, "TestBasicOperations (2)", &buf
, "")
127 check(t
, "TestBasicOperations (3)", &buf
, "")
129 n
, err
:= buf
.Write([]byte(data
[0:1]))
131 t
.Errorf("wrote 1 byte, but n == %d", n
)
134 t
.Errorf("err should always be nil, but err == %s", err
)
136 check(t
, "TestBasicOperations (4)", &buf
, "a")
138 buf
.WriteByte(data
[1])
139 check(t
, "TestBasicOperations (5)", &buf
, "ab")
141 n
, err
= buf
.Write([]byte(data
[2:26]))
143 t
.Errorf("wrote 25 bytes, but n == %d", n
)
145 check(t
, "TestBasicOperations (6)", &buf
, string(data
[0:26]))
148 check(t
, "TestBasicOperations (7)", &buf
, string(data
[0:26]))
151 check(t
, "TestBasicOperations (8)", &buf
, string(data
[0:20]))
153 empty(t
, "TestBasicOperations (9)", &buf
, string(data
[0:20]), make([]byte, 5))
154 empty(t
, "TestBasicOperations (10)", &buf
, "", make([]byte, 100))
156 buf
.WriteByte(data
[1])
157 c
, err
:= buf
.ReadByte()
159 t
.Error("ReadByte unexpected eof")
162 t
.Errorf("ReadByte wrong value c=%v", c
)
164 c
, err
= buf
.ReadByte()
166 t
.Error("ReadByte unexpected not eof")
171 func TestLargeStringWrites(t
*testing
.T
) {
177 for i
:= 3; i
< limit
; i
+= 3 {
178 s
:= fillString(t
, "TestLargeWrites (1)", &buf
, "", 5, data
)
179 empty(t
, "TestLargeStringWrites (2)", &buf
, s
, make([]byte, len(data
)/i
))
181 check(t
, "TestLargeStringWrites (3)", &buf
, "")
184 func TestLargeByteWrites(t
*testing
.T
) {
190 for i
:= 3; i
< limit
; i
+= 3 {
191 s
:= fillBytes(t
, "TestLargeWrites (1)", &buf
, "", 5, testBytes
)
192 empty(t
, "TestLargeByteWrites (2)", &buf
, s
, make([]byte, len(data
)/i
))
194 check(t
, "TestLargeByteWrites (3)", &buf
, "")
197 func TestLargeStringReads(t
*testing
.T
) {
199 for i
:= 3; i
< 30; i
+= 3 {
200 s
:= fillString(t
, "TestLargeReads (1)", &buf
, "", 5, data
[0:len(data
)/i
])
201 empty(t
, "TestLargeReads (2)", &buf
, s
, make([]byte, len(data
)))
203 check(t
, "TestLargeStringReads (3)", &buf
, "")
206 func TestLargeByteReads(t
*testing
.T
) {
208 for i
:= 3; i
< 30; i
+= 3 {
209 s
:= fillBytes(t
, "TestLargeReads (1)", &buf
, "", 5, testBytes
[0:len(testBytes
)/i
])
210 empty(t
, "TestLargeReads (2)", &buf
, s
, make([]byte, len(data
)))
212 check(t
, "TestLargeByteReads (3)", &buf
, "")
215 func TestMixedReadsAndWrites(t
*testing
.T
) {
218 for i
:= 0; i
< 50; i
++ {
219 wlen
:= rand
.Intn(len(data
))
221 s
= fillString(t
, "TestMixedReadsAndWrites (1)", &buf
, s
, 1, data
[0:wlen
])
223 s
= fillBytes(t
, "TestMixedReadsAndWrites (1)", &buf
, s
, 1, testBytes
[0:wlen
])
226 rlen
:= rand
.Intn(len(data
))
227 fub
:= make([]byte, rlen
)
228 n
, _
:= buf
.Read(fub
)
231 empty(t
, "TestMixedReadsAndWrites (2)", &buf
, s
, make([]byte, buf
.Len()))
234 func TestNil(t
*testing
.T
) {
236 if b
.String() != "<nil>" {
237 t
.Errorf("expected <nil>; got %q", b
.String())
241 func TestReadFrom(t
*testing
.T
) {
243 for i
:= 3; i
< 30; i
+= 3 {
244 s
:= fillBytes(t
, "TestReadFrom (1)", &buf
, "", 5, testBytes
[0:len(testBytes
)/i
])
247 empty(t
, "TestReadFrom (2)", &b
, s
, make([]byte, len(data
)))
251 func TestWriteTo(t
*testing
.T
) {
253 for i
:= 3; i
< 30; i
+= 3 {
254 s
:= fillBytes(t
, "TestWriteTo (1)", &buf
, "", 5, testBytes
[0:len(testBytes
)/i
])
257 empty(t
, "TestWriteTo (2)", &b
, s
, make([]byte, len(data
)))
261 func TestRuneIO(t
*testing
.T
) {
263 // Built a test slice while we write the data
264 b
:= make([]byte, utf8
.UTFMax
*NRune
)
267 for r
:= rune(0); r
< NRune
; r
++ {
268 size
:= utf8
.EncodeRune(b
[n
:], r
)
269 nbytes
, err
:= buf
.WriteRune(r
)
271 t
.Fatalf("WriteRune(%U) error: %s", r
, err
)
274 t
.Fatalf("WriteRune(%U) expected %d, got %d", r
, size
, nbytes
)
280 // Check the resulting bytes
281 if !Equal(buf
.Bytes(), b
) {
282 t
.Fatalf("incorrect result from WriteRune: %q not %q", buf
.Bytes(), b
)
285 p
:= make([]byte, utf8
.UTFMax
)
286 // Read it back with ReadRune
287 for r
:= rune(0); r
< NRune
; r
++ {
288 size
:= utf8
.EncodeRune(p
, r
)
289 nr
, nbytes
, err
:= buf
.ReadRune()
290 if nr
!= r || nbytes
!= size || err
!= nil {
291 t
.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r
, nr
, nbytes
, r
, size
, err
)
295 // Check that UnreadRune works
298 for r
:= rune(0); r
< NRune
; r
++ {
299 r1
, size
, _
:= buf
.ReadRune()
300 if err
:= buf
.UnreadRune(); err
!= nil {
301 t
.Fatalf("UnreadRune(%U) got error %q", r
, err
)
303 r2
, nbytes
, err
:= buf
.ReadRune()
304 if r1
!= r2 || r1
!= r || nbytes
!= size || err
!= nil {
305 t
.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r
, r2
, nbytes
, r
, size
, err
)
310 func TestNext(t
*testing
.T
) {
311 b
:= []byte{0, 1, 2, 3, 4}
312 tmp
:= make([]byte, 5)
313 for i
:= 0; i
<= 5; i
++ {
314 for j
:= i
; j
<= 5; j
++ {
315 for k
:= 0; k
<= 6; k
++ {
316 // 0 <= i <= j <= 5; 0 <= k <= 6
317 // Check that if we start with a buffer
318 // of length j at offset i and ask for
319 // Next(k), we get the right bytes.
320 buf
:= NewBuffer(b
[0:j
])
321 n
, _
:= buf
.Read(tmp
[0:i
])
323 t
.Fatalf("Read %d returned %d", i
, n
)
331 t
.Fatalf("in %d,%d: len(Next(%d)) == %d", i
, j
, k
, len(bb
))
333 for l
, v
:= range bb
{
335 t
.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i
, j
, k
, l
, v
, l
+i
)
343 var readBytesTests
= []struct {
349 {"", 0, []string{""}, io
.EOF
},
350 {"a\x00", 0, []string{"a\x00"}, nil},
351 {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
352 {"hello\x01world", 1, []string{"hello\x01"}, nil},
353 {"foo\nbar", 0, []string{"foo\nbar"}, io
.EOF
},
354 {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
355 {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io
.EOF
},
358 func TestReadBytes(t
*testing
.T
) {
359 for _
, test
:= range readBytesTests
{
360 buf
:= NewBufferString(test
.buffer
)
362 for _
, expected
:= range test
.expected
{
364 bytes
, err
= buf
.ReadBytes(test
.delim
)
365 if string(bytes
) != expected
{
366 t
.Errorf("expected %q, got %q", expected
, bytes
)
373 t
.Errorf("expected error %v, got %v", test
.err
, err
)
378 func TestReadString(t
*testing
.T
) {
379 for _
, test
:= range readBytesTests
{
380 buf
:= NewBufferString(test
.buffer
)
382 for _
, expected
:= range test
.expected
{
384 s
, err
= buf
.ReadString(test
.delim
)
386 t
.Errorf("expected %q, got %q", expected
, s
)
393 t
.Errorf("expected error %v, got %v", test
.err
, err
)
398 func BenchmarkReadString(b
*testing
.B
) {
401 data
:= make([]byte, n
)
404 for i
:= 0; i
< b
.N
; i
++ {
405 buf
:= NewBuffer(data
)
406 _
, err
:= buf
.ReadString('x')
413 func TestGrow(t
*testing
.T
) {
416 tmp
:= make([]byte, 72)
417 for _
, startLen
:= range []int{0, 100, 1000, 10000, 100000} {
418 xBytes
:= Repeat(x
, startLen
)
419 for _
, growLen
:= range []int{0, 100, 1000, 10000, 100000} {
420 buf
:= NewBuffer(xBytes
)
421 // If we read, this affects buf.off, which is good to test.
422 readBytes
, _
:= buf
.Read(tmp
)
424 yBytes
:= Repeat(y
, growLen
)
425 // Check no allocation occurs in write, as long as we're single-threaded.
426 var m1
, m2 runtime
.MemStats
427 runtime
.ReadMemStats(&m1
)
429 runtime
.ReadMemStats(&m2
)
430 if runtime
.GOMAXPROCS(-1) == 1 && m1
.Mallocs
!= m2
.Mallocs
{
431 t
.Errorf("allocation occurred during write")
433 // Check that buffer has correct data.
434 if !Equal(buf
.Bytes()[0:startLen
-readBytes
], xBytes
[readBytes
:]) {
435 t
.Errorf("bad initial data at %d %d", startLen
, growLen
)
437 if !Equal(buf
.Bytes()[startLen
-readBytes
:startLen
-readBytes
+growLen
], yBytes
) {
438 t
.Errorf("bad written data at %d %d", startLen
, growLen
)
444 // Was a bug: used to give EOF reading empty slice at EOF.
445 func TestReadEmptyAtEOF(t
*testing
.T
) {
447 slice
:= make([]byte, 0)
448 n
, err
:= b
.Read(slice
)
450 t
.Errorf("read error: %v", err
)
453 t
.Errorf("wrong count; got %d want 0", n
)
457 func TestUnreadByte(t
*testing
.T
) {
459 b
.WriteString("abcdefghijklmnopqrstuvwxyz")
461 _
, err
:= b
.ReadBytes('m')
463 t
.Fatalf("ReadBytes: %v", err
)
468 t
.Fatalf("UnreadByte: %v", err
)
470 c
, err
:= b
.ReadByte()
472 t
.Fatalf("ReadByte: %v", err
)
475 t
.Errorf("ReadByte = %q; want %q", c
, 'm')
479 // Tests that we occasionally compact. Issue 5154.
480 func TestBufferGrowth(t
*testing
.T
) {
482 buf
:= make([]byte, 1024)
485 for i
:= 0; i
< 5<<10; i
++ {
493 // (*Buffer).grow allows for 2x capacity slop before sliding,
494 // so set our error threshold at 3x.
496 t
.Errorf("buffer cap = %d; too big (grew from %d)", cap1
, cap0
)
501 func BenchmarkBufferNotEmptyWriteRead(b
*testing
.B
) {
502 buf
:= make([]byte, 1024)
503 for i
:= 0; i
< b
.N
; i
++ {
506 for i
:= 0; i
< 5<<10; i
++ {
513 // Check that we don't compact too often. From Issue 5154.
514 func BenchmarkBufferFullSmallReads(b
*testing
.B
) {
515 buf
:= make([]byte, 1024)
516 for i
:= 0; i
< b
.N
; i
++ {
519 for b
.Len()+20 < b
.Cap() {
522 for i
:= 0; i
< 5<<10; i
++ {