alloc_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package gfsmux_test
  2. import (
  3. "math/rand"
  4. "testing"
  5. smux "go.gridfinity.dev/gfsmux"
  6. u "go.gridfinity.dev/leaktestfe"
  7. )
  8. func TestAllocGet(t *testing.T) {
  9. defer u.Leakplug(t)
  10. alloc := smux.NewAllocator()
  11. if alloc.Get(0) != nil {
  12. t.Fatal(0)
  13. }
  14. if len(alloc.Get(1)) != 1 {
  15. t.Fatal(1)
  16. }
  17. if len(alloc.Get(2)) != 2 {
  18. t.Fatal(2)
  19. }
  20. if len(alloc.Get(3)) != 3 || cap(alloc.Get(3)) != 4 {
  21. t.Fatal(3)
  22. }
  23. if len(alloc.Get(4)) != 4 {
  24. t.Fatal(4)
  25. }
  26. if len(alloc.Get(1023)) != 1023 || cap(alloc.Get(1023)) != 1024 {
  27. t.Fatal(1023)
  28. }
  29. if len(alloc.Get(1024)) != 1024 {
  30. t.Fatal(1024)
  31. }
  32. if len(alloc.Get(65536)) != 65536 {
  33. t.Fatal(65536)
  34. }
  35. if alloc.Get(65537) != nil {
  36. t.Fatal(65537)
  37. }
  38. }
  39. func TestAllocPut(t *testing.T) {
  40. defer u.Leakplug(t)
  41. alloc := smux.NewAllocator()
  42. if err := alloc.Put(nil); err == nil {
  43. t.Fatal("put nil misbehavior")
  44. }
  45. if err := alloc.Put(make([]byte, 3)); err == nil {
  46. t.Fatal("put elem:3 []bytes misbehavior")
  47. }
  48. if err := alloc.Put(make([]byte, 4)); err != nil {
  49. t.Fatal("put elem:4 []bytes misbehavior")
  50. }
  51. if err := alloc.Put(make([]byte, 1023, 1024)); err != nil {
  52. t.Fatal("put elem:1024 []bytes misbehavior")
  53. }
  54. if err := alloc.Put(make([]byte, 65536)); err != nil {
  55. t.Fatal("put elem:65536 []bytes misbehavior")
  56. }
  57. if err := alloc.Put(make([]byte, 65537)); err == nil {
  58. t.Fatal("put elem:65537 []bytes misbehavior")
  59. }
  60. }
  61. func TestAllocPutThenGet(t *testing.T) {
  62. defer u.Leakplug(t)
  63. alloc := smux.NewAllocator()
  64. data := alloc.Get(4)
  65. alloc.Put(data)
  66. newData := alloc.Get(4)
  67. if cap(data) != cap(newData) {
  68. t.Fatal("different cap while alloc.Get()")
  69. }
  70. }
  71. func BenchmarkMSB(b *testing.B) {
  72. for i := 0; i < b.N; i++ {
  73. smux.Smsb(rand.Int())
  74. }
  75. }