소스 검색

fix all tests

Tao Wen 7 년 전
부모
커밋
b3b30f3fbe

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
 /vendor
+/coverage.txt

+ 16 - 0
safe_struct.go

@@ -10,4 +10,20 @@ func (type2 *safeStructType) FieldByName(name string) StructField {
 		panic("field " + name + " not found")
 	}
 	return &safeField{StructField: field}
+}
+
+func (type2 *safeStructType) Field(i int) StructField {
+	return &safeField{StructField: type2.Type.Field(i)}
+}
+
+func (type2 *safeStructType) FieldByIndex(index []int) StructField {
+	return &safeField{StructField: type2.Type.FieldByIndex(index)}
+}
+
+func (type2 *safeStructType) FieldByNameFunc(match func(string) bool) StructField {
+	field, found := type2.Type.FieldByNameFunc(match)
+	if !found {
+		panic("field match condition not found in " + type2.Type.String())
+	}
+	return &safeField{StructField: field}
 }

+ 12 - 0
test.sh

@@ -0,0 +1,12 @@
+#!/usr/bin/env bash
+
+set -e
+echo "" > coverage.txt
+
+for d in $(go list ./... | grep -v vendor); do
+    go test -coverprofile=profile.out -coverpkg=github.com/modern-go/reflect2 $d
+    if [ -f profile.out ]; then
+        cat profile.out >> coverage.txt
+        rm profile.out
+    fi
+done

+ 23 - 10
tests/map_elem_array_test.go

