-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzero_test.go
157 lines (148 loc) · 2.38 KB
/
zero_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
package types
import (
"reflect"
"testing"
"time"
"unsafe"
)
type (
zero struct {
v interface{}
}
izero interface {
f()
}
)
var (
v zero
iv = zero{}
pv *zero
ipv = &zero{}
av [2]interface{}
iav = [2]interface{}{}
azv [2]zero
iazv = [2]zero{}
apzv [2]*zero
iapzv = [2]*zero{}
aiface [2]izero
iaiface = [2]izero{}
sv []interface{}
isv = []interface{}{}
szv []zero
iszv = []zero{}
spzv []*zero
ispzv = []*zero{}
siface []izero
isiface = []izero{}
mv map[interface{}]interface{}
imv = map[interface{}]interface{}{}
mzv map[interface{}]zero
imzv = map[interface{}]zero{}
mpzv map[interface{}]*zero
impzv = map[interface{}]*zero{}
miface map[interface{}]izero
imiface = map[interface{}]izero{}
chanv chan interface{}
ichanv = make(chan interface{})
funcv func(interface{}) interface{}
ifuncv = func(in interface{}) (out interface{}, err error) { return }
timev time.Time
itimev = time.Time{}
ptimev *time.Time
iptimev = &time.Time{}
iface izero
piface *izero
unpv unsafe.Pointer
npv = &v
npiv = &iv
zeros = []interface{}{
nil,
false,
int(0),
int8(0),
int16(0),
int32(0),
int64(0),
uint(0),
uint8(0),
uint16(0),
uint32(0),
uint64(0),
float32(0),
float64(0),
complex64(0),
complex128(0),
"",
v,
iv,
pv,
av,
iav,
azv,
iazv,
apzv,
iapzv,
aiface,
iaiface,
sv,
szv,
spzv,
siface,
mv,
mzv,
mpzv,
miface,
chanv,
funcv,
timev,
itimev,
ptimev,
iface,
piface,
unpv,
}
nonzeros = []interface{}{
ipv,
isv,
iszv,
ispzv,
isiface,
imv,
imzv,
impzv,
imiface,
ichanv,
ifuncv,
iptimev,
npv,
npiv,
}
)
func Test_IsZeroWithZeros(t *testing.T) {
// it should work with zero of types
for _, v := range zeros {
if !IsZero(v) {
t.Errorf("Expected %#v to be zero.", v)
}
if v == nil {
continue
}
if !reflect.DeepEqual(v, reflect.Zero(reflect.TypeOf(v)).Interface()) {
t.Errorf("Expected %#v to be zero.", v)
}
}
}
func Test_IsZeroWithNonzeros(t *testing.T) {
// it should work with nonzero of types
for _, v := range nonzeros {
if IsZero(v) {
t.Errorf("Expected %#v not to be zero.", v)
}
if v == nil {
continue
}
if reflect.DeepEqual(v, reflect.Zero(reflect.TypeOf(v)).Interface()) {
t.Errorf("Expected %#v not to be zero.", v)
}
}
}