File: clone_common_test.go

package info (click to toggle)
golang-github-huandu-go-clone 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 276 kB
  • sloc: makefile: 2
file content (669 lines) | stat: -rw-r--r-- 16,033 bytes parent folder | download
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
// Copyright 2019 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

package clone

import (
	"bytes"
	"container/list"
	"io"
	"reflect"
	"testing"
	"unsafe"

	"github.com/huandu/go-assert"
)

var testFuncMap = map[string]func(t *testing.T, allocator *Allocator){
	"Basic Clone":                        testClone,
	"Slowly linked list":                 testSlowlyLinkedList,
	"Slowly cycle linked list":           testSlowlyCycleLinkedList,
	"Slowly fix invalid cycle pointers":  testSlowlyFixInvalidCyclePointers,
	"Slowly fix invalid linked pointers": testSlowlyFixInvalidLinkedPointers,
	"Clone array":                        testCloneArray,
	"Clone map":                          testCloneMap,
	"Clone bytes buffer":                 testCloneBytesBuffer,
	"Clone unexported fields":            testCloneUnexportedFields,
	"Clone unexported struct method":     testCloneUnexportedStructMethod,
	"Clone reflect type":                 testCloneReflectType,
}

type T struct {
	Foo int
	Bar map[string]interface{}
}

func testClone(t *testing.T, allocator *Allocator) {
	arr := [4]string{"abc", "def", "ghi"}
	ch := make(chan int, 2)
	fn := func(int) {}
	var it io.Writer = &bytes.Buffer{}
	m := map[interface{}]string{
		"abc": "efg",
		123:   "ghi",
	}
	slice := []string{"xyz", "opq"}
	st := T{
		Foo: 1234,
		Bar: map[string]interface{}{
			"abc": 123,
			"def": "ghi",
		},
	}
	ptr := &st
	complex := []map[string][]*T{
		{
			"abc": {
				{Foo: 456, Bar: map[string]interface{}{"abc": "def"}},
			},
		},
		{
			"def": {
				{Foo: 987, Bar: map[string]interface{}{"abc": "def"}},
				{Foo: 321, Bar: map[string]interface{}{"ghi": "xyz"}},
			},
			"ghi": {
				{Foo: 654, Bar: map[string]interface{}{"def": "abc"}},
			},
		},
	}
	nested := func() interface{} {
		var nested []map[string][]*T
		var nestedPtr *T
		var nestedIf interface{}
		var nestedMap map[string]interface{}
		nested = []map[string][]*T{
			{
				"abc": {
					{Foo: 987, Bar: map[string]interface{}{"def": nil, "nil": nil}},
					{Foo: 321, Bar: map[string]interface{}{"ghi": nil, "def": nil, "cba": nil}},
					{Foo: 456},
					nil,
				},
			},
		}
		nestedPtr = &T{
			Foo: 654,
			Bar: map[string]interface{}{
				"xyz": nested,
				"opq": nil,
			},
		}
		nestedIf = map[string]interface{}{
			"rst": nested,
		}
		nestedMap = map[string]interface{}{}

		// Don't test it due to bug in Go.
		// https://github.com/golang/go/issues/33907
		//nestedMap["opq"] = nestedMap

		nested[0]["abc"][0].Bar["def"] = nested
		nested[0]["abc"][1].Bar["ghi"] = nestedPtr
		nested[0]["abc"][1].Bar["def"] = nestedIf
		nested[0]["abc"][1].Bar["cba"] = nested
		nested[0]["abc"][2].Bar = nestedMap
		nested[0]["abc"][3] = nestedPtr
		nestedPtr.Bar["opq"] = nestedPtr
		return nested
	}()
	var nilSlice []int
	var nilChan chan bool
	var nilPtr *float64
	cases := []interface{}{
		123, "abc", nil, true, testing.TB(nil),
		arr, ch, fn, it, m, ptr, slice, st, nested,
		complex, nilSlice, nilChan, nilPtr,
	}

	for _, c := range cases {
		var v1, v2 interface{}

		if reflect.DeepEqual(c, nested) {
			// Clone doesn't work on nested data.
			v1 = c
		} else {
			v1 = clone(allocator, c)
		}

		v2 = cloneSlowly(allocator, c)
		deepEqual(t, c, v1)
		deepEqual(t, c, v2)
	}
}

