-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepcopy_test.go
105 lines (90 loc) · 1.49 KB
/
deepcopy_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
package deepcopy
import (
. "reflect"
"testing"
)
type Basic struct {
X int
Y float32
}
type NotBasic Basic
type DeepEqualTest struct {
a interface{}
eq bool
}
type Node struct {
Node *Node
}
func Addr(n *Node, depth int) *Node {
for i := 0; i < depth; i++ {
n2 := *n
n = &Node{&n2}
}
return n
}
// Simple functions for DeepEqual tests.
var (
fn1 func() // nil.
)
var (
node1 *Node = new(Node)
node2 *Node = Addr(node1, 1)
node3 *Node = Addr(node1, 4)
node4 *Node = Addr(node1, 4)
)
func init() {
node1 = &Node{node3}
}
var deepEqualTests = []interface{}{
// Equalities
nil,
1,
int32(1),
0.5,
float32(0.5),
"hello",
make([]int, 10),
&[3]int{1, 2, 3},
Basic{1, 0.5},
error(nil),
map[int]string{1: "one", 2: "two"},
fn1,
// Nil vs empty: not the same.
[]int{},
[]int(nil),
map[int]int{},
map[int]int(nil),
// Mismatched types
[]int{1, 2, 3},
[3]int{1, 2, 3},
&[3]interface{}{1, 2, 4},
&[3]interface{}{1, 2, "s"},
NotBasic{1, 0.5},
map[uint]string{1: "one", 2: "two"},
// Pointers
new(int),
new(float32),
new(map[uint]string),
new([]map[uint]string),
new(interface{}),
node1,
node2,
node3,
node4,
}
func TestDeepCopy(t *testing.T) {
for _, test := range deepEqualTests {
//t.Logf("DeepCopy(%v)", test)
if r := DeepCopy(test); !DeepEqual(test, r) {
t.Errorf("DeepCopy(%v) = %v %v", test, r, TypeOf(r))
}
}
}
func BenchmarkDeepCopyNode(b *testing.B) {
b.StopTimer()
v := Addr(nil, 100)
b.StartTimer()
for i := 0; i < b.N; i++ {
DeepCopy(v)
}
}