-
Notifications
You must be signed in to change notification settings - Fork 0
/
cast_test.go
289 lines (273 loc) · 5.59 KB
/
cast_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
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
package cast
import (
"fmt"
"reflect"
"testing"
)
type (
AString string
AInt int
AInt8 int8
AInt16 int16
AInt32 int32
AInt64 int64
AUint uint
AUint8 uint8
AUint16 uint16
AUint32 uint32
AUint64 uint64
AByte byte
AFloat32 float32
AFloat64 float64
AComplex64 complex64
AComplex128 complex128
ABool bool
)
func ExampleStrings() {
type MyString string
show := func(ss []string) {
fmt.Println(ss)
}
mm := []MyString{"hello", "world", "list", "of", "strings"}
ss := Strings(mm)
show(ss)
// Output: [hello world list of strings]
}
func TestCast(t *testing.T) {
t.Parallel()
type args struct {
u any
}
tests := []struct {
name string
args args
want any
}{
{
name: "nil string",
args: args{u: []AString(nil)},
want: ([]string)(nil),
},
{
name: "empty strings array",
args: args{u: make([]AString, 0)},
want: []string{},
},
{
name: "empty strings array with cap",
args: args{u: make([]AString, 0, 10)},
want: []string{},
},
{
name: "string",
args: args{u: []AString{"hello", "world", "list", "of", "strings"}},
want: []string{"hello", "world", "list", "of", "strings"},
},
{
name: "nil int",
args: args{u: ([]AInt)(nil)},
want: ([]int)(nil),
},
{
name: "int",
args: args{u: []AInt{1, 2, 3, 4, 5, 6}},
want: []int{1, 2, 3, 4, 5, 6},
},
{
name: "nil int8",
args: args{u: ([]AInt8)(nil)},
want: ([]int8)(nil),
},
{
name: "int8",
args: args{u: []AInt8{1, 2, 3, 4, 5, 6}},
want: []int8{1, 2, 3, 4, 5, 6},
},
{
name: "nil int16",
args: args{u: ([]AInt16)(nil)},
want: ([]int16)(nil),
},
{
name: "int16",
args: args{u: []AInt16{1, 2, 3, 4, 5, 6}},
want: []int16{1, 2, 3, 4, 5, 6},
},
{
name: "nil int32",
args: args{u: ([]AInt32)(nil)},
want: ([]int32)(nil),
},
{
name: "int32",
args: args{u: []AInt32{1, 2, 3, 4, 5, 6}},
want: []int32{1, 2, 3, 4, 5, 6},
},
{
name: "nil int64",
args: args{u: ([]AInt64)(nil)},
want: ([]int64)(nil),
},
{
name: "int64",
args: args{u: []AInt64{1, 2, 3, 4, 5, 6}},
want: []int64{1, 2, 3, 4, 5, 6},
},
{
name: "nil uint",
args: args{u: ([]AUint)(nil)},
want: ([]uint)(nil),
},
{
name: "uint",
args: args{u: []AUint{1, 2, 3, 4, 5, 6}},
want: []uint{1, 2, 3, 4, 5, 6},
},
{
name: "nil uint8",
args: args{u: ([]AUint8)(nil)},
want: ([]uint8)(nil),
},
{
name: "uint8",
args: args{u: []AUint8{1, 2, 3, 4, 5, 6}},
want: []uint8{1, 2, 3, 4, 5, 6},
},
{
name: "nil uint16",
args: args{u: ([]AUint16)(nil)},
want: ([]uint16)(nil),
},
{
name: "uint16",
args: args{u: []AUint16{1, 2, 3, 4, 5, 6}},
want: []uint16{1, 2, 3, 4, 5, 6},
},
{
name: "nil uint32",
args: args{u: ([]AUint32)(nil)},
want: ([]uint32)(nil),
},
{
name: "uint32",
args: args{u: []AUint32{1, 2, 3, 4, 5, 6}},
want: []uint32{1, 2, 3, 4, 5, 6},
},
{
name: "nil uint64",
args: args{u: ([]AUint64)(nil)},
want: ([]uint64)(nil),
},
{
name: "uint64",
args: args{u: []AUint64{1, 2, 3, 4, 5, 6}},
want: []uint64{1, 2, 3, 4, 5, 6},
},
{
name: "nil byte",
args: args{u: ([]AByte)(nil)},
want: ([]byte)(nil),
},
{
name: "byte",
args: args{u: []AByte{1, 2, 3, 4, 5, 6}},
want: []byte{1, 2, 3, 4, 5, 6},
},
{
name: "nil float32",
args: args{u: ([]AFloat32)(nil)},
want: ([]float32)(nil),
},
{
name: "float32",
args: args{u: []AFloat32{1.6, 2.5, 3.4, 4.3, 5.2, 6.1}},
want: []float32{1.6, 2.5, 3.4, 4.3, 5.2, 6.1},
},
{
name: "nil float64",
args: args{u: ([]AFloat64)(nil)},
want: ([]float64)(nil),
},
{
name: "float64",
args: args{u: []AFloat64{1.6, 2.5, 3.4, 4.3, 5.2, 6.1}},
want: []float64{1.6, 2.5, 3.4, 4.3, 5.2, 6.1},
},
{
name: "nil complex64",
args: args{u: ([]AComplex64)(nil)},
want: ([]complex64)(nil),
},
{
name: "complex64",
args: args{u: []AComplex64{complex(1, 6), complex(2, 5), complex(3, 4), complex(4, 3), complex(5, 2), complex(6, 2)}},
want: []complex64{complex(1, 6), complex(2, 5), complex(3, 4), complex(4, 3), complex(5, 2), complex(6, 2)},
},
{
name: "nil complex128",
args: args{u: ([]AComplex128)(nil)},
want: ([]complex128)(nil),
},
{
name: "complex128",
args: args{u: []AComplex128{complex(1, 6), complex(2, 5), complex(3, 4), complex(4, 3), complex(5, 2), complex(6, 2)}},
want: []complex128{complex(1, 6), complex(2, 5), complex(3, 4), complex(4, 3), complex(5, 2), complex(6, 2)},
},
{
name: "nil bool",
args: args{u: ([]ABool)(nil)},
want: ([]bool)(nil),
},
{
name: "bool",
args: args{u: []ABool{true, false, true, false}},
want: []bool{true, false, true, false},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got any
switch u := tt.args.u.(type) {
case []AString:
got = Strings(u)
case []AInt:
got = Ints(u)
case []AInt8:
got = Int8s(u)
case []AInt16:
got = Int16s(u)
case []AInt32:
got = Int32s(u)
case []AInt64:
got = Int64s(u)
case []AUint:
got = Uints(u)
case []AUint8:
got = Uint8s(u)
case []AUint16:
got = Uint16s(u)
case []AUint32:
got = Uint32s(u)
case []AUint64:
got = Uint64s(u)
case []AByte:
got = Bytes(u)
case []AFloat32:
got = Float32s(u)
case []AFloat64:
got = Float64s(u)
case []AComplex64:
got = Complex64s(u)
case []AComplex128:
got = Complex128s(u)
case []ABool:
got = Bools(u)
default:
t.Fatalf("unknown type")
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}