func deepEqual(t *testing.T, expected, actual interface{}) {
	a := assert.New(t)
	a.Use(&expected, &actual)

	val := reflect.ValueOf(actual)

	// It's not possible to compare chan value.
	if val.Kind() == reflect.Chan {
		cval := reflect.ValueOf(expected)
		a.Equal(cval.Type(), val.Type())
		a.Equal(cval.Cap(), val.Cap())
		return
	}

	if val.Kind() == reflect.Func {
		// It's not possible to compare func value either.
		cval := reflect.ValueOf(expected)
		a.Assert(cval.Type() == val.Type())
		return
	}

	a.Equal(actual, expected)
}

func testSlowlyLinkedList(t *testing.T, allocator *Allocator) {
	a := assert.New(t)
	l := list.New()
	l.PushBack("v1")
	l.PushBack("v2")
	cloned := cloneSlowly(allocator, l).(*list.List)

	a.Equal(l.Len(), cloned.Len())
	a.Equal(l.Front().Value, cloned.Front().Value)
	a.Equal(l.Back().Value, cloned.Back().Value)

	// There must be only two elements in cloned.
	a.Equal(cloned.Back(), cloned.Front().Next())
	a.Equal(cloned.Back().Next(), nil)
}

type cycleLinkedList struct {
	elems []*list.Element
	elem  *list.Element

	list *list.List
}

func testSlowlyCycleLinkedList(t *testing.T, allocator *Allocator) {
	a := assert.New(t)
	l := list.New()
	elem := l.PushBack("123")
	cycle := &cycleLinkedList{
		elems: []*list.Element{elem},
		elem:  elem,
		list:  l,
	}
	cloned := cloneSlowly(allocator, cycle).(*cycleLinkedList)

	a.Equal(l.Len(), cloned.list.Len())
	a.Equal(elem.Value, cloned.list.Front().Value)

	// There must be only one element in cloned.
	a.Equal(cloned.list.Front(), cloned.list.Back())
	a.Equal(cloned.list.Front().Next(), nil)
	a.Equal(cloned.list.Back().Next(), nil)
}

type cycleList struct {
	root cycleElement
	elem *cycleElement
}

type cycleElement struct {
	next *cycleElement
	list *cycleList
}

type cycleComplex struct {
	ch           chan bool
	scalar       int
	scalarArray  *[1]int
	scalarSlice  []string
	scalarStruct *reflect.Value

	_ []*cycleElement
	_ map[*cycleElement]*cycleElement
	_ interface{}

	array          [2]*cycleElement
	slice          []*cycleElement
	iface1, iface2 interface{}
	ptr1, ptr2     *cycleElement

	scalarMap  map[string]int
	plainMap   map[int]*cycleElement
	simpleMap  map[*cycleList]*cycleElement
	complexMap map[*cycleElement]*cycleElement

	pair      cycleElementPair
	pairValue interface{}

	refSlice      *[]*cycleElement
	refComplexMap *map[*cycleElement]*cycleElement
}

type cycleElementPair struct {
	elem1, elem2 *cycleElement
}

func makeCycleElement() *cycleElement {
	list := &cycleList{}
	elem := &cycleElement{
		next: &list.root,
		list: list,
	}
	list.root.next = elem
	list.root.list = list
	list.elem = elem
	return &list.root
}

func (elem *cycleElement) validateCycle(t *testing.T) {
	a := assert.New(t)

	// elem is the &list.root.
	a.Assert(elem == &elem.list.root)
	a.Assert(elem.next == elem.list.elem)
	a.Assert(elem.next.next == elem)
}

