handle_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package go_int32_handle
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "runtime"
  6. "sync"
  7. "testing"
  8. )
  9. func TestFunc1(t *testing.T) {
  10. hm := New[string]()
  11. h1 := testUtilAlloc(hm, t, "hello")
  12. h2 := testUtilAlloc(hm, t, "world")
  13. testUtilGet(hm, t, h1, "hello")
  14. testUtilGet(hm, t, h2, "world")
  15. testUtilRelease(hm, t, h1, "hello")
  16. h3 := testUtilAlloc(hm, t, "gensoukyo")
  17. testUtilGet(hm, t, h3, "gensoukyo")
  18. testUtilRelease(hm, t, h2, "world")
  19. testUtilRelease(hm, t, h3, "gensoukyo")
  20. h4 := testUtilAlloc(hm, t, "satori")
  21. h5 := testUtilAlloc(hm, t, "koishi")
  22. h6 := testUtilAlloc(hm, t, "cirno")
  23. testUtilGet(hm, t, h4, "satori")
  24. testUtilGet(hm, t, h5, "koishi")
  25. testUtilGet(hm, t, h6, "cirno")
  26. }
  27. func TestFunc2(t *testing.T) {
  28. hm := NewWithRange[string](0, 32)
  29. var i int32
  30. for i = 0; i < 31; i++ {
  31. testUtilAlloc(hm, t, fmt.Sprintf("val-%d", i))
  32. }
  33. for i = 0; i < 31; i++ {
  34. testUtilGet(hm, t, i+1, fmt.Sprintf("val-%d", i))
  35. }
  36. testUtilAllocOverflow(hm, t, "val-32")
  37. testUtilRelease(hm, t, 15, "val-14")
  38. hr := testUtilAlloc(hm, t, "val-xr-14")
  39. if hr != 15 {
  40. t.Fatalf("allocate behavior mismatch: expected 15, got %d", hr)
  41. }
  42. testUtilGet(hm, t, hr, "val-xr-14")
  43. }
  44. func TestFunc3(t *testing.T) {
  45. tryCount := 65536
  46. hm := New[string]()
  47. tlist := make([]struct {
  48. h int32
  49. v string
  50. }, 0, tryCount)
  51. t.Log("==== Do Random Alloc and Release ====")
  52. for i := 0; i < tryCount; i++ {
  53. ri := rand.Int31()
  54. rk := rand.Intn(10)
  55. if rk == 0 && len(tlist) > 0 {
  56. sel := rand.Intn(len(tlist))
  57. testUtilReleaseSilent(hm, t, tlist[sel].h, tlist[sel].v)
  58. tlist = append(tlist[:sel], tlist[sel+1:]...)
  59. } else {
  60. v := fmt.Sprintf("val-test-%d", ri)
  61. h := testUtilAllocSilent(hm, t, v)
  62. tlist = append(tlist, struct {
  63. h int32
  64. v string
  65. }{h: h, v: v})
  66. }
  67. }
  68. t.Log("==== Do Verification ====")
  69. for i := 0; i < len(tlist); i++ {
  70. testUtilGetSilent(hm, t, tlist[i].h, tlist[i].v)
  71. }
  72. }
  73. func TestFunc4(t *testing.T) {
  74. eachRoutineTryCount := 65536
  75. routineCount := runtime.NumCPU()
  76. hm := New[string]()
  77. wg := sync.WaitGroup{}
  78. wg.Add(routineCount)
  79. t.Logf("routineCount: %d", routineCount)
  80. t.Log("==== Do Parallel Random Alloc and Release Test ====")
  81. for i := 0; i < routineCount; i++ {
  82. go func() {
  83. testUtilDoRandom(hm, eachRoutineTryCount)
  84. wg.Done()
  85. }()
  86. }
  87. wg.Wait()
  88. t.Log("==== Done ====")
  89. }
  90. func BenchmarkAlloc(b *testing.B) {
  91. hm := New[string]()
  92. b.ResetTimer()
  93. b.RunParallel(func(pb *testing.PB) {
  94. for pb.Next() {
  95. _, _ = hm.AllocateAndPut("hello")
  96. }
  97. })
  98. }
  99. func BenchmarkGet(b *testing.B) {
  100. maxCount := 65536
  101. hm := New[string]()
  102. for i := 0; i < maxCount; i++ {
  103. _, _ = hm.AllocateAndPut(fmt.Sprintf("val-%d", i))
  104. }
  105. b.ResetTimer()
  106. b.RunParallel(func(pb *testing.PB) {
  107. for pb.Next() {
  108. _, _ = hm.Get(int32(rand.Intn(maxCount)))
  109. }
  110. })
  111. }
  112. func BenchmarkComprehensive(b *testing.B) {
  113. hm := New[string]()
  114. b.ResetTimer()
  115. b.RunParallel(func(pb *testing.PB) {
  116. for pb.Next() {
  117. op := rand.Intn(3)
  118. switch op {
  119. case 0:
  120. _, _ = hm.AllocateAndPut("hello")
  121. case 1:
  122. if hm.Count() <= 0 {
  123. _, _ = hm.AllocateAndPut("hello")
  124. } else {
  125. _, _ = hm.Get(int32(rand.Intn(hm.Count())))
  126. }
  127. case 2:
  128. if hm.Count() <= 0 {
  129. _, _ = hm.AllocateAndPut("hello")
  130. } else {
  131. hm.ReleaseSilently(int32(rand.Intn(hm.Count())))
  132. }
  133. }
  134. }
  135. })
  136. }
  137. func testUtilDoRandom(hm *HandleManager[string], tryCount int) {
  138. tlist := make([]struct {
  139. h int32
  140. v string
  141. }, 0, tryCount)
  142. for i := 0; i < tryCount; i++ {
  143. ri := rand.Int31()
  144. rk := rand.Intn(10)
  145. if rk == 0 && len(tlist) > 0 {
  146. sel := rand.Intn(len(tlist))
  147. h := tlist[sel].h
  148. v := tlist[sel].v
  149. releasedVal, ok := hm.Release(h)
  150. if !ok {
  151. panic(fmt.Errorf("failed to release handle %d: not found", h))
  152. }
  153. if releasedVal != v {
  154. panic(fmt.Errorf("release handle %d result mismatched: expected %s, got %s", h, v, releasedVal))
  155. }
  156. tlist = append(tlist[:sel], tlist[sel+1:]...)
  157. } else {
  158. v := fmt.Sprintf("val-test-%d", ri)
  159. h, err := hm.AllocateAndPut(v)
  160. if err != nil {
  161. panic(fmt.Errorf("failed to allocate handle: %v", err))
  162. }
  163. tlist = append(tlist, struct {
  164. h int32
  165. v string
  166. }{h: h, v: v})
  167. }
  168. }
  169. for i := 0; i < len(tlist); i++ {
  170. h := tlist[i].h
  171. v := tlist[i].v
  172. vr, ok := hm.Get(h)
  173. if !ok {
  174. panic(fmt.Errorf("failed to get handle %d: not found", h))
  175. return
  176. }
  177. if vr != v {
  178. panic(fmt.Errorf("get handle %d result mismatched: expected %s, got %s", h, v, vr))
  179. return
  180. }
  181. }
  182. }
  183. func testUtilAlloc(hm *HandleManager[string], t *testing.T, val string) int32 {
  184. h, err := hm.AllocateAndPut(val)
  185. if err != nil {
  186. t.Fatalf("failed to allocate handle: %v", err)
  187. return 0
  188. }
  189. t.Logf("allocated handle %d", h)
  190. return h
  191. }
  192. func testUtilAllocSilent(hm *HandleManager[string], t *testing.T, val string) int32 {
  193. h, err := hm.AllocateAndPut(val)
  194. if err != nil {
  195. t.Fatalf("failed to allocate handle: %v", err)
  196. return 0
  197. }
  198. return h
  199. }
  200. func testUtilAllocOverflow(hm *HandleManager[string], t *testing.T, val string) {
  201. _, err := hm.AllocateAndPut(val)
  202. if err == ErrHandleExceedMax {
  203. t.Log("allocated a overflowed handle got expected error")
  204. return
  205. } else {
  206. t.Fatalf("allocated a overflowed handle got unexpected error: %v", err)
  207. return
  208. }
  209. }
  210. func testUtilGet(hm *HandleManager[string], t *testing.T, h int32, expected string) {
  211. v, ok := hm.Get(h)
  212. if !ok {
  213. t.Fatalf("failed to get handle %d: not found", h)
  214. return
  215. }
  216. if v != expected {
  217. t.Fatalf("get handle %d result mismatched: expected %s, got %s", h, expected, v)
  218. return
  219. }
  220. t.Logf("get handle %d: %v", h, v)
  221. }
  222. func testUtilGetSilent(hm *HandleManager[string], t *testing.T, h int32, expected string) {
  223. v, ok := hm.Get(h)
  224. if !ok {
  225. t.Fatalf("failed to get handle %d: not found", h)
  226. return
  227. }
  228. if v != expected {
  229. t.Fatalf("get handle %d result mismatched: expected %s, got %s", h, expected, v)
  230. return
  231. }
  232. }
  233. func testUtilRelease(hm *HandleManager[string], t *testing.T, h int32, expected string) {
  234. releasedVal, ok := hm.Release(h)
  235. if !ok {
  236. t.Fatalf("failed to release handle %d: not found", h)
  237. return
  238. }
  239. if releasedVal != expected {
  240. t.Fatalf("release handle %d result mismatched: expected %s, got %s", h, expected, releasedVal)
  241. return
  242. }
  243. t.Logf("release handle %d ok", h)
  244. }
  245. func testUtilReleaseSilent(hm *HandleManager[string], t *testing.T, h int32, expected string) {
  246. releasedVal, ok := hm.Release(h)
  247. if !ok {
  248. t.Fatalf("failed to release handle %d: not found", h)
  249. return
  250. }
  251. if releasedVal != expected {
  252. t.Fatalf("release handle %d result mismatched: expected %s, got %s", h, expected, releasedVal)
  253. return
  254. }
  255. }