-
Notifications
You must be signed in to change notification settings - Fork 0
/
gozlib_nativepool_test.go
43 lines (34 loc) · 1.03 KB
/
gozlib_nativepool_test.go
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
package gozlib
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAllocAndReturnVariousSize(t *testing.T) {
pool := NewNativeSlicePool()
defer pool.Free()
const maxBits = 20 // 1 << 20 == 1M max
for bitCount := 0; bitCount <= maxBits; bitCount++ {
size := 1 << bitCount
data := pool.Acquire(size)
assert.Equal(t, 0, len(data))
assert.Equal(t, size, cap(data))
pool.Return(data)
}
}
func TestNativePoolAllocAndReuse(t *testing.T) {
const desiredBufferSize = 1024
pool := NewNativeSlicePool()
defer pool.Free()
data := pool.Acquire(desiredBufferSize)
assert.Equal(t, 0, len(data))
assert.Equal(t, desiredBufferSize, cap(data))
tag := []byte{'a', 'b', 'c', '1', '2', '3'}
// the returned slice should be reused and unmodified
// in a real application, anything acquired from the pool should be considered unitialized memory
data = data[:len(tag)]
copy(data, tag)
pool.Return(data)
dataAfterReturned := pool.Acquire(desiredBufferSize)
actual := dataAfterReturned[:len(tag)]
assert.Equal(t, tag, actual)
}