github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/map_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 "github.com/v2pro/plz/reflect2" 6 "github.com/v2pro/plz/test/must" 7 "github.com/v2pro/plz/countlog" 8 "github.com/v2pro/plz/test" 9 "unsafe" 10 "reflect" 11 ) 12 13 func Test_map(t *testing.T) { 14 var pInt = func(val int) *int { 15 return &val 16 } 17 t.Run("New", testOp(func(api reflect2.API) interface{} { 18 valType := api.TypeOf(map[int]int{}) 19 m := valType.New().(*map[int]int) 20 return m 21 })) 22 t.Run("IsNil", testOp(func(api reflect2.API) interface{} { 23 valType := api.TypeOf(map[int]int{}) 24 var nilMap map[int]int 25 m := map[int]int{} 26 return []interface{}{ 27 valType.IsNil(&nilMap), 28 valType.IsNil(&m), 29 } 30 })) 31 t.Run("MakeMap", testOp(func(api reflect2.API) interface{} { 32 valType := api.TypeOf(map[int]int{}).(reflect2.MapType) 33 m := *(valType.MakeMap(0).(*map[int]int)) 34 m[2] = 4 35 m[3] = 9 36 return m 37 })) 38 t.Run("UnsafeMakeMap", test.Case(func(ctx *countlog.Context) { 39 valType := reflect2.TypeOf(map[int]int{}).(reflect2.MapType) 40 m := *(*map[int]int)(valType.UnsafeMakeMap(0)) 41 m[2] = 4 42 m[3] = 9 43 })) 44 t.Run("PackEFace", test.Case(func(ctx *countlog.Context) { 45 valType := reflect2.TypeOf(map[int]int{}).(reflect2.MapType) 46 m := valType.UnsafeMakeMap(0) 47 must.Equal(&map[int]int{}, valType.PackEFace(unsafe.Pointer(m))) 48 })) 49 t.Run("Indirect", testOp(func(api reflect2.API) interface{} { 50 valType := reflect2.TypeOf(map[int]int{}) 51 return valType.Indirect(&map[int]int{}) 52 })) 53 t.Run("SetIndex", testOp(func(api reflect2.API) interface{} { 54 obj := map[int]int{} 55 valType := api.TypeOf(obj).(reflect2.MapType) 56 valType.SetIndex(&obj, pInt(2), pInt(4)) 57 valType.SetIndex(&obj, pInt(3), pInt(9)) 58 must.Equal(4, obj[2]) 59 return obj 60 })) 61 t.Run("UnsafeSetIndex", test.Case(func(ctx *countlog.Context) { 62 obj := map[int]int{} 63 valType := reflect2.TypeOf(obj).(reflect2.MapType) 64 valType.UnsafeSetIndex(unsafe.Pointer(&obj), reflect2.PtrOf(2), reflect2.PtrOf(4)) 65 must.Equal(map[int]int{2: 4}, obj) 66 })) 67 t.Run("GetIndex", testOp(func(api reflect2.API) interface{} { 68 obj := map[int]int{3: 9, 2: 4} 69 valType := api.TypeOf(obj).(reflect2.MapType) 70 return []interface{}{ 71 *valType.GetIndex(&obj, pInt(3)).(*int), 72 valType.GetIndex(&obj, pInt(0)).(*int), 73 } 74 })) 75 t.Run("UnsafeGetIndex", test.Case(func(ctx *countlog.Context) { 76 obj := map[int]int{3: 9, 2: 4} 77 valType := reflect2.TypeOf(obj).(reflect2.MapType) 78 elem := valType.UnsafeGetIndex(unsafe.Pointer(&obj), reflect2.PtrOf(3)) 79 must.Equal(9, *(*int)(elem)) 80 })) 81 t.Run("Iterate", testOp(func(api reflect2.API) interface{} { 82 obj := map[int]int{2: 4} 83 valType := api.TypeOf(obj).(reflect2.MapType) 84 iter := valType.Iterate(&obj) 85 must.Pass(iter.HasNext(), "api", api) 86 key1, elem1 := iter.Next() 87 must.Pass(!iter.HasNext(), "api", api) 88 return []interface{}{key1, elem1} 89 })) 90 t.Run("UnsafeIterate", test.Case(func(ctx *countlog.Context) { 91 obj := map[int]int{2: 4} 92 valType := reflect2.TypeOf(obj).(reflect2.MapType) 93 iter := valType.UnsafeIterate(unsafe.Pointer(&obj)) 94 must.Pass(iter.HasNext()) 95 key, elem := iter.UnsafeNext() 96 must.Equal(2, *(*int)(key)) 97 must.Equal(4, *(*int)(elem)) 98 })) 99 } 100 101 func Benchmark_map_unsafe(b *testing.B) { 102 obj := map[int]int{} 103 valType := reflect2.TypeOf(obj).(*reflect2.UnsafeMapType) 104 m := unsafe.Pointer(&obj) 105 b.ReportAllocs() 106 b.ResetTimer() 107 for i := 0; i < b.N; i++ { 108 valType.UnsafeSetIndex(m, reflect2.PtrOf(2), reflect2.PtrOf(4)) 109 } 110 } 111 112 func Benchmark_map_safe(b *testing.B) { 113 obj := map[int]int{} 114 val := reflect.ValueOf(obj) 115 b.ReportAllocs() 116 b.ResetTimer() 117 for i := 0; i < b.N; i++ { 118 val.SetMapIndex(reflect.ValueOf(2), reflect.ValueOf(4)) 119 } 120 }