alloc_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package gfsmux_test
  2. import (
  3. "math/rand"
  4. "testing"
  5. smux "github.com/johnsonjh/gfsmux"
  6. u "github.com/johnsonjh/leaktestfe"
  7. )
  8. func TestAllocGet(
  9. t *testing.T,
  10. ) {
  11. defer u.Leakplug(
  12. t,
  13. )
  14. alloc := smux.NewAllocator()
  15. if alloc.Get(
  16. 0,
  17. ) != nil {
  18. t.Fatal(
  19. 0,
  20. )
  21. }
  22. if len(
  23. alloc.Get(
  24. 1,
  25. ),
  26. ) != 1 {
  27. t.Fatal(
  28. 1,
  29. )
  30. }
  31. if len(
  32. alloc.Get(
  33. 2,
  34. ),
  35. ) != 2 {
  36. t.Fatal(
  37. 2,
  38. )
  39. }
  40. if len(
  41. alloc.Get(
  42. 3,
  43. ),
  44. ) != 3 || cap(
  45. alloc.Get(
  46. 3,
  47. ),
  48. ) != 4 {
  49. t.Fatal(
  50. 3,
  51. )
  52. }
  53. if len(
  54. alloc.Get(
  55. 4,
  56. ),
  57. ) != 4 {
  58. t.Fatal(
  59. 4,
  60. )
  61. }
  62. if len(
  63. alloc.Get(
  64. 1023,
  65. ),
  66. ) != 1023 || cap(
  67. alloc.Get(
  68. 1023,
  69. ),
  70. ) != 1024 {
  71. t.Fatal(
  72. 1023,
  73. )
  74. }
  75. if len(
  76. alloc.Get(
  77. 1024,
  78. ),
  79. ) != 1024 {
  80. t.Fatal(
  81. 1024,
  82. )
  83. }
  84. if len(
  85. alloc.Get(
  86. 65536,
  87. ),
  88. ) != 65536 {
  89. t.Fatal(
  90. 65536,
  91. )
  92. }
  93. if alloc.Get(
  94. 65537,
  95. ) != nil {
  96. t.Fatal(
  97. 65537,
  98. )
  99. }
  100. }
  101. func TestAllocPut(
  102. t *testing.T,
  103. ) {
  104. defer u.Leakplug(
  105. t,
  106. )
  107. alloc := smux.NewAllocator()
  108. if err := alloc.Put(
  109. nil,
  110. ); err == nil {
  111. t.Fatal(
  112. "put nil misbehavior",
  113. )
  114. }
  115. if err := alloc.Put(
  116. make(
  117. []byte,
  118. 3,
  119. ),
  120. ); err == nil {
  121. t.Fatal(
  122. "put elem:3 []bytes misbehavior",
  123. )
  124. }
  125. if err := alloc.Put(
  126. make(
  127. []byte,
  128. 4,
  129. ),
  130. ); err != nil {
  131. t.Fatal(
  132. "put elem:4 []bytes misbehavior",
  133. )
  134. }
  135. if err := alloc.Put(
  136. make(
  137. []byte,
  138. 1023,
  139. 1024,
  140. ),
  141. ); err != nil {
  142. t.Fatal(
  143. "put elem:1024 []bytes misbehavior",
  144. )
  145. }
  146. if err := alloc.Put(
  147. make(
  148. []byte,
  149. 65536,
  150. ),
  151. ); err != nil {
  152. t.Fatal(
  153. "put elem:65536 []bytes misbehavior",
  154. )
  155. }
  156. if err := alloc.Put(
  157. make(
  158. []byte,
  159. 65537,
  160. ),
  161. ); err == nil {
  162. t.Fatal(
  163. "put elem:65537 []bytes misbehavior",
  164. )
  165. }
  166. }
  167. func TestAllocPutThenGet(
  168. t *testing.T,
  169. ) {
  170. defer u.Leakplug(
  171. t,
  172. )
  173. alloc := smux.NewAllocator()
  174. data := alloc.Get(
  175. 4,
  176. )
  177. alloc.Put(
  178. data,
  179. )
  180. newData := alloc.Get(
  181. 4,
  182. )
  183. if cap(
  184. data,
  185. ) != cap(
  186. newData,
  187. ) {
  188. t.Fatal(
  189. "different cap while alloc.Get()",
  190. )
  191. }
  192. }
  193. func BenchmarkMSB(
  194. b *testing.B,
  195. ) {
  196. for i := 0; i < b.N; i++ {
  197. smux.Smsb(
  198. rand.Int(),
  199. )
  200. }
  201. }