@@ -6,39 +6,52 @@ import (
 )
 
 func Test_map_elem_array(t *testing.T) {
+	var pInt = func(val int) *int {
+		return &val
+	}
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[int][2]*int{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, [2]*int{(*int)(reflect2.PtrOf(1)), (*int)(reflect2.PtrOf(2))})
-		valType.SetIndex(obj, 3, [2]*int{(*int)(reflect2.PtrOf(3)), (*int)(reflect2.PtrOf(4))})
+		elem1 := [2]*int{(*int)(reflect2.PtrOf(1)), (*int)(reflect2.PtrOf(2))}
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := [2]*int{(*int)(reflect2.PtrOf(3)), (*int)(reflect2.PtrOf(4))}
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 	t.Run("SetIndex zero length array", testOp(func(api reflect2.API) interface{} {
 		obj := map[int][0]*int{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, [0]*int{})
-		valType.SetIndex(obj, 3, [0]*int{})
+		elem1 := [0]*int{}
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := [0]*int{}
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 	t.Run("SetIndex single ptr array", testOp(func(api reflect2.API) interface{} {
 		obj := map[int][1]*int{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, [1]*int{(*int)(reflect2.PtrOf(1))})
-		valType.SetIndex(obj, 3, [1]*int{})
+		elem1 := [1]*int{(*int)(reflect2.PtrOf(1))}
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := [1]*int{}
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 	t.Run("SetIndex single chan array", testOp(func(api reflect2.API) interface{} {
 		obj := map[int][1]chan int{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, [1]chan int{})
-		valType.SetIndex(obj, 3, [1]chan int{})
+		elem1 := [1]chan int{}
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := [1]chan int{}
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 	t.Run("SetIndex single func array", testOp(func(api reflect2.API) interface{} {
 		obj := map[int][1]func(){}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, [1]func(){})
-		valType.SetIndex(obj, 3, [1]func(){})
+		elem1 := [1]func(){}
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := [1]func(){}
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 }

+ 13 - 6
tests/map_elem_bytes_test.go

@@ -6,29 +6,36 @@ import (
 	"github.com/modern-go/test"
 	"github.com/modern-go/test/must"
 	"context"
+	"unsafe"
 )
 
 func Test_map_elem_bytes(t *testing.T) {
+	var pInt = func(val int) *int {
+		return &val
+	}
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[int][]byte{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, []byte("hello"))
-		valType.SetIndex(obj, 3, nil)
+		elem1 := []byte("hello")
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := []byte(nil)
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 	t.Run("UnsafeSetIndex", test.Case(func(ctx context.Context) {
 		obj := map[int][]byte{}
 		valType := reflect2.TypeOf(obj).(reflect2.MapType)
 		hello := []byte("hello")
-		valType.UnsafeSetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(2), reflect2.PtrOf(hello))
-		valType.UnsafeSetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(3), nil)
+		valType.UnsafeSetIndex(unsafe.Pointer(&obj), reflect2.PtrOf(2), unsafe.Pointer(&hello))
+		elem2 := []byte(nil)
+		valType.UnsafeSetIndex(unsafe.Pointer(&obj), reflect2.PtrOf(3), unsafe.Pointer(&elem2))
 		must.Equal([]byte("hello"), obj[2])
 		must.Nil(obj[3])
 	}))
 	t.Run("UnsafeGetIndex", test.Case(func(ctx context.Context) {
 		obj := map[int][]byte{2: []byte("hello")}
 		valType := reflect2.TypeOf(obj).(reflect2.MapType)
-		elem := valType.UnsafeGetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(2))
-		must.Equal([]byte("hello"), valType.Elem().PackEFace(elem))
+		elem := valType.UnsafeGetIndex(unsafe.Pointer(&obj), reflect2.PtrOf(2))
+		must.Equal([]byte("hello"), valType.Elem().UnsafeIndirect(elem))
 	}))
 }

+ 16 - 11
tests/map_elem_eface_test.go

@@ -10,39 +10,44 @@ import (
 )
 
 func Test_map_elem_eface(t *testing.T) {
+	var pInt = func(val int) *int {
+		return &val
+	}
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[int]interface{}{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, 4)
-		valType.SetIndex(obj, 3, nil)
+		elem1 := interface{}(4)
+		valType.SetIndex(&obj, pInt(2), &elem1)
+		elem2 := interface{}(nil)
+		valType.SetIndex(&obj, pInt(3), &elem2)
 		return obj
 	}))
 	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[int]interface{}{3: 9, 2: nil}
 		valType := api.TypeOf(obj).(reflect2.MapType)
 		return []interface{}{
-			valType.GetIndex(obj, 3),
-			valType.GetIndex(obj, 2),
-			valType.GetIndex(obj, 0),
+			valType.GetIndex(&obj, pInt(3)),
+			valType.GetIndex(&obj, pInt(2)),
+			valType.GetIndex(&obj, pInt(0)),
 		}
 	}))
 	t.Run("TryGetIndex", test.Case(func(ctx context.Context) {
 		obj := map[int]interface{}{3: 9, 2: nil}
 		valType := reflect2.TypeOf(obj).(reflect2.MapType)
-		elem, found := valType.TryGetIndex(obj, 3)
-		must.Equal(9, elem)
+		elem, found := valType.TryGetIndex(&obj, pInt(3))
+		must.Equal(9, *elem.(*interface{}))
 		must.Pass(found)
-		elem, found = valType.TryGetIndex(obj, 2)
-		must.Nil(elem)
+		elem, found = valType.TryGetIndex(&obj, pInt(2))
+		must.Nil(*elem.(*interface{}))
 		must.Pass(found)
-		elem, found = valType.TryGetIndex(obj, 0)
+		elem, found = valType.TryGetIndex(&obj, pInt(0))
 		must.Nil(elem)
 		must.Pass(!found)
 	}))
 	t.Run("Iterate", testOp(func(api reflect2.API) interface{} {
 		obj := map[int]interface{}{2: 4}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		iter := valType.Iterate(obj)
+		iter := valType.Iterate(&obj)
 		must.Pass(iter.HasNext(), "api", api)
 		key1, elem1 := iter.Next()
 		must.Pass(!iter.HasNext(), "api", api)

+ 13 - 10
tests/map_elem_struct_test.go

@@ -7,11 +7,14 @@ import (
 )
 
 func Test_map_elem_struct(t *testing.T) {
+	var pInt = func(val int) *int {
+		return &val
+	}
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[int]time.Time{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, time.Time{})
-		valType.SetIndex(obj, 3, time.Time{})
+		valType.SetIndex(&obj, pInt(2), &time.Time{})
+		valType.SetIndex(&obj, pInt(3), &time.Time{})
 		return obj
 	}))
 	t.Run("SetIndex single ptr struct", testOp(func(api reflect2.API) interface{} {
@@ -20,8 +23,8 @@ func Test_map_elem_struct(t *testing.T) {
 		}
 		obj := map[int]TestObject{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, TestObject{})
-		valType.SetIndex(obj, 3, TestObject{})
+		valType.SetIndex(&obj, pInt(2), &TestObject{})
+		valType.SetIndex(&obj, pInt(3), &TestObject{})
 		return obj
 	}))
 	t.Run("SetIndex single map struct", testOp(func(api reflect2.API) interface{} {
@@ -30,8 +33,8 @@ func Test_map_elem_struct(t *testing.T) {
 		}
 		obj := map[int]TestObject{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, TestObject{})
-		valType.SetIndex(obj, 3, TestObject{})
+		valType.SetIndex(&obj, pInt(2), &TestObject{})
+		valType.SetIndex(&obj, pInt(3), &TestObject{})
 		return obj
 	}))
 	t.Run("SetIndex single chan struct", testOp(func(api reflect2.API) interface{} {
@@ -40,8 +43,8 @@ func Test_map_elem_struct(t *testing.T) {
 		}
 		obj := map[int]TestObject{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, TestObject{})
-		valType.SetIndex(obj, 3, TestObject{})
+		valType.SetIndex(&obj, pInt(2), &TestObject{})
+		valType.SetIndex(&obj, pInt(3), &TestObject{})
 		return obj
 	}))
 	t.Run("SetIndex single func struct", testOp(func(api reflect2.API) interface{} {
@@ -50,8 +53,8 @@ func Test_map_elem_struct(t *testing.T) {
 		}
 		obj := map[int]TestObject{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, 2, TestObject{})
-		valType.SetIndex(obj, 3, TestObject{})
+		valType.SetIndex(&obj, pInt(2), &TestObject{})
+		valType.SetIndex(&obj, pInt(3), &TestObject{})
 		return obj
 	}))
 }

+ 1 - 1
tests/map_key_eface_test.go

@@ -34,7 +34,7 @@ func Test_map_key_eface(t *testing.T) {
 	t.Run("Iterate", testOp(func(api reflect2.API) interface{} {
 		obj := map[interface{}]int{2: 4}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		iter := valType.Iterate(obj)
+		iter := valType.Iterate(&obj)
 		must.Pass(iter.HasNext(), "api", api)
 		key1, elem1 := iter.Next()
 		must.Pass(!iter.HasNext(), "api", api)

+ 18 - 8
tests/map_key_iface_test.go

@@ -13,14 +13,21 @@ func (err intError) Error() string {
 }
 
 func Test_map_iface_key(t *testing.T) {
+	var pInt = func(val int) *int {
+		return &val
+	}
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[error]int{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		valType.SetIndex(obj, intError(2), 4)
-		valType.SetIndex(obj, intError(2), 9)
-		valType.SetIndex(obj, nil, 9)
+		key1 := error(intError(2))
+		valType.SetIndex(&obj, &key1, pInt(4))
+		key2 := error(intError(2))
+		valType.SetIndex(&obj, &key2, pInt(9))
+		key3 := error(nil)
+		valType.SetIndex(&obj, &key3, pInt(9))
 		must.Panic(func() {
-			valType.SetIndex(obj, "", 9)
+			key4 := ""
+			valType.SetIndex(&obj, &key4, pInt(9))
 		})
 		return obj
 	}))
@@ -28,17 +35,20 @@ func Test_map_iface_key(t *testing.T) {
 		obj := map[error]int{intError(3): 9, intError(2): 4}
 		valType := api.TypeOf(obj).(reflect2.MapType)
 		must.Panic(func() {
-			valType.GetIndex(obj, "")
+			key1 := ""
+			valType.GetIndex(obj, &key1)
 		})
+		key2 := error(intError(3))
+		key3 := error(nil)
 		return []interface{}{
-			valType.GetIndex(obj, intError(3)),
-			valType.GetIndex(obj, nil),
+			valType.GetIndex(&obj, &key2),
+			valType.GetIndex(&obj, &key3),
 		}
 	}))
 	t.Run("Iterate", testOp(func(api reflect2.API) interface{} {
 		obj := map[error]int{intError(2): 4}
 		valType := api.TypeOf(obj).(reflect2.MapType)
-		iter := valType.Iterate(obj)
+		iter := valType.Iterate(&obj)
 		must.Pass(iter.HasNext(), "api", api)
 		key1, elem1 := iter.Next()
 		must.Pass(!iter.HasNext(), "api", api)

+ 10 - 6
tests/map_key_ptr_test.go

@@ -18,8 +18,8 @@ func Test_map_key_ptr(t *testing.T) {
 		obj := map[*int]int{}
 		valType := api.TypeOf(obj).(reflect2.MapType)
 		key := pInt(2)
-		valType.SetIndex(obj, &key, 4)
-		valType.SetIndex(obj, &key, 9)
+		valType.SetIndex(&obj, &key, pInt(4))
+		valType.SetIndex(&obj, &key, pInt(9))
 		//valType.SetIndex(obj, nil, 9)
 		return obj[pInt(2)]
 	}))
@@ -27,16 +27,20 @@ func Test_map_key_ptr(t *testing.T) {
 		obj := map[*int]int{}
 		valType := reflect2.TypeOf(obj).(reflect2.MapType)
 		v := pInt(2)
-		valType.UnsafeSetIndex(reflect2.PtrOf(obj), unsafe.Pointer(v), reflect2.PtrOf(4))
+		valType.UnsafeSetIndex(
+			unsafe.Pointer(&obj), unsafe.Pointer(&v), reflect2.PtrOf(4))
 		must.Equal(4, obj[v])
 	}))
 	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := map[*int]int{pInt(3): 9, pInt(2): 4}
 		valType := api.TypeOf(obj).(reflect2.MapType)
+		key1 := pInt(3)
+		key2 := pInt(2)
+		key3 := (*int)(nil)
 		return []interface{}{
-			valType.GetIndex(obj, pInt(3)),
-			valType.GetIndex(obj, pInt(2)),
-			valType.GetIndex(obj, nil),
+			valType.GetIndex(&obj, &key1),
+			valType.GetIndex(&obj, &key2),
+			valType.GetIndex(&obj, &key3),
 		}
 	}))
 	t.Run("Iterate", testOp(func(api reflect2.API) interface{} {

+ 16 - 8
tests/slice_array_test.go

@@ -9,29 +9,37 @@ func Test_slice_array(t *testing.T) {
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := [][1]int{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, [1]int{1})
-		valType.SetIndex(obj, 1, [1]int{2})
+		elem1 := [1]int{1}
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := [1]int{2}
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 	t.Run("SetIndex single ptr struct", testOp(func(api reflect2.API) interface{} {
 		obj := [][1]*int{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, [1]*int{})
-		valType.SetIndex(obj, 1, [1]*int{})
+		elem1 := [1]*int{}
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := [1]*int{}
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 	t.Run("SetIndex single chan struct", testOp(func(api reflect2.API) interface{} {
 		obj := [][1]chan int{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, [1]chan int{})
-		valType.SetIndex(obj, 1, [1]chan int{})
+		elem1 := [1]chan int{}
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := [1]chan int{}
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 	t.Run("SetIndex single func struct", testOp(func(api reflect2.API) interface{} {
 		obj := [][1]func(){{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, [1]func(){})
-		valType.SetIndex(obj, 1, [1]func(){})
+		elem1 := [1]func(){}
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := [1]func(){}
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 }

+ 4 - 2
tests/slice_bytes_test.go

@@ -9,8 +9,10 @@ func Test_slice_bytes(t *testing.T) {
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := [][]byte{[]byte("hello"), []byte("world")}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(&obj, 0, []byte("hi"))
-		valType.SetIndex(&obj, 1, []byte("there"))
+		elem1 := []byte("hi")
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := []byte("there")
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 }

+ 14 - 15
tests/slice_eface_test.go

@@ -8,7 +8,6 @@ import (
 	"github.com/modern-go/test/must"
 	"unsafe"
 	"github.com/modern-go/test/should"
-	"fmt"
 	"context"
 )
 
@@ -16,17 +15,17 @@ func Test_slice_eface(t *testing.T) {
 	t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
 		valType := api.TypeOf([]interface{}{}).(reflect2.SliceType)
 		obj := valType.MakeSlice(5, 10)
-		obj.([]interface{})[0] = 100
-		obj.([]interface{})[4] = 20
+		(*obj.(*[]interface{}))[0] = 100
+		(*obj.(*[]interface{}))[4] = 20
 		return obj
 	}))
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := []interface{}{1, nil}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
 		elem0 := interface{}(100)
-		valType.SetIndex(obj, 0, &elem0)
+		valType.SetIndex(&obj, 0, &elem0)
 		elem1 := interface{}(20)
-		valType.SetIndex(obj, 1, &elem1)
+		valType.SetIndex(&obj, 1, &elem1)
 		return obj
 	}))
 	t.Run("UnsafeSetIndex", test.Case(func(ctx context.Context) {
@@ -41,16 +40,15 @@ func Test_slice_eface(t *testing.T) {
 	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := []interface{}{1, nil}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		fmt.Println(api, *valType.GetIndex(obj, 0).(*interface{}))
 		return []interface{}{
-			valType.GetIndex(obj, 0),
-			valType.GetIndex(obj, 1),
+			valType.GetIndex(&obj, 0),
+			valType.GetIndex(&obj, 1),
 		}
 	}))
 	t.Run("UnsafeGetIndex", test.Case(func(ctx context.Context) {
 		obj := []interface{}{1, nil}
 		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
-		elem0 := valType.UnsafeGetIndex(reflect2.PtrOf(obj), 0)
+		elem0 := valType.UnsafeGetIndex(unsafe.Pointer(&obj), 0)
 		must.Equal(1, *(*interface{})(elem0))
 	}))
 	t.Run("Append", testOp(func(api reflect2.API) interface{} {
@@ -58,9 +56,11 @@ func Test_slice_eface(t *testing.T) {
 		obj[0] = 1
 		obj[1] = 2
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.Append(&obj, 3)
+		elem1 := interface{}(3)
+		valType.Append(&obj, &elem1)
 		// will trigger grow
-		valType.Append(&obj, 4)
+		elem2 := interface{}(4)
+		valType.Append(&obj, &elem2)
 		return obj
 	}))
 	t.Run("UnsafeAppend", test.Case(func(ctx context.Context) {
@@ -68,11 +68,10 @@ func Test_slice_eface(t *testing.T) {
 		obj[0] = 1
 		obj[1] = 2
 		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
-		ptr := reflect2.PtrOf(obj)
 		var elem2 interface{} = 3
-		valType.UnsafeAppend(ptr, unsafe.Pointer(&elem2))
+		valType.UnsafeAppend(unsafe.Pointer(&obj), unsafe.Pointer(&elem2))
 		var elem3 interface{} = 4
-		valType.UnsafeAppend(ptr, unsafe.Pointer(&elem3))
-		should.Equal([]interface{}{1, 2, 3, 4}, valType.PackEFace(ptr))
+		valType.UnsafeAppend(unsafe.Pointer(&obj), unsafe.Pointer(&elem3))
+		should.Equal([]interface{}{1, 2, 3, 4}, valType.UnsafeIndirect(unsafe.Pointer(&obj)))
 	}))
 }

+ 6 - 8
tests/slice_map_test.go

@@ -9,15 +9,15 @@ func Test_slice_map(t *testing.T) {
 	t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
 		valType := api.TypeOf([]map[int]int{}).(reflect2.SliceType)
 		obj := valType.MakeSlice(5, 10)
-		obj.([]map[int]int)[0] = map[int]int{1:1}
-		obj.([]map[int]int)[4] = map[int]int{2:2}
+		(*obj.(*[]map[int]int))[0] = map[int]int{1:1}
+		(*obj.(*[]map[int]int))[4] = map[int]int{2:2}
 		return obj
 	}))
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := []map[int]int{{1: 1}, nil}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, &map[int]int{10:10})
-		valType.SetIndex(obj, 1, &map[int]int{2:2})
+		valType.SetIndex(&obj, 0, &map[int]int{10:10})
+		valType.SetIndex(&obj, 1, &map[int]int{2:2})
 		return obj
 	}))
 	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
@@ -26,8 +26,6 @@ func Test_slice_map(t *testing.T) {
 		return []interface{}{
 			valType.GetIndex(&obj, 0),
 			valType.GetIndex(&obj, 1),
-			valType.GetIndex(obj, 0),
-			valType.GetIndex(obj, 1),
 		}
 	}))
 	t.Run("Append", testOp(func(api reflect2.API) interface{} {
@@ -35,9 +33,9 @@ func Test_slice_map(t *testing.T) {
 		obj[0] = map[int]int{1:1}
 		obj[1] = map[int]int{2:2}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.Append(obj, map[int]int{3:3})
+		valType.Append(&obj, &map[int]int{3:3})
 		// will trigger grow
-		valType.Append(obj, map[int]int{4:4})
+		valType.Append(&obj, &map[int]int{4:4})
 		return obj
 	}))
 }

+ 14 - 10
tests/slice_ptr_test.go

@@ -17,22 +17,26 @@ func Test_slice_ptr(t *testing.T) {
 	t.Run("MakeSlice", testOp(func(api reflect2.API) interface{} {
 		valType := api.TypeOf([]*int{}).(reflect2.SliceType)
 		obj := valType.MakeSlice(5, 10)
-		obj.([]*int)[0] = pInt(1)
-		obj.([]*int)[4] = pInt(5)
+		(*obj.(*[]*int))[0] = pInt(1)
+		(*obj.(*[]*int))[4] = pInt(5)
 		return obj
 	}))
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := []*int{pInt(1), nil}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, pInt(2))
-		valType.SetIndex(obj, 1, pInt(3))
+		elem1 := pInt(2)
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := pInt(3)
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 	t.Run("UnsafeSetIndex", test.Case(func(ctx context.Context) {
 		obj := []*int{pInt(1), nil}
 		valType := reflect2.TypeOf(obj).(reflect2.SliceType)
-		valType.UnsafeSetIndex(reflect2.PtrOf(obj), 0, unsafe.Pointer(pInt(2)))
-		valType.UnsafeSetIndex(reflect2.PtrOf(obj), 1, unsafe.Pointer(pInt(1)))
+		elem1 := pInt(2)
+		valType.UnsafeSetIndex(unsafe.Pointer(&obj), 0, unsafe.Pointer(&elem1))
+		elem2 := pInt(1)
+		valType.UnsafeSetIndex(unsafe.Pointer(&obj), 1, unsafe.Pointer(&elem2))
 		must.Equal([]*int{pInt(2), pInt(1)}, obj)
 	}))
 	t.Run("GetIndex", testOp(func(api reflect2.API) interface{} {
@@ -41,8 +45,6 @@ func Test_slice_ptr(t *testing.T) {
 		return []interface{}{
 			valType.GetIndex(&obj, 0),
 			valType.GetIndex(&obj, 1),
-			valType.GetIndex(obj, 0),
-			valType.GetIndex(obj, 1),
 		}
 	}))
 	t.Run("Append", testOp(func(api reflect2.API) interface{} {
@@ -50,9 +52,11 @@ func Test_slice_ptr(t *testing.T) {
 		obj[0] = pInt(1)
 		obj[1] = pInt(2)
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.Append(obj, pInt(3))
+		elem1 := pInt(3)
+		valType.Append(&obj, &elem1)
 		// will trigger grow
-		valType.Append(obj, pInt(4))
+		elem2 := pInt(4)
+		valType.Append(&obj, &elem2)
 		return obj
 	}))
 }

+ 4 - 2
tests/slice_string_test.go

@@ -9,8 +9,10 @@ func Test_slice_string(t *testing.T) {
 	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
 		obj := []string{"hello", "world"}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(&obj, 0, "hi")
-		valType.SetIndex(&obj, 1, "there")
+		elem1 := "hi"
+		valType.SetIndex(&obj, 0, &elem1)
+		elem2 := "there"
+		valType.SetIndex(&obj, 1, &elem2)
 		return obj
 	}))
 }

