...

Source file src/bufio/bufio_test.go

Documentation: bufio

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

View as plain text