1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
package sync
import (
"testing"
)
func TestGetAndPutBytesBuffer(t *testing.T) {
buf := GetBytesBuffer()
if buf == nil {
t.Error("nil was not expected")
}
initialLen := buf.Len()
buf.Grow(initialLen * 2)
grownLen := buf.Len()
PutBytesBuffer(buf)
buf = GetBytesBuffer()
if buf.Len() != grownLen {
t.Error("bytes buffer was not reused")
}
buf2 := GetBytesBuffer()
if buf2.Len() != initialLen {
t.Errorf("new bytes buffer length: wanted %d got %d", initialLen, buf2.Len())
}
}
func TestGetAndPutByteSlice(t *testing.T) {
slice := GetByteSlice()
if slice == nil {
t.Error("nil was not expected")
}
wanted := 16 * 1024
got := len(*slice)
if wanted != got {
t.Errorf("byte slice length: wanted %d got %d", wanted, got)
}
newByteSlice := make([]byte, wanted*2)
PutByteSlice(&newByteSlice)
newSlice := GetByteSlice()
if len(*newSlice) != len(newByteSlice) {
t.Error("byte slice was not reused")
}
}
|