2014-03-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / bufio / bufio_test.go
blob41bd3d45633854c02d7041c0cec83b9a65f70cc4
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 bufio_test
7 import (
8 . "bufio"
9 "bytes"
10 "errors"
11 "fmt"
12 "io"
13 "io/ioutil"
14 "strings"
15 "testing"
16 "testing/iotest"
17 "unicode/utf8"
20 // Reads from a reader and rot13s the result.
21 type rot13Reader struct {
22 r io.Reader
25 func newRot13Reader(r io.Reader) *rot13Reader {
26 r13 := new(rot13Reader)
27 r13.r = r
28 return r13
31 func (r13 *rot13Reader) Read(p []byte) (int, error) {
32 n, err := r13.r.Read(p)
33 if err != nil {
34 return n, err
36 for i := 0; i < n; i++ {
37 c := p[i] | 0x20 // lowercase byte
38 if 'a' <= c && c <= 'm' {
39 p[i] += 13
40 } else if 'n' <= c && c <= 'z' {
41 p[i] -= 13
44 return n, nil
47 // Call ReadByte to accumulate the text of a file
48 func readBytes(buf *Reader) string {
49 var b [1000]byte
50 nb := 0
51 for {
52 c, err := buf.ReadByte()
53 if err == io.EOF {
54 break
56 if err == nil {
57 b[nb] = c
58 nb++
59 } else if err != iotest.ErrTimeout {
60 panic("Data: " + err.Error())
63 return string(b[0:nb])
66 func TestReaderSimple(t *testing.T) {
67 data := "hello world"
68 b := NewReader(bytes.NewBufferString(data))
69 if s := readBytes(b); s != "hello world" {
70 t.Errorf("simple hello world test failed: got %q", s)
73 b = NewReader(newRot13Reader(bytes.NewBufferString(data)))
74 if s := readBytes(b); s != "uryyb jbeyq" {
75 t.Errorf("rot13 hello world test failed: got %q", s)
79 type readMaker struct {
80 name string
81 fn func(io.Reader) io.Reader
84 var readMakers = []readMaker{
85 {"full", func(r io.Reader) io.Reader { return r }},
86 {"byte", iotest.OneByteReader},
87 {"half", iotest.HalfReader},
88 {"data+err", iotest.DataErrReader},
89 {"timeout", iotest.TimeoutReader},
92 // Call ReadString (which ends up calling everything else)
93 // to accumulate the text of a file.
94 func readLines(b *Reader) string {
95 s := ""
96 for {
97 s1, err := b.ReadString('\n')
98 if err == io.EOF {
99 break
101 if err != nil && err != iotest.ErrTimeout {
102 panic("GetLines: " + err.Error())
104 s += s1
106 return s
109 // Call Read to accumulate the text of a file
110 func reads(buf *Reader, m int) string {
111 var b [1000]byte
112 nb := 0
113 for {
114 n, err := buf.Read(b[nb : nb+m])
115 nb += n
116 if err == io.EOF {
117 break
120 return string(b[0:nb])
123 type bufReader struct {
124 name string
125 fn func(*Reader) string
128 var bufreaders = []bufReader{
129 {"1", func(b *Reader) string { return reads(b, 1) }},
130 {"2", func(b *Reader) string { return reads(b, 2) }},
131 {"3", func(b *Reader) string { return reads(b, 3) }},
132 {"4", func(b *Reader) string { return reads(b, 4) }},
133 {"5", func(b *Reader) string { return reads(b, 5) }},
134 {"7", func(b *Reader) string { return reads(b, 7) }},
135 {"bytes", readBytes},
136 {"lines", readLines},
139 const minReadBufferSize = 16
141 var bufsizes = []int{
142 minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096,
145 func TestReader(t *testing.T) {
146 var texts [31]string
147 str := ""
148 all := ""
149 for i := 0; i < len(texts)-1; i++ {
150 texts[i] = str + "\n"
151 all += texts[i]
152 str += string(i%26 + 'a')
154 texts[len(texts)-1] = all
156 for h := 0; h < len(texts); h++ {
157 text := texts[h]
158 for i := 0; i < len(readMakers); i++ {
159 for j := 0; j < len(bufreaders); j++ {
160 for k := 0; k < len(bufsizes); k++ {
161 readmaker := readMakers[i]
162 bufreader := bufreaders[j]
163 bufsize := bufsizes[k]
164 read := readmaker.fn(bytes.NewBufferString(text))
165 buf := NewReaderSize(read, bufsize)
166 s := bufreader.fn(buf)
167 if s != text {
168 t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q",
169 readmaker.name, bufreader.name, bufsize, text, s)
177 // A StringReader delivers its data one string segment at a time via Read.
178 type StringReader struct {
179 data []string
180 step int
183 func (r *StringReader) Read(p []byte) (n int, err error) {
184 if r.step < len(r.data) {
185 s := r.data[r.step]
186 n = copy(p, s)
187 r.step++
188 } else {
189 err = io.EOF
191 return
194 func readRuneSegments(t *testing.T, segments []string) {
195 got := ""
196 want := strings.Join(segments, "")
197 r := NewReader(&StringReader{data: segments})
198 for {
199 r, _, err := r.ReadRune()
200 if err != nil {
201 if err != io.EOF {
202 return
204 break
206 got += string(r)
208 if got != want {
209 t.Errorf("segments=%v got=%s want=%s", segments, got, want)
213 var segmentList = [][]string{
215 {""},
216 {"日", "本語"},
217 {"\u65e5", "\u672c", "\u8a9e"},
218 {"\U000065e5", "\U0000672c", "\U00008a9e"},
219 {"\xe6", "\x97\xa5\xe6", "\x9c\xac\xe8\xaa\x9e"},
220 {"Hello", ", ", "World", "!"},
221 {"Hello", ", ", "", "World", "!"},
224 func TestReadRune(t *testing.T) {
225 for _, s := range segmentList {
226 readRuneSegments(t, s)
230 func TestUnreadRune(t *testing.T) {
231 got := ""
232 segments := []string{"Hello, world:", "日本語"}
233 data := strings.Join(segments, "")
234 r := NewReader(&StringReader{data: segments})
235 // Normal execution.
236 for {
237 r1, _, err := r.ReadRune()
238 if err != nil {
239 if err != io.EOF {
240 t.Error("unexpected EOF")
242 break
244 got += string(r1)
245 // Put it back and read it again
246 if err = r.UnreadRune(); err != nil {
247 t.Error("unexpected error on UnreadRune:", err)
249 r2, _, err := r.ReadRune()
250 if err != nil {
251 t.Error("unexpected error reading after unreading:", err)
253 if r1 != r2 {
254 t.Errorf("incorrect rune after unread: got %c wanted %c", r1, r2)
257 if got != data {
258 t.Errorf("want=%q got=%q", data, got)
262 // Test that UnreadRune fails if the preceding operation was not a ReadRune.
263 func TestUnreadRuneError(t *testing.T) {
264 buf := make([]byte, 3) // All runes in this test are 3 bytes long
265 r := NewReader(&StringReader{data: []string{"日本語日本語日本語"}})
266 if r.UnreadRune() == nil {
267 t.Error("expected error on UnreadRune from fresh buffer")
269 _, _, err := r.ReadRune()
270 if err != nil {
271 t.Error("unexpected error on ReadRune (1):", err)
273 if err = r.UnreadRune(); err != nil {
274 t.Error("unexpected error on UnreadRune (1):", err)
276 if r.UnreadRune() == nil {
277 t.Error("expected error after UnreadRune (1)")
279 // Test error after Read.
280 _, _, err = r.ReadRune() // reset state
281 if err != nil {
282 t.Error("unexpected error on ReadRune (2):", err)
284 _, err = r.Read(buf)
285 if err != nil {
286 t.Error("unexpected error on Read (2):", err)
288 if r.UnreadRune() == nil {
289 t.Error("expected error after Read (2)")
291 // Test error after ReadByte.
292 _, _, err = r.ReadRune() // reset state
293 if err != nil {
294 t.Error("unexpected error on ReadRune (2):", err)
296 for _ = range buf {
297 _, err = r.ReadByte()
298 if err != nil {
299 t.Error("unexpected error on ReadByte (2):", err)
302 if r.UnreadRune() == nil {
303 t.Error("expected error after ReadByte")
305 // Test error after UnreadByte.
306 _, _, err = r.ReadRune() // reset state
307 if err != nil {
308 t.Error("unexpected error on ReadRune (3):", err)
310 _, err = r.ReadByte()
311 if err != nil {
312 t.Error("unexpected error on ReadByte (3):", err)
314 err = r.UnreadByte()
315 if err != nil {
316 t.Error("unexpected error on UnreadByte (3):", err)
318 if r.UnreadRune() == nil {
319 t.Error("expected error after UnreadByte (3)")
323 func TestUnreadRuneAtEOF(t *testing.T) {
324 // UnreadRune/ReadRune should error at EOF (was a bug; used to panic)
325 r := NewReader(strings.NewReader("x"))
326 r.ReadRune()
327 r.ReadRune()
328 r.UnreadRune()
329 _, _, err := r.ReadRune()
330 if err == nil {
331 t.Error("expected error at EOF")
332 } else if err != io.EOF {
333 t.Error("expected EOF; got", err)
337 func TestReadWriteRune(t *testing.T) {
338 const NRune = 1000
339 byteBuf := new(bytes.Buffer)
340 w := NewWriter(byteBuf)
341 // Write the runes out using WriteRune
342 buf := make([]byte, utf8.UTFMax)
343 for r := rune(0); r < NRune; r++ {
344 size := utf8.EncodeRune(buf, r)
345 nbytes, err := w.WriteRune(r)
346 if err != nil {
347 t.Fatalf("WriteRune(0x%x) error: %s", r, err)
349 if nbytes != size {
350 t.Fatalf("WriteRune(0x%x) expected %d, got %d", r, size, nbytes)
353 w.Flush()
355 r := NewReader(byteBuf)
356 // Read them back with ReadRune
357 for r1 := rune(0); r1 < NRune; r1++ {
358 size := utf8.EncodeRune(buf, r1)
359 nr, nbytes, err := r.ReadRune()
360 if nr != r1 || nbytes != size || err != nil {
361 t.Fatalf("ReadRune(0x%x) got 0x%x,%d not 0x%x,%d (err=%s)", r1, nr, nbytes, r1, size, err)
366 func TestWriter(t *testing.T) {
367 var data [8192]byte
369 for i := 0; i < len(data); i++ {
370 data[i] = byte(' ' + i%('~'-' '))
372 w := new(bytes.Buffer)
373 for i := 0; i < len(bufsizes); i++ {
374 for j := 0; j < len(bufsizes); j++ {
375 nwrite := bufsizes[i]
376 bs := bufsizes[j]
378 // Write nwrite bytes using buffer size bs.
379 // Check that the right amount makes it out
380 // and that the data is correct.
382 w.Reset()
383 buf := NewWriterSize(w, bs)
384 context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs)
385 n, e1 := buf.Write(data[0:nwrite])
386 if e1 != nil || n != nwrite {
387 t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1)
388 continue
390 if e := buf.Flush(); e != nil {
391 t.Errorf("%s: buf.Flush = %v", context, e)
394 written := w.Bytes()
395 if len(written) != nwrite {
396 t.Errorf("%s: %d bytes written", context, len(written))
398 for l := 0; l < len(written); l++ {
399 if written[i] != data[i] {
400 t.Errorf("wrong bytes written")
401 t.Errorf("want=%q", data[0:len(written)])
402 t.Errorf("have=%q", written)
409 // Check that write errors are returned properly.
411 type errorWriterTest struct {
412 n, m int
413 err error
414 expect error
417 func (w errorWriterTest) Write(p []byte) (int, error) {
418 return len(p) * w.n / w.m, w.err
421 var errorWriterTests = []errorWriterTest{
422 {0, 1, nil, io.ErrShortWrite},
423 {1, 2, nil, io.ErrShortWrite},
424 {1, 1, nil, nil},
425 {0, 1, io.ErrClosedPipe, io.ErrClosedPipe},
426 {1, 2, io.ErrClosedPipe, io.ErrClosedPipe},
427 {1, 1, io.ErrClosedPipe, io.ErrClosedPipe},
430 func TestWriteErrors(t *testing.T) {
431 for _, w := range errorWriterTests {
432 buf := NewWriter(w)
433 _, e := buf.Write([]byte("hello world"))
434 if e != nil {
435 t.Errorf("Write hello to %v: %v", w, e)
436 continue
438 // Two flushes, to verify the error is sticky.
439 for i := 0; i < 2; i++ {
440 e = buf.Flush()
441 if e != w.expect {
442 t.Errorf("Flush %d/2 %v: got %v, wanted %v", i+1, w, e, w.expect)
448 func TestNewReaderSizeIdempotent(t *testing.T) {
449 const BufSize = 1000
450 b := NewReaderSize(bytes.NewBufferString("hello world"), BufSize)
451 // Does it recognize itself?
452 b1 := NewReaderSize(b, BufSize)
453 if b1 != b {
454 t.Error("NewReaderSize did not detect underlying Reader")
456 // Does it wrap if existing buffer is too small?
457 b2 := NewReaderSize(b, 2*BufSize)
458 if b2 == b {
459 t.Error("NewReaderSize did not enlarge buffer")
463 func TestNewWriterSizeIdempotent(t *testing.T) {
464 const BufSize = 1000
465 b := NewWriterSize(new(bytes.Buffer), BufSize)
466 // Does it recognize itself?
467 b1 := NewWriterSize(b, BufSize)
468 if b1 != b {
469 t.Error("NewWriterSize did not detect underlying Writer")
471 // Does it wrap if existing buffer is too small?
472 b2 := NewWriterSize(b, 2*BufSize)
473 if b2 == b {
474 t.Error("NewWriterSize did not enlarge buffer")
478 func TestWriteString(t *testing.T) {
479 const BufSize = 8
480 buf := new(bytes.Buffer)
481 b := NewWriterSize(buf, BufSize)
482 b.WriteString("0") // easy
483 b.WriteString("123456") // still easy
484 b.WriteString("7890") // easy after flush
485 b.WriteString("abcdefghijklmnopqrstuvwxy") // hard
486 b.WriteString("z")
487 if err := b.Flush(); err != nil {
488 t.Error("WriteString", err)
490 s := "01234567890abcdefghijklmnopqrstuvwxyz"
491 if string(buf.Bytes()) != s {
492 t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes()))
496 func TestBufferFull(t *testing.T) {
497 const longString = "And now, hello, world! It is the time for all good men to come to the aid of their party"
498 buf := NewReaderSize(strings.NewReader(longString), minReadBufferSize)
499 line, err := buf.ReadSlice('!')
500 if string(line) != "And now, hello, " || err != ErrBufferFull {
501 t.Errorf("first ReadSlice(,) = %q, %v", line, err)
503 line, err = buf.ReadSlice('!')
504 if string(line) != "world!" || err != nil {
505 t.Errorf("second ReadSlice(,) = %q, %v", line, err)
509 func TestPeek(t *testing.T) {
510 p := make([]byte, 10)
511 // string is 16 (minReadBufferSize) long.
512 buf := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize)
513 if s, err := buf.Peek(1); string(s) != "a" || err != nil {
514 t.Fatalf("want %q got %q, err=%v", "a", string(s), err)
516 if s, err := buf.Peek(4); string(s) != "abcd" || err != nil {
517 t.Fatalf("want %q got %q, err=%v", "abcd", string(s), err)
519 if _, err := buf.Peek(32); err != ErrBufferFull {
520 t.Fatalf("want ErrBufFull got %v", err)
522 if _, err := buf.Read(p[0:3]); string(p[0:3]) != "abc" || err != nil {
523 t.Fatalf("want %q got %q, err=%v", "abc", string(p[0:3]), err)
525 if s, err := buf.Peek(1); string(s) != "d" || err != nil {
526 t.Fatalf("want %q got %q, err=%v", "d", string(s), err)
528 if s, err := buf.Peek(2); string(s) != "de" || err != nil {
529 t.Fatalf("want %q got %q, err=%v", "de", string(s), err)
531 if _, err := buf.Read(p[0:3]); string(p[0:3]) != "def" || err != nil {
532 t.Fatalf("want %q got %q, err=%v", "def", string(p[0:3]), err)
534 if s, err := buf.Peek(4); string(s) != "ghij" || err != nil {
535 t.Fatalf("want %q got %q, err=%v", "ghij", string(s), err)
537 if _, err := buf.Read(p[0:]); string(p[0:]) != "ghijklmnop" || err != nil {
538 t.Fatalf("want %q got %q, err=%v", "ghijklmnop", string(p[0:minReadBufferSize]), err)
540 if s, err := buf.Peek(0); string(s) != "" || err != nil {
541 t.Fatalf("want %q got %q, err=%v", "", string(s), err)
543 if _, err := buf.Peek(1); err != io.EOF {
544 t.Fatalf("want EOF got %v", err)
547 // Test for issue 3022, not exposing a reader's error on a successful Peek.
548 buf = NewReaderSize(dataAndEOFReader("abcd"), 32)
549 if s, err := buf.Peek(2); string(s) != "ab" || err != nil {
550 t.Errorf(`Peek(2) on "abcd", EOF = %q, %v; want "ab", nil`, string(s), err)
552 if s, err := buf.Peek(4); string(s) != "abcd" || err != nil {
553 t.Errorf(`Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`, string(s), err)
555 if n, err := buf.Read(p[0:5]); string(p[0:n]) != "abcd" || err != nil {
556 t.Fatalf("Read after peek = %q, %v; want abcd, EOF", p[0:n], err)
558 if n, err := buf.Read(p[0:1]); string(p[0:n]) != "" || err != io.EOF {
559 t.Fatalf(`second Read after peek = %q, %v; want "", EOF`, p[0:n], err)
563 type dataAndEOFReader string
565 func (r dataAndEOFReader) Read(p []byte) (int, error) {
566 return copy(p, r), io.EOF
569 func TestPeekThenUnreadRune(t *testing.T) {
570 // This sequence used to cause a crash.
571 r := NewReader(strings.NewReader("x"))
572 r.ReadRune()
573 r.Peek(1)
574 r.UnreadRune()
575 r.ReadRune() // Used to panic here
578 var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy")
579 var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy")
580 var testInputrn = []byte("012\r\n345\r\n678\r\n9ab\r\ncde\r\nfgh\r\nijk\r\nlmn\r\nopq\r\nrst\r\nuvw\r\nxy\r\n\n\r\n")
582 // TestReader wraps a []byte and returns reads of a specific length.
583 type testReader struct {
584 data []byte
585 stride int
588 func (t *testReader) Read(buf []byte) (n int, err error) {
589 n = t.stride
590 if n > len(t.data) {
591 n = len(t.data)
593 if n > len(buf) {
594 n = len(buf)
596 copy(buf, t.data)
597 t.data = t.data[n:]
598 if len(t.data) == 0 {
599 err = io.EOF
601 return
604 func testReadLine(t *testing.T, input []byte) {
605 //for stride := 1; stride < len(input); stride++ {
606 for stride := 1; stride < 2; stride++ {
607 done := 0
608 reader := testReader{input, stride}
609 l := NewReaderSize(&reader, len(input)+1)
610 for {
611 line, isPrefix, err := l.ReadLine()
612 if len(line) > 0 && err != nil {
613 t.Errorf("ReadLine returned both data and error: %s", err)
615 if isPrefix {
616 t.Errorf("ReadLine returned prefix")
618 if err != nil {
619 if err != io.EOF {
620 t.Fatalf("Got unknown error: %s", err)
622 break
624 if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) {
625 t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line)
627 done += len(line)
629 if done != len(testOutput) {
630 t.Errorf("ReadLine didn't return everything: got: %d, want: %d (stride: %d)", done, len(testOutput), stride)
635 func TestReadLine(t *testing.T) {
636 testReadLine(t, testInput)
637 testReadLine(t, testInputrn)
640 func TestLineTooLong(t *testing.T) {
641 data := make([]byte, 0)
642 for i := 0; i < minReadBufferSize*5/2; i++ {
643 data = append(data, '0'+byte(i%10))
645 buf := bytes.NewBuffer(data)
646 l := NewReaderSize(buf, minReadBufferSize)
647 line, isPrefix, err := l.ReadLine()
648 if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil {
649 t.Errorf("bad result for first line: got %q want %q %v", line, data[:minReadBufferSize], err)
651 data = data[len(line):]
652 line, isPrefix, err = l.ReadLine()
653 if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil {
654 t.Errorf("bad result for second line: got %q want %q %v", line, data[:minReadBufferSize], err)
656 data = data[len(line):]
657 line, isPrefix, err = l.ReadLine()
658 if isPrefix || !bytes.Equal(line, data[:minReadBufferSize/2]) || err != nil {
659 t.Errorf("bad result for third line: got %q want %q %v", line, data[:minReadBufferSize/2], err)
661 line, isPrefix, err = l.ReadLine()
662 if isPrefix || err == nil {
663 t.Errorf("expected no more lines: %x %s", line, err)
667 func TestReadAfterLines(t *testing.T) {
668 line1 := "this is line1"
669 restData := "this is line2\nthis is line 3\n"
670 inbuf := bytes.NewBuffer([]byte(line1 + "\n" + restData))
671 outbuf := new(bytes.Buffer)
672 maxLineLength := len(line1) + len(restData)/2
673 l := NewReaderSize(inbuf, maxLineLength)
674 line, isPrefix, err := l.ReadLine()
675 if isPrefix || err != nil || string(line) != line1 {
676 t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line))
678 n, err := io.Copy(outbuf, l)
679 if int(n) != len(restData) || err != nil {
680 t.Errorf("bad result for Read: n=%d err=%v", n, err)
682 if outbuf.String() != restData {
683 t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData)
687 func TestReadEmptyBuffer(t *testing.T) {
688 l := NewReaderSize(new(bytes.Buffer), minReadBufferSize)
689 line, isPrefix, err := l.ReadLine()
690 if err != io.EOF {
691 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
695 func TestLinesAfterRead(t *testing.T) {
696 l := NewReaderSize(bytes.NewBuffer([]byte("foo")), minReadBufferSize)
697 _, err := ioutil.ReadAll(l)
698 if err != nil {
699 t.Error(err)
700 return
703 line, isPrefix, err := l.ReadLine()
704 if err != io.EOF {
705 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
709 func TestReadLineNonNilLineOrError(t *testing.T) {
710 r := NewReader(strings.NewReader("line 1\n"))
711 for i := 0; i < 2; i++ {
712 l, _, err := r.ReadLine()
713 if l != nil && err != nil {
714 t.Fatalf("on line %d/2; ReadLine=%#v, %v; want non-nil line or Error, but not both",
715 i+1, l, err)
720 type readLineResult struct {
721 line []byte
722 isPrefix bool
723 err error
726 var readLineNewlinesTests = []struct {
727 input string
728 expect []readLineResult
730 {"012345678901234\r\n012345678901234\r\n", []readLineResult{
731 {[]byte("012345678901234"), true, nil},
732 {nil, false, nil},
733 {[]byte("012345678901234"), true, nil},
734 {nil, false, nil},
735 {nil, false, io.EOF},
737 {"0123456789012345\r012345678901234\r", []readLineResult{
738 {[]byte("0123456789012345"), true, nil},
739 {[]byte("\r012345678901234"), true, nil},
740 {[]byte("\r"), false, nil},
741 {nil, false, io.EOF},
745 func TestReadLineNewlines(t *testing.T) {
746 for _, e := range readLineNewlinesTests {
747 testReadLineNewlines(t, e.input, e.expect)
751 func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) {
752 b := NewReaderSize(strings.NewReader(input), minReadBufferSize)
753 for i, e := range expect {
754 line, isPrefix, err := b.ReadLine()
755 if !bytes.Equal(line, e.line) {
756 t.Errorf("%q call %d, line == %q, want %q", input, i, line, e.line)
757 return
759 if isPrefix != e.isPrefix {
760 t.Errorf("%q call %d, isPrefix == %v, want %v", input, i, isPrefix, e.isPrefix)
761 return
763 if err != e.err {
764 t.Errorf("%q call %d, err == %v, want %v", input, i, err, e.err)
765 return
770 func createTestInput(n int) []byte {
771 input := make([]byte, n)
772 for i := range input {
773 // 101 and 251 are arbitrary prime numbers.
774 // The idea is to create an input sequence
775 // which doesn't repeat too frequently.
776 input[i] = byte(i % 251)
777 if i%101 == 0 {
778 input[i] ^= byte(i / 101)
781 return input
784 func TestReaderWriteTo(t *testing.T) {
785 input := createTestInput(8192)
786 r := NewReader(onlyReader{bytes.NewBuffer(input)})
787 w := new(bytes.Buffer)
788 if n, err := r.WriteTo(w); err != nil || n != int64(len(input)) {
789 t.Fatalf("r.WriteTo(w) = %d, %v, want %d, nil", n, err, len(input))
792 for i, val := range w.Bytes() {
793 if val != input[i] {
794 t.Errorf("after write: out[%d] = %#x, want %#x", i, val, input[i])
799 type errorWriterToTest struct {
800 rn, wn int
801 rerr, werr error
802 expected error
805 func (r errorWriterToTest) Read(p []byte) (int, error) {
806 return len(p) * r.rn, r.rerr
809 func (w errorWriterToTest) Write(p []byte) (int, error) {
810 return len(p) * w.wn, w.werr
813 var errorWriterToTests = []errorWriterToTest{
814 {1, 0, nil, io.ErrClosedPipe, io.ErrClosedPipe},
815 {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe},
816 {0, 0, io.ErrUnexpectedEOF, io.ErrClosedPipe, io.ErrClosedPipe},
817 {0, 1, io.EOF, nil, nil},
820 func TestReaderWriteToErrors(t *testing.T) {
821 for i, rw := range errorWriterToTests {
822 r := NewReader(rw)
823 if _, err := r.WriteTo(rw); err != rw.expected {
824 t.Errorf("r.WriteTo(errorWriterToTests[%d]) = _, %v, want _,%v", i, err, rw.expected)
829 func TestWriterReadFrom(t *testing.T) {
830 ws := []func(io.Writer) io.Writer{
831 func(w io.Writer) io.Writer { return onlyWriter{w} },
832 func(w io.Writer) io.Writer { return w },
835 rs := []func(io.Reader) io.Reader{
836 iotest.DataErrReader,
837 func(r io.Reader) io.Reader { return r },
840 for ri, rfunc := range rs {
841 for wi, wfunc := range ws {
842 input := createTestInput(8192)
843 b := new(bytes.Buffer)
844 w := NewWriter(wfunc(b))
845 r := rfunc(bytes.NewBuffer(input))
846 if n, err := w.ReadFrom(r); err != nil || n != int64(len(input)) {
847 t.Errorf("ws[%d],rs[%d]: w.ReadFrom(r) = %d, %v, want %d, nil", wi, ri, n, err, len(input))
848 continue
850 if err := w.Flush(); err != nil {
851 t.Errorf("Flush returned %v", err)
852 continue
854 if got, want := b.String(), string(input); got != want {
855 t.Errorf("ws[%d], rs[%d]:\ngot %q\nwant %q\n", wi, ri, got, want)
861 type errorReaderFromTest struct {
862 rn, wn int
863 rerr, werr error
864 expected error
867 func (r errorReaderFromTest) Read(p []byte) (int, error) {
868 return len(p) * r.rn, r.rerr
871 func (w errorReaderFromTest) Write(p []byte) (int, error) {
872 return len(p) * w.wn, w.werr
875 var errorReaderFromTests = []errorReaderFromTest{
876 {0, 1, io.EOF, nil, nil},
877 {1, 1, io.EOF, nil, nil},
878 {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe},
879 {0, 0, io.ErrClosedPipe, io.ErrShortWrite, io.ErrClosedPipe},
880 {1, 0, nil, io.ErrShortWrite, io.ErrShortWrite},
883 func TestWriterReadFromErrors(t *testing.T) {
884 for i, rw := range errorReaderFromTests {
885 w := NewWriter(rw)
886 if _, err := w.ReadFrom(rw); err != rw.expected {
887 t.Errorf("w.ReadFrom(errorReaderFromTests[%d]) = _, %v, want _,%v", i, err, rw.expected)
892 // TestWriterReadFromCounts tests that using io.Copy to copy into a
893 // bufio.Writer does not prematurely flush the buffer. For example, when
894 // buffering writes to a network socket, excessive network writes should be
895 // avoided.
896 func TestWriterReadFromCounts(t *testing.T) {
897 var w0 writeCountingDiscard
898 b0 := NewWriterSize(&w0, 1234)
899 b0.WriteString(strings.Repeat("x", 1000))
900 if w0 != 0 {
901 t.Fatalf("write 1000 'x's: got %d writes, want 0", w0)
903 b0.WriteString(strings.Repeat("x", 200))
904 if w0 != 0 {
905 t.Fatalf("write 1200 'x's: got %d writes, want 0", w0)
907 io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 30))})
908 if w0 != 0 {
909 t.Fatalf("write 1230 'x's: got %d writes, want 0", w0)
911 io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 9))})
912 if w0 != 1 {
913 t.Fatalf("write 1239 'x's: got %d writes, want 1", w0)
916 var w1 writeCountingDiscard
917 b1 := NewWriterSize(&w1, 1234)
918 b1.WriteString(strings.Repeat("x", 1200))
919 b1.Flush()
920 if w1 != 1 {
921 t.Fatalf("flush 1200 'x's: got %d writes, want 1", w1)
923 b1.WriteString(strings.Repeat("x", 89))
924 if w1 != 1 {
925 t.Fatalf("write 1200 + 89 'x's: got %d writes, want 1", w1)
927 io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 700))})
928 if w1 != 1 {
929 t.Fatalf("write 1200 + 789 'x's: got %d writes, want 1", w1)
931 io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))})
932 if w1 != 2 {
933 t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1)
935 b1.Flush()
936 if w1 != 3 {
937 t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1)
941 // A writeCountingDiscard is like ioutil.Discard and counts the number of times
942 // Write is called on it.
943 type writeCountingDiscard int
945 func (w *writeCountingDiscard) Write(p []byte) (int, error) {
946 *w++
947 return len(p), nil
950 type negativeReader int
952 func (r *negativeReader) Read([]byte) (int, error) { return -1, nil }
954 func TestNegativeRead(t *testing.T) {
955 // should panic with a description pointing at the reader, not at itself.
956 // (should NOT panic with slice index error, for example.)
957 b := NewReader(new(negativeReader))
958 defer func() {
959 switch err := recover().(type) {
960 case nil:
961 t.Fatal("read did not panic")
962 case error:
963 if !strings.Contains(err.Error(), "reader returned negative count from Read") {
964 t.Fatalf("wrong panic: %v", err)
966 default:
967 t.Fatalf("unexpected panic value: %T(%v)", err, err)
970 b.Read(make([]byte, 100))
973 var errFake = errors.New("fake error")
975 type errorThenGoodReader struct {
976 didErr bool
977 nread int
980 func (r *errorThenGoodReader) Read(p []byte) (int, error) {
981 r.nread++
982 if !r.didErr {
983 r.didErr = true
984 return 0, errFake
986 return len(p), nil
989 func TestReaderClearError(t *testing.T) {
990 r := &errorThenGoodReader{}
991 b := NewReader(r)
992 buf := make([]byte, 1)
993 if _, err := b.Read(nil); err != nil {
994 t.Fatalf("1st nil Read = %v; want nil", err)
996 if _, err := b.Read(buf); err != errFake {
997 t.Fatalf("1st Read = %v; want errFake", err)
999 if _, err := b.Read(nil); err != nil {
1000 t.Fatalf("2nd nil Read = %v; want nil", err)
1002 if _, err := b.Read(buf); err != nil {
1003 t.Fatalf("3rd Read with buffer = %v; want nil", err)
1005 if r.nread != 2 {
1006 t.Errorf("num reads = %d; want 2", r.nread)
1010 // Test for golang.org/issue/5947
1011 func TestWriterReadFromWhileFull(t *testing.T) {
1012 buf := new(bytes.Buffer)
1013 w := NewWriterSize(buf, 10)
1015 // Fill buffer exactly.
1016 n, err := w.Write([]byte("0123456789"))
1017 if n != 10 || err != nil {
1018 t.Fatalf("Write returned (%v, %v), want (10, nil)", n, err)
1021 // Use ReadFrom to read in some data.
1022 n2, err := w.ReadFrom(strings.NewReader("abcdef"))
1023 if n2 != 6 || err != nil {
1024 t.Fatalf("ReadFrom returned (%v, %v), want (6, nil)", n, err)
1028 func TestReaderReset(t *testing.T) {
1029 r := NewReader(strings.NewReader("foo foo"))
1030 buf := make([]byte, 3)
1031 r.Read(buf)
1032 if string(buf) != "foo" {
1033 t.Errorf("buf = %q; want foo", buf)
1035 r.Reset(strings.NewReader("bar bar"))
1036 all, err := ioutil.ReadAll(r)
1037 if err != nil {
1038 t.Fatal(err)
1040 if string(all) != "bar bar" {
1041 t.Errorf("ReadAll = %q; want bar bar", all)
1045 func TestWriterReset(t *testing.T) {
1046 var buf1, buf2 bytes.Buffer
1047 w := NewWriter(&buf1)
1048 w.WriteString("foo")
1049 w.Reset(&buf2) // and not flushed
1050 w.WriteString("bar")
1051 w.Flush()
1052 if buf1.String() != "" {
1053 t.Errorf("buf1 = %q; want empty", buf1.String())
1055 if buf2.String() != "bar" {
1056 t.Errorf("buf2 = %q; want bar", buf2.String())
1060 // An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have.
1061 type onlyReader struct {
1062 r io.Reader
1065 func (r onlyReader) Read(b []byte) (int, error) {
1066 return r.r.Read(b)
1069 // An onlyWriter only implements io.Writer, no matter what other methods the underlying implementation may have.
1070 type onlyWriter struct {
1071 w io.Writer
1074 func (w onlyWriter) Write(b []byte) (int, error) {
1075 return w.w.Write(b)
1078 func BenchmarkReaderCopyOptimal(b *testing.B) {
1079 // Optimal case is where the underlying reader implements io.WriterTo
1080 for i := 0; i < b.N; i++ {
1081 b.StopTimer()
1082 src := NewReader(bytes.NewBuffer(make([]byte, 8192)))
1083 dst := onlyWriter{new(bytes.Buffer)}
1084 b.StartTimer()
1085 io.Copy(dst, src)
1089 func BenchmarkReaderCopyUnoptimal(b *testing.B) {
1090 // Unoptimal case is where the underlying reader doesn't implement io.WriterTo
1091 for i := 0; i < b.N; i++ {
1092 b.StopTimer()
1093 src := NewReader(onlyReader{bytes.NewBuffer(make([]byte, 8192))})
1094 dst := onlyWriter{new(bytes.Buffer)}
1095 b.StartTimer()
1096 io.Copy(dst, src)
1100 func BenchmarkReaderCopyNoWriteTo(b *testing.B) {
1101 for i := 0; i < b.N; i++ {
1102 b.StopTimer()
1103 src := onlyReader{NewReader(bytes.NewBuffer(make([]byte, 8192)))}
1104 dst := onlyWriter{new(bytes.Buffer)}
1105 b.StartTimer()
1106 io.Copy(dst, src)
1110 func BenchmarkWriterCopyOptimal(b *testing.B) {
1111 // Optimal case is where the underlying writer implements io.ReaderFrom
1112 for i := 0; i < b.N; i++ {
1113 b.StopTimer()
1114 src := onlyReader{bytes.NewBuffer(make([]byte, 8192))}
1115 dst := NewWriter(new(bytes.Buffer))
1116 b.StartTimer()
1117 io.Copy(dst, src)
1121 func BenchmarkWriterCopyUnoptimal(b *testing.B) {
1122 for i := 0; i < b.N; i++ {
1123 b.StopTimer()
1124 src := onlyReader{bytes.NewBuffer(make([]byte, 8192))}
1125 dst := NewWriter(onlyWriter{new(bytes.Buffer)})
1126 b.StartTimer()
1127 io.Copy(dst, src)
1131 func BenchmarkWriterCopyNoReadFrom(b *testing.B) {
1132 for i := 0; i < b.N; i++ {
1133 b.StopTimer()
1134 src := onlyReader{bytes.NewBuffer(make([]byte, 8192))}
1135 dst := onlyWriter{NewWriter(new(bytes.Buffer))}
1136 b.StartTimer()
1137 io.Copy(dst, src)
1141 func BenchmarkReaderEmpty(b *testing.B) {
1142 b.ReportAllocs()
1143 str := strings.Repeat("x", 16<<10)
1144 for i := 0; i < b.N; i++ {
1145 br := NewReader(strings.NewReader(str))
1146 n, err := io.Copy(ioutil.Discard, br)
1147 if err != nil {
1148 b.Fatal(err)
1150 if n != int64(len(str)) {
1151 b.Fatal("wrong length")
1156 func BenchmarkWriterEmpty(b *testing.B) {
1157 b.ReportAllocs()
1158 str := strings.Repeat("x", 1<<10)
1159 bs := []byte(str)
1160 for i := 0; i < b.N; i++ {
1161 bw := NewWriter(ioutil.Discard)
1162 bw.Flush()
1163 bw.WriteByte('a')
1164 bw.Flush()
1165 bw.WriteRune('B')
1166 bw.Flush()
1167 bw.Write(bs)
1168 bw.Flush()
1169 bw.WriteString(str)
1170 bw.Flush()
1174 func BenchmarkWriterFlush(b *testing.B) {
1175 b.ReportAllocs()
1176 bw := NewWriter(ioutil.Discard)
1177 str := strings.Repeat("x", 50)
1178 for i := 0; i < b.N; i++ {
1179 bw.WriteString(str)
1180 bw.Flush()