[RS6000] function for linux64 SUBSUBTARGET_OVERRIDE_OPTIONS
[official-gcc.git] / libgo / go / bytes / buffer_test.go
blobfec5ef8a35f637dacd1c95b9ad3034b6f3559308
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.
5 package bytes_test
7 import (
8 . "bytes"
9 "io"
10 "math/rand"
11 "testing"
12 "unicode/utf8"
15 const N = 10000 // make this bigger for a larger (and slower) test
16 var testString string // test data for write tests
17 var testBytes []byte // test data; same as testString but as a slice.
19 type negativeReader struct{}
21 func (r *negativeReader) Read([]byte) (int, error) { return -1, nil }
23 func init() {
24 testBytes = make([]byte, N)
25 for i := 0; i < N; i++ {
26 testBytes[i] = 'a' + byte(i%26)
28 testString = string(testBytes)
31 // Verify that contents of buf match the string s.
32 func check(t *testing.T, testname string, buf *Buffer, s string) {
33 bytes := buf.Bytes()
34 str := buf.String()
35 if buf.Len() != len(bytes) {
36 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes))
39 if buf.Len() != len(str) {
40 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str))
43 if buf.Len() != len(s) {
44 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s))
47 if string(bytes) != s {
48 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s)
52 // Fill buf through n writes of string fus.
53 // The initial contents of buf corresponds to the string s;
54 // the result is the final contents of buf returned as a string.
55 func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string {
56 check(t, testname+" (fill 1)", buf, s)
57 for ; n > 0; n-- {
58 m, err := buf.WriteString(fus)
59 if m != len(fus) {
60 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus))
62 if err != nil {
63 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
65 s += fus
66 check(t, testname+" (fill 4)", buf, s)
68 return s
71 // Fill buf through n writes of byte slice fub.
72 // The initial contents of buf corresponds to the string s;
73 // the result is the final contents of buf returned as a string.
74 func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
75 check(t, testname+" (fill 1)", buf, s)
76 for ; n > 0; n-- {
77 m, err := buf.Write(fub)
78 if m != len(fub) {
79 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub))
81 if err != nil {
82 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
84 s += string(fub)
85 check(t, testname+" (fill 4)", buf, s)
87 return s
90 func TestNewBuffer(t *testing.T) {
91 buf := NewBuffer(testBytes)
92 check(t, "NewBuffer", buf, testString)
95 func TestNewBufferString(t *testing.T) {
96 buf := NewBufferString(testString)
97 check(t, "NewBufferString", buf, testString)
100 // Empty buf through repeated reads into fub.
101 // The initial contents of buf corresponds to the string s.
102 func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
103 check(t, testname+" (empty 1)", buf, s)
105 for {
106 n, err := buf.Read(fub)
107 if n == 0 {
108 break
110 if err != nil {
111 t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err)
113 s = s[n:]
114 check(t, testname+" (empty 3)", buf, s)
117 check(t, testname+" (empty 4)", buf, "")
120 func TestBasicOperations(t *testing.T) {
121 var buf Buffer
123 for i := 0; i < 5; i++ {
124 check(t, "TestBasicOperations (1)", &buf, "")
126 buf.Reset()
127 check(t, "TestBasicOperations (2)", &buf, "")
129 buf.Truncate(0)
130 check(t, "TestBasicOperations (3)", &buf, "")
132 n, err := buf.Write(testBytes[0:1])
133 if want := 1; err != nil || n != want {
134 t.Errorf("Write: got (%d, %v), want (%d, %v)", n, err, want, nil)
136 check(t, "TestBasicOperations (4)", &buf, "a")
138 buf.WriteByte(testString[1])
139 check(t, "TestBasicOperations (5)", &buf, "ab")
141 n, err = buf.Write(testBytes[2:26])
142 if want := 24; err != nil || n != want {
143 t.Errorf("Write: got (%d, %v), want (%d, %v)", n, err, want, nil)
145 check(t, "TestBasicOperations (6)", &buf, testString[0:26])
147 buf.Truncate(26)
148 check(t, "TestBasicOperations (7)", &buf, testString[0:26])
150 buf.Truncate(20)
151 check(t, "TestBasicOperations (8)", &buf, testString[0:20])
153 empty(t, "TestBasicOperations (9)", &buf, testString[0:20], make([]byte, 5))
154 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
156 buf.WriteByte(testString[1])
157 c, err := buf.ReadByte()
158 if want := testString[1]; err != nil || c != want {
159 t.Errorf("ReadByte: got (%q, %v), want (%q, %v)", c, err, want, nil)
161 c, err = buf.ReadByte()
162 if err != io.EOF {
163 t.Errorf("ReadByte: got (%q, %v), want (%q, %v)", c, err, byte(0), io.EOF)
168 func TestLargeStringWrites(t *testing.T) {
169 var buf Buffer
170 limit := 30
171 if testing.Short() {
172 limit = 9
174 for i := 3; i < limit; i += 3 {
175 s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, testString)
176 empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(testString)/i))
178 check(t, "TestLargeStringWrites (3)", &buf, "")
181 func TestLargeByteWrites(t *testing.T) {
182 var buf Buffer
183 limit := 30
184 if testing.Short() {
185 limit = 9
187 for i := 3; i < limit; i += 3 {
188 s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes)
189 empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(testString)/i))
191 check(t, "TestLargeByteWrites (3)", &buf, "")
194 func TestLargeStringReads(t *testing.T) {
195 var buf Buffer
196 for i := 3; i < 30; i += 3 {
197 s := fillString(t, "TestLargeReads (1)", &buf, "", 5, testString[0:len(testString)/i])
198 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
200 check(t, "TestLargeStringReads (3)", &buf, "")
203 func TestLargeByteReads(t *testing.T) {
204 var buf Buffer
205 for i := 3; i < 30; i += 3 {
206 s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
207 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(testString)))
209 check(t, "TestLargeByteReads (3)", &buf, "")
212 func TestMixedReadsAndWrites(t *testing.T) {
213 var buf Buffer
214 s := ""
215 for i := 0; i < 50; i++ {
216 wlen := rand.Intn(len(testString))
217 if i%2 == 0 {
218 s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testString[0:wlen])
219 } else {
220 s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen])
223 rlen := rand.Intn(len(testString))
224 fub := make([]byte, rlen)
225 n, _ := buf.Read(fub)
226 s = s[n:]
228 empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
231 func TestCapWithPreallocatedSlice(t *testing.T) {
232 buf := NewBuffer(make([]byte, 10))
233 n := buf.Cap()
234 if n != 10 {
235 t.Errorf("expected 10, got %d", n)
239 func TestCapWithSliceAndWrittenData(t *testing.T) {
240 buf := NewBuffer(make([]byte, 0, 10))
241 buf.Write([]byte("test"))
242 n := buf.Cap()
243 if n != 10 {
244 t.Errorf("expected 10, got %d", n)
248 func TestNil(t *testing.T) {
249 var b *Buffer
250 if b.String() != "<nil>" {
251 t.Errorf("expected <nil>; got %q", b.String())
255 func TestReadFrom(t *testing.T) {
256 var buf Buffer
257 for i := 3; i < 30; i += 3 {
258 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
259 var b Buffer
260 b.ReadFrom(&buf)
261 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(testString)))
265 type panicReader struct{ panic bool }
267 func (r panicReader) Read(p []byte) (int, error) {
268 if r.panic {
269 panic(nil)
271 return 0, io.EOF
274 // Make sure that an empty Buffer remains empty when
275 // it is "grown" before a Read that panics
276 func TestReadFromPanicReader(t *testing.T) {
278 // First verify non-panic behaviour
279 var buf Buffer
280 i, err := buf.ReadFrom(panicReader{})
281 if err != nil {
282 t.Fatal(err)
284 if i != 0 {
285 t.Fatalf("unexpected return from bytes.ReadFrom (1): got: %d, want %d", i, 0)
287 check(t, "TestReadFromPanicReader (1)", &buf, "")
289 // Confirm that when Reader panics, the empty buffer remains empty
290 var buf2 Buffer
291 defer func() {
292 recover()
293 check(t, "TestReadFromPanicReader (2)", &buf2, "")
295 buf2.ReadFrom(panicReader{panic: true})
298 func TestReadFromNegativeReader(t *testing.T) {
299 var b Buffer
300 defer func() {
301 switch err := recover().(type) {
302 case nil:
303 t.Fatal("bytes.Buffer.ReadFrom didn't panic")
304 case error:
305 // this is the error string of errNegativeRead
306 wantError := "bytes.Buffer: reader returned negative count from Read"
307 if err.Error() != wantError {
308 t.Fatalf("recovered panic: got %v, want %v", err.Error(), wantError)
310 default:
311 t.Fatalf("unexpected panic value: %#v", err)
315 b.ReadFrom(new(negativeReader))
318 func TestWriteTo(t *testing.T) {
319 var buf Buffer
320 for i := 3; i < 30; i += 3 {
321 s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
322 var b Buffer
323 buf.WriteTo(&b)
324 empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(testString)))
328 func TestRuneIO(t *testing.T) {
329 const NRune = 1000
330 // Built a test slice while we write the data
331 b := make([]byte, utf8.UTFMax*NRune)
332 var buf Buffer
333 n := 0
334 for r := rune(0); r < NRune; r++ {
335 size := utf8.EncodeRune(b[n:], r)
336 nbytes, err := buf.WriteRune(r)
337 if err != nil {
338 t.Fatalf("WriteRune(%U) error: %s", r, err)
340 if nbytes != size {
341 t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes)
343 n += size
345 b = b[0:n]
347 // Check the resulting bytes
348 if !Equal(buf.Bytes(), b) {
349 t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b)
352 p := make([]byte, utf8.UTFMax)
353 // Read it back with ReadRune
354 for r := rune(0); r < NRune; r++ {
355 size := utf8.EncodeRune(p, r)
356 nr, nbytes, err := buf.ReadRune()
357 if nr != r || nbytes != size || err != nil {
358 t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err)
362 // Check that UnreadRune works
363 buf.Reset()
365 // check at EOF
366 if err := buf.UnreadRune(); err == nil {
367 t.Fatal("UnreadRune at EOF: got no error")
369 if _, _, err := buf.ReadRune(); err == nil {
370 t.Fatal("ReadRune at EOF: got no error")
372 if err := buf.UnreadRune(); err == nil {
373 t.Fatal("UnreadRune after ReadRune at EOF: got no error")
376 // check not at EOF
377 buf.Write(b)
378 for r := rune(0); r < NRune; r++ {
379 r1, size, _ := buf.ReadRune()
380 if err := buf.UnreadRune(); err != nil {
381 t.Fatalf("UnreadRune(%U) got error %q", r, err)
383 r2, nbytes, err := buf.ReadRune()
384 if r1 != r2 || r1 != r || nbytes != size || err != nil {
385 t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err)
390 func TestNext(t *testing.T) {
391 b := []byte{0, 1, 2, 3, 4}
392 tmp := make([]byte, 5)
393 for i := 0; i <= 5; i++ {
394 for j := i; j <= 5; j++ {
395 for k := 0; k <= 6; k++ {
396 // 0 <= i <= j <= 5; 0 <= k <= 6
397 // Check that if we start with a buffer
398 // of length j at offset i and ask for
399 // Next(k), we get the right bytes.
400 buf := NewBuffer(b[0:j])
401 n, _ := buf.Read(tmp[0:i])
402 if n != i {
403 t.Fatalf("Read %d returned %d", i, n)
405 bb := buf.Next(k)
406 want := k
407 if want > j-i {
408 want = j - i
410 if len(bb) != want {
411 t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb))
413 for l, v := range bb {
414 if v != byte(l+i) {
415 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i)
423 var readBytesTests = []struct {
424 buffer string
425 delim byte
426 expected []string
427 err error
429 {"", 0, []string{""}, io.EOF},
430 {"a\x00", 0, []string{"a\x00"}, nil},
431 {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
432 {"hello\x01world", 1, []string{"hello\x01"}, nil},
433 {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF},
434 {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
435 {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF},
438 func TestReadBytes(t *testing.T) {
439 for _, test := range readBytesTests {
440 buf := NewBufferString(test.buffer)
441 var err error
442 for _, expected := range test.expected {
443 var bytes []byte
444 bytes, err = buf.ReadBytes(test.delim)
445 if string(bytes) != expected {
446 t.Errorf("expected %q, got %q", expected, bytes)
448 if err != nil {
449 break
452 if err != test.err {
453 t.Errorf("expected error %v, got %v", test.err, err)
458 func TestReadString(t *testing.T) {
459 for _, test := range readBytesTests {
460 buf := NewBufferString(test.buffer)
461 var err error
462 for _, expected := range test.expected {
463 var s string
464 s, err = buf.ReadString(test.delim)
465 if s != expected {
466 t.Errorf("expected %q, got %q", expected, s)
468 if err != nil {
469 break
472 if err != test.err {
473 t.Errorf("expected error %v, got %v", test.err, err)
478 func BenchmarkReadString(b *testing.B) {
479 const n = 32 << 10
481 data := make([]byte, n)
482 data[n-1] = 'x'
483 b.SetBytes(int64(n))
484 for i := 0; i < b.N; i++ {
485 buf := NewBuffer(data)
486 _, err := buf.ReadString('x')
487 if err != nil {
488 b.Fatal(err)
493 func TestGrow(t *testing.T) {
494 x := []byte{'x'}
495 y := []byte{'y'}
496 tmp := make([]byte, 72)
497 for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
498 for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
499 xBytes := Repeat(x, startLen)
501 buf := NewBuffer(xBytes)
502 // If we read, this affects buf.off, which is good to test.
503 readBytes, _ := buf.Read(tmp)
504 yBytes := Repeat(y, growLen)
505 allocs := testing.AllocsPerRun(100, func() {
506 buf.Grow(growLen)
507 buf.Write(yBytes)
509 // Check no allocation occurs in write, as long as we're single-threaded.
510 if allocs != 0 {
511 t.Errorf("allocation occurred during write")
513 // Check that buffer has correct data.
514 if !Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) {
515 t.Errorf("bad initial data at %d %d", startLen, growLen)
517 if !Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
518 t.Errorf("bad written data at %d %d", startLen, growLen)
524 func TestGrowOverflow(t *testing.T) {
525 defer func() {
526 if err := recover(); err != ErrTooLarge {
527 t.Errorf("after too-large Grow, recover() = %v; want %v", err, ErrTooLarge)
531 buf := NewBuffer(make([]byte, 1))
532 const maxInt = int(^uint(0) >> 1)
533 buf.Grow(maxInt)
536 // Was a bug: used to give EOF reading empty slice at EOF.
537 func TestReadEmptyAtEOF(t *testing.T) {
538 b := new(Buffer)
539 slice := make([]byte, 0)
540 n, err := b.Read(slice)
541 if err != nil {
542 t.Errorf("read error: %v", err)
544 if n != 0 {
545 t.Errorf("wrong count; got %d want 0", n)
549 func TestUnreadByte(t *testing.T) {
550 b := new(Buffer)
552 // check at EOF
553 if err := b.UnreadByte(); err == nil {
554 t.Fatal("UnreadByte at EOF: got no error")
556 if _, err := b.ReadByte(); err == nil {
557 t.Fatal("ReadByte at EOF: got no error")
559 if err := b.UnreadByte(); err == nil {
560 t.Fatal("UnreadByte after ReadByte at EOF: got no error")
563 // check not at EOF
564 b.WriteString("abcdefghijklmnopqrstuvwxyz")
566 // after unsuccessful read
567 if n, err := b.Read(nil); n != 0 || err != nil {
568 t.Fatalf("Read(nil) = %d,%v; want 0,nil", n, err)
570 if err := b.UnreadByte(); err == nil {
571 t.Fatal("UnreadByte after Read(nil): got no error")
574 // after successful read
575 if _, err := b.ReadBytes('m'); err != nil {
576 t.Fatalf("ReadBytes: %v", err)
578 if err := b.UnreadByte(); err != nil {
579 t.Fatalf("UnreadByte: %v", err)
581 c, err := b.ReadByte()
582 if err != nil {
583 t.Fatalf("ReadByte: %v", err)
585 if c != 'm' {
586 t.Errorf("ReadByte = %q; want %q", c, 'm')
590 // Tests that we occasionally compact. Issue 5154.
591 func TestBufferGrowth(t *testing.T) {
592 var b Buffer
593 buf := make([]byte, 1024)
594 b.Write(buf[0:1])
595 var cap0 int
596 for i := 0; i < 5<<10; i++ {
597 b.Write(buf)
598 b.Read(buf)
599 if i == 0 {
600 cap0 = b.Cap()
603 cap1 := b.Cap()
604 // (*Buffer).grow allows for 2x capacity slop before sliding,
605 // so set our error threshold at 3x.
606 if cap1 > cap0*3 {
607 t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0)
611 func BenchmarkWriteByte(b *testing.B) {
612 const n = 4 << 10
613 b.SetBytes(n)
614 buf := NewBuffer(make([]byte, n))
615 for i := 0; i < b.N; i++ {
616 buf.Reset()
617 for i := 0; i < n; i++ {
618 buf.WriteByte('x')
623 func BenchmarkWriteRune(b *testing.B) {
624 const n = 4 << 10
625 const r = '☺'
626 b.SetBytes(int64(n * utf8.RuneLen(r)))
627 buf := NewBuffer(make([]byte, n*utf8.UTFMax))
628 for i := 0; i < b.N; i++ {
629 buf.Reset()
630 for i := 0; i < n; i++ {
631 buf.WriteRune(r)
636 // From Issue 5154.
637 func BenchmarkBufferNotEmptyWriteRead(b *testing.B) {
638 buf := make([]byte, 1024)
639 for i := 0; i < b.N; i++ {
640 var b Buffer
641 b.Write(buf[0:1])
642 for i := 0; i < 5<<10; i++ {
643 b.Write(buf)
644 b.Read(buf)
649 // Check that we don't compact too often. From Issue 5154.
650 func BenchmarkBufferFullSmallReads(b *testing.B) {
651 buf := make([]byte, 1024)
652 for i := 0; i < b.N; i++ {
653 var b Buffer
654 b.Write(buf)
655 for b.Len()+20 < b.Cap() {
656 b.Write(buf[:10])
658 for i := 0; i < 5<<10; i++ {
659 b.Read(buf[:1])
660 b.Write(buf[:1])