github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/internal/reflect/reflect_test.go (about)

     1  package reflect
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"unsafe"
     7  )
     8  
     9  type testStruct struct {
    10  	u1 uintptr
    11  }
    12  
    13  func TestFuncReflect(t *testing.T) {
    14  	// 测试函数的参数列表
    15  	typs := FuncInputTypeListReturnValue([]reflect.Type{
    16  		reflect.TypeOf([]int(nil)),
    17  		reflect.TypeOf((*testStruct)(nil)),
    18  	}, 0, nil, false)
    19  	_ = typs[0].Interface().([]int)
    20  	_ = typs[1].Interface().(*testStruct)
    21  	// 测试函数的返回值列表
    22  	typs2 := FuncOutputTypeList([]reflect.Type{
    23  		reflect.TypeOf((*int)(nil)),
    24  		reflect.TypeOf((*testStruct)(nil)),
    25  	}, func(i int) bool {
    26  		return true
    27  	}, true)
    28  	_ = typs2[0].(*int)
    29  	_ = typs2[1].(*testStruct)
    30  }
    31  
    32  func TestTypeTo(t *testing.T) {
    33  	eface := typeToEfaceNew(reflect.TypeOf(new(int64)))
    34  	_ = eface.(*int64)
    35  	// Map Pointer
    36  	eface = typeToEfaceNew(reflect.TypeOf(new(map[string]int)))
    37  	_ = eface.(*map[string]int)
    38  	// Map No Pointer
    39  	eface = typeToEfaceNew(reflect.TypeOf(map[string]int{}))
    40  	_ = eface.(map[string]int)
    41  
    42  	// Type No New
    43  	eface = typeToEfaceNoNew(reflect.TypeOf(*new(int)), 10)
    44  	if eface != 10 {
    45  		panic(interface{}("typeToEfaceNoNew return value failed"))
    46  	}
    47  }
    48  
    49  func TestConcurrentTypeTo(t *testing.T) {
    50  
    51  }
    52  
    53  func TestReflectConv(t *testing.T) {
    54  	i := typeToEfaceNew(reflect.TypeOf(new(int64)))
    55  	_ = i.(*int64)
    56  	v, _ := ToTypePtr(map[string]int{"hello": 1111})
    57  	v = createMapPtr(map[int]int{1: 1})
    58  	mapV := v.(*map[int]int)
    59  	(*mapV)[1] = 2
    60  }
    61  
    62  func createMapPtr(val interface{}) interface{} {
    63  	ptrTyp := reflect.PtrTo(reflect.TypeOf(val))
    64  	eface := &Eface{}
    65  	eface.typ = (*[2]unsafe.Pointer)(unsafe.Pointer(&ptrTyp))[1]
    66  	var ptr = reflect.ValueOf(val).Pointer()
    67  	eface.data = unsafe.Pointer(&ptr)
    68  	return *(*interface{})(unsafe.Pointer(eface))
    69  }
    70  
    71  func TestDeepEqualNotType(t *testing.T) {
    72  	normalCmp := []interface{}{
    73  		123, 123,
    74  		"str1", "str1",
    75  		"str2", "str2",
    76  		map[string]int{"heheda": 123},
    77  		map[string]int{"heheda": 123},
    78  	}
    79  	for i := 0; i < len(normalCmp); i += 2 {
    80  		if !DeepEqualNotType(normalCmp[i], normalCmp[i+1]) {
    81  			t.Fatal("DeepEqualNotType normalCmp is not equal, index == ", i)
    82  		}
    83  	}
    84  	sliceCmp1 := []interface{}{
    85  		[]interface{}{1, 2, 3, 4},
    86  		[]int{1, 2, 3, 4},
    87  		[]interface{}{"s1", "s2", "s3"},
    88  		[]string{"s1", "s2", "s3"},
    89  		[]interface{}{"s1", 123, []interface{}{"hehe", "haha"}},
    90  		[]interface{}{"s1", 123, []string{"hehe", "haha"}},
    91  	}
    92  	for i := 0; i < len(sliceCmp1); i += 2 {
    93  		if !DeepEqualNotType(sliceCmp1[i], sliceCmp1[i+1]) {
    94  			t.Fatal("DeepEqualNotType sliceCmp1 is not equal, index == ", i)
    95  		}
    96  	}
    97  	sliceCmp2 := []interface{}{
    98  		[]interface{}{1, "heheda", "lalala", "wahaha"},
    99  		[]interface{}{1, "heheda", "lalala", "wahaha"},
   100  		[]interface{}{map[string]int{"map1": 123}, 123, "hehe"},
   101  		[]interface{}{map[string]int{"map1": 123}, 123, "hehe"},
   102  		[]interface{}{123, "sss", "ssr", []interface{}{123, "234", 456, "789"}},
   103  		[]interface{}{123, "sss", "ssr", []interface{}{123, "234", 456, "789"}},
   104  		[]interface{}{123, "sss", "ssr3", []interface{}{123, "234", 456, "789"}}, // 判错
   105  		[]interface{}{123, "sss", "ssr4", []interface{}{123, "234", 456, "789"}}, // 判错
   106  	}
   107  	for i := 0; i < len(sliceCmp2)-2; i += 2 {
   108  		if !DeepEqualNotType(sliceCmp2[i], sliceCmp2[i+1]) {
   109  			t.Fatal("DeepEqualNotType sliceCmp2 is not equal, index == ", i)
   110  		}
   111  	}
   112  	if DeepEqualNotType(SliceIndex(sliceCmp2, -1), SliceIndex(sliceCmp2, -2)) {
   113  		t.Fatal("DeepEqualNotType sliceCmp2 is not equal")
   114  	}
   115  }