func testSlowlyFixInvalidCyclePointers(t *testing.T, allocator *Allocator) {
	var scalarArray [1]int
	scalarStruct := reflect.ValueOf(1)
	value := &cycleComplex{
		ch:           make(chan bool),
		scalar:       123,
		scalarArray:  &scalarArray,
		scalarSlice:  []string{"hello"},
		scalarStruct: &scalarStruct,

		array:  [2]*cycleElement{makeCycleElement(), makeCycleElement()},
		slice:  []*cycleElement{makeCycleElement(), makeCycleElement()},
		iface1: makeCycleElement(),
		iface2: makeCycleElement(),
		ptr1:   makeCycleElement(),
		ptr2:   makeCycleElement(),

		scalarMap: map[string]int{
			"foo": 123,
		},
		plainMap: map[int]*cycleElement{
			123: makeCycleElement(),
		},
		simpleMap: map[*cycleList]*cycleElement{
			makeCycleElement().list: makeCycleElement(),
		},
		complexMap: map[*cycleElement]*cycleElement{
			makeCycleElement(): makeCycleElement(),
		},
	}
	value.refSlice = &value.slice
	value.refComplexMap = &value.complexMap
	cloned := cloneSlowly(allocator, value).(*cycleComplex)

	cloned.array[0].validateCycle(t)
	cloned.array[1].validateCycle(t)
	cloned.slice[0].validateCycle(t)
	cloned.slice[1].validateCycle(t)

	cloned.iface1.(*cycleElement).validateCycle(t)
	cloned.iface2.(*cycleElement).validateCycle(t)
	cloned.ptr1.validateCycle(t)
	cloned.ptr2.validateCycle(t)
	cloned.plainMap[123].validateCycle(t)

	for k, v := range cloned.simpleMap {
		k.root.validateCycle(t)
		k.elem.next.validateCycle(t)
		v.validateCycle(t)
	}

	for k, v := range cloned.complexMap {
		k.validateCycle(t)
		v.validateCycle(t)
	}

	a := assert.New(t)
	a.Assert(cloned.refSlice == &cloned.slice)
	a.Assert(cloned.refComplexMap == &cloned.complexMap)
}

func makeLinkedElements() (elem1, elem2 *cycleElement) {
	list := &cycleList{}
	elem1 = &list.root
	elem2 = &cycleElement{
		next: &list.root,
		list: list,
	}
	list.root.next = &cycleElement{}
	list.elem = elem2

	return
}

func (elem *cycleElement) validateLinked(t *testing.T) {
	a := assert.New(t)

	// elem is the elem2.
	a.Assert(elem == elem.list.elem)
	a.Assert(elem.next == &elem.list.root)
	a.Assert(elem.next.next.next == nil)
}

func testSlowlyFixInvalidLinkedPointers(t *testing.T, allocator *Allocator) {
	value := &cycleComplex{
		array: func() (elems [2]*cycleElement) {
			elems[0], elems[1] = makeLinkedElements()
			return
		}(),
		slice: func() []*cycleElement {
			elem1, elem2 := makeLinkedElements()
			return []*cycleElement{elem1, elem2}
		}(),

		scalarMap: map[string]int{
			"foo": 123,
		},
		plainMap: func() map[int]*cycleElement {
			elem1, elem2 := makeLinkedElements()
			return map[int]*cycleElement{
				1: elem1,
				2: elem2,
			}
		}(),
		simpleMap: func() map[*cycleList]*cycleElement {
			elem1, elem2 := makeLinkedElements()
			return map[*cycleList]*cycleElement{
				elem2.list: elem1,
			}
		}(),
		complexMap: func() map[*cycleElement]*cycleElement {
			elem1, elem2 := makeLinkedElements()
			return map[*cycleElement]*cycleElement{
				elem1: elem2,
			}
		}(),
	}
	value.refSlice = &value.slice
	value.refComplexMap = &value.complexMap
	value.iface1, value.iface2 = makeLinkedElements()
	value.ptr1, value.ptr2 = makeLinkedElements()
	value.pair.elem1, value.pair.elem2 = makeLinkedElements()
	var pair cycleElementPair
	pair.elem1, pair.elem2 = makeLinkedElements()
	value.pairValue = pair
	cloned := cloneSlowly(allocator, value).(*cycleComplex)

	cloned.array[1].validateLinked(t)
	cloned.slice[1].validateLinked(t)

	cloned.iface2.(*cycleElement).validateLinked(t)
	cloned.ptr2.validateLinked(t)
	cloned.plainMap[2].validateLinked(t)

	for k := range cloned.simpleMap {
		k.elem.validateLinked(t)
	}

	for _, v := range cloned.complexMap {
		v.validateLinked(t)
	}

	value.pair.elem2.validateLinked(t)
	value.pairValue.(cycleElementPair).elem2.validateLinked(t)

	a := assert.New(t)
	a.Assert(cloned.refSlice == &cloned.slice)
	a.Assert(cloned.refComplexMap == &cloned.complexMap)
}

