Fixed Buffer.DumpTo()

This commit is contained in:
Victor Starodub
2016-04-05 01:49:49 +03:00
parent 21a6cb0062
commit bc7232a0c0
3 changed files with 83 additions and 3 deletions
+2 -1
View File
@@ -31,7 +31,8 @@ test: generate root
go test \
$(PKG)/tests \
$(PKG)/jlexer \
$(PKG)/gen
$(PKG)/gen \
$(PKG)/buffer
go test -benchmem -tags use_easyjson -bench . $(PKG)/benchmark
bench-other: generate root
+2 -2
View File
@@ -158,14 +158,14 @@ func (b *Buffer) Size() int {
func (b *Buffer) DumpTo(w io.Writer) (written int, err error) {
var n int
for _, buf := range b.bufs {
if err != nil {
if err == nil {
n, err = w.Write(buf)
written += n
}
putBuf(buf)
}
if err != nil {
if err == nil {
n, err = w.Write(b.Buf)
written += n
}
+79
View File
@@ -0,0 +1,79 @@
package buffer
import (
"bytes"
"testing"
)
func TestAppendByte(t *testing.T) {
var b Buffer
var want []byte
for i := 0; i < 1000; i++ {
b.AppendByte(1)
b.AppendByte(2)
want = append(want, 1, 2)
}
got := b.BuildBytes()
if !bytes.Equal(got, want) {
t.Errorf("BuildBytes() = %v; want %v", got, want)
}
}
func TestAppendBytes(t *testing.T) {
var b Buffer
var want []byte
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte{1, 2})
want = append(want, 1, 2)
}
got := b.BuildBytes()
if !bytes.Equal(got, want) {
t.Errorf("BuildBytes() = %v; want %v", got, want)
}
}
func TestAppendString(t *testing.T) {
var b Buffer
var want []byte
s := "test"
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte(s))
want = append(want, s...)
}
got := b.BuildBytes()
if !bytes.Equal(got, want) {
t.Errorf("BuildBytes() = %v; want %v", got, want)
}
}
func TestDumpTo(t *testing.T) {
var b Buffer
var want []byte
s := "test"
for i := 0; i < 1000; i++ {
b.AppendBytes([]byte(s))
want = append(want, s...)
}
out := &bytes.Buffer{}
n, err := b.DumpTo(out)
if err != nil {
t.Errorf("DumpTo() error: %v", err)
}
got := out.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("DumpTo(): got %v; want %v", got, want)
}
if n != len(want) {
t.Errorf("DumpTo() = %v; want %v", n, len(want))
}
}