| 10 | ) |
| 11 | |
| 12 | func TestPoolGet(t *testing.T) { |
| 13 | testPool := NewPool("foo", 5, 2, 7) |
| 14 | cases := []struct { |
| 15 | size int |
| 16 | expectedCap int |
| 17 | tooLarge bool |
| 18 | }{ |
| 19 | { // under the smallest pool size, should return an unaligned slice |
| 20 | size: 3, |
| 21 | expectedCap: 3, |
| 22 | }, |
| 23 | { // minBucket is exclusive. 5 is technically an unaligned slice |
| 24 | size: 5, |
| 25 | expectedCap: 5, |
| 26 | }, |
| 27 | { |
| 28 | size: 6, |
| 29 | expectedCap: 12, |
| 30 | }, |
| 31 | { |
| 32 | size: 12, |
| 33 | expectedCap: 12, |
| 34 | }, |
| 35 | { |
| 36 | size: 15, |
| 37 | expectedCap: 19, |
| 38 | }, |
| 39 | { // over the largest pool size, should return an unaligned slice |
| 40 | size: 20, |
| 41 | expectedCap: 20, |
| 42 | tooLarge: true, |
| 43 | }, |
| 44 | } |
| 45 | for _, c := range cases { |
| 46 | for i := 0; i < 10; i++ { |
| 47 | ret := testPool.Get(c.size) |
| 48 | require.Equal(t, c.expectedCap, cap(ret)) |
| 49 | putBucket := testPool.Put(ret) |
| 50 | |
| 51 | if !c.tooLarge { |
| 52 | require.Equal(t, testPool.bucketFor(cap(ret)), putBucket) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestPoolSlicesAreAlwaysLargeEnough(t *testing.T) { |
| 59 | testPool := NewPool("foo", 100, 200, 5) |