func testCloneArray(t *testing.T, allocator *Allocator) {
	a := assert.New(t)
	arr := [2]*T{
		{
			Foo: 123,
			Bar: map[string]interface{}{
				"abc": 123,
			},
		},
		{
			Foo: 456,
			Bar: map[string]interface{}{
				"def": 456,
				"ghi": 789,
			},
		},
	}
	cloned := clone(allocator, arr).([2]*T)
	a.Use(&arr, &cloned)

	a.Equal(arr, cloned)

	// arr is not changed if cloned is mutated.
	cloned[0].Foo = 987
	cloned[1].Bar["ghi"] = 321
	a.Equal(arr[0].Foo, 123)
	a.Equal(arr[1].Bar["ghi"], 789)
}

func testCloneMap(t *testing.T, allocator *Allocator) {
	a := assert.New(t)
	m := map[string]*T{
		"abc": {
			Foo: 123,
			Bar: map[string]interface{}{
				"abc": 321,
			},
		},
		"def": {
			Foo: 456,
			Bar: map[string]interface{}{
				"def": 789,
			},
		},
	}
	cloned := clone(allocator, m).(map[string]*T)
	a.Use(&m, &cloned)

	a.Equal(m, cloned)

	// m is not changed if cloned is mutated.
	cloned["abc"].Foo = 321
	cloned["def"].Bar["def"] = 987
	a.Equal(m["abc"].Foo, 123)
	a.Equal(m["def"].Bar["def"], 789)
}

func testCloneBytesBuffer(t *testing.T, allocator *Allocator) {
	a := assert.New(t)
	buf := &bytes.Buffer{}
	buf.WriteString("Hello, world!")
	dummy := make([]byte, len("Hello, "))
	buf.Read(dummy)
	cloned := clone(allocator, buf).(*bytes.Buffer)
	a.Use(&buf, &cloned)

	// Data must be cloned.
	a.Equal(buf.Len(), cloned.Len())
	a.Equal(buf.String(), cloned.String())

	// Data must not share the same address.
	from := buf.Bytes()
	to := cloned.Bytes()
	a.Assert(&from[0] != &to[0])

	buf.WriteString("!!!!!")
	a.NotEqual(buf.Len(), cloned.Len())
	a.NotEqual(buf.String(), cloned.String())
}

type Simple struct {
	Foo int
	Bar string
}

type Unexported struct {
	insider
}

type insider struct {
	i             int
	i8            int8
	i16           int16
	i32           int32
	i64           int64
	u             uint
	u8            uint8
	u16           uint16
	u32           uint32
	u64           uint64
	uptr          uintptr
	b             bool
	s             string
	f32           float32
	f64           float64
	c64           complex64
	c128          complex128
	arr           [4]string
	arrPtr        *[10]byte
	ch            chan bool
	fn            func(s string) string
	method        func([]byte) (int, error)
	iface         io.Writer
	ifaceScalar   io.Writer
	_             interface{}
	m             map[string]interface{}
	ptr           *Unexported
	_             *Unexported
	slice         []*Unexported
	st            Simple
	unsafePointer unsafe.Pointer
	t             reflect.Type

	Simple
}

type scalarWriter int8

func (scalarWriter) Write(p []byte) (n int, err error) { return }

