github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/map_key_ptr_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/test" 8 "github.com/v2pro/plz/countlog" 9 "unsafe" 10 ) 11 12 func Test_map_key_ptr(t *testing.T) { 13 var pInt = func(val int) *int { 14 return &val 15 } 16 t.Run("SetIndex", testOp(func(api reflect2.API) interface{} { 17 obj := map[*int]int{} 18 valType := api.TypeOf(obj).(reflect2.MapType) 19 key := pInt(2) 20 valType.SetIndex(obj, &key, 4) 21 valType.SetIndex(obj, &key, 9) 22 //valType.SetIndex(obj, nil, 9) 23 return obj[pInt(2)] 24 })) 25 t.Run("UnsafeSetIndex", test.Case(func(ctx *countlog.Context) { 26 obj := map[*int]int{} 27 valType := reflect2.TypeOf(obj).(reflect2.MapType) 28 v := pInt(2) 29 valType.UnsafeSetIndex(reflect2.PtrOf(obj), unsafe.Pointer(v), reflect2.PtrOf(4)) 30 must.Equal(4, obj[v]) 31 })) 32 t.Run("GetIndex", testOp(func(api reflect2.API) interface{} { 33 obj := map[*int]int{pInt(3): 9, pInt(2): 4} 34 valType := api.TypeOf(obj).(reflect2.MapType) 35 return []interface{}{ 36 valType.GetIndex(obj, pInt(3)), 37 valType.GetIndex(obj, pInt(2)), 38 valType.GetIndex(obj, nil), 39 } 40 })) 41 t.Run("Iterate", testOp(func(api reflect2.API) interface{} { 42 obj := map[*int]int{pInt(2): 4} 43 valType := api.TypeOf(obj).(reflect2.MapType) 44 iter := valType.Iterate(&obj) 45 must.Pass(iter.HasNext(), "api", api) 46 key1, elem1 := iter.Next() 47 must.Pass(!iter.HasNext(), "api", api) 48 return []interface{}{key1, elem1} 49 })) 50 }