From 9677a9f71777d75f3def0b0cb238050a30ec6a67 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Wed, 29 Jul 2015 01:26:10 -0800 Subject: Fixes mm-1355 adds rate limiting apis --- .../_workspace/src/gopkg.in/bufio.v1/.travis.yml | 11 - Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE | 27 - Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile | 2 - Godeps/_workspace/src/gopkg.in/bufio.v1/README.md | 4 - Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go | 413 ------ .../src/gopkg.in/bufio.v1/buffer_test.go | 527 -------- Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go | 728 ---------- .../_workspace/src/gopkg.in/bufio.v1/bufio_test.go | 1418 -------------------- .../src/gopkg.in/bufio.v1/export_test.go | 9 - 9 files changed, 3139 deletions(-) delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/README.md delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go delete mode 100644 Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go (limited to 'Godeps/_workspace/src/gopkg.in/bufio.v1') diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml b/Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml deleted file mode 100644 index ccca6bb4a..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: go - -go: - - 1.0 - - 1.1 - - 1.2 - - tip - -install: - - go get launchpad.net/gocheck - - go get gopkg.in/bufio.v1 diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE b/Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE deleted file mode 100644 index 07a316cbf..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/LICENSE +++ /dev/null @@ -1,27 +0,0 @@ -Copyright (c) 2013 The bufio Authors. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above -copyright notice, this list of conditions and the following disclaimer -in the documentation and/or other materials provided with the -distribution. - * Neither the name of Google Inc. nor the names of its -contributors may be used to endorse or promote products derived from -this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile b/Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile deleted file mode 100644 index 038ed47e9..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -all: - go test gopkg.in/bufio.v1 diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/README.md b/Godeps/_workspace/src/gopkg.in/bufio.v1/README.md deleted file mode 100644 index bfb85ee54..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/README.md +++ /dev/null @@ -1,4 +0,0 @@ -bufio -===== - -This is a fork of the http://golang.org/pkg/bufio/ package. It adds `ReadN` method that allows reading next `n` bytes from the internal buffer without allocating intermediate buffer. This method works just like the [Buffer.Next](http://golang.org/pkg/bytes/#Buffer.Next) method, but has slightly different signature. diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go deleted file mode 100644 index 8b915605b..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer.go +++ /dev/null @@ -1,413 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package bufio - -// Simple byte buffer for marshaling data. - -import ( - "bytes" - "errors" - "io" - "unicode/utf8" -) - -// A Buffer is a variable-sized buffer of bytes with Read and Write methods. -// The zero value for Buffer is an empty buffer ready to use. -type Buffer struct { - buf []byte // contents are the bytes buf[off : len(buf)] - off int // read at &buf[off], write at &buf[len(buf)] - runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune - bootstrap [64]byte // memory to hold first slice; helps small buffers (Printf) avoid allocation. - lastRead readOp // last read operation, so that Unread* can work correctly. -} - -// The readOp constants describe the last action performed on -// the buffer, so that UnreadRune and UnreadByte can -// check for invalid usage. -type readOp int - -const ( - opInvalid readOp = iota // Non-read operation. - opReadRune // Read rune. - opRead // Any other read operation. -) - -// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer. -var ErrTooLarge = errors.New("bytes.Buffer: too large") - -// Bytes returns a slice of the contents of the unread portion of the buffer; -// len(b.Bytes()) == b.Len(). If the caller changes the contents of the -// returned slice, the contents of the buffer will change provided there -// are no intervening method calls on the Buffer. -func (b *Buffer) Bytes() []byte { return b.buf[b.off:] } - -// String returns the contents of the unread portion of the buffer -// as a string. If the Buffer is a nil pointer, it returns "". -func (b *Buffer) String() string { - if b == nil { - // Special case, useful in debugging. - return "" - } - return string(b.buf[b.off:]) -} - -// Len returns the number of bytes of the unread portion of the buffer; -// b.Len() == len(b.Bytes()). -func (b *Buffer) Len() int { return len(b.buf) - b.off } - -// Truncate discards all but the first n unread bytes from the buffer. -// It panics if n is negative or greater than the length of the buffer. -func (b *Buffer) Truncate(n int) { - b.lastRead = opInvalid - switch { - case n < 0 || n > b.Len(): - panic("bytes.Buffer: truncation out of range") - case n == 0: - // Reuse buffer space. - b.off = 0 - } - b.buf = b.buf[0 : b.off+n] -} - -// Reset resets the buffer so it has no content. -// b.Reset() is the same as b.Truncate(0). -func (b *Buffer) Reset() { b.Truncate(0) } - -// grow grows the buffer to guarantee space for n more bytes. -// It returns the index where bytes should be written. -// If the buffer can't grow it will panic with ErrTooLarge. -func (b *Buffer) grow(n int) int { - m := b.Len() - // If buffer is empty, reset to recover space. - if m == 0 && b.off != 0 { - b.Truncate(0) - } - if len(b.buf)+n > cap(b.buf) { - var buf []byte - if b.buf == nil && n <= len(b.bootstrap) { - buf = b.bootstrap[0:] - } else if m+n <= cap(b.buf)/2 { - // We can slide things down instead of allocating a new - // slice. We only need m+n <= cap(b.buf) to slide, but - // we instead let capacity get twice as large so we - // don't spend all our time copying. - copy(b.buf[:], b.buf[b.off:]) - buf = b.buf[:m] - } else { - // not enough space anywhere - buf = makeSlice(2*cap(b.buf) + n) - copy(buf, b.buf[b.off:]) - } - b.buf = buf - b.off = 0 - } - b.buf = b.buf[0 : b.off+m+n] - return b.off + m -} - -// Grow grows the buffer's capacity, if necessary, to guarantee space for -// another n bytes. After Grow(n), at least n bytes can be written to the -// buffer without another allocation. -// If n is negative, Grow will panic. -// If the buffer can't grow it will panic with ErrTooLarge. -func (b *Buffer) Grow(n int) { - if n < 0 { - panic("bytes.Buffer.Grow: negative count") - } - m := b.grow(n) - b.buf = b.buf[0:m] -} - -// Write appends the contents of p to the buffer, growing the buffer as -// needed. The return value n is the length of p; err is always nil. If the -// buffer becomes too large, Write will panic with ErrTooLarge. -func (b *Buffer) Write(p []byte) (n int, err error) { - b.lastRead = opInvalid - m := b.grow(len(p)) - return copy(b.buf[m:], p), nil -} - -// WriteString appends the contents of s to the buffer, growing the buffer as -// needed. The return value n is the length of s; err is always nil. If the -// buffer becomes too large, WriteString will panic with ErrTooLarge. -func (b *Buffer) WriteString(s string) (n int, err error) { - b.lastRead = opInvalid - m := b.grow(len(s)) - return copy(b.buf[m:], s), nil -} - -// MinRead is the minimum slice size passed to a Read call by -// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond -// what is required to hold the contents of r, ReadFrom will not grow the -// underlying buffer. -const MinRead = 512 - -// ReadFrom reads data from r until EOF and appends it to the buffer, growing -// the buffer as needed. The return value n is the number of bytes read. Any -// error except io.EOF encountered during the read is also returned. If the -// buffer becomes too large, ReadFrom will panic with ErrTooLarge. -func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { - b.lastRead = opInvalid - // If buffer is empty, reset to recover space. - if b.off >= len(b.buf) { - b.Truncate(0) - } - for { - if free := cap(b.buf) - len(b.buf); free < MinRead { - // not enough space at end - newBuf := b.buf - if b.off+free < MinRead { - // not enough space using beginning of buffer; - // double buffer capacity - newBuf = makeSlice(2*cap(b.buf) + MinRead) - } - copy(newBuf, b.buf[b.off:]) - b.buf = newBuf[:len(b.buf)-b.off] - b.off = 0 - } - m, e := r.Read(b.buf[len(b.buf):cap(b.buf)]) - b.buf = b.buf[0 : len(b.buf)+m] - n += int64(m) - if e == io.EOF { - break - } - if e != nil { - return n, e - } - } - return n, nil // err is EOF, so return nil explicitly -} - -// makeSlice allocates a slice of size n. If the allocation fails, it panics -// with ErrTooLarge. -func makeSlice(n int) []byte { - // If the make fails, give a known error. - defer func() { - if recover() != nil { - panic(ErrTooLarge) - } - }() - return make([]byte, n) -} - -// WriteTo writes data to w until the buffer is drained or an error occurs. -// The return value n is the number of bytes written; it always fits into an -// int, but it is int64 to match the io.WriterTo interface. Any error -// encountered during the write is also returned. -func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { - b.lastRead = opInvalid - if b.off < len(b.buf) { - nBytes := b.Len() - m, e := w.Write(b.buf[b.off:]) - if m > nBytes { - panic("bytes.Buffer.WriteTo: invalid Write count") - } - b.off += m - n = int64(m) - if e != nil { - return n, e - } - // all bytes should have been written, by definition of - // Write method in io.Writer - if m != nBytes { - return n, io.ErrShortWrite - } - } - // Buffer is now empty; reset. - b.Truncate(0) - return -} - -// WriteByte appends the byte c to the buffer, growing the buffer as needed. -// The returned error is always nil, but is included to match bufio.Writer's -// WriteByte. If the buffer becomes too large, WriteByte will panic with -// ErrTooLarge. -func (b *Buffer) WriteByte(c byte) error { - b.lastRead = opInvalid - m := b.grow(1) - b.buf[m] = c - return nil -} - -// WriteRune appends the UTF-8 encoding of Unicode code point r to the -// buffer, returning its length and an error, which is always nil but is -// included to match bufio.Writer's WriteRune. The buffer is grown as needed; -// if it becomes too large, WriteRune will panic with ErrTooLarge. -func (b *Buffer) WriteRune(r rune) (n int, err error) { - if r < utf8.RuneSelf { - b.WriteByte(byte(r)) - return 1, nil - } - n = utf8.EncodeRune(b.runeBytes[0:], r) - b.Write(b.runeBytes[0:n]) - return n, nil -} - -// Read reads the next len(p) bytes from the buffer or until the buffer -// is drained. The return value n is the number of bytes read. If the -// buffer has no data to return, err is io.EOF (unless len(p) is zero); -// otherwise it is nil. -func (b *Buffer) Read(p []byte) (n int, err error) { - b.lastRead = opInvalid - if b.off >= len(b.buf) { - // Buffer is empty, reset to recover space. - b.Truncate(0) - if len(p) == 0 { - return - } - return 0, io.EOF - } - n = copy(p, b.buf[b.off:]) - b.off += n - if n > 0 { - b.lastRead = opRead - } - return -} - -// Next returns a slice containing the next n bytes from the buffer, -// advancing the buffer as if the bytes had been returned by Read. -// If there are fewer than n bytes in the buffer, Next returns the entire buffer. -// The slice is only valid until the next call to a read or write method. -func (b *Buffer) Next(n int) []byte { - b.lastRead = opInvalid - m := b.Len() - if n > m { - n = m - } - data := b.buf[b.off : b.off+n] - b.off += n - if n > 0 { - b.lastRead = opRead - } - return data -} - -// ReadByte reads and returns the next byte from the buffer. -// If no byte is available, it returns error io.EOF. -func (b *Buffer) ReadByte() (c byte, err error) { - b.lastRead = opInvalid - if b.off >= len(b.buf) { - // Buffer is empty, reset to recover space. - b.Truncate(0) - return 0, io.EOF - } - c = b.buf[b.off] - b.off++ - b.lastRead = opRead - return c, nil -} - -// ReadRune reads and returns the next UTF-8-encoded -// Unicode code point from the buffer. -// If no bytes are available, the error returned is io.EOF. -// If the bytes are an erroneous UTF-8 encoding, it -// consumes one byte and returns U+FFFD, 1. -func (b *Buffer) ReadRune() (r rune, size int, err error) { - b.lastRead = opInvalid - if b.off >= len(b.buf) { - // Buffer is empty, reset to recover space. - b.Truncate(0) - return 0, 0, io.EOF - } - b.lastRead = opReadRune - c := b.buf[b.off] - if c < utf8.RuneSelf { - b.off++ - return rune(c), 1, nil - } - r, n := utf8.DecodeRune(b.buf[b.off:]) - b.off += n - return r, n, nil -} - -// UnreadRune unreads the last rune returned by ReadRune. -// If the most recent read or write operation on the buffer was -// not a ReadRune, UnreadRune returns an error. (In this regard -// it is stricter than UnreadByte, which will unread the last byte -// from any read operation.) -func (b *Buffer) UnreadRune() error { - if b.lastRead != opReadRune { - return errors.New("bytes.Buffer: UnreadRune: previous operation was not ReadRune") - } - b.lastRead = opInvalid - if b.off > 0 { - _, n := utf8.DecodeLastRune(b.buf[0:b.off]) - b.off -= n - } - return nil -} - -// UnreadByte unreads the last byte returned by the most recent -// read operation. If write has happened since the last read, UnreadByte -// returns an error. -func (b *Buffer) UnreadByte() error { - if b.lastRead != opReadRune && b.lastRead != opRead { - return errors.New("bytes.Buffer: UnreadByte: previous operation was not a read") - } - b.lastRead = opInvalid - if b.off > 0 { - b.off-- - } - return nil -} - -// ReadBytes reads until the first occurrence of delim in the input, -// returning a slice containing the data up to and including the delimiter. -// If ReadBytes encounters an error before finding a delimiter, -// it returns the data read before the error and the error itself (often io.EOF). -// ReadBytes returns err != nil if and only if the returned data does not end in -// delim. -func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) { - slice, err := b.readSlice(delim) - // return a copy of slice. The buffer's backing array may - // be overwritten by later calls. - line = append(line, slice...) - return -} - -// readSlice is like ReadBytes but returns a reference to internal buffer data. -func (b *Buffer) readSlice(delim byte) (line []byte, err error) { - i := bytes.IndexByte(b.buf[b.off:], delim) - end := b.off + i + 1 - if i < 0 { - end = len(b.buf) - err = io.EOF - } - line = b.buf[b.off:end] - b.off = end - b.lastRead = opRead - return line, err -} - -// ReadString reads until the first occurrence of delim in the input, -// returning a string containing the data up to and including the delimiter. -// If ReadString encounters an error before finding a delimiter, -// it returns the data read before the error and the error itself (often io.EOF). -// ReadString returns err != nil if and only if the returned data does not end -// in delim. -func (b *Buffer) ReadString(delim byte) (line string, err error) { - slice, err := b.readSlice(delim) - return string(slice), err -} - -// NewBuffer creates and initializes a new Buffer using buf as its initial -// contents. It is intended to prepare a Buffer to read existing data. It -// can also be used to size the internal buffer for writing. To do that, -// buf should have the desired capacity but a length of zero. -// -// In most cases, new(Buffer) (or just declaring a Buffer variable) is -// sufficient to initialize a Buffer. -func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} } - -// NewBufferString creates and initializes a new Buffer using string s as its -// initial contents. It is intended to prepare a buffer to read an existing -// string. -// -// In most cases, new(Buffer) (or just declaring a Buffer variable) is -// sufficient to initialize a Buffer. -func NewBufferString(s string) *Buffer { - return &Buffer{buf: []byte(s)} -} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go deleted file mode 100644 index ca1ac2105..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/buffer_test.go +++ /dev/null @@ -1,527 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package bufio - -import ( - "bytes" - "io" - "math/rand" - "runtime" - "testing" - "unicode/utf8" -) - -const N = 10000 // make this bigger for a larger (and slower) test -var data string // test data for write tests -var testBytes []byte // test data; same as data but as a slice. - -func init() { - testBytes = make([]byte, N) - for i := 0; i < N; i++ { - testBytes[i] = 'a' + byte(i%26) - } - data = string(testBytes) -} - -// Verify that contents of buf match the string s. -func check(t *testing.T, testname string, buf *Buffer, s string) { - bytes := buf.Bytes() - str := buf.String() - if buf.Len() != len(bytes) { - t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes)) - } - - if buf.Len() != len(str) { - t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str)) - } - - if buf.Len() != len(s) { - t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s)) - } - - if string(bytes) != s { - t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s) - } -} - -// Fill buf through n writes of string fus. -// The initial contents of buf corresponds to the string s; -// the result is the final contents of buf returned as a string. -func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string { - check(t, testname+" (fill 1)", buf, s) - for ; n > 0; n-- { - m, err := buf.WriteString(fus) - if m != len(fus) { - t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus)) - } - if err != nil { - t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) - } - s += fus - check(t, testname+" (fill 4)", buf, s) - } - return s -} - -// Fill buf through n writes of byte slice fub. -// The initial contents of buf corresponds to the string s; -// the result is the final contents of buf returned as a string. -func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string { - check(t, testname+" (fill 1)", buf, s) - for ; n > 0; n-- { - m, err := buf.Write(fub) - if m != len(fub) { - t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub)) - } - if err != nil { - t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err) - } - s += string(fub) - check(t, testname+" (fill 4)", buf, s) - } - return s -} - -func TestNewBuffer(t *testing.T) { - buf := NewBuffer(testBytes) - check(t, "NewBuffer", buf, data) -} - -func TestNewBufferString(t *testing.T) { - buf := NewBufferString(data) - check(t, "NewBufferString", buf, data) -} - -// Empty buf through repeated reads into fub. -// The initial contents of buf corresponds to the string s. -func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) { - check(t, testname+" (empty 1)", buf, s) - - for { - n, err := buf.Read(fub) - if n == 0 { - break - } - if err != nil { - t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err) - } - s = s[n:] - check(t, testname+" (empty 3)", buf, s) - } - - check(t, testname+" (empty 4)", buf, "") -} - -func TestBasicOperations(t *testing.T) { - var buf Buffer - - for i := 0; i < 5; i++ { - check(t, "TestBasicOperations (1)", &buf, "") - - buf.Reset() - check(t, "TestBasicOperations (2)", &buf, "") - - buf.Truncate(0) - check(t, "TestBasicOperations (3)", &buf, "") - - n, err := buf.Write([]byte(data[0:1])) - if n != 1 { - t.Errorf("wrote 1 byte, but n == %d", n) - } - if err != nil { - t.Errorf("err should always be nil, but err == %s", err) - } - check(t, "TestBasicOperations (4)", &buf, "a") - - buf.WriteByte(data[1]) - check(t, "TestBasicOperations (5)", &buf, "ab") - - n, err = buf.Write([]byte(data[2:26])) - if n != 24 { - t.Errorf("wrote 25 bytes, but n == %d", n) - } - check(t, "TestBasicOperations (6)", &buf, string(data[0:26])) - - buf.Truncate(26) - check(t, "TestBasicOperations (7)", &buf, string(data[0:26])) - - buf.Truncate(20) - check(t, "TestBasicOperations (8)", &buf, string(data[0:20])) - - empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5)) - empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100)) - - buf.WriteByte(data[1]) - c, err := buf.ReadByte() - if err != nil { - t.Error("ReadByte unexpected eof") - } - if c != data[1] { - t.Errorf("ReadByte wrong value c=%v", c) - } - c, err = buf.ReadByte() - if err == nil { - t.Error("ReadByte unexpected not eof") - } - } -} - -func TestLargeStringWrites(t *testing.T) { - var buf Buffer - limit := 30 - if testing.Short() { - limit = 9 - } - for i := 3; i < limit; i += 3 { - s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data) - empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i)) - } - check(t, "TestLargeStringWrites (3)", &buf, "") -} - -func TestLargeByteWrites(t *testing.T) { - var buf Buffer - limit := 30 - if testing.Short() { - limit = 9 - } - for i := 3; i < limit; i += 3 { - s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes) - empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i)) - } - check(t, "TestLargeByteWrites (3)", &buf, "") -} - -func TestLargeStringReads(t *testing.T) { - var buf Buffer - for i := 3; i < 30; i += 3 { - s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i]) - empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) - } - check(t, "TestLargeStringReads (3)", &buf, "") -} - -func TestLargeByteReads(t *testing.T) { - var buf Buffer - for i := 3; i < 30; i += 3 { - s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) - empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data))) - } - check(t, "TestLargeByteReads (3)", &buf, "") -} - -func TestMixedReadsAndWrites(t *testing.T) { - var buf Buffer - s := "" - for i := 0; i < 50; i++ { - wlen := rand.Intn(len(data)) - if i%2 == 0 { - s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen]) - } else { - s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen]) - } - - rlen := rand.Intn(len(data)) - fub := make([]byte, rlen) - n, _ := buf.Read(fub) - s = s[n:] - } - empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len())) -} - -func TestNil(t *testing.T) { - var b *Buffer - if b.String() != "" { - t.Errorf("expected ; got %q", b.String()) - } -} - -func TestReadFrom(t *testing.T) { - var buf Buffer - for i := 3; i < 30; i += 3 { - s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) - var b Buffer - b.ReadFrom(&buf) - empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data))) - } -} - -func TestWriteTo(t *testing.T) { - var buf Buffer - for i := 3; i < 30; i += 3 { - s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i]) - var b Buffer - buf.WriteTo(&b) - empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data))) - } -} - -func TestRuneIO(t *testing.T) { - const NRune = 1000 - // Built a test slice while we write the data - b := make([]byte, utf8.UTFMax*NRune) - var buf Buffer - n := 0 - for r := rune(0); r < NRune; r++ { - size := utf8.EncodeRune(b[n:], r) - nbytes, err := buf.WriteRune(r) - if err != nil { - t.Fatalf("WriteRune(%U) error: %s", r, err) - } - if nbytes != size { - t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes) - } - n += size - } - b = b[0:n] - - // Check the resulting bytes - if !bytes.Equal(buf.Bytes(), b) { - t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b) - } - - p := make([]byte, utf8.UTFMax) - // Read it back with ReadRune - for r := rune(0); r < NRune; r++ { - size := utf8.EncodeRune(p, r) - nr, nbytes, err := buf.ReadRune() - if nr != r || nbytes != size || err != nil { - t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err) - } - } - - // Check that UnreadRune works - buf.Reset() - buf.Write(b) - for r := rune(0); r < NRune; r++ { - r1, size, _ := buf.ReadRune() - if err := buf.UnreadRune(); err != nil { - t.Fatalf("UnreadRune(%U) got error %q", r, err) - } - r2, nbytes, err := buf.ReadRune() - if r1 != r2 || r1 != r || nbytes != size || err != nil { - t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err) - } - } -} - -func TestNext(t *testing.T) { - b := []byte{0, 1, 2, 3, 4} - tmp := make([]byte, 5) - for i := 0; i <= 5; i++ { - for j := i; j <= 5; j++ { - for k := 0; k <= 6; k++ { - // 0 <= i <= j <= 5; 0 <= k <= 6 - // Check that if we start with a buffer - // of length j at offset i and ask for - // Next(k), we get the right bytes. - buf := NewBuffer(b[0:j]) - n, _ := buf.Read(tmp[0:i]) - if n != i { - t.Fatalf("Read %d returned %d", i, n) - } - bb := buf.Next(k) - want := k - if want > j-i { - want = j - i - } - if len(bb) != want { - t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb)) - } - for l, v := range bb { - if v != byte(l+i) { - t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i) - } - } - } - } - } -} - -var readBytesTests = []struct { - buffer string - delim byte - expected []string - err error -}{ - {"", 0, []string{""}, io.EOF}, - {"a\x00", 0, []string{"a\x00"}, nil}, - {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil}, - {"hello\x01world", 1, []string{"hello\x01"}, nil}, - {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF}, - {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil}, - {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF}, -} - -func TestReadBytes(t *testing.T) { - for _, test := range readBytesTests { - buf := NewBufferString(test.buffer) - var err error - for _, expected := range test.expected { - var bytes []byte - bytes, err = buf.ReadBytes(test.delim) - if string(bytes) != expected { - t.Errorf("expected %q, got %q", expected, bytes) - } - if err != nil { - break - } - } - if err != test.err { - t.Errorf("expected error %v, got %v", test.err, err) - } - } -} - -func TestReadString(t *testing.T) { - for _, test := range readBytesTests { - buf := NewBufferString(test.buffer) - var err error - for _, expected := range test.expected { - var s string - s, err = buf.ReadString(test.delim) - if s != expected { - t.Errorf("expected %q, got %q", expected, s) - } - if err != nil { - break - } - } - if err != test.err { - t.Errorf("expected error %v, got %v", test.err, err) - } - } -} - -func BenchmarkReadString(b *testing.B) { - const n = 32 << 10 - - data := make([]byte, n) - data[n-1] = 'x' - b.SetBytes(int64(n)) - for i := 0; i < b.N; i++ { - buf := NewBuffer(data) - _, err := buf.ReadString('x') - if err != nil { - b.Fatal(err) - } - } -} - -func TestGrow(t *testing.T) { - x := []byte{'x'} - y := []byte{'y'} - tmp := make([]byte, 72) - for _, startLen := range []int{0, 100, 1000, 10000, 100000} { - xBytes := bytes.Repeat(x, startLen) - for _, growLen := range []int{0, 100, 1000, 10000, 100000} { - buf := NewBuffer(xBytes) - // If we read, this affects buf.off, which is good to test. - readBytes, _ := buf.Read(tmp) - buf.Grow(growLen) - yBytes := bytes.Repeat(y, growLen) - // Check no allocation occurs in write, as long as we're single-threaded. - var m1, m2 runtime.MemStats - runtime.ReadMemStats(&m1) - buf.Write(yBytes) - runtime.ReadMemStats(&m2) - if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs { - t.Errorf("allocation occurred during write") - } - // Check that buffer has correct data. - if !bytes.Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) { - t.Errorf("bad initial data at %d %d", startLen, growLen) - } - if !bytes.Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) { - t.Errorf("bad written data at %d %d", startLen, growLen) - } - } - } -} - -// Was a bug: used to give EOF reading empty slice at EOF. -func TestReadEmptyAtEOF(t *testing.T) { - b := new(Buffer) - slice := make([]byte, 0) - n, err := b.Read(slice) - if err != nil { - t.Errorf("read error: %v", err) - } - if n != 0 { - t.Errorf("wrong count; got %d want 0", n) - } -} - -func TestBufferUnreadByte(t *testing.T) { - b := new(Buffer) - b.WriteString("abcdefghijklmnopqrstuvwxyz") - - _, err := b.ReadBytes('m') - if err != nil { - t.Fatalf("ReadBytes: %v", err) - } - - err = b.UnreadByte() - if err != nil { - t.Fatalf("UnreadByte: %v", err) - } - c, err := b.ReadByte() - if err != nil { - t.Fatalf("ReadByte: %v", err) - } - if c != 'm' { - t.Errorf("ReadByte = %q; want %q", c, 'm') - } -} - -// Tests that we occasionally compact. Issue 5154. -func TestBufferGrowth(t *testing.T) { - var b Buffer - buf := make([]byte, 1024) - b.Write(buf[0:1]) - var cap0 int - for i := 0; i < 5<<10; i++ { - b.Write(buf) - b.Read(buf) - if i == 0 { - cap0 = b.Cap() - } - } - cap1 := b.Cap() - // (*Buffer).grow allows for 2x capacity slop before sliding, - // so set our error threshold at 3x. - if cap1 > cap0*3 { - t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0) - } -} - -// From Issue 5154. -func BenchmarkBufferNotEmptyWriteRead(b *testing.B) { - buf := make([]byte, 1024) - for i := 0; i < b.N; i++ { - var b Buffer - b.Write(buf[0:1]) - for i := 0; i < 5<<10; i++ { - b.Write(buf) - b.Read(buf) - } - } -} - -// Check that we don't compact too often. From Issue 5154. -func BenchmarkBufferFullSmallReads(b *testing.B) { - buf := make([]byte, 1024) - for i := 0; i < b.N; i++ { - var b Buffer - b.Write(buf) - for b.Len()+20 < b.Cap() { - b.Write(buf[:10]) - } - for i := 0; i < 5<<10; i++ { - b.Read(buf[:1]) - b.Write(buf[:1]) - } - } -} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go deleted file mode 100644 index 8f5cdc084..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio.go +++ /dev/null @@ -1,728 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer -// object, creating another object (Reader or Writer) that also implements -// the interface but provides buffering and some help for textual I/O. -package bufio - -import ( - "bytes" - "errors" - "io" - "unicode/utf8" -) - -const ( - defaultBufSize = 4096 -) - -var ( - ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte") - ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune") - ErrBufferFull = errors.New("bufio: buffer full") - ErrNegativeCount = errors.New("bufio: negative count") -) - -// Buffered input. - -// Reader implements buffering for an io.Reader object. -type Reader struct { - buf []byte - rd io.Reader - r, w int - err error - lastByte int - lastRuneSize int -} - -const minReadBufferSize = 16 -const maxConsecutiveEmptyReads = 100 - -// NewReaderSize returns a new Reader whose buffer has at least the specified -// size. If the argument io.Reader is already a Reader with large enough -// size, it returns the underlying Reader. -func NewReaderSize(rd io.Reader, size int) *Reader { - // Is it already a Reader? - b, ok := rd.(*Reader) - if ok && len(b.buf) >= size { - return b - } - if size < minReadBufferSize { - size = minReadBufferSize - } - r := new(Reader) - r.reset(make([]byte, size), rd) - return r -} - -// NewReader returns a new Reader whose buffer has the default size. -func NewReader(rd io.Reader) *Reader { - return NewReaderSize(rd, defaultBufSize) -} - -// Reset discards any buffered data, resets all state, and switches -// the buffered reader to read from r. -func (b *Reader) Reset(r io.Reader) { - b.reset(b.buf, r) -} - -func (b *Reader) reset(buf []byte, r io.Reader) { - *b = Reader{ - buf: buf, - rd: r, - lastByte: -1, - lastRuneSize: -1, - } -} - -var errNegativeRead = errors.New("bufio: reader returned negative count from Read") - -// fill reads a new chunk into the buffer. -func (b *Reader) fill() { - // Slide existing data to beginning. - if b.r > 0 { - copy(b.buf, b.buf[b.r:b.w]) - b.w -= b.r - b.r = 0 - } - - if b.w >= len(b.buf) { - panic("bufio: tried to fill full buffer") - } - - // Read new data: try a limited number of times. - for i := maxConsecutiveEmptyReads; i > 0; i-- { - n, err := b.rd.Read(b.buf[b.w:]) - if n < 0 { - panic(errNegativeRead) - } - b.w += n - if err != nil { - b.err = err - return - } - if n > 0 { - return - } - } - b.err = io.ErrNoProgress -} - -func (b *Reader) readErr() error { - err := b.err - b.err = nil - return err -} - -// Peek returns the next n bytes without advancing the reader. The bytes stop -// being valid at the next read call. If Peek returns fewer than n bytes, it -// also returns an error explaining why the read is short. The error is -// ErrBufferFull if n is larger than b's buffer size. -func (b *Reader) Peek(n int) ([]byte, error) { - if n < 0 { - return nil, ErrNegativeCount - } - if n > len(b.buf) { - return nil, ErrBufferFull - } - // 0 <= n <= len(b.buf) - for b.w-b.r < n && b.err == nil { - b.fill() // b.w-b.r < len(b.buf) => buffer is not full - } - m := b.w - b.r - if m > n { - m = n - } - var err error - if m < n { - err = b.readErr() - if err == nil { - err = ErrBufferFull - } - } - return b.buf[b.r : b.r+m], err -} - -// Read reads data into p. -// It returns the number of bytes read into p. -// It calls Read at most once on the underlying Reader, -// hence n may be less than len(p). -// At EOF, the count will be zero and err will be io.EOF. -func (b *Reader) Read(p []byte) (n int, err error) { - n = len(p) - if n == 0 { - return 0, b.readErr() - } - if b.r == b.w { - if b.err != nil { - return 0, b.readErr() - } - if len(p) >= len(b.buf) { - // Large read, empty buffer. - // Read directly into p to avoid copy. - n, b.err = b.rd.Read(p) - if n < 0 { - panic(errNegativeRead) - } - if n > 0 { - b.lastByte = int(p[n-1]) - b.lastRuneSize = -1 - } - return n, b.readErr() - } - b.fill() // buffer is empty - if b.w == b.r { - return 0, b.readErr() - } - } - - if n > b.w-b.r { - n = b.w - b.r - } - copy(p[0:n], b.buf[b.r:]) - b.r += n - b.lastByte = int(b.buf[b.r-1]) - b.lastRuneSize = -1 - return n, nil -} - -// ReadByte reads and returns a single byte. -// If no byte is available, returns an error. -func (b *Reader) ReadByte() (c byte, err error) { - b.lastRuneSize = -1 - for b.r == b.w { - if b.err != nil { - return 0, b.readErr() - } - b.fill() // buffer is empty - } - c = b.buf[b.r] - b.r++ - b.lastByte = int(c) - return c, nil -} - -// UnreadByte unreads the last byte. Only the most recently read byte can be unread. -func (b *Reader) UnreadByte() error { - if b.lastByte < 0 || b.r == 0 && b.w > 0 { - return ErrInvalidUnreadByte - } - // b.r > 0 || b.w == 0 - if b.r > 0 { - b.r-- - } else { - // b.r == 0 && b.w == 0 - b.w = 1 - } - b.buf[b.r] = byte(b.lastByte) - b.lastByte = -1 - b.lastRuneSize = -1 - return nil -} - -// ReadRune reads a single UTF-8 encoded Unicode character and returns the -// rune and its size in bytes. If the encoded rune is invalid, it consumes one byte -// and returns unicode.ReplacementChar (U+FFFD) with a size of 1. -func (b *Reader) ReadRune() (r rune, size int, err error) { - for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil && b.w-b.r < len(b.buf) { - b.fill() // b.w-b.r < len(buf) => buffer is not full - } - b.lastRuneSize = -1 - if b.r == b.w { - return 0, 0, b.readErr() - } - r, size = rune(b.buf[b.r]), 1 - if r >= 0x80 { - r, size = utf8.DecodeRune(b.buf[b.r:b.w]) - } - b.r += size - b.lastByte = int(b.buf[b.r-1]) - b.lastRuneSize = size - return r, size, nil -} - -// UnreadRune unreads the last rune. If the most recent read operation on -// the buffer was not a ReadRune, UnreadRune returns an error. (In this -// regard it is stricter than UnreadByte, which will unread the last byte -// from any read operation.) -func (b *Reader) UnreadRune() error { - if b.lastRuneSize < 0 || b.r < b.lastRuneSize { - return ErrInvalidUnreadRune - } - b.r -= b.lastRuneSize - b.lastByte = -1 - b.lastRuneSize = -1 - return nil -} - -// Buffered returns the number of bytes that can be read from the current buffer. -func (b *Reader) Buffered() int { return b.w - b.r } - -// ReadSlice reads until the first occurrence of delim in the input, -// returning a slice pointing at the bytes in the buffer. -// The bytes stop being valid at the next read. -// If ReadSlice encounters an error before finding a delimiter, -// it returns all the data in the buffer and the error itself (often io.EOF). -// ReadSlice fails with error ErrBufferFull if the buffer fills without a delim. -// Because the data returned from ReadSlice will be overwritten -// by the next I/O operation, most clients should use -// ReadBytes or ReadString instead. -// ReadSlice returns err != nil if and only if line does not end in delim. -func (b *Reader) ReadSlice(delim byte) (line []byte, err error) { - for { - // Search buffer. - if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 { - line = b.buf[b.r : b.r+i+1] - b.r += i + 1 - break - } - - // Pending error? - if b.err != nil { - line = b.buf[b.r:b.w] - b.r = b.w - err = b.readErr() - break - } - - // Buffer full? - if n := b.Buffered(); n >= len(b.buf) { - b.r = b.w - line = b.buf - err = ErrBufferFull - break - } - - b.fill() // buffer is not full - } - - // Handle last byte, if any. - if i := len(line) - 1; i >= 0 { - b.lastByte = int(line[i]) - } - - return -} - -// ReadN tries to read exactly n bytes. -// The bytes stop being valid at the next read call. -// If ReadN encounters an error before reading n bytes, -// it returns all the data in the buffer and the error itself (often io.EOF). -// ReadN fails with error ErrBufferFull if the buffer fills -// without reading N bytes. -// Because the data returned from ReadN will be overwritten -// by the next I/O operation, most clients should use -// ReadBytes or ReadString instead. -func (b *Reader) ReadN(n int) ([]byte, error) { - for b.Buffered() < n { - if b.err != nil { - buf := b.buf[b.r:b.w] - b.r = b.w - return buf, b.readErr() - } - - // Buffer is full? - if b.Buffered() >= len(b.buf) { - b.r = b.w - return b.buf, ErrBufferFull - } - - b.fill() - } - buf := b.buf[b.r : b.r+n] - b.r += n - return buf, nil -} - -// ReadLine is a low-level line-reading primitive. Most callers should use -// ReadBytes('\n') or ReadString('\n') instead or use a Scanner. -// -// ReadLine tries to return a single line, not including the end-of-line bytes. -// If the line was too long for the buffer then isPrefix is set and the -// beginning of the line is returned. The rest of the line will be returned -// from future calls. isPrefix will be false when returning the last fragment -// of the line. The returned buffer is only valid until the next call to -// ReadLine. ReadLine either returns a non-nil line or it returns an error, -// never both. -// -// The text returned from ReadLine does not include the line end ("\r\n" or "\n"). -// No indication or error is given if the input ends without a final line end. -// Calling UnreadByte after ReadLine will always unread the last byte read -// (possibly a character belonging to the line end) even if that byte is not -// part of the line returned by ReadLine. -func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error) { - line, err = b.ReadSlice('\n') - if err == ErrBufferFull { - // Handle the case where "\r\n" straddles the buffer. - if len(line) > 0 && line[len(line)-1] == '\r' { - // Put the '\r' back on buf and drop it from line. - // Let the next call to ReadLine check for "\r\n". - if b.r == 0 { - // should be unreachable - panic("bufio: tried to rewind past start of buffer") - } - b.r-- - line = line[:len(line)-1] - } - return line, true, nil - } - - if len(line) == 0 { - if err != nil { - line = nil - } - return - } - err = nil - - if line[len(line)-1] == '\n' { - drop := 1 - if len(line) > 1 && line[len(line)-2] == '\r' { - drop = 2 - } - line = line[:len(line)-drop] - } - return -} - -// ReadBytes reads until the first occurrence of delim in the input, -// returning a slice containing the data up to and including the delimiter. -// If ReadBytes encounters an error before finding a delimiter, -// it returns the data read before the error and the error itself (often io.EOF). -// ReadBytes returns err != nil if and only if the returned data does not end in -// delim. -// For simple uses, a Scanner may be more convenient. -func (b *Reader) ReadBytes(delim byte) (line []byte, err error) { - // Use ReadSlice to look for array, - // accumulating full buffers. - var frag []byte - var full [][]byte - err = nil - - for { - var e error - frag, e = b.ReadSlice(delim) - if e == nil { // got final fragment - break - } - if e != ErrBufferFull { // unexpected error - err = e - break - } - - // Make a copy of the buffer. - buf := make([]byte, len(frag)) - copy(buf, frag) - full = append(full, buf) - } - - // Allocate new buffer to hold the full pieces and the fragment. - n := 0 - for i := range full { - n += len(full[i]) - } - n += len(frag) - - // Copy full pieces and fragment in. - buf := make([]byte, n) - n = 0 - for i := range full { - n += copy(buf[n:], full[i]) - } - copy(buf[n:], frag) - return buf, err -} - -// ReadString reads until the first occurrence of delim in the input, -// returning a string containing the data up to and including the delimiter. -// If ReadString encounters an error before finding a delimiter, -// it returns the data read before the error and the error itself (often io.EOF). -// ReadString returns err != nil if and only if the returned data does not end in -// delim. -// For simple uses, a Scanner may be more convenient. -func (b *Reader) ReadString(delim byte) (line string, err error) { - bytes, err := b.ReadBytes(delim) - line = string(bytes) - return line, err -} - -// WriteTo implements io.WriterTo. -func (b *Reader) WriteTo(w io.Writer) (n int64, err error) { - n, err = b.writeBuf(w) - if err != nil { - return - } - - if r, ok := b.rd.(io.WriterTo); ok { - m, err := r.WriteTo(w) - n += m - return n, err - } - - if w, ok := w.(io.ReaderFrom); ok { - m, err := w.ReadFrom(b.rd) - n += m - return n, err - } - - if b.w-b.r < len(b.buf) { - b.fill() // buffer not full - } - - for b.r < b.w { - // b.r < b.w => buffer is not empty - m, err := b.writeBuf(w) - n += m - if err != nil { - return n, err - } - b.fill() // buffer is empty - } - - if b.err == io.EOF { - b.err = nil - } - - return n, b.readErr() -} - -// writeBuf writes the Reader's buffer to the writer. -func (b *Reader) writeBuf(w io.Writer) (int64, error) { - n, err := w.Write(b.buf[b.r:b.w]) - if n < b.r-b.w { - panic(errors.New("bufio: writer did not write all data")) - } - b.r += n - return int64(n), err -} - -// buffered output - -// Writer implements buffering for an io.Writer object. -// If an error occurs writing to a Writer, no more data will be -// accepted and all subsequent writes will return the error. -// After all data has been written, the client should call the -// Flush method to guarantee all data has been forwarded to -// the underlying io.Writer. -type Writer struct { - err error - buf []byte - n int - wr io.Writer -} - -// NewWriterSize returns a new Writer whose buffer has at least the specified -// size. If the argument io.Writer is already a Writer with large enough -// size, it returns the underlying Writer. -func NewWriterSize(w io.Writer, size int) *Writer { - // Is it already a Writer? - b, ok := w.(*Writer) - if ok && len(b.buf) >= size { - return b - } - if size <= 0 { - size = defaultBufSize - } - return &Writer{ - buf: make([]byte, size), - wr: w, - } -} - -// NewWriter returns a new Writer whose buffer has the default size. -func NewWriter(w io.Writer) *Writer { - return NewWriterSize(w, defaultBufSize) -} - -// Reset discards any unflushed buffered data, clears any error, and -// resets b to write its output to w. -func (b *Writer) Reset(w io.Writer) { - b.err = nil - b.n = 0 - b.wr = w -} - -// Flush writes any buffered data to the underlying io.Writer. -func (b *Writer) Flush() error { - err := b.flush() - return err -} - -func (b *Writer) flush() error { - if b.err != nil { - return b.err - } - if b.n == 0 { - return nil - } - n, err := b.wr.Write(b.buf[0:b.n]) - if n < b.n && err == nil { - err = io.ErrShortWrite - } - if err != nil { - if n > 0 && n < b.n { - copy(b.buf[0:b.n-n], b.buf[n:b.n]) - } - b.n -= n - b.err = err - return err - } - b.n = 0 - return nil -} - -// Available returns how many bytes are unused in the buffer. -func (b *Writer) Available() int { return len(b.buf) - b.n } - -// Buffered returns the number of bytes that have been written into the current buffer. -func (b *Writer) Buffered() int { return b.n } - -// Write writes the contents of p into the buffer. -// It returns the number of bytes written. -// If nn < len(p), it also returns an error explaining -// why the write is short. -func (b *Writer) Write(p []byte) (nn int, err error) { - for len(p) > b.Available() && b.err == nil { - var n int - if b.Buffered() == 0 { - // Large write, empty buffer. - // Write directly from p to avoid copy. - n, b.err = b.wr.Write(p) - } else { - n = copy(b.buf[b.n:], p) - b.n += n - b.flush() - } - nn += n - p = p[n:] - } - if b.err != nil { - return nn, b.err - } - n := copy(b.buf[b.n:], p) - b.n += n - nn += n - return nn, nil -} - -// WriteByte writes a single byte. -func (b *Writer) WriteByte(c byte) error { - if b.err != nil { - return b.err - } - if b.Available() <= 0 && b.flush() != nil { - return b.err - } - b.buf[b.n] = c - b.n++ - return nil -} - -// WriteRune writes a single Unicode code point, returning -// the number of bytes written and any error. -func (b *Writer) WriteRune(r rune) (size int, err error) { - if r < utf8.RuneSelf { - err = b.WriteByte(byte(r)) - if err != nil { - return 0, err - } - return 1, nil - } - if b.err != nil { - return 0, b.err - } - n := b.Available() - if n < utf8.UTFMax { - if b.flush(); b.err != nil { - return 0, b.err - } - n = b.Available() - if n < utf8.UTFMax { - // Can only happen if buffer is silly small. - return b.WriteString(string(r)) - } - } - size = utf8.EncodeRune(b.buf[b.n:], r) - b.n += size - return size, nil -} - -// WriteString writes a string. -// It returns the number of bytes written. -// If the count is less than len(s), it also returns an error explaining -// why the write is short. -func (b *Writer) WriteString(s string) (int, error) { - nn := 0 - for len(s) > b.Available() && b.err == nil { - n := copy(b.buf[b.n:], s) - b.n += n - nn += n - s = s[n:] - b.flush() - } - if b.err != nil { - return nn, b.err - } - n := copy(b.buf[b.n:], s) - b.n += n - nn += n - return nn, nil -} - -// ReadFrom implements io.ReaderFrom. -func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) { - if b.Buffered() == 0 { - if w, ok := b.wr.(io.ReaderFrom); ok { - return w.ReadFrom(r) - } - } - var m int - for { - if b.Available() == 0 { - if err1 := b.flush(); err1 != nil { - return n, err1 - } - } - nr := 0 - for nr < maxConsecutiveEmptyReads { - m, err = r.Read(b.buf[b.n:]) - if m != 0 || err != nil { - break - } - nr++ - } - if nr == maxConsecutiveEmptyReads { - return n, io.ErrNoProgress - } - b.n += m - n += int64(m) - if err != nil { - break - } - } - if err == io.EOF { - // If we filled the buffer exactly, flush pre-emptively. - if b.Available() == 0 { - err = b.flush() - } else { - err = nil - } - } - return n, err -} - -// buffered input and output - -// ReadWriter stores pointers to a Reader and a Writer. -// It implements io.ReadWriter. -type ReadWriter struct { - *Reader - *Writer -} - -// NewReadWriter allocates a new ReadWriter that dispatches to r and w. -func NewReadWriter(r *Reader, w *Writer) *ReadWriter { - return &ReadWriter{r, w} -} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go deleted file mode 100644 index f19d9bd28..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/bufio_test.go +++ /dev/null @@ -1,1418 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package bufio_test - -import ( - "bytes" - "errors" - "fmt" - "io" - "io/ioutil" - "strings" - "testing" - "testing/iotest" - "time" - "unicode/utf8" - - . "gopkg.in/bufio.v1" -) - -// Reads from a reader and rot13s the result. -type rot13Reader struct { - r io.Reader -} - -func newRot13Reader(r io.Reader) *rot13Reader { - r13 := new(rot13Reader) - r13.r = r - return r13 -} - -func (r13 *rot13Reader) Read(p []byte) (int, error) { - n, err := r13.r.Read(p) - if err != nil { - return n, err - } - for i := 0; i < n; i++ { - c := p[i] | 0x20 // lowercase byte - if 'a' <= c && c <= 'm' { - p[i] += 13 - } else if 'n' <= c && c <= 'z' { - p[i] -= 13 - } - } - return n, nil -} - -// Call ReadByte to accumulate the text of a file -func readBytes(buf *Reader) string { - var b [1000]byte - nb := 0 - for { - c, err := buf.ReadByte() - if err == io.EOF { - break - } - if err == nil { - b[nb] = c - nb++ - } else if err != iotest.ErrTimeout { - panic("Data: " + err.Error()) - } - } - return string(b[0:nb]) -} - -func TestReaderSimple(t *testing.T) { - data := "hello world" - b := NewReader(strings.NewReader(data)) - if s := readBytes(b); s != "hello world" { - t.Errorf("simple hello world test failed: got %q", s) - } - - b = NewReader(newRot13Reader(strings.NewReader(data))) - if s := readBytes(b); s != "uryyb jbeyq" { - t.Errorf("rot13 hello world test failed: got %q", s) - } -} - -type readMaker struct { - name string - fn func(io.Reader) io.Reader -} - -var readMakers = []readMaker{ - {"full", func(r io.Reader) io.Reader { return r }}, - {"byte", iotest.OneByteReader}, - {"half", iotest.HalfReader}, - {"data+err", iotest.DataErrReader}, - {"timeout", iotest.TimeoutReader}, -} - -// Call ReadString (which ends up calling everything else) -// to accumulate the text of a file. -func readLines(b *Reader) string { - s := "" - for { - s1, err := b.ReadString('\n') - if err == io.EOF { - break - } - if err != nil && err != iotest.ErrTimeout { - panic("GetLines: " + err.Error()) - } - s += s1 - } - return s -} - -// Call Read to accumulate the text of a file -func reads(buf *Reader, m int) string { - var b [1000]byte - nb := 0 - for { - n, err := buf.Read(b[nb : nb+m]) - nb += n - if err == io.EOF { - break - } - } - return string(b[0:nb]) -} - -type bufReader struct { - name string - fn func(*Reader) string -} - -var bufreaders = []bufReader{ - {"1", func(b *Reader) string { return reads(b, 1) }}, - {"2", func(b *Reader) string { return reads(b, 2) }}, - {"3", func(b *Reader) string { return reads(b, 3) }}, - {"4", func(b *Reader) string { return reads(b, 4) }}, - {"5", func(b *Reader) string { return reads(b, 5) }}, - {"7", func(b *Reader) string { return reads(b, 7) }}, - {"bytes", readBytes}, - {"lines", readLines}, -} - -const minReadBufferSize = 16 - -var bufsizes = []int{ - 0, minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096, -} - -func TestReader(t *testing.T) { - var texts [31]string - str := "" - all := "" - for i := 0; i < len(texts)-1; i++ { - texts[i] = str + "\n" - all += texts[i] - str += string(i%26 + 'a') - } - texts[len(texts)-1] = all - - for h := 0; h < len(texts); h++ { - text := texts[h] - for i := 0; i < len(readMakers); i++ { - for j := 0; j < len(bufreaders); j++ { - for k := 0; k < len(bufsizes); k++ { - readmaker := readMakers[i] - bufreader := bufreaders[j] - bufsize := bufsizes[k] - read := readmaker.fn(strings.NewReader(text)) - buf := NewReaderSize(read, bufsize) - s := bufreader.fn(buf) - if s != text { - t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q", - readmaker.name, bufreader.name, bufsize, text, s) - } - } - } - } - } -} - -type zeroReader struct{} - -func (zeroReader) Read(p []byte) (int, error) { - return 0, nil -} - -func TestZeroReader(t *testing.T) { - var z zeroReader - r := NewReader(z) - - c := make(chan error) - go func() { - _, err := r.ReadByte() - c <- err - }() - - select { - case err := <-c: - if err == nil { - t.Error("error expected") - } else if err != io.ErrNoProgress { - t.Error("unexpected error:", err) - } - case <-time.After(time.Second): - t.Error("test timed out (endless loop in ReadByte?)") - } -} - -// A StringReader delivers its data one string segment at a time via Read. -type StringReader struct { - data []string - step int -} - -func (r *StringReader) Read(p []byte) (n int, err error) { - if r.step < len(r.data) { - s := r.data[r.step] - n = copy(p, s) - r.step++ - } else { - err = io.EOF - } - return -} - -func readRuneSegments(t *testing.T, segments []string) { - got := "" - want := strings.Join(segments, "") - r := NewReader(&StringReader{data: segments}) - for { - r, _, err := r.ReadRune() - if err != nil { - if err != io.EOF { - return - } - break - } - got += string(r) - } - if got != want { - t.Errorf("segments=%v got=%s want=%s", segments, got, want) - } -} - -var segmentList = [][]string{ - {}, - {""}, - {"日", "本語"}, - {"\u65e5", "\u672c", "\u8a9e"}, - {"\U000065e5", "\U0000672c", "\U00008a9e"}, - {"\xe6", "\x97\xa5\xe6", "\x9c\xac\xe8\xaa\x9e"}, - {"Hello", ", ", "World", "!"}, - {"Hello", ", ", "", "World", "!"}, -} - -func TestReadRune(t *testing.T) { - for _, s := range segmentList { - readRuneSegments(t, s) - } -} - -func TestUnreadRune(t *testing.T) { - segments := []string{"Hello, world:", "日本語"} - r := NewReader(&StringReader{data: segments}) - got := "" - want := strings.Join(segments, "") - // Normal execution. - for { - r1, _, err := r.ReadRune() - if err != nil { - if err != io.EOF { - t.Error("unexpected error on ReadRune:", err) - } - break - } - got += string(r1) - // Put it back and read it again. - if err = r.UnreadRune(); err != nil { - t.Fatal("unexpected error on UnreadRune:", err) - } - r2, _, err := r.ReadRune() - if err != nil { - t.Fatal("unexpected error reading after unreading:", err) - } - if r1 != r2 { - t.Fatalf("incorrect rune after unread: got %c, want %c", r1, r2) - } - } - if got != want { - t.Errorf("got %q, want %q", got, want) - } -} - -func TestReaderUnreadByte(t *testing.T) { - segments := []string{"Hello, ", "world"} - r := NewReader(&StringReader{data: segments}) - got := "" - want := strings.Join(segments, "") - // Normal execution. - for { - b1, err := r.ReadByte() - if err != nil { - if err != io.EOF { - t.Error("unexpected error on ReadByte:", err) - } - break - } - got += string(b1) - // Put it back and read it again. - if err = r.UnreadByte(); err != nil { - t.Fatal("unexpected error on UnreadByte:", err) - } - b2, err := r.ReadByte() - if err != nil { - t.Fatal("unexpected error reading after unreading:", err) - } - if b1 != b2 { - t.Fatalf("incorrect byte after unread: got %q, want %q", b1, b2) - } - } - if got != want { - t.Errorf("got %q, want %q", got, want) - } -} - -func TestUnreadByteMultiple(t *testing.T) { - segments := []string{"Hello, ", "world"} - data := strings.Join(segments, "") - for n := 0; n <= len(data); n++ { - r := NewReader(&StringReader{data: segments}) - // Read n bytes. - for i := 0; i < n; i++ { - b, err := r.ReadByte() - if err != nil { - t.Fatalf("n = %d: unexpected error on ReadByte: %v", n, err) - } - if b != data[i] { - t.Fatalf("n = %d: incorrect byte returned from ReadByte: got %q, want %q", n, b, data[i]) - } - } - // Unread one byte if there is one. - if n > 0 { - if err := r.UnreadByte(); err != nil { - t.Errorf("n = %d: unexpected error on UnreadByte: %v", n, err) - } - } - // Test that we cannot unread any further. - if err := r.UnreadByte(); err == nil { - t.Errorf("n = %d: expected error on UnreadByte", n) - } - } -} - -func TestUnreadByteOthers(t *testing.T) { - // A list of readers to use in conjunction with UnreadByte. - var readers = []func(*Reader, byte) ([]byte, error){ - (*Reader).ReadBytes, - (*Reader).ReadSlice, - func(r *Reader, delim byte) ([]byte, error) { - data, err := r.ReadString(delim) - return []byte(data), err - }, - // ReadLine doesn't fit the data/pattern easily - // so we leave it out. It should be covered via - // the ReadSlice test since ReadLine simply calls - // ReadSlice, and it's that function that handles - // the last byte. - } - - // Try all readers with UnreadByte. - for rno, read := range readers { - // Some input data that is longer than the minimum reader buffer size. - const n = 10 - var buf bytes.Buffer - for i := 0; i < n; i++ { - buf.WriteString("abcdefg") - } - - r := NewReaderSize(&buf, minReadBufferSize) - readTo := func(delim byte, want string) { - data, err := read(r, delim) - if err != nil { - t.Fatalf("#%d: unexpected error reading to %c: %v", rno, delim, err) - } - if got := string(data); got != want { - t.Fatalf("#%d: got %q, want %q", rno, got, want) - } - } - - // Read the data with occasional UnreadByte calls. - for i := 0; i < n; i++ { - readTo('d', "abcd") - for j := 0; j < 3; j++ { - if err := r.UnreadByte(); err != nil { - t.Fatalf("#%d: unexpected error on UnreadByte: %v", rno, err) - } - readTo('d', "d") - } - readTo('g', "efg") - } - - // All data should have been read. - _, err := r.ReadByte() - if err != io.EOF { - t.Errorf("#%d: got error %v; want EOF", rno, err) - } - } -} - -// Test that UnreadRune fails if the preceding operation was not a ReadRune. -func TestUnreadRuneError(t *testing.T) { - buf := make([]byte, 3) // All runes in this test are 3 bytes long - r := NewReader(&StringReader{data: []string{"日本語日本語日本語"}}) - if r.UnreadRune() == nil { - t.Error("expected error on UnreadRune from fresh buffer") - } - _, _, err := r.ReadRune() - if err != nil { - t.Error("unexpected error on ReadRune (1):", err) - } - if err = r.UnreadRune(); err != nil { - t.Error("unexpected error on UnreadRune (1):", err) - } - if r.UnreadRune() == nil { - t.Error("expected error after UnreadRune (1)") - } - // Test error after Read. - _, _, err = r.ReadRune() // reset state - if err != nil { - t.Error("unexpected error on ReadRune (2):", err) - } - _, err = r.Read(buf) - if err != nil { - t.Error("unexpected error on Read (2):", err) - } - if r.UnreadRune() == nil { - t.Error("expected error after Read (2)") - } - // Test error after ReadByte. - _, _, err = r.ReadRune() // reset state - if err != nil { - t.Error("unexpected error on ReadRune (2):", err) - } - for _ = range buf { - _, err = r.ReadByte() - if err != nil { - t.Error("unexpected error on ReadByte (2):", err) - } - } - if r.UnreadRune() == nil { - t.Error("expected error after ReadByte") - } - // Test error after UnreadByte. - _, _, err = r.ReadRune() // reset state - if err != nil { - t.Error("unexpected error on ReadRune (3):", err) - } - _, err = r.ReadByte() - if err != nil { - t.Error("unexpected error on ReadByte (3):", err) - } - err = r.UnreadByte() - if err != nil { - t.Error("unexpected error on UnreadByte (3):", err) - } - if r.UnreadRune() == nil { - t.Error("expected error after UnreadByte (3)") - } -} - -func TestUnreadRuneAtEOF(t *testing.T) { - // UnreadRune/ReadRune should error at EOF (was a bug; used to panic) - r := NewReader(strings.NewReader("x")) - r.ReadRune() - r.ReadRune() - r.UnreadRune() - _, _, err := r.ReadRune() - if err == nil { - t.Error("expected error at EOF") - } else if err != io.EOF { - t.Error("expected EOF; got", err) - } -} - -func TestReadWriteRune(t *testing.T) { - const NRune = 1000 - byteBuf := new(bytes.Buffer) - w := NewWriter(byteBuf) - // Write the runes out using WriteRune - buf := make([]byte, utf8.UTFMax) - for r := rune(0); r < NRune; r++ { - size := utf8.EncodeRune(buf, r) - nbytes, err := w.WriteRune(r) - if err != nil { - t.Fatalf("WriteRune(0x%x) error: %s", r, err) - } - if nbytes != size { - t.Fatalf("WriteRune(0x%x) expected %d, got %d", r, size, nbytes) - } - } - w.Flush() - - r := NewReader(byteBuf) - // Read them back with ReadRune - for r1 := rune(0); r1 < NRune; r1++ { - size := utf8.EncodeRune(buf, r1) - nr, nbytes, err := r.ReadRune() - if nr != r1 || nbytes != size || err != nil { - t.Fatalf("ReadRune(0x%x) got 0x%x,%d not 0x%x,%d (err=%s)", r1, nr, nbytes, r1, size, err) - } - } -} - -func TestWriter(t *testing.T) { - var data [8192]byte - - for i := 0; i < len(data); i++ { - data[i] = byte(' ' + i%('~'-' ')) - } - w := new(bytes.Buffer) - for i := 0; i < len(bufsizes); i++ { - for j := 0; j < len(bufsizes); j++ { - nwrite := bufsizes[i] - bs := bufsizes[j] - - // Write nwrite bytes using buffer size bs. - // Check that the right amount makes it out - // and that the data is correct. - - w.Reset() - buf := NewWriterSize(w, bs) - context := fmt.Sprintf("nwrite=%d bufsize=%d", nwrite, bs) - n, e1 := buf.Write(data[0:nwrite]) - if e1 != nil || n != nwrite { - t.Errorf("%s: buf.Write %d = %d, %v", context, nwrite, n, e1) - continue - } - if e := buf.Flush(); e != nil { - t.Errorf("%s: buf.Flush = %v", context, e) - } - - written := w.Bytes() - if len(written) != nwrite { - t.Errorf("%s: %d bytes written", context, len(written)) - } - for l := 0; l < len(written); l++ { - if written[i] != data[i] { - t.Errorf("wrong bytes written") - t.Errorf("want=%q", data[0:len(written)]) - t.Errorf("have=%q", written) - } - } - } - } -} - -// Check that write errors are returned properly. - -type errorWriterTest struct { - n, m int - err error - expect error -} - -func (w errorWriterTest) Write(p []byte) (int, error) { - return len(p) * w.n / w.m, w.err -} - -var errorWriterTests = []errorWriterTest{ - {0, 1, nil, io.ErrShortWrite}, - {1, 2, nil, io.ErrShortWrite}, - {1, 1, nil, nil}, - {0, 1, io.ErrClosedPipe, io.ErrClosedPipe}, - {1, 2, io.ErrClosedPipe, io.ErrClosedPipe}, - {1, 1, io.ErrClosedPipe, io.ErrClosedPipe}, -} - -func TestWriteErrors(t *testing.T) { - for _, w := range errorWriterTests { - buf := NewWriter(w) - _, e := buf.Write([]byte("hello world")) - if e != nil { - t.Errorf("Write hello to %v: %v", w, e) - continue - } - // Two flushes, to verify the error is sticky. - for i := 0; i < 2; i++ { - e = buf.Flush() - if e != w.expect { - t.Errorf("Flush %d/2 %v: got %v, wanted %v", i+1, w, e, w.expect) - } - } - } -} - -func TestNewReaderSizeIdempotent(t *testing.T) { - const BufSize = 1000 - b := NewReaderSize(strings.NewReader("hello world"), BufSize) - // Does it recognize itself? - b1 := NewReaderSize(b, BufSize) - if b1 != b { - t.Error("NewReaderSize did not detect underlying Reader") - } - // Does it wrap if existing buffer is too small? - b2 := NewReaderSize(b, 2*BufSize) - if b2 == b { - t.Error("NewReaderSize did not enlarge buffer") - } -} - -func TestNewWriterSizeIdempotent(t *testing.T) { - const BufSize = 1000 - b := NewWriterSize(new(bytes.Buffer), BufSize) - // Does it recognize itself? - b1 := NewWriterSize(b, BufSize) - if b1 != b { - t.Error("NewWriterSize did not detect underlying Writer") - } - // Does it wrap if existing buffer is too small? - b2 := NewWriterSize(b, 2*BufSize) - if b2 == b { - t.Error("NewWriterSize did not enlarge buffer") - } -} - -func TestWriteString(t *testing.T) { - const BufSize = 8 - buf := new(bytes.Buffer) - b := NewWriterSize(buf, BufSize) - b.WriteString("0") // easy - b.WriteString("123456") // still easy - b.WriteString("7890") // easy after flush - b.WriteString("abcdefghijklmnopqrstuvwxy") // hard - b.WriteString("z") - if err := b.Flush(); err != nil { - t.Error("WriteString", err) - } - s := "01234567890abcdefghijklmnopqrstuvwxyz" - if string(buf.Bytes()) != s { - t.Errorf("WriteString wants %q gets %q", s, string(buf.Bytes())) - } -} - -func TestBufferFull(t *testing.T) { - const longString = "And now, hello, world! It is the time for all good men to come to the aid of their party" - buf := NewReaderSize(strings.NewReader(longString), minReadBufferSize) - line, err := buf.ReadSlice('!') - if string(line) != "And now, hello, " || err != ErrBufferFull { - t.Errorf("first ReadSlice(,) = %q, %v", line, err) - } - line, err = buf.ReadSlice('!') - if string(line) != "world!" || err != nil { - t.Errorf("second ReadSlice(,) = %q, %v", line, err) - } -} - -func TestPeek(t *testing.T) { - p := make([]byte, 10) - // string is 16 (minReadBufferSize) long. - buf := NewReaderSize(strings.NewReader("abcdefghijklmnop"), minReadBufferSize) - if s, err := buf.Peek(1); string(s) != "a" || err != nil { - t.Fatalf("want %q got %q, err=%v", "a", string(s), err) - } - if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { - t.Fatalf("want %q got %q, err=%v", "abcd", string(s), err) - } - if _, err := buf.Peek(-1); err != ErrNegativeCount { - t.Fatalf("want ErrNegativeCount got %v", err) - } - if _, err := buf.Peek(32); err != ErrBufferFull { - t.Fatalf("want ErrBufFull got %v", err) - } - if _, err := buf.Read(p[0:3]); string(p[0:3]) != "abc" || err != nil { - t.Fatalf("want %q got %q, err=%v", "abc", string(p[0:3]), err) - } - if s, err := buf.Peek(1); string(s) != "d" || err != nil { - t.Fatalf("want %q got %q, err=%v", "d", string(s), err) - } - if s, err := buf.Peek(2); string(s) != "de" || err != nil { - t.Fatalf("want %q got %q, err=%v", "de", string(s), err) - } - if _, err := buf.Read(p[0:3]); string(p[0:3]) != "def" || err != nil { - t.Fatalf("want %q got %q, err=%v", "def", string(p[0:3]), err) - } - if s, err := buf.Peek(4); string(s) != "ghij" || err != nil { - t.Fatalf("want %q got %q, err=%v", "ghij", string(s), err) - } - if _, err := buf.Read(p[0:]); string(p[0:]) != "ghijklmnop" || err != nil { - t.Fatalf("want %q got %q, err=%v", "ghijklmnop", string(p[0:minReadBufferSize]), err) - } - if s, err := buf.Peek(0); string(s) != "" || err != nil { - t.Fatalf("want %q got %q, err=%v", "", string(s), err) - } - if _, err := buf.Peek(1); err != io.EOF { - t.Fatalf("want EOF got %v", err) - } - - // Test for issue 3022, not exposing a reader's error on a successful Peek. - buf = NewReaderSize(dataAndEOFReader("abcd"), 32) - if s, err := buf.Peek(2); string(s) != "ab" || err != nil { - t.Errorf(`Peek(2) on "abcd", EOF = %q, %v; want "ab", nil`, string(s), err) - } - if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { - t.Errorf(`Peek(4) on "abcd", EOF = %q, %v; want "abcd", nil`, string(s), err) - } - if n, err := buf.Read(p[0:5]); string(p[0:n]) != "abcd" || err != nil { - t.Fatalf("Read after peek = %q, %v; want abcd, EOF", p[0:n], err) - } - if n, err := buf.Read(p[0:1]); string(p[0:n]) != "" || err != io.EOF { - t.Fatalf(`second Read after peek = %q, %v; want "", EOF`, p[0:n], err) - } -} - -type dataAndEOFReader string - -func (r dataAndEOFReader) Read(p []byte) (int, error) { - return copy(p, r), io.EOF -} - -func TestPeekThenUnreadRune(t *testing.T) { - // This sequence used to cause a crash. - r := NewReader(strings.NewReader("x")) - r.ReadRune() - r.Peek(1) - r.UnreadRune() - r.ReadRune() // Used to panic here -} - -var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy") -var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy") -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") - -// TestReader wraps a []byte and returns reads of a specific length. -type testReader struct { - data []byte - stride int -} - -func (t *testReader) Read(buf []byte) (n int, err error) { - n = t.stride - if n > len(t.data) { - n = len(t.data) - } - if n > len(buf) { - n = len(buf) - } - copy(buf, t.data) - t.data = t.data[n:] - if len(t.data) == 0 { - err = io.EOF - } - return -} - -func testReadLine(t *testing.T, input []byte) { - //for stride := 1; stride < len(input); stride++ { - for stride := 1; stride < 2; stride++ { - done := 0 - reader := testReader{input, stride} - l := NewReaderSize(&reader, len(input)+1) - for { - line, isPrefix, err := l.ReadLine() - if len(line) > 0 && err != nil { - t.Errorf("ReadLine returned both data and error: %s", err) - } - if isPrefix { - t.Errorf("ReadLine returned prefix") - } - if err != nil { - if err != io.EOF { - t.Fatalf("Got unknown error: %s", err) - } - break - } - if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) { - t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line) - } - done += len(line) - } - if done != len(testOutput) { - t.Errorf("ReadLine didn't return everything: got: %d, want: %d (stride: %d)", done, len(testOutput), stride) - } - } -} - -func TestReadLine(t *testing.T) { - testReadLine(t, testInput) - testReadLine(t, testInputrn) -} - -func TestLineTooLong(t *testing.T) { - data := make([]byte, 0) - for i := 0; i < minReadBufferSize*5/2; i++ { - data = append(data, '0'+byte(i%10)) - } - buf := bytes.NewReader(data) - l := NewReaderSize(buf, minReadBufferSize) - line, isPrefix, err := l.ReadLine() - if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { - t.Errorf("bad result for first line: got %q want %q %v", line, data[:minReadBufferSize], err) - } - data = data[len(line):] - line, isPrefix, err = l.ReadLine() - if !isPrefix || !bytes.Equal(line, data[:minReadBufferSize]) || err != nil { - t.Errorf("bad result for second line: got %q want %q %v", line, data[:minReadBufferSize], err) - } - data = data[len(line):] - line, isPrefix, err = l.ReadLine() - if isPrefix || !bytes.Equal(line, data[:minReadBufferSize/2]) || err != nil { - t.Errorf("bad result for third line: got %q want %q %v", line, data[:minReadBufferSize/2], err) - } - line, isPrefix, err = l.ReadLine() - if isPrefix || err == nil { - t.Errorf("expected no more lines: %x %s", line, err) - } -} - -func TestReadAfterLines(t *testing.T) { - line1 := "this is line1" - restData := "this is line2\nthis is line 3\n" - inbuf := bytes.NewReader([]byte(line1 + "\n" + restData)) - outbuf := new(bytes.Buffer) - maxLineLength := len(line1) + len(restData)/2 - l := NewReaderSize(inbuf, maxLineLength) - line, isPrefix, err := l.ReadLine() - if isPrefix || err != nil || string(line) != line1 { - t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line)) - } - n, err := io.Copy(outbuf, l) - if int(n) != len(restData) || err != nil { - t.Errorf("bad result for Read: n=%d err=%v", n, err) - } - if outbuf.String() != restData { - t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData) - } -} - -func TestReadEmptyBuffer(t *testing.T) { - l := NewReaderSize(new(bytes.Buffer), minReadBufferSize) - line, isPrefix, err := l.ReadLine() - if err != io.EOF { - t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) - } -} - -func TestLinesAfterRead(t *testing.T) { - l := NewReaderSize(bytes.NewReader([]byte("foo")), minReadBufferSize) - _, err := ioutil.ReadAll(l) - if err != nil { - t.Error(err) - return - } - - line, isPrefix, err := l.ReadLine() - if err != io.EOF { - t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err) - } -} - -func TestReadLineNonNilLineOrError(t *testing.T) { - r := NewReader(strings.NewReader("line 1\n")) - for i := 0; i < 2; i++ { - l, _, err := r.ReadLine() - if l != nil && err != nil { - t.Fatalf("on line %d/2; ReadLine=%#v, %v; want non-nil line or Error, but not both", - i+1, l, err) - } - } -} - -type readLineResult struct { - line []byte - isPrefix bool - err error -} - -var readLineNewlinesTests = []struct { - input string - expect []readLineResult -}{ - {"012345678901234\r\n012345678901234\r\n", []readLineResult{ - {[]byte("012345678901234"), true, nil}, - {nil, false, nil}, - {[]byte("012345678901234"), true, nil}, - {nil, false, nil}, - {nil, false, io.EOF}, - }}, - {"0123456789012345\r012345678901234\r", []readLineResult{ - {[]byte("0123456789012345"), true, nil}, - {[]byte("\r012345678901234"), true, nil}, - {[]byte("\r"), false, nil}, - {nil, false, io.EOF}, - }}, -} - -func TestReadLineNewlines(t *testing.T) { - for _, e := range readLineNewlinesTests { - testReadLineNewlines(t, e.input, e.expect) - } -} - -func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) { - b := NewReaderSize(strings.NewReader(input), minReadBufferSize) - for i, e := range expect { - line, isPrefix, err := b.ReadLine() - if !bytes.Equal(line, e.line) { - t.Errorf("%q call %d, line == %q, want %q", input, i, line, e.line) - return - } - if isPrefix != e.isPrefix { - t.Errorf("%q call %d, isPrefix == %v, want %v", input, i, isPrefix, e.isPrefix) - return - } - if err != e.err { - t.Errorf("%q call %d, err == %v, want %v", input, i, err, e.err) - return - } - } -} - -func createTestInput(n int) []byte { - input := make([]byte, n) - for i := range input { - // 101 and 251 are arbitrary prime numbers. - // The idea is to create an input sequence - // which doesn't repeat too frequently. - input[i] = byte(i % 251) - if i%101 == 0 { - input[i] ^= byte(i / 101) - } - } - return input -} - -func TestReaderWriteTo(t *testing.T) { - input := createTestInput(8192) - r := NewReader(onlyReader{bytes.NewReader(input)}) - w := new(bytes.Buffer) - if n, err := r.WriteTo(w); err != nil || n != int64(len(input)) { - t.Fatalf("r.WriteTo(w) = %d, %v, want %d, nil", n, err, len(input)) - } - - for i, val := range w.Bytes() { - if val != input[i] { - t.Errorf("after write: out[%d] = %#x, want %#x", i, val, input[i]) - } - } -} - -type errorWriterToTest struct { - rn, wn int - rerr, werr error - expected error -} - -func (r errorWriterToTest) Read(p []byte) (int, error) { - return len(p) * r.rn, r.rerr -} - -func (w errorWriterToTest) Write(p []byte) (int, error) { - return len(p) * w.wn, w.werr -} - -var errorWriterToTests = []errorWriterToTest{ - {1, 0, nil, io.ErrClosedPipe, io.ErrClosedPipe}, - {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, - {0, 0, io.ErrUnexpectedEOF, io.ErrClosedPipe, io.ErrClosedPipe}, - {0, 1, io.EOF, nil, nil}, -} - -func TestReaderWriteToErrors(t *testing.T) { - for i, rw := range errorWriterToTests { - r := NewReader(rw) - if _, err := r.WriteTo(rw); err != rw.expected { - t.Errorf("r.WriteTo(errorWriterToTests[%d]) = _, %v, want _,%v", i, err, rw.expected) - } - } -} - -func TestWriterReadFrom(t *testing.T) { - ws := []func(io.Writer) io.Writer{ - func(w io.Writer) io.Writer { return onlyWriter{w} }, - func(w io.Writer) io.Writer { return w }, - } - - rs := []func(io.Reader) io.Reader{ - iotest.DataErrReader, - func(r io.Reader) io.Reader { return r }, - } - - for ri, rfunc := range rs { - for wi, wfunc := range ws { - input := createTestInput(8192) - b := new(bytes.Buffer) - w := NewWriter(wfunc(b)) - r := rfunc(bytes.NewReader(input)) - if n, err := w.ReadFrom(r); err != nil || n != int64(len(input)) { - t.Errorf("ws[%d],rs[%d]: w.ReadFrom(r) = %d, %v, want %d, nil", wi, ri, n, err, len(input)) - continue - } - if err := w.Flush(); err != nil { - t.Errorf("Flush returned %v", err) - continue - } - if got, want := b.String(), string(input); got != want { - t.Errorf("ws[%d], rs[%d]:\ngot %q\nwant %q\n", wi, ri, got, want) - } - } - } -} - -type errorReaderFromTest struct { - rn, wn int - rerr, werr error - expected error -} - -func (r errorReaderFromTest) Read(p []byte) (int, error) { - return len(p) * r.rn, r.rerr -} - -func (w errorReaderFromTest) Write(p []byte) (int, error) { - return len(p) * w.wn, w.werr -} - -var errorReaderFromTests = []errorReaderFromTest{ - {0, 1, io.EOF, nil, nil}, - {1, 1, io.EOF, nil, nil}, - {0, 1, io.ErrClosedPipe, nil, io.ErrClosedPipe}, - {0, 0, io.ErrClosedPipe, io.ErrShortWrite, io.ErrClosedPipe}, - {1, 0, nil, io.ErrShortWrite, io.ErrShortWrite}, -} - -func TestWriterReadFromErrors(t *testing.T) { - for i, rw := range errorReaderFromTests { - w := NewWriter(rw) - if _, err := w.ReadFrom(rw); err != rw.expected { - t.Errorf("w.ReadFrom(errorReaderFromTests[%d]) = _, %v, want _,%v", i, err, rw.expected) - } - } -} - -// TestWriterReadFromCounts tests that using io.Copy to copy into a -// bufio.Writer does not prematurely flush the buffer. For example, when -// buffering writes to a network socket, excessive network writes should be -// avoided. -func TestWriterReadFromCounts(t *testing.T) { - var w0 writeCountingDiscard - b0 := NewWriterSize(&w0, 1234) - b0.WriteString(strings.Repeat("x", 1000)) - if w0 != 0 { - t.Fatalf("write 1000 'x's: got %d writes, want 0", w0) - } - b0.WriteString(strings.Repeat("x", 200)) - if w0 != 0 { - t.Fatalf("write 1200 'x's: got %d writes, want 0", w0) - } - io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 30))}) - if w0 != 0 { - t.Fatalf("write 1230 'x's: got %d writes, want 0", w0) - } - io.Copy(b0, onlyReader{strings.NewReader(strings.Repeat("x", 9))}) - if w0 != 1 { - t.Fatalf("write 1239 'x's: got %d writes, want 1", w0) - } - - var w1 writeCountingDiscard - b1 := NewWriterSize(&w1, 1234) - b1.WriteString(strings.Repeat("x", 1200)) - b1.Flush() - if w1 != 1 { - t.Fatalf("flush 1200 'x's: got %d writes, want 1", w1) - } - b1.WriteString(strings.Repeat("x", 89)) - if w1 != 1 { - t.Fatalf("write 1200 + 89 'x's: got %d writes, want 1", w1) - } - io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 700))}) - if w1 != 1 { - t.Fatalf("write 1200 + 789 'x's: got %d writes, want 1", w1) - } - io.Copy(b1, onlyReader{strings.NewReader(strings.Repeat("x", 600))}) - if w1 != 2 { - t.Fatalf("write 1200 + 1389 'x's: got %d writes, want 2", w1) - } - b1.Flush() - if w1 != 3 { - t.Fatalf("flush 1200 + 1389 'x's: got %d writes, want 3", w1) - } -} - -// A writeCountingDiscard is like ioutil.Discard and counts the number of times -// Write is called on it. -type writeCountingDiscard int - -func (w *writeCountingDiscard) Write(p []byte) (int, error) { - *w++ - return len(p), nil -} - -type negativeReader int - -func (r *negativeReader) Read([]byte) (int, error) { return -1, nil } - -func TestNegativeRead(t *testing.T) { - // should panic with a description pointing at the reader, not at itself. - // (should NOT panic with slice index error, for example.) - b := NewReader(new(negativeReader)) - defer func() { - switch err := recover().(type) { - case nil: - t.Fatal("read did not panic") - case error: - if !strings.Contains(err.Error(), "reader returned negative count from Read") { - t.Fatalf("wrong panic: %v", err) - } - default: - t.Fatalf("unexpected panic value: %T(%v)", err, err) - } - }() - b.Read(make([]byte, 100)) -} - -var errFake = errors.New("fake error") - -type errorThenGoodReader struct { - didErr bool - nread int -} - -func (r *errorThenGoodReader) Read(p []byte) (int, error) { - r.nread++ - if !r.didErr { - r.didErr = true - return 0, errFake - } - return len(p), nil -} - -func TestReaderClearError(t *testing.T) { - r := &errorThenGoodReader{} - b := NewReader(r) - buf := make([]byte, 1) - if _, err := b.Read(nil); err != nil { - t.Fatalf("1st nil Read = %v; want nil", err) - } - if _, err := b.Read(buf); err != errFake { - t.Fatalf("1st Read = %v; want errFake", err) - } - if _, err := b.Read(nil); err != nil { - t.Fatalf("2nd nil Read = %v; want nil", err) - } - if _, err := b.Read(buf); err != nil { - t.Fatalf("3rd Read with buffer = %v; want nil", err) - } - if r.nread != 2 { - t.Errorf("num reads = %d; want 2", r.nread) - } -} - -// Test for golang.org/issue/5947 -func TestWriterReadFromWhileFull(t *testing.T) { - buf := new(bytes.Buffer) - w := NewWriterSize(buf, 10) - - // Fill buffer exactly. - n, err := w.Write([]byte("0123456789")) - if n != 10 || err != nil { - t.Fatalf("Write returned (%v, %v), want (10, nil)", n, err) - } - - // Use ReadFrom to read in some data. - n2, err := w.ReadFrom(strings.NewReader("abcdef")) - if n2 != 6 || err != nil { - t.Fatalf("ReadFrom returned (%v, %v), want (6, nil)", n2, err) - } -} - -type emptyThenNonEmptyReader struct { - r io.Reader - n int -} - -func (r *emptyThenNonEmptyReader) Read(p []byte) (int, error) { - if r.n <= 0 { - return r.r.Read(p) - } - r.n-- - return 0, nil -} - -// Test for golang.org/issue/7611 -func TestWriterReadFromUntilEOF(t *testing.T) { - buf := new(bytes.Buffer) - w := NewWriterSize(buf, 5) - - // Partially fill buffer - n, err := w.Write([]byte("0123")) - if n != 4 || err != nil { - t.Fatalf("Write returned (%v, %v), want (4, nil)", n, err) - } - - // Use ReadFrom to read in some data. - r := &emptyThenNonEmptyReader{r: strings.NewReader("abcd"), n: 3} - n2, err := w.ReadFrom(r) - if n2 != 4 || err != nil { - t.Fatalf("ReadFrom returned (%v, %v), want (4, nil)", n2, err) - } - w.Flush() - if got, want := string(buf.Bytes()), "0123abcd"; got != want { - t.Fatalf("buf.Bytes() returned %q, want %q", got, want) - } -} - -func TestWriterReadFromErrNoProgress(t *testing.T) { - buf := new(bytes.Buffer) - w := NewWriterSize(buf, 5) - - // Partially fill buffer - n, err := w.Write([]byte("0123")) - if n != 4 || err != nil { - t.Fatalf("Write returned (%v, %v), want (4, nil)", n, err) - } - - // Use ReadFrom to read in some data. - r := &emptyThenNonEmptyReader{r: strings.NewReader("abcd"), n: 100} - n2, err := w.ReadFrom(r) - if n2 != 0 || err != io.ErrNoProgress { - t.Fatalf("buf.Bytes() returned (%v, %v), want (0, io.ErrNoProgress)", n2, err) - } -} - -func TestReaderReset(t *testing.T) { - r := NewReader(strings.NewReader("foo foo")) - buf := make([]byte, 3) - r.Read(buf) - if string(buf) != "foo" { - t.Errorf("buf = %q; want foo", buf) - } - r.Reset(strings.NewReader("bar bar")) - all, err := ioutil.ReadAll(r) - if err != nil { - t.Fatal(err) - } - if string(all) != "bar bar" { - t.Errorf("ReadAll = %q; want bar bar", all) - } -} - -func TestWriterReset(t *testing.T) { - var buf1, buf2 bytes.Buffer - w := NewWriter(&buf1) - w.WriteString("foo") - w.Reset(&buf2) // and not flushed - w.WriteString("bar") - w.Flush() - if buf1.String() != "" { - t.Errorf("buf1 = %q; want empty", buf1.String()) - } - if buf2.String() != "bar" { - t.Errorf("buf2 = %q; want bar", buf2.String()) - } -} - -// An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have. -type onlyReader struct { - io.Reader -} - -// An onlyWriter only implements io.Writer, no matter what other methods the underlying implementation may have. -type onlyWriter struct { - io.Writer -} - -func BenchmarkReaderCopyOptimal(b *testing.B) { - // Optimal case is where the underlying reader implements io.WriterTo - srcBuf := bytes.NewBuffer(make([]byte, 8192)) - src := NewReader(srcBuf) - dstBuf := new(bytes.Buffer) - dst := onlyWriter{dstBuf} - for i := 0; i < b.N; i++ { - srcBuf.Reset() - src.Reset(srcBuf) - dstBuf.Reset() - io.Copy(dst, src) - } -} - -func BenchmarkReaderCopyUnoptimal(b *testing.B) { - // Unoptimal case is where the underlying reader doesn't implement io.WriterTo - srcBuf := bytes.NewBuffer(make([]byte, 8192)) - src := NewReader(onlyReader{srcBuf}) - dstBuf := new(bytes.Buffer) - dst := onlyWriter{dstBuf} - for i := 0; i < b.N; i++ { - srcBuf.Reset() - src.Reset(onlyReader{srcBuf}) - dstBuf.Reset() - io.Copy(dst, src) - } -} - -func BenchmarkReaderCopyNoWriteTo(b *testing.B) { - srcBuf := bytes.NewBuffer(make([]byte, 8192)) - srcReader := NewReader(srcBuf) - src := onlyReader{srcReader} - dstBuf := new(bytes.Buffer) - dst := onlyWriter{dstBuf} - for i := 0; i < b.N; i++ { - srcBuf.Reset() - srcReader.Reset(srcBuf) - dstBuf.Reset() - io.Copy(dst, src) - } -} - -func BenchmarkReaderWriteToOptimal(b *testing.B) { - const bufSize = 16 << 10 - buf := make([]byte, bufSize) - r := bytes.NewReader(buf) - srcReader := NewReaderSize(onlyReader{r}, 1<<10) - if _, ok := ioutil.Discard.(io.ReaderFrom); !ok { - b.Fatal("ioutil.Discard doesn't support ReaderFrom") - } - for i := 0; i < b.N; i++ { - r.Seek(0, 0) - srcReader.Reset(onlyReader{r}) - n, err := srcReader.WriteTo(ioutil.Discard) - if err != nil { - b.Fatal(err) - } - if n != bufSize { - b.Fatalf("n = %d; want %d", n, bufSize) - } - } -} - -func BenchmarkWriterCopyOptimal(b *testing.B) { - // Optimal case is where the underlying writer implements io.ReaderFrom - srcBuf := bytes.NewBuffer(make([]byte, 8192)) - src := onlyReader{srcBuf} - dstBuf := new(bytes.Buffer) - dst := NewWriter(dstBuf) - for i := 0; i < b.N; i++ { - srcBuf.Reset() - dstBuf.Reset() - dst.Reset(dstBuf) - io.Copy(dst, src) - } -} - -func BenchmarkWriterCopyUnoptimal(b *testing.B) { - srcBuf := bytes.NewBuffer(make([]byte, 8192)) - src := onlyReader{srcBuf} - dstBuf := new(bytes.Buffer) - dst := NewWriter(onlyWriter{dstBuf}) - for i := 0; i < b.N; i++ { - srcBuf.Reset() - dstBuf.Reset() - dst.Reset(onlyWriter{dstBuf}) - io.Copy(dst, src) - } -} - -func BenchmarkWriterCopyNoReadFrom(b *testing.B) { - srcBuf := bytes.NewBuffer(make([]byte, 8192)) - src := onlyReader{srcBuf} - dstBuf := new(bytes.Buffer) - dstWriter := NewWriter(dstBuf) - dst := onlyWriter{dstWriter} - for i := 0; i < b.N; i++ { - srcBuf.Reset() - dstBuf.Reset() - dstWriter.Reset(dstBuf) - io.Copy(dst, src) - } -} - -func BenchmarkReaderEmpty(b *testing.B) { - b.ReportAllocs() - str := strings.Repeat("x", 16<<10) - for i := 0; i < b.N; i++ { - br := NewReader(strings.NewReader(str)) - n, err := io.Copy(ioutil.Discard, br) - if err != nil { - b.Fatal(err) - } - if n != int64(len(str)) { - b.Fatal("wrong length") - } - } -} - -func BenchmarkWriterEmpty(b *testing.B) { - b.ReportAllocs() - str := strings.Repeat("x", 1<<10) - bs := []byte(str) - for i := 0; i < b.N; i++ { - bw := NewWriter(ioutil.Discard) - bw.Flush() - bw.WriteByte('a') - bw.Flush() - bw.WriteRune('B') - bw.Flush() - bw.Write(bs) - bw.Flush() - bw.WriteString(str) - bw.Flush() - } -} - -func BenchmarkWriterFlush(b *testing.B) { - b.ReportAllocs() - bw := NewWriter(ioutil.Discard) - str := strings.Repeat("x", 50) - for i := 0; i < b.N; i++ { - bw.WriteString(str) - bw.Flush() - } -} diff --git a/Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go b/Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go deleted file mode 100644 index 16629d022..000000000 --- a/Godeps/_workspace/src/gopkg.in/bufio.v1/export_test.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package bufio - -func (b *Buffer) Cap() int { - return cap(b.buf) -} -- cgit v1.2.3-1-g7c22