func testCloneUnexportedFields(t *testing.T, allocator *Allocator) {
	a := assert.New(t)
	unexported := &Unexported{
		insider: insider{
			i:    -1,
			i8:   -8,
			i16:  -16,
			i32:  -32,
			i64:  -64,
			u:    1,
			u8:   8,
			u16:  16,
			u32:  32,
			u64:  64,
			uptr: uintptr(0xDEADC0DE),
			b:    true,
			s:    "hello",
			f32:  3.2,
			f64:  6.4,
			c64:  complex(6, 4),
			c128: complex(12, 8),
			arr: [4]string{
				"a", "b", "c", "d",
			},
			arrPtr: &[10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
			ch:     make(chan bool, 5),
			fn: func(s string) string {
				return s + ", world!"
			},
			method:      bytes.NewBufferString("method").Write,
			iface:       bytes.NewBufferString("interface"),
			ifaceScalar: scalarWriter(123),
			m: map[string]interface{}{
				"key": "value",
			},
			unsafePointer: unsafe.Pointer(&Unexported{}),
			st: Simple{
				Foo: 123,
				Bar: "bar1",
			},
			Simple: Simple{
				Foo: 456,
				Bar: "bar2",
			},
			t: reflect.TypeOf(&Simple{}),
		},
	}
	unexported.m["loop"] = &unexported.m

	// Make pointer cycles.
	unexported.ptr = unexported
	unexported.slice = []*Unexported{unexported}
	cloned := cloneSlowly(allocator, unexported).(*Unexported)
	a.Use(&unexported, &cloned)

	// unsafe.Pointer is shadow copied.
	a.Assert(cloned.unsafePointer == unexported.unsafePointer)
	unexported.unsafePointer = nil
	cloned.unsafePointer = nil

	// chan cannot be compared, but its buffer can be verified.
	a.Equal(cap(cloned.ch), cap(unexported.ch))
	unexported.ch = nil
	cloned.ch = nil

	// fn cannot be compared, but it can be called.
	a.Equal(cloned.fn("Hello"), unexported.fn("Hello"))
	unexported.fn = nil
	cloned.fn = nil

	// method cannot be compared, but it can be called.
	a.Assert(cloned.method != nil)
	a.NilError(cloned.method([]byte("1234")))
	unexported.method = nil
	cloned.method = nil

	// cloned.m["loop"] must be exactly the same map of cloned.m.
	a.Assert(reflect.ValueOf(cloned.m["loop"]).Elem().Pointer() == reflect.ValueOf(cloned.m).Pointer())

	// Don't test this map in reflect.DeepEqual due to bug in Go.
	// https://github.com/golang/go/issues/33907
	unexported.m["loop"] = nil
	cloned.m["loop"] = nil

	// reflect.Type should be copied by value.
	a.Equal(reflect.ValueOf(cloned.t).Pointer(), reflect.ValueOf(unexported.t).Pointer())

	// Finally, everything else should equal.
	a.Equal(unexported, cloned)
}

func testCloneUnexportedStructMethod(t *testing.T, allocator *Allocator) {
	a := assert.New(t)

	// Another complex case: clone a struct and a map of struct instead of ptr to a struct.
	st := insider{
		m: map[string]interface{}{
			"insider": insider{
				method: bytes.NewBufferString("method").Write,
			},
		},
	}
	cloned := clone(allocator, st).(insider)
	a.Use(&st, &cloned)

	// For a struct copy, there is a tricky way to copy method. Test it.
	a.Assert(cloned.m["insider"].(insider).method != nil)
	n, err := cloned.m["insider"].(insider).method([]byte("1234"))
	a.NilError(err)
	a.Equal(n, 4)
}

func testCloneReflectType(t *testing.T, allocator *Allocator) {
	a := assert.New(t)

	// reflect.rtype should not be deeply cloned.
	foo := reflect.TypeOf("foo")
	cloned := clone(allocator, foo).(reflect.Type)
	a.Use(&foo, &cloned)

	from := reflect.ValueOf(foo)
	to := reflect.ValueOf(cloned)

	a.Assert(from.Pointer() == to.Pointer())
}