Commit 4d239bce authored by Shawn Smith's avatar Shawn Smith Committed by Dave Cheney

bufio: improve NewReaderSize, Peek, and UnreadByte test coverage

R=golang-codereviews, dave
CC=golang-codereviews
https://golang.org/cl/42990045
parent a39f3b29
...@@ -139,7 +139,7 @@ var bufreaders = []bufReader{ ...@@ -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) { ...@@ -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) { ...@@ -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)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment