github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/unsafe/reflectx/runtime_test.go (about) 1 package reflectx 2 3 import ( 4 "reflect" 5 "testing" 6 "unsafe" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestMakeSlice(t *testing.T) { 12 i64Typ := reflect.TypeOf(int64(0)) 13 _slice, header := MakeSlice(i64Typ, 3, 6) 14 assert.NotNil(t, _slice) 15 assert.Equal(t, 3, header.Len) 16 assert.Equal(t, 6, header.Cap) 17 18 slice := _slice.([]int64) 19 slice[0] = 0 20 slice[1] = 1 21 slice[2] = 2 22 slice = append(slice, 3, 4) 23 assert.Equal(t, []int64{0, 1, 2, 3, 4}, slice) 24 assert.Equal(t, 5, len(slice)) 25 assert.Equal(t, 6, cap(slice)) 26 assert.Equal(t, 3, header.Len) 27 assert.Equal(t, 6, header.Cap) 28 29 getI64 := func(p unsafe.Pointer) int64 { 30 return *(*int64)(p) 31 } 32 i64Size := unsafe.Sizeof(int64(0)) 33 assert.Equal(t, int64(0), getI64(ArrayAt(header.Data, 0, i64Size))) 34 assert.Equal(t, int64(1), getI64(ArrayAt(header.Data, 1, i64Size))) 35 assert.Equal(t, int64(2), getI64(ArrayAt(header.Data, 2, i64Size))) 36 assert.Equal(t, int64(3), getI64(ArrayAt(header.Data, 3, i64Size))) 37 assert.Equal(t, int64(4), getI64(ArrayAt(header.Data, 4, i64Size))) 38 assert.Equal(t, int64(0), getI64(ArrayAt(header.Data, 5, i64Size))) 39 } 40 41 func TestMapLen(t *testing.T) { 42 m := make(map[int]bool) 43 assert.Equal(t, 0, MapLen(m)) 44 45 m[1] = true 46 m[2] = false 47 assert.Equal(t, 2, MapLen(m)) 48 49 type key struct { 50 A string 51 B [100]byte 52 C uint64 53 } 54 type value struct { 55 A string 56 B [100]byte 57 C uint64 58 } 59 60 var dummyBytes [100]byte 61 var m2 map[key]*value 62 assert.Equal(t, 0, MapLen(m2)) 63 m2 = make(map[key]*value, 10) 64 assert.Equal(t, 0, MapLen(m2)) 65 m2[key{"abc", dummyBytes, 1234567890}] = &value{"abc", dummyBytes, 1234567890} 66 m2[key{"abc", dummyBytes, 1234567890}] = &value{"abc", dummyBytes, 1234567890} 67 m2[key{"def", dummyBytes, 1234567890}] = &value{"def", dummyBytes, 1234567890} 68 assert.Equal(t, 2, MapLen(m2)) 69 70 var m3 map[key]value 71 assert.Equal(t, 0, MapLen(m3)) 72 m3 = make(map[key]value, 100) 73 assert.Equal(t, 0, MapLen(m3)) 74 m3[key{"abc", dummyBytes, 1234567890}] = value{"abc", dummyBytes, 1234567890} 75 m3[key{"abc", dummyBytes, 1234567890}] = value{"abc", dummyBytes, 1234567890} 76 m3[key{"def", dummyBytes, 1234567890}] = value{"def", dummyBytes, 1234567890} 77 assert.Equal(t, 2, MapLen(m3)) 78 } 79 80 func TestTypedMemMove(t *testing.T) { 81 a := &recurtype2{ 82 A: "A", 83 b: 1, 84 self: &recurtype2{ 85 A: "AA", 86 b: 11, 87 self: nil, 88 }, 89 } 90 b := &recurtype2{} 91 92 typ := RTypeOf(a) 93 TypedMemMove(typ.Elem(), unsafe.Pointer(b), unsafe.Pointer(a)) 94 95 assert.Equal(t, a.A, b.A) 96 assert.Equal(t, a.b, b.b) 97 assert.Equal(t, a.self, b.self) 98 } 99 100 func TestTypedSliceCopy(t *testing.T) { 101 slice1 := []string{"a", "b", "c"} 102 slice2 := make([]string, 5) 103 104 _, s1header := UnpackSlice(slice1) 105 _, s2header := UnpackSlice(slice2) 106 107 elemType := RTypeOf(slice1).Elem() 108 TypedSliceCopy(elemType, *s2header, *s1header) 109 110 assert.Equal(t, slice1, slice2[:3]) 111 assert.Len(t, slice2, 5) 112 assert.Equal(t, []string{"", ""}, slice2[3:5]) 113 }