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