pool_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package pool
  2. import (
  3. "math/bits"
  4. "math/rand"
  5. "testing"
  6. )
  7. func TestGet(t *testing.T) {
  8. if Get(0) != nil {
  9. t.Fatal(0)
  10. return
  11. }
  12. if len(Get(1)) != 1 {
  13. t.Fatal(1)
  14. return
  15. }
  16. if len(Get(2)) != 2 {
  17. t.Fatal(2)
  18. return
  19. }
  20. if len(Get(3)) != 3 || cap(Get(3)) != 64 {
  21. t.Fatal(3)
  22. return
  23. }
  24. if len(Get(4)) != 4 {
  25. t.Fatal(4)
  26. return
  27. }
  28. if len(Get(1023)) != 1023 || cap(Get(1023)) != 1024 {
  29. t.Fatal(1023)
  30. return
  31. }
  32. if len(Get(1024)) != 1024 {
  33. t.Fatal(1024)
  34. return
  35. }
  36. if len(Get(65536)) != 65536 {
  37. t.Fatal(65536)
  38. return
  39. }
  40. }
  41. func TestPut(t *testing.T) {
  42. if err := Put(nil); err == nil {
  43. t.Fatal("put nil misbehavior")
  44. return
  45. }
  46. b := make([]byte, 3)
  47. if err := Put(b); err == nil {
  48. t.Fatal("put elem:3 []bytes misbehavior")
  49. return
  50. }
  51. b = make([]byte, 64)
  52. if err := Put(b); err != nil {
  53. t.Fatal("put elem:4 []bytes misbehavior")
  54. return
  55. }
  56. b = make([]byte, 1023, 1024)
  57. if err := Put(b); err != nil {
  58. t.Fatal("put elem:1024 []bytes misbehavior")
  59. return
  60. }
  61. b = make([]byte, 65536)
  62. if err := Put(b); err != nil {
  63. t.Fatal("put elem:65536 []bytes misbehavior")
  64. return
  65. }
  66. }
  67. func TestPutThenGet(t *testing.T) {
  68. data := Get(4)
  69. Put(data)
  70. newData := Get(4)
  71. if cap(data) != cap(newData) {
  72. t.Fatal("different cap while Get()")
  73. return
  74. }
  75. }
  76. func BenchmarkMSB(b *testing.B) {
  77. for b.Loop() {
  78. msb(rand.Int())
  79. }
  80. }
  81. func BenchmarkAlloc(b *testing.B) {
  82. for i := 0; b.Loop(); i++ {
  83. pbuf := Get(i % 65536)
  84. Put(pbuf)
  85. }
  86. }
  87. func TestDebrujin(t *testing.T) {
  88. for i := 1; i <= 65536; i++ {
  89. a := int(msb(i))
  90. b := bits.Len(uint(i))
  91. if a+1 != b {
  92. t.Fatal("debrujin")
  93. return
  94. }
  95. }
  96. }
  97. func TestPutSizeMismatch(t *testing.T) {
  98. data := Get(1024)
  99. if data == nil {
  100. t.Fatal("Get(1024) failed")
  101. }
  102. // simulate a slice operation that reduces capacity
  103. // e.g. data[1:]
  104. // cap becomes 1023
  105. sliced := (data)[1:]
  106. if err := Put(sliced); err == nil {
  107. t.Fatal("Put() should fail with mismatched capacity")
  108. }
  109. // simulate a slice operation that keeps capacity
  110. // e.g. data[:512]
  111. // cap remains 1024
  112. sliced = (data)[:512]
  113. if err := Put(sliced); err != nil {
  114. t.Fatal("Put() should succeed with matched capacity")
  115. }
  116. }