github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/bufio/bufio_test.go (about) 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. 4 5 package bufio_test 6 7 import ( 8 . "bufio" 9 "bytes" 10 "errors" 11 "fmt" 12 "io" 13 "io/ioutil" 14 "strings" 15 "testing" 16 "testing/iotest" 17 "time" 18 "unicode/utf8" 19 ) 20 21 // Reads from a reader and rot13s the result. 22 type rot13Reader struct { 23 r io.Reader 24 } 25 26 func newRot13Reader(r io.Reader) *rot13Reader { 27 r13 := new(rot13Reader) 28 r13.r = r 29 return r13 30 } 31 32 func (r13 *rot13Reader) Read(p []byte) (int, error) { 33 n, err := r13.r.Read(p) 34 for i := 0; i < n; i++ { 35 c := p[i] | 0x20 // lowercase byte 36 if 'a' <= c && c <= 'm' { 37 p[i] += 13 38 } else if 'n' <= c && c <= 'z' { 39 p[i] -= 13 40 } 41 } 42 return n, err 43 } 44 45 // Call ReadByte to accumulate the text of a file 46 func readBytes(buf *Reader) string { 47 var b [1000]byte 48 nb := 0 49 for { 50 c, err := buf.ReadByte() 51 if err == io.EOF { 52 break 53 } 54 if err == nil { 55 b[nb] = c 56 nb++ 57 } else if err != iotest.ErrTimeout { 58 panic("Data: " + err.Error()) 59 } 60 } 61 return string(b[0:nb]) 62 } 63 64 func TestReaderSimple(t *testing.T) { 65 data := "hello world" 66 b := NewReader(strings.NewReader(data)) 67 if s := readBytes(b); s != "hello world" { 68 t.Errorf("simple hello world test failed: got %q", s) 69 } 70 71 b = NewReader(newRot13Reader(strings.NewReader(data))) 72 if s := readBytes(b); s != "uryyb jbeyq" { 73 t.Errorf("rot13 hello world test failed: got %q", s) 74 } 75 } 76 77 type readMaker struct { 78 name string 79 fn func(io.Reader) io.Reader 80 } 81 82 var readMakers = []readMaker{ 83 {"full", func(r io.Reader) io.Reader { return r }}, 84 {"byte", iotest.OneByteReader}, 85 {"half", iotest.HalfReader}, 86 {"data+err", iotest.DataErrReader}, 87 {"timeout", iotest.TimeoutReader}, 88 } 89 90 // Call ReadString (which ends up calling everything else) 91 // to accumulate the text of a file. 92 func readLines(b *Reader) string { 93 s := "" 94 for { 95 s1, err := b.ReadString('\n') 96 if err == io.EOF { 97 break 98 } 99 if err != nil && err != iotest.ErrTimeout { 100 panic("GetLines: " + err.Error()) 101 } 102 s += s1 103 } 104 return s 105 } 106 107 // Call Read to accumulate the text of a file 108 func reads(buf *Reader, m int) string { 109 var b [1000]byte 110 nb := 0 111 for { 112 n, err := buf.Read(b[nb : nb+m]) 113 nb += n 114 if err == io.EOF { 115 break 116 } 117 } 118 return string(b[0:nb]) 119 } 120 121 type bufReader struct { 122 name string 123 fn func(*Reader) string 124 } 125 126 var bufreaders = []bufReader{ 127 {"1", func(b *Reader) string { return reads(b, 1) }}, 128 {"2", func(b *Reader) string { return reads(b, 2) }}, 129 {"3", func(b *Reader) string { return reads(b, 3) }}, 130 {"4", func(b *Reader) string { return reads(b, 4) }}, 131 {"5", func(b *Reader) string { return reads(b, 5) }}, 132 {"7", func(b *Reader) string { return reads(b, 7) }}, 133 {"bytes", readBytes}, 134 {"lines", readLines}, 135 } 136 137 const minReadBufferSize = 16 138 139 var bufsizes = []int{ 140 0, minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096, 141 } 142 143 func TestReader(t *testing.T) { 144 var texts [31]string 145 str := "" 146 all := "" 147 for i := 0; i < len(texts)-1; i++ { 148 texts[i] = str + "\n" 149 all += texts[i] 150 str += string(i%26 + 'a') 151 } 152 texts[len(texts)-1] = all 153 154 for h := 0; h < len(texts); h++ { 155 text := texts[h] 156 for i := 0; i < len(readMakers); i++ { 157 for j := 0; j < len(bufreaders); j++ { 158 for k := 0; k < len(bufsizes); k++ { 159 readmaker := readMakers[i] 160 bufreader := bufreaders[j] 161 bufsize := bufsizes[k] 162 read := readmaker.fn(strings.NewReader(text)) 163 buf := NewReaderSize(read, bufsize) 164 s := bufreader.fn(buf) 165 if s != text { 166 t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q", 167 readmaker.name, bufreader.name, bufsize, text, s) 168 } 169 } 170 } 171 } 172 } 173 } 174 175 type zeroReader struct{} 176 177 func (zeroReader) Read(p []byte) (int, error) { 178 return 0, nil 179 } 180 181 func TestZeroReader(t *testing.T) { 182 var z zeroReader 183 r := NewReader(z) 184 185 c := make(chan error) 186 go func() { 187 _, err := r.ReadByte() 188 c <- err 189 }() 190 191 select { 192 case err := <-c: 193 if err == nil { 194 t.Error("error expected") 195 } else if err != io.ErrNoProgress { 196 t.Error("unexpected error:", err) 197 } 198 case <-time.After(time.Second): 199 t.Error("test timed out (endless loop in ReadByte?)") 200 } 201 } 202 203 // A StringReader delivers its data one string segment at a time via Read. 204 type StringReader struct { 205 data []string 206 step int 207 } 208 209 func (r *StringReader) Read(p []byte) (n int, err error) { 210 if r.step < len(r.data) { 211 s := r.data[r.step] 212 n = copy(p, s) 213 r.step++ 214 } else { 215 err = io.EOF 216 } 217 return 218 } 219 220 func readRuneSegments(t *testing.T, segments []string) { 221 got := "" 222 want := strings.Join(segments, "") 223 r := NewReader(&StringReader{data: segments}) 224 for { 225 r, _, err := r.ReadRune() 226 if err != nil { 227 if err != io.EOF { 228 return 229 } 230 break 231 } 232 got += string(r) 233 } 234 if got != want { 235 t.Errorf("segments=%v got=%s want=%s", segments, got, want) 236 } 237 } 238 239 var segmentList = [][]string{ 240 {}, 241 {""}, 242 {"日", "本語"}, 243 {"\u65e5", "\u672c", "\u8a9e"}, 244 {"\U000065e5", "\U0000672c", "\U00008a9e"}, 245 {"\xe6", "\x97\xa5\xe6", "\x9c\xac\xe8\xaa\x9e"}, 246 {"Hello", ", ", "World", "!"}, 247 {"Hello", ", ", "", "World", "!"}, 248 } 249 250 func TestReadRune(t *testing.T) { 251 for _, s := range segmentList { 252 readRuneSegments(t, s) 253 } 254 } 255 256 func TestUnreadRune(t *testing.T) { 257 segments := []string{"Hello, world:", "日本語"} 258 r := NewReader(&StringReader{data: segments}) 259 got := "" 260 want := strings.Join(segments, "") 261 // Normal execution. 262 for { 263 r1, _, err := r.ReadRune() 264 if err != nil { 265 if err != io.EOF { 266 t.Error("unexpected error on ReadRune:", err) 267 } 268 break 269 } 270 got += string(r1) 271 // Put it back and read it again. 272 if err = r.UnreadRune(); err != nil { 273 t.Fatal("unexpected error on UnreadRune:", err) 274 } 275 r2, _, err := r.ReadRune() 276 if err != nil { 277 t.Fatal("unexpected error reading after unreading:", err) 278 } 279 if r1 != r2 { 280 t.Fatalf("incorrect rune after unread: got %c, want %c", r1, r2) 281 } 282 } 283 if got != want { 284 t.Errorf("got %q, want %q", got, want) 285 } 286 } 287 288 func TestUnreadByte(t *testing.T) { 289 segments := []string{"Hello, ", "world"} 290 r := NewReader(&StringReader{data: segments}) 291 got := "" 292 want := strings.Join(segments, "") 293 // Normal execution. 294 for { 295 b1, err := r.ReadByte() 296 if err != nil { 297 if err != io.EOF { 298 t.Error("unexpected error on ReadByte:", err) 299 } 300 break 301 } 302 got += string(b1) 303 // Put it back and read it again. 304 if err = r.UnreadByte(); err != nil { 305 t.Fatal("unexpected error on UnreadByte:", err) 306 } 307 b2, err := r.ReadByte() 308 if err != nil { 309 t.Fatal("unexpected error reading after unreading:", err) 310 } 311 if b1 != b2 { 312 t.Fatalf("incorrect byte after unread: got %q, want %q", b1, b2) 313 } 314 } 315 if got != want { 316 t.Errorf("got %q, want %q", got, want) 317 } 318 } 319 320 func TestUnreadByteMultiple(t *testing.T) { 321 segments := []string{"Hello, ", "world"} 322 data := strings.Join(segments, "") 323 for n := 0; n <= len(data); n++ { 324 r := NewReader(&StringReader{data: segments}) 325 // Read n bytes. 326 for i := 0; i < n; i++ { 327 b, err := r.ReadByte() 328 if err != nil { 329 t.Fatalf("n = %d: unexpected error on ReadByte: %v", n, err) 330 } 331 if b != data[i] { 332 t.Fatalf("n = %d: incorrect byte returned from ReadByte: got %q, want %q", n, b, data[i]) 333 } 334 } 335 // Unread one byte if there is one. 336 if n > 0 { 337 if err := r.UnreadByte(); err != nil { 338 t.Errorf("n = %d: unexpected error on UnreadByte: %v", n, err) 339 } 340 } 341 // Test that we cannot unread any further. 342 if err := r.UnreadByte(); err == nil { 343 t.Errorf("n = %d: expected error on UnreadByte", n) 344 } 345 } 346 } 347 348 func TestUnreadByteOthers(t *testing.T) { 349 // A list of readers to use in conjunction with UnreadByte. 350 var readers = []func(*Reader, byte) ([]byte, error){ 351 (*Reader).ReadBytes, 352 (*Reader).ReadSlice, 353 func(r *Reader, delim byte) ([]byte, error) { 354 data, err := r.ReadString(delim) 355 return []byte(data), err 356 }, 357 // ReadLine doesn't fit the data/pattern easily 358 // so we leave it out. It should be covered via 359 // the ReadSlice test since ReadLine simply calls 360 // ReadSlice, and it's that function that handles 361 // the last byte. 362 } 363 364 // Try all readers with UnreadByte. 365 for rno, read := range readers { 366 // Some input data that is longer than the minimum reader buffer size. 367 const n = 10 368 var buf bytes.Buffer 369 for i := 0; i < n; i++ { 370 buf.WriteString("abcdefg") 371 } 372 373 r := NewReaderSize(&buf, minReadBufferSize) 374 readTo := func(delim byte, want string) { 375 data, err := read(r, delim) 376 if err != nil { 377 t.Fatalf("#%d: unexpected error reading to %c: %v", rno, delim, err) 378 } 379 if got := string(data); got != want { 380 t.Fatalf("#%d: got %q, want %q", rno, got, want) 381 } 382 } 383 384 // Read the data with occasional UnreadByte calls. 385 for i := 0; i < n; i++ { 386 readTo('d', "abcd") 387 for j := 0; j < 3; j++ { 388 if err := r.UnreadByte(); err != nil { 389 t.Fatalf("#%d: unexpected error on UnreadByte: %v", rno, err) 390 } 391 readTo('d', "d") 392 } 393 readTo('g', "efg") 394 } 395 396 // All data should have been read. 397 _, err := r.ReadByte() 398 if err != io.EOF { 399 t.Errorf("#%d: got error %v; want EOF", rno, err) 400 } 401 } 402 } 403 404 // Test that UnreadRune fails if the preceding operation was not a ReadRune. 405 func TestUnreadRuneError(t *testing.T) { 406 buf := make([]byte, 3) // All runes in this test are 3 bytes long 407 r := NewReader(&StringReader{data: []string{"日本語日本語日本語"}}) 408 if r.UnreadRune() == nil { 409 t.Error("expected error on UnreadRune from fresh buffer") 410 } 411 _, _, err := r.ReadRune() 412 if err != nil { 413 t.Error("unexpected error on ReadRune (1):", err) 414 } 415 if err = r.UnreadRune(); err != nil { 416 t.Error("unexpected error on UnreadRune (1):", err) 417 } 418 if r.UnreadRune() == nil { 419 t.Error("expected error after UnreadRune (1)") 420 } 421 // Test error after Read. 422 _, _, err = r.ReadRune() // reset state 423 if err != nil { 424 t.Error("unexpected error on ReadRune (2):", err) 425 } 426 _, err = r.Read(buf) 427 if err != nil { 428 t.Error("unexpected error on Read (2):", err) 429 } 430 if r.UnreadRune() == nil { 431 t.Error("expected error after Read (2)") 432 } 433 // Test error after ReadByte. 434 _, _, err = r.ReadRune() // reset state 435 if err != nil { 436 t.Error("unexpected error on ReadRune (2):", err) 437 } 438 for range buf { 439 _, err = r.ReadByte() 440 if err != nil { 441 t.Error("unexpected error on ReadByte (2):", err) 442 } 443 } 444 if r.UnreadRune() == nil { 445 t.Error("expected error after ReadByte") 446 } 447 // Test error after UnreadByte. 448 _, _, err = r.ReadRune() // reset state 449 if err != nil { 450 t.Error("unexpected error on ReadRune (3):", err) 451 } 452 _, err = r.ReadByte() 453 if err != nil { 454 t.Error("unexpected error on ReadByte (3):", err) 455 } 456 err = r.UnreadByte() 457 if err != nil { 458 t.Error("unexpected error on UnreadByte (3):", err) 459 } 460 if r.UnreadRune() == nil { 461 t.Error("expected error after UnreadByte (3)") 462 } 463 // Test error after ReadSlice. 464 _, _, err = r.ReadRune() // reset state 465 if err != nil { 466 t.Error("unexpected error on ReadRune (4):", err) 467 } 468 _, err = r.ReadSlice(0) 469 if err != io.EOF { 470 t.Error("unexpected error on ReadSlice (4):", err) 471 } 472 if r.UnreadRune() == nil { 473 t.Error("expected error after ReadSlice (4)") 474 } 475 } 476 477 func TestUnreadRuneAtEOF(t *testing.T) { 478 // UnreadRune/ReadRune should error at EOF (was a bug; used to panic) 479 r := NewReader(strings.NewReader("x")) 480 r.ReadRune() 481 r.ReadRune() 482 r.UnreadRune() 483 _, _, err := r.ReadRune() 484 if err == nil { 485 t.Error("expected error at EOF") 486 } else if err != io.EOF { 487 t.Error("expected EOF; got", err) 488 } 489 } 490 491 func TestReadWriteRune(t *testing.T) { 492 const NRune = 1000 493 byteBuf := new(bytes.Buffer) 494 w := NewWriter(byteBuf) 495 // Write the runes out using WriteRune 496 buf := make([]byte, utf8.UTFMax) 497 for r := rune(0); r < NRune; r++ { 498 size := utf8.EncodeRune(buf, r) 499 nbytes, err := w.WriteRune(r) 500 if err != nil { 501 t.Fatalf("WriteRune(0x%x) error: %s", r, err) 502 } 503 if nbytes != size { 504 t.Fatalf("WriteRune(0x%x) expected %d, got %d", r, size, nbytes) 505 } 506 } 507 w.Flush() 508 509 r := NewReader(byteBuf) 510 // Read them back with ReadRune 511 for r1 := rune(0); r1 < NRune; r1++ { 512 size := utf8.EncodeRune(buf, r1) 513 nr, nbytes, err := r.ReadRune() 514 if nr != r1 || nbytes != size || err != nil { 515 t.Fatalf("ReadRune(0x%x) got 0x%x,%d not 0x%x,%d (err=%s)", r1, nr, nbytes, r1, size, err) 516 } 517 } 518 } 519 520 func TestWriter(t *testing.T) { 521 var data [8192]byte 522 523 for i := 0; i < len(data); i++ { 524 data[i] = byte(' ' + i%('~'-' ')) 525 } 526 w := new(bytes.Buffer) 527 for i := 0; i < len(bufsizes); i++ { 528 for j := 0; j < len(bufsizes); j++ { 529 nwrite := bufsizes[i] 530 bs := bufsizes[j] 531 532 // Write nwrite bytes using buffer size bs. 533 // Check that the right amount makes it out 534 // and that the data is correct. 535 536 w.Reset() 537 buf := NewWriterSize(w, bs) 538 context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs) 539 n, e1 := buf.Write(data[0:nwrite]) 540 if e1 != nil || n != nwrite { 541 t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1) 542 continue 543 } 544 if e := buf.Flush(); e != nil { 545 t.Errorf("%s: buf.Flush = %v", context, e) 546 } 547 548 written := w.Bytes() 549 if len(written) != nwrite { 550 t.Errorf("%s: %d bytes written", context, len(written)) 551 } 552 for l := 0; l < len(written); l++ { 553 if written[i] != data[i] { 554 t.Errorf("wrong bytes written") 555 t.Errorf("want=%q", data[0:len(written)]) 556 t.Errorf("have=%q", written) 557 } 558 } 559 } 560 } 561 } 562 563 // Check that write errors are returned properly. 564 565 type errorWriterTest struct { 566 n, m int 567 err error 568 expect error 569 } 570 571 func (w errorWriterTest) Write(p []byte) (int, error) { 572 return len(p) * w.n / w.m, w.err 573 } 574 575 var errorWriterTests = []errorWriterTest{ 576 {0, 1, nil, io.ErrShortWrite}, 577 {1, 2, nil, io.ErrShortWrite}, 578 {1, 1, nil, nil}, 579 {0, 1, io.ErrClosedPipe, io.ErrClosedPipe}, 580 {1, 2, io.ErrClosedPipe, io.ErrClosedPipe}, 581 {1, 1, io.ErrClosedPipe, io.ErrClosedPipe}, 582 } 583 584 func TestWriteErrors(t *testing.T) { 585 for _, w := range errorWriterTests { 586 buf := NewWriter(w) 587 _, e := buf.Write([]byte("hello world")) 588 if e != nil { 589 t.Errorf("Write hello to %v: %v", w, e) 590 continue 591 } 592 // Two flushes, to verify the error is sticky. 593 for i := 0; i < 2; i++ { 594 e = buf.Flush() 595 if e != w.expect { 596 t.Errorf("Flush %d/2 %v: got %v, wanted %v", i+1, w, e, w.expect) 597 } 598 } 599 } 600 } 601 602 func TestNewReaderSizeIdempotent(t *testing.T) { 603 const BufSize = 1000 604 b := NewReaderSize(strings.NewReader("hello world"), BufSize) 605 // Does it recognize itself? 606 b1 := NewReaderSize(b, BufSize) 607 if b1 != b { 608 t.Error("NewReaderSize did not detect underlying Reader") 609 } 610 // Does it wrap if existing buffer is too small? 611 b2 := NewReaderSize(b, 2*BufSize) 612 if b2 == b { 613 t.Error("NewReaderSize did not enlarge buffer") 614 } 615 } 616 617 func TestNewWriterSizeIdempotent(t *testing.T) { 618 const BufSize = 1000 619 b := NewWriterSize(new(bytes.Buffer), BufSize) 620 // Does it recognize itself? 621 b1 := NewWriterSize(b, BufSize) 622 if b1 != b { 623 t.Error("NewWriterSize did not detect underlying Writer") 624 } 625 // Does it wrap if existing buffer is too small? 626 b2 := NewWriterSize(b, 2*BufSize) 627 if b2 == b { 628 t.Error("NewWriterSize did not enlarge buffer") 629 } 630 } 631 632 func TestWriteString(t *testing.T) { 633 const BufSize = 8 634 buf := new(bytes.Buffer) 635 b := NewWriterSize(buf, BufSize) 636 b.WriteString("0") // easy 637 b.WriteString("123456") // still easy 638 b.WriteString("7890") // easy after flush 639 b.WriteString("abcdefghijklmnopqrstuvwxy") // hard 640 b.WriteString("z") 641 if err := b.Flush(); err != nil { 642 t.Error("WriteString", err) 643 } 644 s := "01234567890abcdefghijklmnopqrstuvwxyz" 645 if string(buf.Bytes()) != s { 646 t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes())) 647 } 648 } 649 650 func TestBufferFull(t *testing.T) { 651 const longString = "And now, hello, world! It is the time for all good men to come to the aid of their party" 652 buf := NewReaderSize(strings.NewReader(longString), minReadBufferSize) 653 line, err := buf.ReadSlice('!') 654 if string(line) != "And now, hello, " || err != ErrBufferFull { 655 t.Errorf("first ReadSlice(,) = %q, %v", line, err) 656 } 657 line, err = buf.ReadSlice('!') 658 if string(line) != "world!" || err != nil { 659 t.Errorf("second ReadSlice(,) = %q, %v", line, err) 660 } 661 } 662 663 func TestPeek(t *testing.T) { 664 p := make([]byte, 10) 665 // string is 16 (minReadBufferSize) long. 666 buf := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize) 667 if s, err := buf.Peek(1); string(s) != "a" || err != nil { 668 t.Fatalf("want %q got %q, err=%v", "a", string(s), err) 669 } 670 if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { 671 t.Fatalf("want %q got %q, err=%v", "abcd", string(s), err) 672 } 673 if _, err := buf.Peek(-1); err != ErrNegativeCount { 674 t.Fatalf("want ErrNegativeCount got %v", err) 675 } 676 if _, err := buf.Peek(32); err != ErrBufferFull { 677 t.Fatalf("want ErrBufFull got %v", err) 678 } 679 if _, err := buf.Read(p[0:3]); string(p[0:3]) != "abc" || err != nil { 680 t.Fatalf("want %q got %q, err=%v", "abc", string(p[0:3]), err) 681 } 682 if s, err := buf.Peek(1); string(s) != "d" || err != nil { 683 t.Fatalf("want %q got %q, err=%v", "d", string(s), err) 684 } 685 if s, err := buf.Peek(2); string(s) != "de" || err != nil { 686 t.Fatalf("want %q got %q, err=%v", "de", string(s), err) 687 } 688 if _, err := buf.Read(p[0:3]); string(p[0:3]) != "def" || err != nil { 689 t.Fatalf("want %q got %q, err=%v", "def", string(p[0:3]), err) 690 } 691 if s, err := buf.Peek(4); string(s) != "ghij" || err != nil { 692 t.Fatalf("want %q got %q, err=%v", "ghij", string(s), err) 693 } 694 if _, err := buf.Read(p[0:]); string(p[0:]) != "ghijklmnop" || err != nil { 695 t.Fatalf("want %q got %q, err=%v", "ghijklmnop", string(p[0:minReadBufferSize]), err) 696 } 697 if s, err := buf.Peek(0); string(s) != "" || err != nil { 698 t.Fatalf("want %q got %q, err=%v", "", string(s), err) 699 } 700 if _, err := buf.Peek(1); err != io.EOF { 701 t.Fatalf("want EOF got %v", err) 702 } 703 704 // Test for issue 3022, not exposing a reader's error on a successful Peek. 705 buf = NewReaderSize(dataAndEOFReader("abcd"), 32) 706 if s, err := buf.Peek(2); string(s) != "ab" || err != nil { 707 t.Errorf(`Peek(2) on "abcd", EOF = %q, %v; want "ab", nil`, string(s), err) 708 } 709 if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { 710 t.Errorf(`Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`, string(s), err) 711 } 712 if n, err := buf.Read(p[0:5]); string(p[0:n]) != "abcd" || err != nil { 713 t.Fatalf("Read after peek = %q, %v; want abcd, EOF", p[0:n], err) 714 } 715 if n, err := buf.Read(p[0:1]); string(p[0:n]) != "" || err != io.EOF { 716 t.Fatalf(`second Read after peek = %q, %v; want "", EOF`, p[0:n], err) 717 } 718 } 719 720 type dataAndEOFReader string 721 722 func (r dataAndEOFReader) Read(p []byte) (int, error) { 723 return copy(p, r), io.EOF 724 } 725 726 func TestPeekThenUnreadRune(t *testing.T) { 727 // This sequence used to cause a crash. 728 r := NewReader(strings.NewReader("x")) 729 r.ReadRune() 730 r.Peek(1) 731 r.UnreadRune() 732 r.ReadRune() // Used to panic here 733 } 734 735 var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy") 736 var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy") 737 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") 738 739 // TestReader wraps a []byte and returns reads of a specific length. 740 type testReader struct { 741 data []byte 742 stride int 743 } 744 745 func (t *testReader) Read(buf []byte) (n int, err error) { 746 n = t.stride 747 if n > len(t.data) { 748 n = len(t.data) 749 } 750 if n > len(buf) { 751 n = len(buf) 752 } 753 copy(buf, t.data) 754 t.data = t.data[n:] 755 if len(t.data) == 0 { 756 err = io.EOF 757 } 758 return 759 } 760 761 func testReadLine(t *testing.T, input []byte) { 762 //for stride := 1; stride < len(input); stride++ { 763 for stride := 1; stride < 2; stride++ { 764 done := 0 765 reader := testReader{input, stride} 766 l := NewReaderSize(&reader, len(input)+1) 767 for { 768 line, isPrefix, err := l.ReadLine() 769 if len(line) > 0 && err != nil { 770 t.Errorf("ReadLine returned both data and error: %s", err) 771 } 772 if isPrefix { 773 t.Errorf("ReadLine returned prefix") 774 } 775 if err != nil { 776 if err != io.EOF { 777 t.Fatalf("Got unknown error: %s", err) 778 } 779 break 780 } 781 if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) { 782 t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line) 783 } 784 done += len(line) 785 } 786 if done != len(testOutput) { 787 t.Errorf("ReadLine didn't return everything: got: %d, want: %d (stride: %d)", done, len(testOutput), stride) 788 } 789 } 790 } 791 792 func TestReadLine(t *testing.T) { 793 testReadLine(t, testInput) 794 testReadLine(t, testInputrn) 795 } 796 797 func TestLineTooLong(t *testing.T) { 798 data := make([]byte, 0) 799 for i := 0; i < minReadBufferSize*5/2; i++ { 800 data = append(data, '0'+byte(i%10)) 801 } 802 buf := bytes.NewReader(data) 803 l := NewReaderSize(buf, minReadBufferSize) 804 line, isPrefix, err := l.ReadLine() 805 if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { 806 t.Errorf("bad result for first line: got %q want %q %v", line, data[:minReadBufferSize], err) 807 } 808 data = data[len(line):] 809 line, isPrefix, err = l.ReadLine() 810 if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { 811 t.Errorf("bad result for second line: got %q want %q %v", line, data[:minReadBufferSize], err) 812 } 813 data = data[len(line):] 814 line, isPrefix, err = l.ReadLine() 815 if isPrefix || !bytes.Equal(line, data[:minReadBufferSize/2]) || err != nil { 816 t.Errorf("bad result for third line: got %q want %q %v", line, data[:minReadBufferSize/2], err) 817 } 818 line, isPrefix, err = l.ReadLine() 819 if isPrefix || err == nil { 820 t.Errorf("expected no more lines: %x %s", line, err) 821 } 822 } 823 824 func TestReadAfterLines(t *testing.T) { 825 line1 := "this is line1" 826 restData := "this is line2\nthis is line 3\n" 827 inbuf := bytes.NewReader([]byte(line1 + "\n" + restData)) 828 outbuf := new(bytes.Buffer) 829 maxLineLength := len(line1) + len(restData)/2 830 l := NewReaderSize(inbuf, maxLineLength) 831 line, isPrefix, err := l.ReadLine() 832 if isPrefix || err != nil || string(line) != line1 { 833 t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line)) 834 } 835 n, err := io.Copy(outbuf, l) 836 if int(n) != len(restData) || err != nil { 837 t.Errorf("bad result for Read: n=%d err=%v", n, err) 838 } 839 if outbuf.String() != restData { 840 t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData) 841 } 842 } 843 844 func TestReadEmptyBuffer(t *testing.T) { 845 l := NewReaderSize(new(bytes.Buffer), minReadBufferSize) 846 line, isPrefix, err := l.ReadLine() 847 if err != io.EOF { 848 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) 849 } 850 } 851 852 func TestLinesAfterRead(t *testing.T) { 853 l := NewReaderSize(bytes.NewReader([]byte("foo")), minReadBufferSize) 854 _, err := ioutil.ReadAll(l) 855 if err != nil { 856 t.Error(err) 857 return 858 } 859 860 line, isPrefix, err := l.ReadLine() 861 if err != io.EOF { 862 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) 863 } 864 } 865 866 func TestReadLineNonNilLineOrError(t *testing.T) { 867 r := NewReader(strings.NewReader("line 1\n")) 868 for i := 0; i < 2; i++ { 869 l, _, err := r.ReadLine() 870 if l != nil && err != nil { 871 t.Fatalf("on line %d/2; ReadLine=%#v, %v; want non-nil line or Error, but not both", 872 i+1, l, err) 873 } 874 } 875 } 876 877 type readLineResult struct { 878 line []byte 879 isPrefix bool 880 err error 881 } 882 883 var readLineNewlinesTests = []struct { 884 input string 885 expect []readLineResult 886 }{ 887 {"012345678901234\r\n012345678901234\r\n", []readLineResult{ 888 {[]byte("012345678901234"), true, nil}, 889 {nil, false, nil}, 890 {[]byte("012345678901234"), true, nil}, 891 {nil, false, nil}, 892 {nil, false, io.EOF}, 893 }}, 894 {"0123456789012345\r012345678901234\r", []readLineResult{ 895 {[]byte("0123456789012345"), true, nil}, 896 {[]byte("\r012345678901234"), true, nil}, 897 {[]byte("\r"), false, nil}, 898 {nil, false, io.EOF}, 899 }}, 900 } 901 902 func TestReadLineNewlines(t *testing.T) { 903 for _, e := range readLineNewlinesTests { 904 testReadLineNewlines(t, e.input, e.expect) 905 } 906 } 907 908 func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) { 909 b := NewReaderSize(strings.NewReader(input), minReadBufferSize) 910 for i, e := range expect { 911 line, isPrefix, err := b.ReadLine() 912 if !bytes.Equal(line, e.line) { 913 t.Errorf("%q call %d, line == %q, want %q", input, i, line, e.line) 914 return 915 } 916 if isPrefix != e.isPrefix { 917 t.Errorf("%q call %d, isPrefix == %v, want %v", input, i, isPrefix, e.isPrefix) 918 return 919 } 920 if err != e.err { 921 t.Errorf("%q call %d, err == %v, want %v", input, i, err, e.err) 922 return 923 } 924 } 925 } 926 927 func createTestInput(n int) []byte { 928 input := make([]byte, n) 929 for i := range input { 930 // 101 and 251 are arbitrary prime numbers. 931 // The idea is to create an input sequence 932 // which doesn't repeat too frequently. 933 input[i] = byte(i % 251) 934 if i%101 == 0 { 935 input[i] ^= byte(i / 101) 936 } 937 } 938 return input 939 } 940 941 func TestReaderWriteTo(t *testing.T) { 942 input := createTestInput(8192) 943 r := NewReader(onlyReader{bytes.NewReader(input)}) 944 w := new(bytes.Buffer) 945 if n, err := r.WriteTo(w); err != nil || n != int64(len(input)) { 946 t.Fatalf("r.WriteTo(w) = %d, %v, want %d, nil", n, err, len(input)) 947 } 948 949 for i, val := range w.Bytes() { 950 if val != input[i] { 951 t.Errorf("after write: out[%d] = %#x, want %#x", i, val, input[i]) 952 } 953 } 954 } 955 956 type errorWriterToTest struct { 957 rn, wn int 958 rerr, werr error 959 expected error 960 } 961 962 func (r errorWriterToTest) Read(p []byte) (int, error) { 963 return len(p) * r.rn, r.rerr 964 } 965 966 func (w errorWriterToTest) Write(p []byte) (int, error) { 967 return len(p) * w.wn, w.werr 968 } 969 970 var errorWriterToTests = []errorWriterToTest{ 971 {1, 0, nil, io.ErrClosedPipe, io.ErrClosedPipe}, 972 {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, 973 {0, 0, io.ErrUnexpectedEOF, io.ErrClosedPipe, io.ErrClosedPipe}, 974 {0, 1, io.EOF, nil, nil}, 975 } 976 977 func TestReaderWriteToErrors(t *testing.T) { 978 for i, rw := range errorWriterToTests { 979 r := NewReader(rw) 980 if _, err := r.WriteTo(rw); err != rw.expected { 981 t.Errorf("r.WriteTo(errorWriterToTests[%d]) = _, %v, want _,%v", i, err, rw.expected) 982 } 983 } 984 } 985 986 func TestWriterReadFrom(t *testing.T) { 987 ws := []func(io.Writer) io.Writer{ 988 func(w io.Writer) io.Writer { return onlyWriter{w} }, 989 func(w io.Writer) io.Writer { return w }, 990 } 991 992 rs := []func(io.Reader) io.Reader{ 993 iotest.DataErrReader, 994 func(r io.Reader) io.Reader { return r }, 995 } 996 997 for ri, rfunc := range rs { 998 for wi, wfunc := range ws { 999 input := createTestInput(8192) 1000 b := new(bytes.Buffer) 1001 w := NewWriter(wfunc(b)) 1002 r := rfunc(bytes.NewReader(input)) 1003 if n, err := w.ReadFrom(r); err != nil || n != int64(len(input)) { 1004 t.Errorf("ws[%d],rs[%d]: w.ReadFrom(r) = %d, %v, want %d, nil", wi, ri, n, err, len(input)) 1005 continue 1006 } 1007 if err := w.Flush(); err != nil { 1008 t.Errorf("Flush returned %v", err) 1009 continue 1010 } 1011 if got, want := b.String(), string(input); got != want { 1012 t.Errorf("ws[%d], rs[%d]:\ngot %q\nwant %q\n", wi, ri, got, want) 1013 } 1014 } 1015 } 1016 } 1017 1018 type errorReaderFromTest struct { 1019 rn, wn int 1020 rerr, werr error 1021 expected error 1022 } 1023 1024 func (r errorReaderFromTest) Read(p []byte) (int, error) { 1025 return len(p) * r.rn, r.rerr 1026 } 1027 1028 func (w errorReaderFromTest) Write(p []byte) (int, error) { 1029 return len(p) * w.wn, w.werr 1030 } 1031 1032 var errorReaderFromTests = []errorReaderFromTest{ 1033 {0, 1, io.EOF, nil, nil}, 1034 {1, 1, io.EOF, nil, nil}, 1035 {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, 1036 {0, 0, io.ErrClosedPipe, io.ErrShortWrite, io.ErrClosedPipe}, 1037 {1, 0, nil, io.ErrShortWrite, io.ErrShortWrite}, 1038 } 1039 1040 func TestWriterReadFromErrors(t *testing.T) { 1041 for i, rw := range errorReaderFromTests { 1042 w := NewWriter(rw) 1043 if _, err := w.ReadFrom(rw); err != rw.expected { 1044 t.Errorf("w.ReadFrom(errorReaderFromTests[%d]) = _, %v, want _,%v", i, err, rw.expected) 1045 } 1046 } 1047 } 1048 1049 // TestWriterReadFromCounts tests that using io.Copy to copy into a 1050 // bufio.Writer does not prematurely flush the buffer. For example, when 1051 // buffering writes to a network socket, excessive network writes should be 1052 // avoided. 1053 func TestWriterReadFromCounts(t *testing.T) { 1054 var w0 writeCountingDiscard 1055 b0 := NewWriterSize(&w0, 1234) 1056 b0.WriteString(strings.Repeat("x", 1000)) 1057 if w0 != 0 { 1058 t.Fatalf("write 1000 'x's: got %d writes, want 0", w0) 1059 } 1060 b0.WriteString(strings.Repeat("x", 200)) 1061 if w0 != 0 { 1062 t.Fatalf("write 1200 'x's: got %d writes, want 0", w0) 1063 } 1064 io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 30))}) 1065 if w0 != 0 { 1066 t.Fatalf("write 1230 'x's: got %d writes, want 0", w0) 1067 } 1068 io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 9))}) 1069 if w0 != 1 { 1070 t.Fatalf("write 1239 'x's: got %d writes, want 1", w0) 1071 } 1072 1073 var w1 writeCountingDiscard 1074 b1 := NewWriterSize(&w1, 1234) 1075 b1.WriteString(strings.Repeat("x", 1200)) 1076 b1.Flush() 1077 if w1 != 1 { 1078 t.Fatalf("flush 1200 'x's: got %d writes, want 1", w1) 1079 } 1080 b1.WriteString(strings.Repeat("x", 89)) 1081 if w1 != 1 { 1082 t.Fatalf("write 1200 + 89 'x's: got %d writes, want 1", w1) 1083 } 1084 io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 700))}) 1085 if w1 != 1 { 1086 t.Fatalf("write 1200 + 789 'x's: got %d writes, want 1", w1) 1087 } 1088 io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))}) 1089 if w1 != 2 { 1090 t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1) 1091 } 1092 b1.Flush() 1093 if w1 != 3 { 1094 t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1) 1095 } 1096 } 1097 1098 // A writeCountingDiscard is like ioutil.Discard and counts the number of times 1099 // Write is called on it. 1100 type writeCountingDiscard int 1101 1102 func (w *writeCountingDiscard) Write(p []byte) (int, error) { 1103 *w++ 1104 return len(p), nil 1105 } 1106 1107 type negativeReader int 1108 1109 func (r *negativeReader) Read([]byte) (int, error) { return -1, nil } 1110 1111 func TestNegativeRead(t *testing.T) { 1112 // should panic with a description pointing at the reader, not at itself. 1113 // (should NOT panic with slice index error, for example.) 1114 b := NewReader(new(negativeReader)) 1115 defer func() { 1116 switch err := recover().(type) { 1117 case nil: 1118 t.Fatal("read did not panic") 1119 case error: 1120 if !strings.Contains(err.Error(), "reader returned negative count from Read") { 1121 t.Fatalf("wrong panic: %v", err) 1122 } 1123 default: 1124 t.Fatalf("unexpected panic value: %T(%v)", err, err) 1125 } 1126 }() 1127 b.Read(make([]byte, 100)) 1128 } 1129 1130 var errFake = errors.New("fake error") 1131 1132 type errorThenGoodReader struct { 1133 didErr bool 1134 nread int 1135 } 1136 1137 func (r *errorThenGoodReader) Read(p []byte) (int, error) { 1138 r.nread++ 1139 if !r.didErr { 1140 r.didErr = true 1141 return 0, errFake 1142 } 1143 return len(p), nil 1144 } 1145 1146 func TestReaderClearError(t *testing.T) { 1147 r := &errorThenGoodReader{} 1148 b := NewReader(r) 1149 buf := make([]byte, 1) 1150 if _, err := b.Read(nil); err != nil { 1151 t.Fatalf("1st nil Read = %v; want nil", err) 1152 } 1153 if _, err := b.Read(buf); err != errFake { 1154 t.Fatalf("1st Read = %v; want errFake", err) 1155 } 1156 if _, err := b.Read(nil); err != nil { 1157 t.Fatalf("2nd nil Read = %v; want nil", err) 1158 } 1159 if _, err := b.Read(buf); err != nil { 1160 t.Fatalf("3rd Read with buffer = %v; want nil", err) 1161 } 1162 if r.nread != 2 { 1163 t.Errorf("num reads = %d; want 2", r.nread) 1164 } 1165 } 1166 1167 // Test for golang.org/issue/5947 1168 func TestWriterReadFromWhileFull(t *testing.T) { 1169 buf := new(bytes.Buffer) 1170 w := NewWriterSize(buf, 10) 1171 1172 // Fill buffer exactly. 1173 n, err := w.Write([]byte("0123456789")) 1174 if n != 10 || err != nil { 1175 t.Fatalf("Write returned (%v, %v), want (10, nil)", n, err) 1176 } 1177 1178 // Use ReadFrom to read in some data. 1179 n2, err := w.ReadFrom(strings.NewReader("abcdef")) 1180 if n2 != 6 || err != nil { 1181 t.Fatalf("ReadFrom returned (%v, %v), want (6, nil)", n2, err) 1182 } 1183 } 1184 1185 type emptyThenNonEmptyReader struct { 1186 r io.Reader 1187 n int 1188 } 1189 1190 func (r *emptyThenNonEmptyReader) Read(p []byte) (int, error) { 1191 if r.n <= 0 { 1192 return r.r.Read(p) 1193 } 1194 r.n-- 1195 return 0, nil 1196 } 1197 1198 // Test for golang.org/issue/7611 1199 func TestWriterReadFromUntilEOF(t *testing.T) { 1200 buf := new(bytes.Buffer) 1201 w := NewWriterSize(buf, 5) 1202 1203 // Partially fill buffer 1204 n, err := w.Write([]byte("0123")) 1205 if n != 4 || err != nil { 1206 t.Fatalf("Write returned (%v, %v), want (4, nil)", n, err) 1207 } 1208 1209 // Use ReadFrom to read in some data. 1210 r := &emptyThenNonEmptyReader{r: strings.NewReader("abcd"), n: 3} 1211 n2, err := w.ReadFrom(r) 1212 if n2 != 4 || err != nil { 1213 t.Fatalf("ReadFrom returned (%v, %v), want (4, nil)", n2, err) 1214 } 1215 w.Flush() 1216 if got, want := string(buf.Bytes()), "0123abcd"; got != want { 1217 t.Fatalf("buf.Bytes() returned %q, want %q", got, want) 1218 } 1219 } 1220 1221 func TestWriterReadFromErrNoProgress(t *testing.T) { 1222 buf := new(bytes.Buffer) 1223 w := NewWriterSize(buf, 5) 1224 1225 // Partially fill buffer 1226 n, err := w.Write([]byte("0123")) 1227 if n != 4 || err != nil { 1228 t.Fatalf("Write returned (%v, %v), want (4, nil)", n, err) 1229 } 1230 1231 // Use ReadFrom to read in some data. 1232 r := &emptyThenNonEmptyReader{r: strings.NewReader("abcd"), n: 100} 1233 n2, err := w.ReadFrom(r) 1234 if n2 != 0 || err != io.ErrNoProgress { 1235 t.Fatalf("buf.Bytes() returned (%v, %v), want (0, io.ErrNoProgress)", n2, err) 1236 } 1237 } 1238 1239 func TestReaderReset(t *testing.T) { 1240 r := NewReader(strings.NewReader("foo foo")) 1241 buf := make([]byte, 3) 1242 r.Read(buf) 1243 if string(buf) != "foo" { 1244 t.Errorf("buf = %q; want foo", buf) 1245 } 1246 r.Reset(strings.NewReader("bar bar")) 1247 all, err := ioutil.ReadAll(r) 1248 if err != nil { 1249 t.Fatal(err) 1250 } 1251 if string(all) != "bar bar" { 1252 t.Errorf("ReadAll = %q; want bar bar", all) 1253 } 1254 } 1255 1256 func TestWriterReset(t *testing.T) { 1257 var buf1, buf2 bytes.Buffer 1258 w := NewWriter(&buf1) 1259 w.WriteString("foo") 1260 w.Reset(&buf2) // and not flushed 1261 w.WriteString("bar") 1262 w.Flush() 1263 if buf1.String() != "" { 1264 t.Errorf("buf1 = %q; want empty", buf1.String()) 1265 } 1266 if buf2.String() != "bar" { 1267 t.Errorf("buf2 = %q; want bar", buf2.String()) 1268 } 1269 } 1270 1271 func TestReaderDiscard(t *testing.T) { 1272 tests := []struct { 1273 name string 1274 r io.Reader 1275 bufSize int // 0 means 16 1276 peekSize int 1277 1278 n int // input to Discard 1279 1280 want int // from Discard 1281 wantErr error // from Discard 1282 1283 wantBuffered int 1284 }{ 1285 { 1286 name: "normal case", 1287 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"), 1288 peekSize: 16, 1289 n: 6, 1290 want: 6, 1291 wantBuffered: 10, 1292 }, 1293 { 1294 name: "discard causing read", 1295 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"), 1296 n: 6, 1297 want: 6, 1298 wantBuffered: 10, 1299 }, 1300 { 1301 name: "discard all without peek", 1302 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"), 1303 n: 26, 1304 want: 26, 1305 wantBuffered: 0, 1306 }, 1307 { 1308 name: "discard more than end", 1309 r: strings.NewReader("abcdefghijklmnopqrstuvwxyz"), 1310 n: 27, 1311 want: 26, 1312 wantErr: io.EOF, 1313 wantBuffered: 0, 1314 }, 1315 // Any error from filling shouldn't show up until we 1316 // get past the valid bytes. Here we return we return 5 valid bytes at the same time 1317 // as an error, but test that we don't see the error from Discard. 1318 { 1319 name: "fill error, discard less", 1320 r: newScriptedReader(func(p []byte) (n int, err error) { 1321 if len(p) < 5 { 1322 panic("unexpected small read") 1323 } 1324 return 5, errors.New("5-then-error") 1325 }), 1326 n: 4, 1327 want: 4, 1328 wantErr: nil, 1329 wantBuffered: 1, 1330 }, 1331 { 1332 name: "fill error, discard equal", 1333 r: newScriptedReader(func(p []byte) (n int, err error) { 1334 if len(p) < 5 { 1335 panic("unexpected small read") 1336 } 1337 return 5, errors.New("5-then-error") 1338 }), 1339 n: 5, 1340 want: 5, 1341 wantErr: nil, 1342 wantBuffered: 0, 1343 }, 1344 { 1345 name: "fill error, discard more", 1346 r: newScriptedReader(func(p []byte) (n int, err error) { 1347 if len(p) < 5 { 1348 panic("unexpected small read") 1349 } 1350 return 5, errors.New("5-then-error") 1351 }), 1352 n: 6, 1353 want: 5, 1354 wantErr: errors.New("5-then-error"), 1355 wantBuffered: 0, 1356 }, 1357 // Discard of 0 shouldn't cause a read: 1358 { 1359 name: "discard zero", 1360 r: newScriptedReader(), // will panic on Read 1361 n: 0, 1362 want: 0, 1363 wantErr: nil, 1364 wantBuffered: 0, 1365 }, 1366 { 1367 name: "discard negative", 1368 r: newScriptedReader(), // will panic on Read 1369 n: -1, 1370 want: 0, 1371 wantErr: ErrNegativeCount, 1372 wantBuffered: 0, 1373 }, 1374 } 1375 for _, tt := range tests { 1376 br := NewReaderSize(tt.r, tt.bufSize) 1377 if tt.peekSize > 0 { 1378 peekBuf, err := br.Peek(tt.peekSize) 1379 if err != nil { 1380 t.Errorf("%s: Peek(%d): %v", tt.name, tt.peekSize, err) 1381 continue 1382 } 1383 if len(peekBuf) != tt.peekSize { 1384 t.Errorf("%s: len(Peek(%d)) = %v; want %v", tt.name, tt.peekSize, len(peekBuf), tt.peekSize) 1385 continue 1386 } 1387 } 1388 discarded, err := br.Discard(tt.n) 1389 if ge, we := fmt.Sprint(err), fmt.Sprint(tt.wantErr); discarded != tt.want || ge != we { 1390 t.Errorf("%s: Discard(%d) = (%v, %v); want (%v, %v)", tt.name, tt.n, discarded, ge, tt.want, we) 1391 continue 1392 } 1393 if bn := br.Buffered(); bn != tt.wantBuffered { 1394 t.Errorf("%s: after Discard, Buffered = %d; want %d", tt.name, bn, tt.wantBuffered) 1395 } 1396 } 1397 1398 } 1399 1400 // An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have. 1401 type onlyReader struct { 1402 io.Reader 1403 } 1404 1405 // An onlyWriter only implements io.Writer, no matter what other methods the underlying implementation may have. 1406 type onlyWriter struct { 1407 io.Writer 1408 } 1409 1410 // A scriptedReader is an io.Reader that executes its steps sequentially. 1411 type scriptedReader []func(p []byte) (n int, err error) 1412 1413 func (sr *scriptedReader) Read(p []byte) (n int, err error) { 1414 if len(*sr) == 0 { 1415 panic("too many Read calls on scripted Reader. No steps remain.") 1416 } 1417 step := (*sr)[0] 1418 *sr = (*sr)[1:] 1419 return step(p) 1420 } 1421 1422 func newScriptedReader(steps ...func(p []byte) (n int, err error)) io.Reader { 1423 sr := scriptedReader(steps) 1424 return &sr 1425 } 1426 1427 func BenchmarkReaderCopyOptimal(b *testing.B) { 1428 // Optimal case is where the underlying reader implements io.WriterTo 1429 srcBuf := bytes.NewBuffer(make([]byte, 8192)) 1430 src := NewReader(srcBuf) 1431 dstBuf := new(bytes.Buffer) 1432 dst := onlyWriter{dstBuf} 1433 for i := 0; i < b.N; i++ { 1434 srcBuf.Reset() 1435 src.Reset(srcBuf) 1436 dstBuf.Reset() 1437 io.Copy(dst, src) 1438 } 1439 } 1440 1441 func BenchmarkReaderCopyUnoptimal(b *testing.B) { 1442 // Unoptimal case is where the underlying reader doesn't implement io.WriterTo 1443 srcBuf := bytes.NewBuffer(make([]byte, 8192)) 1444 src := NewReader(onlyReader{srcBuf}) 1445 dstBuf := new(bytes.Buffer) 1446 dst := onlyWriter{dstBuf} 1447 for i := 0; i < b.N; i++ { 1448 srcBuf.Reset() 1449 src.Reset(onlyReader{srcBuf}) 1450 dstBuf.Reset() 1451 io.Copy(dst, src) 1452 } 1453 } 1454 1455 func BenchmarkReaderCopyNoWriteTo(b *testing.B) { 1456 srcBuf := bytes.NewBuffer(make([]byte, 8192)) 1457 srcReader := NewReader(srcBuf) 1458 src := onlyReader{srcReader} 1459 dstBuf := new(bytes.Buffer) 1460 dst := onlyWriter{dstBuf} 1461 for i := 0; i < b.N; i++ { 1462 srcBuf.Reset() 1463 srcReader.Reset(srcBuf) 1464 dstBuf.Reset() 1465 io.Copy(dst, src) 1466 } 1467 } 1468 1469 func BenchmarkReaderWriteToOptimal(b *testing.B) { 1470 const bufSize = 16 << 10 1471 buf := make([]byte, bufSize) 1472 r := bytes.NewReader(buf) 1473 srcReader := NewReaderSize(onlyReader{r}, 1<<10) 1474 if _, ok := ioutil.Discard.(io.ReaderFrom); !ok { 1475 b.Fatal("ioutil.Discard doesn't support ReaderFrom") 1476 } 1477 for i := 0; i < b.N; i++ { 1478 r.Seek(0, 0) 1479 srcReader.Reset(onlyReader{r}) 1480 n, err := srcReader.WriteTo(ioutil.Discard) 1481 if err != nil { 1482 b.Fatal(err) 1483 } 1484 if n != bufSize { 1485 b.Fatalf("n = %d; want %d", n, bufSize) 1486 } 1487 } 1488 } 1489 1490 func BenchmarkWriterCopyOptimal(b *testing.B) { 1491 // Optimal case is where the underlying writer implements io.ReaderFrom 1492 srcBuf := bytes.NewBuffer(make([]byte, 8192)) 1493 src := onlyReader{srcBuf} 1494 dstBuf := new(bytes.Buffer) 1495 dst := NewWriter(dstBuf) 1496 for i := 0; i < b.N; i++ { 1497 srcBuf.Reset() 1498 dstBuf.Reset() 1499 dst.Reset(dstBuf) 1500 io.Copy(dst, src) 1501 } 1502 } 1503 1504 func BenchmarkWriterCopyUnoptimal(b *testing.B) { 1505 srcBuf := bytes.NewBuffer(make([]byte, 8192)) 1506 src := onlyReader{srcBuf} 1507 dstBuf := new(bytes.Buffer) 1508 dst := NewWriter(onlyWriter{dstBuf}) 1509 for i := 0; i < b.N; i++ { 1510 srcBuf.Reset() 1511 dstBuf.Reset() 1512 dst.Reset(onlyWriter{dstBuf}) 1513 io.Copy(dst, src) 1514 } 1515 } 1516 1517 func BenchmarkWriterCopyNoReadFrom(b *testing.B) { 1518 srcBuf := bytes.NewBuffer(make([]byte, 8192)) 1519 src := onlyReader{srcBuf} 1520 dstBuf := new(bytes.Buffer) 1521 dstWriter := NewWriter(dstBuf) 1522 dst := onlyWriter{dstWriter} 1523 for i := 0; i < b.N; i++ { 1524 srcBuf.Reset() 1525 dstBuf.Reset() 1526 dstWriter.Reset(dstBuf) 1527 io.Copy(dst, src) 1528 } 1529 } 1530 1531 func BenchmarkReaderEmpty(b *testing.B) { 1532 b.ReportAllocs() 1533 str := strings.Repeat("x", 16<<10) 1534 for i := 0; i < b.N; i++ { 1535 br := NewReader(strings.NewReader(str)) 1536 n, err := io.Copy(ioutil.Discard, br) 1537 if err != nil { 1538 b.Fatal(err) 1539 } 1540 if n != int64(len(str)) { 1541 b.Fatal("wrong length") 1542 } 1543 } 1544 } 1545 1546 func BenchmarkWriterEmpty(b *testing.B) { 1547 b.ReportAllocs() 1548 str := strings.Repeat("x", 1<<10) 1549 bs := []byte(str) 1550 for i := 0; i < b.N; i++ { 1551 bw := NewWriter(ioutil.Discard) 1552 bw.Flush() 1553 bw.WriteByte('a') 1554 bw.Flush() 1555 bw.WriteRune('B') 1556 bw.Flush() 1557 bw.Write(bs) 1558 bw.Flush() 1559 bw.WriteString(str) 1560 bw.Flush() 1561 } 1562 } 1563 1564 func BenchmarkWriterFlush(b *testing.B) { 1565 b.ReportAllocs() 1566 bw := NewWriter(ioutil.Discard) 1567 str := strings.Repeat("x", 50) 1568 for i := 0; i < b.N; i++ { 1569 bw.WriteString(str) 1570 bw.Flush() 1571 } 1572 }