-
Notifications
You must be signed in to change notification settings - Fork 1
/
deepcopy_ptr_test.go
87 lines (77 loc) · 1.47 KB
/
deepcopy_ptr_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package deepcopy
import (
"testing"
"github.com/stretchr/testify/assert"
)
// 测试指针
func Test_Ptr_OK(t *testing.T) {
type interfaceTest struct {
Iptr *int
Fptr *float64
}
for _, tc := range []testCase{
func() testCase {
d := interfaceTest{}
src := interfaceTest{
Iptr: new(int),
Fptr: new(float64),
}
*src.Iptr = 3
*src.Fptr = 3.3
Copy(&d, &src).Do()
return testCase{got: d, need: src}
}(),
} {
assert.Equal(t, tc.need, tc.got)
}
}
func Test_Ptr_OKCopyEx(t *testing.T) {
type interfaceTest struct {
Iptr *int
Fptr *float64
}
for _, tc := range []testCase{
func() testCase {
d := interfaceTest{}
src := interfaceTest{
Iptr: new(int),
Fptr: new(float64),
}
*src.Iptr = 3
*src.Fptr = 3.3
err := CopyEx(&d, &src)
assert.NoError(t, err)
return testCase{got: d, need: src}
}(),
} {
assert.Equal(t, tc.need, tc.got)
}
}
// 测试指针特殊情况
// 只要不崩溃就是对的
func Test_Ptr_Special(t *testing.T) {
for _, tc := range []testCase{
// dst 是空指针
func() testCase {
Copy((*int)(nil), new(int)).Do()
return testCase{}
}(),
// dst, src是不同类型
func() testCase {
Copy("hello", new(int)).Do()
return testCase{}
}(),
// dst 是双指针
func() testCase {
n := 3
dst := 0
dstPtr := &dst
dstPtrPtr := &dstPtr
err := Copy(dstPtrPtr, &n).Do()
assert.NoError(t, err)
return testCase{}
}(),
} {
assert.Equal(t, tc.need, tc.got)
}
}