1
0
mirror of https://github.com/golang/go synced 2024-11-12 07:10:22 -07:00

bufio: improve NewReaderSize, Peek, and UnreadByte test coverage

R=golang-codereviews, dave
CC=golang-codereviews
https://golang.org/cl/42990045
This commit is contained in:
Shawn Smith 2014-01-01 22:26:22 +11:00 committed by Dave Cheney
parent a39f3b29ec
commit 4d239bcea2

View File

@ -139,7 +139,7 @@ var bufreaders = []bufReader{
const minReadBufferSize = 16 const minReadBufferSize = 16
var bufsizes = []int{ var bufsizes = []int{
minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096, 0, minReadBufferSize, 23, 32, 46, 64, 93, 128, 1024, 4096,
} }
func TestReader(t *testing.T) { func TestReader(t *testing.T) {
@ -259,6 +259,38 @@ func TestUnreadRune(t *testing.T) {
} }
} }
func TestUnreadByte(t *testing.T) {
want := "Hello, world"
got := ""
segments := []string{"Hello, ", "world"}
r := NewReader(&StringReader{data: segments})
// Normal execution.
for {
b1, err := r.ReadByte()
if err != nil {
if err != io.EOF {
t.Fatal("unexpected EOF")
}
break
}
got += string(b1)
// Put it back and read it again
if err = r.UnreadByte(); err != nil {
t.Fatalf("unexpected error on UnreadByte: %v", err)
}
b2, err := r.ReadByte()
if err != nil {
t.Fatalf("unexpected error reading after unreading: %v", err)
}
if b1 != b2 {
t.Fatalf("incorrect byte after unread: got %c wanted %c", b1, b2)
}
}
if got != want {
t.Errorf("got=%q want=%q", got, want)
}
}
// Test that UnreadRune fails if the preceding operation was not a ReadRune. // Test that UnreadRune fails if the preceding operation was not a ReadRune.
func TestUnreadRuneError(t *testing.T) { func TestUnreadRuneError(t *testing.T) {
buf := make([]byte, 3) // All runes in this test are 3 bytes long buf := make([]byte, 3) // All runes in this test are 3 bytes long
@ -516,6 +548,9 @@ func TestPeek(t *testing.T) {
if s, err := buf.Peek(4); string(s) != "abcd" || err != nil { if s, err := buf.Peek(4); string(s) != "abcd" || err != nil {
t.Fatalf("want %q got %q, err=%v", "abcd", string(s), err) 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 { if _, err := buf.Peek(32); err != ErrBufferFull {
t.Fatalf("want ErrBufFull got %v", err) t.Fatalf("want ErrBufFull got %v", err)
} }