slice_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package test
  2. import (
  3. "testing"
  4. "github.com/modern-go/reflect2"
  5. "github.com/modern-go/test"
  6. "github.com/modern-go/test/must"
  7. "context"
  8. )
  9. func Test_slice(t *testing.T) {
  10. var pInt = func(val int) *int {
  11. return &val
  12. }
  13. t.Run("New", testOp(func(api reflect2.API) interface{} {
  14. valType := api.TypeOf([]int{})
  15. obj := *valType.New().(*[]int)
  16. obj = append(obj, 1)
  17. return obj
  18. }))
  19. t.Run("IsNil", testOp(func(api reflect2.API) interface{} {
  20. valType := api.TypeOf([]int{})
  21. var nilSlice []int
  22. s := []int{}
  23. return []interface{}{
  24. valType.IsNil(&nilSlice),
  25. valType.IsNil(&s),
  26. valType.IsNil(nil),
  27. }
  28. }))
  29. t.Run("SetNil", testOp(func(api reflect2.API) interface{} {
  30. valType := api.TypeOf([]int{}).(reflect2.SliceType)
  31. s := []int{1}
  32. valType.SetNil(&s)
  33. return s
  34. }))
  35. t.Run("Set", testOp(func(api reflect2.API) interface{} {
  36. valType := api.TypeOf([]int{}).(reflect2.SliceType)
  37. s1 := []int{1}
  38. s2 := []int{2}
  39. valType.Set(&s1, &s2)
  40. return s1
  41. }))
  42. t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
  43. valType := api.TypeOf([]int{}).(reflect2.SliceType)
  44. obj := *(valType.MakeSlice(5, 10).(*[]int))
  45. obj[0] = 100
  46. obj[4] = 20
  47. return obj
  48. }))
  49. t.Run("UnsafeMakeSlice", test.Case(func(ctx context.Context) {
  50. valType := reflect2.TypeOf([]int{}).(reflect2.SliceType)
  51. obj := valType.UnsafeMakeSlice(5, 10)
  52. must.Equal(&[]int{0, 0, 0, 0, 0}, valType.PackEFace(obj))
  53. }))
  54. t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
  55. obj := []int{1, 2}
  56. valType := api.TypeOf(obj).(reflect2.SliceType)
  57. valType.SetIndex(&obj, 0, pInt(100))
  58. valType.SetIndex(&obj, 1, pInt(20))
  59. return obj
  60. }))
  61. t.Run("UnsafeSetIndex", test.Case(func(ctx context.Context) {
  62. obj := []int{1, 2}
  63. valType := reflect2.TypeOf(obj).(reflect2.SliceType)
  64. valType.UnsafeSetIndex(reflect2.PtrOf(obj), 0, reflect2.PtrOf(100))
  65. valType.UnsafeSetIndex(reflect2.PtrOf(obj), 1, reflect2.PtrOf(10))
  66. must.Equal([]int{100, 10}, obj)
  67. }))
  68. t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
  69. obj := []int{1, 2}
  70. valType := api.TypeOf(obj).(reflect2.SliceType)
  71. return []interface{}{
  72. valType.GetIndex(&obj, 1).(*int),
  73. }
  74. }))
  75. t.Run("UnsafeGetIndex", test.Case(func(ctx context.Context) {
  76. obj := []int{1, 2}
  77. valType := reflect2.TypeOf(obj).(reflect2.SliceType)
  78. elem0 := valType.UnsafeGetIndex(reflect2.PtrOf(obj), 0)
  79. must.Equal(1, *(*int)(elem0))
  80. elem1 := valType.UnsafeGetIndex(reflect2.PtrOf(obj), 1)
  81. must.Equal(2, *(*int)(elem1))
  82. }))
  83. t.Run("Append", testOp(func(api reflect2.API) interface{} {
  84. obj := make([]int, 2, 3)
  85. obj[0] = 1
  86. obj[1] = 2
  87. valType := api.TypeOf(obj).(reflect2.SliceType)
  88. ptr := &obj
  89. valType.Append(ptr, pInt(3))
  90. // will trigger grow
  91. valType.Append(ptr, pInt(4))
  92. return ptr
  93. }))
  94. t.Run("UnsafeAppend", test.Case(func(ctx context.Context) {
  95. obj := make([]int, 2, 3)
  96. obj[0] = 1
  97. obj[1] = 2
  98. valType := reflect2.TypeOf(obj).(reflect2.SliceType)
  99. ptr := reflect2.PtrOf(obj)
  100. valType.UnsafeAppend(ptr, reflect2.PtrOf(3))
  101. valType.UnsafeAppend(ptr, reflect2.PtrOf(4))
  102. must.Equal(&[]int{1, 2, 3, 4}, valType.PackEFace(ptr))
  103. }))
  104. t.Run("Grow", testOp(func(api reflect2.API) interface{} {
  105. obj := make([]int, 2, 3)
  106. obj[0] = 1
  107. obj[1] = 2
  108. valType := reflect2.TypeOf(obj).(reflect2.SliceType)
  109. valType.Grow(&obj, 4)
  110. return obj
  111. }))
  112. }