+ 8 - 8
tests/slice_struct_test.go

@@ -16,8 +16,8 @@ func Test_slice_struct(t *testing.T) {
 		}
 		obj := []TestObject{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, &TestObject{1, 3})
-		valType.SetIndex(obj, 1, &TestObject{2, 4})
+		valType.SetIndex(&obj, 0, &TestObject{1, 3})
+		valType.SetIndex(&obj, 1, &TestObject{2, 4})
 		return obj
 	}))
 	t.Run("SetIndex single ptr struct", testOp(func(api reflect2.API) interface{} {
@@ -26,8 +26,8 @@ func Test_slice_struct(t *testing.T) {
 		}
 		obj := []TestObject{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, &TestObject{pInt(1)})
-		valType.SetIndex(obj, 1, &TestObject{pInt(2)})
+		valType.SetIndex(&obj, 0, &TestObject{pInt(1)})
+		valType.SetIndex(&obj, 1, &TestObject{pInt(2)})
 		return obj
 	}))
 	t.Run("SetIndex single chan struct", testOp(func(api reflect2.API) interface{} {
@@ -36,8 +36,8 @@ func Test_slice_struct(t *testing.T) {
 		}
 		obj := []TestObject{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, TestObject{})
-		valType.SetIndex(obj, 1, TestObject{})
+		valType.SetIndex(&obj, 0, &TestObject{})
+		valType.SetIndex(&obj, 1, &TestObject{})
 		return obj
 	}))
 	t.Run("SetIndex single func struct", testOp(func(api reflect2.API) interface{} {
@@ -46,8 +46,8 @@ func Test_slice_struct(t *testing.T) {
 		}
 		obj := []TestObject{{}, {}}
 		valType := api.TypeOf(obj).(reflect2.SliceType)
-		valType.SetIndex(obj, 0, TestObject{})
-		valType.SetIndex(obj, 1, TestObject{})
+		valType.SetIndex(&obj, 0, &TestObject{})
+		valType.SetIndex(&obj, 1, &TestObject{})
 		return obj
 	}))
 }