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