github.com/hedzr/evendeep@v0.4.8/zed_test.go (about) 1 package evendeep_test 2 3 import ( 4 "bytes" 5 "reflect" 6 "testing" 7 "time" 8 "unsafe" 9 10 "github.com/hedzr/evendeep" 11 "github.com/hedzr/evendeep/internal/tool" 12 "github.com/hedzr/evendeep/typ" 13 ) 14 15 func TestBytesBuffer(t *testing.T) { 16 var v bytes.Buffer 17 vv := reflect.ValueOf(v) 18 t.Logf("%v.%v", vv.Type().PkgPath(), vv.Type().Name()) 19 } 20 21 // canConvert reports whether the value v can be converted to type t. 22 // If v.CanConvert(t) returns true then v.Convert(t) will not panic. 23 func canConvert(v *reflect.Value, t reflect.Type) bool { 24 vt := v.Type() 25 if !vt.ConvertibleTo(t) { 26 return false 27 } 28 // Currently the only conversion that is OK in terms of type 29 // but that can panic depending on the value is converting 30 // from slice to pointer-to-array. 31 if vt.Kind() == reflect.Slice && t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Array { 32 n := t.Elem().Len() 33 type sliceHeader struct { 34 Data unsafe.Pointer 35 Len int 36 Cap int 37 } 38 h := (*sliceHeader)(unsafe.Pointer(v.Pointer())) 39 if n > h.Len { 40 return false 41 } 42 } 43 return true 44 } 45 46 func TestTimeStruct(t *testing.T) { 47 48 timeZone, _ := time.LoadLocation("America/Phoenix") 49 tm := time.Date(1999, 3, 13, 5, 57, 11, 1901, timeZone) 50 tm2 := time.Date(2003, 9, 1, 23, 59, 59, 3579, timeZone) 51 52 src := &tm 53 dst := &tm2 54 55 vs := reflect.ValueOf(src) 56 vd := reflect.ValueOf(dst) 57 58 t.Logf("%v -> %v", vs.Type(), vd.Type()) 59 60 if canConvert(&vs, vd.Type()) { 61 if vd.Elem().CanSet() { 62 vd.Elem().Set(vs.Elem()) 63 return 64 } else { 65 t.Logf("vd.CanSet == false") 66 } 67 } else { 68 t.Logf("vs.CanConvert == false") 69 } 70 } 71 72 func TestUintptr(t *testing.T) { 73 74 x0 := evendeep.X0{} 75 up := unsafe.Pointer(&x0) 76 77 vv := reflect.ValueOf(&up) 78 t.Logf("%v (%v) | %v", vv.Type(), vv.Type().Kind(), vv.Interface()) 79 vv = vv.Elem() 80 t.Logf("%v (%v) | %v", vv.Type(), vv.Type().Kind(), vv.Interface()) 81 82 var a *int 83 v1 := reflect.ValueOf(&a) 84 t.Logf("%v (%v) | %v", v1.Type(), v1.Type().Kind(), v1.Interface()) 85 v1 = v1.Elem() 86 t.Logf("%v (%v) | %v", v1.Type(), v1.Type().Kind(), v1.Interface()) 87 88 defer func() { 89 if e := recover(); e != nil { 90 t.Logf("[recover] %v", e) 91 } 92 }() 93 v1 = v1.Elem() // should report panic: reflect: call of reflect.Value.Type on zero Value 94 95 // these following codes would never be reached. 96 t.Logf("%v (%v) | %v", v1.Type(), v1.Type().Kind(), v1.Interface()) 97 v1 = v1.Elem() 98 t.Logf("%v (%v) | %v", v1.Type(), v1.Type().Kind(), v1.Interface()) 99 } 100 101 func TestInspectStruct(t *testing.T) { 102 em := new(evendeep.Employee) 103 tool.InspectStruct(em) 104 } 105 106 func TestDeepCopyFromOutside(t *testing.T) { 107 // defer dbglog.NewCaptureLog(t).Release() 108 109 nn := []int{2, 9, 77, 111, 23, 29} 110 var a [2]string 111 a[0] = "Hello" 112 a[1] = "World" 113 114 x0 := evendeep.X0{} 115 x1 := evendeep.X1{ 116 A: uintptr(unsafe.Pointer(&x0)), 117 H: make(chan int, 5), 118 M: unsafe.Pointer(&x0), 119 // E: []*X0{&x0}, 120 N: nn[1:3], 121 O: a, 122 Q: a, 123 } 124 125 t.Run("DeepCopy()", func(t *testing.T) { 126 var ret typ.Any 127 x2ind := evendeep.X2{N: nn[1:3]} 128 x2 := &x2ind 129 130 ret = evendeep.DeepCopy(&x1, &x2, evendeep.WithIgnoreNames("Shit", "Memo", "Name")) 131 testIfBadCopy(t, x1, x2ind, ret, "DeepCopy x1 -> x2", true) 132 }) 133 }