alloc_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package pool
  2. import (
  3. "testing"
  4. "github.com/metacubex/randv2"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestAllocGet(t *testing.T) {
  8. alloc := NewAllocator()
  9. assert.Nil(t, alloc.Get(0))
  10. assert.Equal(t, 1, len(alloc.Get(1)))
  11. assert.Equal(t, 2, len(alloc.Get(2)))
  12. assert.Equal(t, 3, len(alloc.Get(3)))
  13. assert.Equal(t, 64, cap(alloc.Get(3)))
  14. assert.Equal(t, 64, cap(alloc.Get(4)))
  15. assert.Equal(t, 1023, len(alloc.Get(1023)))
  16. assert.Equal(t, 1024, cap(alloc.Get(1023)))
  17. assert.Equal(t, 1024, len(alloc.Get(1024)))
  18. assert.Equal(t, 65536, len(alloc.Get(65536)))
  19. assert.Equal(t, 65537, len(alloc.Get(65537)))
  20. }
  21. func TestAllocPut(t *testing.T) {
  22. alloc := NewAllocator()
  23. assert.Nil(t, alloc.Put(nil), "put nil misbehavior")
  24. assert.NotNil(t, alloc.Put(make([]byte, 3)), "put elem:3 []bytes misbehavior")
  25. assert.Nil(t, alloc.Put(make([]byte, 4)), "put elem:4 []bytes misbehavior")
  26. assert.Nil(t, alloc.Put(make([]byte, 1023, 1024)), "put elem:1024 []bytes misbehavior")
  27. assert.Nil(t, alloc.Put(make([]byte, 65536)), "put elem:65536 []bytes misbehavior")
  28. assert.Nil(t, alloc.Put(make([]byte, 65537)), "put elem:65537 []bytes misbehavior")
  29. }
  30. func TestAllocPutThenGet(t *testing.T) {
  31. alloc := NewAllocator()
  32. data := alloc.Get(4)
  33. alloc.Put(data)
  34. newData := alloc.Get(4)
  35. assert.Equal(t, cap(data), cap(newData), "different cap while alloc.Get()")
  36. }
  37. func BenchmarkMSB(b *testing.B) {
  38. for i := 0; i < b.N; i++ {
  39. msb(randv2.Int())
  40. }
  41. }