-
Notifications
You must be signed in to change notification settings - Fork 12
/
pcopy_ptr_basetype_test.go
259 lines (236 loc) · 5.26 KB
/
pcopy_ptr_basetype_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright [2020-2023] [guonaihong]
package pcopy
import (
"fmt"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
// 测试基础类型的指针
type test_BaseType_ptr_Dst struct {
A *int
B *int8
C *int16
D *int32
E *int64
F *uint
G *uint8
H *uint16
I *uint32
J *uint64
K *uintptr
L *float32
M *float64
N *complex64
O *complex128
P *bool
Q *string
// R *interface{} TODO
}
type test_BaseType_ptr_Src struct {
A int
B int8
C int16
D int32
E int64
F uint
G uint8
H uint16
I uint32
J uint64
K uintptr
L float32
M float64
N complex64
O complex128
P bool
Q string
// R interface{}
}
func newDef[T any](t T) (rv *T) {
return &t
}
var local_test_BaseType_ptr_Dst = test_BaseType_ptr_Dst{
A: newDef(1),
B: newDef(int8(2)),
C: newDef(int16(3)),
D: newDef(int32(4)),
E: newDef(int64(5)),
F: newDef(uint(6)),
G: newDef(uint8(7)),
H: newDef(uint16(8)),
I: newDef(uint32(9)),
J: newDef(uint64(10)),
K: newDef(uintptr(11)),
L: newDef(float32(12.12)),
M: newDef(float64(13.13)),
N: newDef(complex64(14.14)),
O: newDef(complex128(15.15)),
P: newDef(true),
Q: newDef("hello"),
// R: newDef("world"),
}
var local_test_BaseType_ptr_Src = test_BaseType_ptr_Src{
A: 1,
B: int8(2),
C: int16(3),
D: int32(4),
E: int64(5),
F: uint(6),
G: uint8(7),
H: uint16(8),
I: uint32(9),
J: uint64(10),
K: uintptr(11),
L: float32(12.12),
M: float64(13.13),
N: complex64(14.14),
O: complex128(15.15),
P: true,
Q: "hello",
// R: "world",
}
func Test_Ptr_BaseType1_1(t *testing.T) {
err := Preheat(&test_BaseType_ptr_Dst{}, &local_test_BaseType_ptr_Dst)
assert.NoError(t, err, "Preheat(&test_BaseType_ptr_Dst{}, &local_test_BaseType_ptr_Dst)")
var opts []Option
for _, b := range []bool{true, false} {
if b {
opts = append(opts, WithUsePreheat())
} else {
opts = []Option{}
}
var dst test_BaseType_ptr_Dst
err = Copy(&dst, &local_test_BaseType_ptr_Dst, opts...)
assert.NoError(t, err, "Copy(&dst, &local_test_BaseType_ptr_Dst, WithUsePreheat()")
assert.Equal(t, local_test_BaseType_ptr_Dst, dst)
assert.NotEqual(t, unsafe.Pointer(local_test_BaseType_ptr_Dst.A), unsafe.Pointer(dst.A), fmt.Sprintf("local_test_BaseType_ptr_Dst.A: %p, dst.A: %p", local_test_BaseType_ptr_Dst.A, dst.A))
assert.Equal(t, *local_test_BaseType_ptr_Dst.A, *dst.A)
}
}
func Test_Ptr_BaseType1_2(t *testing.T) {
err := Preheat(&test_BaseType_ptr_Dst{}, &test_BaseType_ptr_Dst{})
assert.NoError(t, err)
var opts []Option
for _, b := range []bool{true, false} {
if b {
opts = append(opts, WithUsePreheat())
} else {
opts = []Option{}
}
var dst test_BaseType_ptr_Dst
err = Copy(&dst, &local_test_BaseType_ptr_Dst, opts...)
assert.NoError(t, err)
assert.Equal(t, local_test_BaseType_ptr_Dst, dst)
assert.NotEqual(t, unsafe.Pointer(local_test_BaseType_ptr_Dst.A), unsafe.Pointer(dst.A))
assert.Equal(t, *local_test_BaseType_ptr_Dst.A, *dst.A)
}
}
func Test_Ptr_BaseType2(t *testing.T) {
err := Preheat(&test_BaseType_ptr_Src{}, &test_BaseType_ptr_Dst{})
assert.NoError(t, err)
// Preheat(&test_BaseType_ptr_Dst{}, &test_BaseType_ptr_Src{})
var opts []Option
for _, b := range []bool{true, false} {
if b {
opts = append(opts, WithUsePreheat())
} else {
opts = []Option{}
}
var dst test_BaseType_ptr_Src
err = Copy(&dst, &local_test_BaseType_ptr_Dst, opts...)
assert.NoError(t, err)
assert.Equal(t, local_test_BaseType_ptr_Src, dst)
}
}
func Test_Ptr_BaseType3(t *testing.T) {
err := Preheat(&test_BaseType_ptr_Dst{}, &test_BaseType_ptr_Src{})
assert.NoError(t, err)
var opts []Option
for _, b := range []bool{true, false} {
if b {
opts = append(opts, WithUsePreheat())
} else {
opts = []Option{}
}
var dst test_BaseType_ptr_Dst
err = Copy(&dst, &local_test_BaseType_ptr_Src, opts...)
assert.NoError(t, err)
fmt.Printf("dst: %+v\n", dst)
fmt.Printf(".A %p\n", dst.A)
assert.Equal(t, local_test_BaseType_ptr_Dst, dst)
}
}
// 测试指针
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)
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 := Copy(&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))
return testCase{}
}(),
// dst, src是不同类型
func() testCase {
s := "hello"
Copy(&s, new(int))
return testCase{}
}(),
// dst 是双指针
func() testCase {
n := 3
dst := 0
dstPtr := &dst
dstPtrPtr := &dstPtr
err := Copy(dstPtrPtr, &n)
assert.NoError(t, err)
return testCase{}
}(),
} {
assert.Equal(t, tc.need, tc.got)
}
}