slot_pool_test.go 873 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package mem
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "testing"
  5. )
  6. func TestAllocateFree(t *testing.T) {
  7. buf := Allocate(12)
  8. Free(buf)
  9. if cap(buf) != min_size {
  10. t.Errorf("min size error allocated capacity=%d", cap(buf))
  11. }
  12. if len(buf) != 12 {
  13. t.Errorf("size error")
  14. }
  15. buf = Allocate(4883)
  16. Free(buf)
  17. if cap(buf) != 1024<<bitCount(4883) {
  18. t.Errorf("min size error allocated capacity=%d", cap(buf))
  19. }
  20. if len(buf) != 4883 {
  21. t.Errorf("size error")
  22. }
  23. }
  24. func TestAllocateFreeEdgeCases(t *testing.T) {
  25. assert.Equal(t, 1, bitCount(2048))
  26. assert.Equal(t, 2, bitCount(2049))
  27. buf := Allocate(2048)
  28. Free(buf)
  29. buf = Allocate(2049)
  30. Free(buf)
  31. }
  32. func TestBitCount(t *testing.T) {
  33. count := bitCount(12)
  34. if count != 0 {
  35. t.Errorf("bitCount error count=%d", count)
  36. }
  37. if count != bitCount(min_size) {
  38. t.Errorf("bitCount error count=%d", count)
  39. }
  40. }