mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: Add Buffer.ReadBytes, Buffer.ReadString
R=golang-dev, r CC=golang-dev https://golang.org/cl/4000046
This commit is contained in:
parent
19d9a40845
commit
c9bf30cf19
3 changed files with 59 additions and 3 deletions
|
|
@ -6,6 +6,7 @@ package bytes_test
|
|||
|
||||
import (
|
||||
. "bytes"
|
||||
"os"
|
||||
"rand"
|
||||
"testing"
|
||||
"utf8"
|
||||
|
|
@ -238,7 +239,7 @@ func TestMixedReadsAndWrites(t *testing.T) {
|
|||
func TestNil(t *testing.T) {
|
||||
var b *Buffer
|
||||
if b.String() != "<nil>" {
|
||||
t.Errorf("expcted <nil>; got %q", b.String())
|
||||
t.Errorf("expected <nil>; got %q", b.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -347,3 +348,27 @@ func TestNext(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
var readBytesTests = []struct {
|
||||
buffer []byte
|
||||
delim byte
|
||||
expected []byte
|
||||
err os.Error
|
||||
}{
|
||||
{err: os.EOF},
|
||||
{[]byte{}, 0, []byte{}, os.EOF},
|
||||
{[]byte("a\x00"), 0, []byte("a\x00"), nil},
|
||||
{[]byte("hello\x01world"), 1, []byte("hello\x01"), nil},
|
||||
{[]byte("foo\nbar"), 0, []byte("foo\nbar"), os.EOF},
|
||||
{[]byte("alpha beta gamma"), ' ', []byte("alpha "), nil},
|
||||
}
|
||||
|
||||
func TestReadBytes(t *testing.T) {
|
||||
for _, test := range readBytesTests {
|
||||
buf := NewBuffer(test.buffer)
|
||||
bytes, err := buf.ReadBytes(test.delim)
|
||||
if !Equal(bytes, test.expected) || err != test.err {
|
||||
t.Errorf("expected %q, %v got %q, %v", test.expected, test.err, bytes, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue