Skip to content
This repository was archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #28 from mdlayher/master
Browse files Browse the repository at this point in the history
buffer_test: add TestBufferWrite
  • Loading branch information
bradfitz committed May 3, 2015
2 parents 8348f2f + ec0c78a commit 9516364
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
7 changes: 4 additions & 3 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type buffer struct {
}

var (
errReadEmpty = errors.New("read from empty buffer")
errWriteFull = errors.New("write on full buffer")
errReadEmpty = errors.New("read from empty buffer")
errWriteClosed = errors.New("write on closed buffer")
errWriteFull = errors.New("write on full buffer")
)

// Read copies bytes from the buffer into p.
Expand All @@ -45,7 +46,7 @@ func (b *buffer) Len() int {
// It is an error to write more data than the buffer can hold.
func (b *buffer) Write(p []byte) (n int, err error) {
if b.closed {
return 0, errors.New("closed")
return 0, errWriteClosed
}

// Slide existing data to beginning.
Expand Down
81 changes: 81 additions & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,84 @@ func TestBufferRead(t *testing.T) {
}
}
}

var bufferWriteTests = []struct {
buf buffer
write, wn int
werr error
wbuf buffer
}{
{
buf: buffer{
buf: []byte{},
},
wbuf: buffer{
buf: []byte{},
},
},
{
buf: buffer{
buf: []byte{1, 'a'},
},
write: 1,
wn: 1,
wbuf: buffer{
buf: []byte{0, 'a'},
w: 1,
},
},
{
buf: buffer{
buf: []byte{'a', 1},
r: 1,
w: 1,
},
write: 2,
wn: 2,
wbuf: buffer{
buf: []byte{0, 0},
w: 2,
},
},
{
buf: buffer{
buf: []byte{},
r: 1,
closed: true,
},
write: 5,
werr: errWriteClosed,
wbuf: buffer{
buf: []byte{},
r: 1,
closed: true,
},
},
{
buf: buffer{
buf: []byte{},
},
write: 5,
werr: errWriteFull,
wbuf: buffer{
buf: []byte{},
},
},
}

func TestBufferWrite(t *testing.T) {
for i, tt := range bufferWriteTests {
n, err := tt.buf.Write(make([]byte, tt.write))
if n != tt.wn {
t.Errorf("#%d: wrote %d bytes; want %d", i, n, tt.wn)
continue
}
if err != tt.werr {
t.Errorf("#%d: error = %v; want %v", i, err, tt.werr)
continue
}
if !reflect.DeepEqual(tt.buf, tt.wbuf) {
t.Errorf("#%d: buf = %q; want %q", i, tt.buf, tt.wbuf)
}
}
}

0 comments on commit 9516364

Please sign in to comment.