-
Notifications
You must be signed in to change notification settings - Fork 1
/
implode_test.go
354 lines (303 loc) · 10.2 KB
/
implode_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
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
package gophplib
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/elliotchance/orderedmap/v2"
)
var multiTypedArr = []any{
2,
0,
-639,
true,
"GO",
false,
nil,
"",
" ",
"string\x00with\x00...\000",
}
func ExampleImplode() {
// Arg1 is empty array and arg2 is nil
fmt.Println(Implode([]any{}))
// Arg1 is array and arg2 is nil
fmt.Println(Implode([]string{"foo", "bar", "baz"}))
// Arg1 is string and Arg2 is string array
fmt.Println(Implode(":", []string{"foo", "bar", "baz"}))
// Arg1 is string and arg2 is int array
fmt.Println(Implode(", ", []int{1, 2}))
// Arg1 is string and arg2 is float64 array
fmt.Println(Implode(", ", []float64{1.1, 2.2}))
// Arg1 is string and arg2 is boolean array
fmt.Println(Implode(", ", []bool{false, true}))
//Arg1 is string and arg2 is emtpy array
fmt.Println(Implode(", ", []any{}))
// Arg1 is string and Arg2 is 2D array
fmt.Println(Implode(":", []any{"foo", []string{"bar", "baz"}, "burp"}))
// Output:
// <nil>
// foobarbaz <nil>
// foo:bar:baz <nil>
// 1, 2 <nil>
// 1.1, 2.2 <nil>
// , 1 <nil>
// <nil>
// foo:Array:burp <nil>
}
func ExampleImplode_variation() {
// Arg1 is string 'TRUE' and arg2 is multi typed array
result, ok := Implode("TRUE", multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is true and arg2 is multi typed array
result, ok = Implode(true, multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is false and arg2 is multi typed array
result, ok = Implode(false, multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is array and arg2 is multi typed array
fmt.Println(Implode([]string{"key1", "key2"}, multiTypedArr))
// Arg1 is empty string and arg2 is multi typed array
result, ok = Implode("", multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is blank string and arg2 is multi typed array
result, ok = Implode(" ", multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is string contains null bytes string and arg2 is multi typed array
result, ok = Implode("bet\x00ween", multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is nil and arg2 is multi typed array
result, ok = Implode(nil, multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is negative int arg2 is multi typed array
result, ok = Implode(-0, multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is null bytes string arg2 is multi typed array
result, ok = Implode(`\0`, multiTypedArr)
fmt.Println(strings.Replace(result, "\x00", "NUL", -1), ok)
// Arg1 is array and arg2 is not nil
fmt.Println(Implode([]any{1, "2", 3.45, true}, "sep"))
// Arg1 is string and arg2 is object array
fmt.Println(Implode(", ", []any{Cat{"nabi", 3}}))
// Initialize small scale map
smallMap := orderedmap.NewOrderedMap[any, any]()
smallMap.Set("key1", "value1")
smallMap.Set("key2", "value2")
fmt.Println(Implode(", ", smallMap))
// Output:
// 2TRUE0TRUE-639TRUE1TRUEGOTRUETRUETRUETRUE TRUEstringNULwithNUL...NUL <nil>
// 2101-639111GO1111 1stringNULwithNUL...NUL <nil>
// 20-6391GO stringNULwithNUL...NUL <nil>
// key1Arraykey2 <nil>
// 20-6391GO stringNULwithNUL...NUL <nil>
// 2 0 -639 1 GO stringNULwithNUL...NUL <nil>
// 2betNULween0betNULween-639betNULween1betNULweenGObetNULweenbetNULweenbetNULweenbetNULween betNULweenstringNULwithNUL...NUL <nil>
// 20-6391GO stringNULwithNUL...NUL <nil>
// 2000-639010GO0000 0stringNULwithNUL...NUL <nil>
// 2\00\0-639\01\0GO\0\0\0\0 \0stringNULwithNUL...NUL <nil>
// 1sep2sep3.45sep1 <nil>
// name is nabi and 3 years old <nil>
// value1, value2 <nil>
}
func ExampleImplode_error() {
// File resource
fmt.Println(Implode(", ", getFile()))
// Only arg1
fmt.Println(Implode("glue"))
// Arg2 is int
fmt.Println(Implode("glue", 1234))
// Arg2 is nil
fmt.Println(Implode("glue", nil))
// Arg1 is int
fmt.Println(Implode(12, "pieces"))
// Arg1 is nil
fmt.Println(Implode(nil, "abcd"))
// Arg2 is an object array that can't convert to a string
fmt.Println(Implode("foo", []any{Dog{"choco", 5}, Cat{"nabi", 3}}))
// Output:
// invalid arguments passed, got string, *os.File
// argument must be one of array, slice, or ordered map, but got string
// invalid arguments passed, got string, int
// invalid arguments passed, got string, <nil>
// invalid arguments passed, got int, string
// invalid arguments passed, got <nil>, string
// unsupported type in array : gophplib.Dog
}
func TestImplode(t *testing.T) {
om := orderedmap.NewOrderedMap[any, any]()
om.Set("key1", "value1")
om.Set("key2", "value2")
testCases := []struct {
arg1 any
arg2 any
string
}{
{[]any{}, nil, ""},
{[]string{"foo", "bar", "baz"}, nil, "foobarbaz"},
{":", []string{"foo", "bar", "baz"}, "foo:bar:baz"},
{", ", []int{1, 2}, "1, 2"},
{", ", []float64{1.1, 2.2}, "1.1, 2.2"},
{", ", []bool{false, true}, ", 1"},
{", ", []any{}, ""},
{":", []any{"foo", []string{"bar", "baz"}, "burp"}, "foo:Array:burp"},
{"TRUE", multiTypedArr, "2TRUE0TRUE-639TRUE1TRUEGOTRUETRUETRUETRUE TRUEstring\x00with\x00...\x00"},
{true, multiTypedArr, "2101-639111GO1111 1string\x00with\x00...\x00"},
{false, multiTypedArr, "20-6391GO string\x00with\x00...\x00"},
{[]string{"key1", "key2"}, multiTypedArr, "key1Arraykey2"},
{"", multiTypedArr, "20-6391GO string\x00with\x00...\x00"},
{" ", multiTypedArr, "2 0 -639 1 GO string\x00with\x00...\x00"},
{"bet\x00ween", multiTypedArr, "2bet\x00ween0bet\x00ween-639bet\x00ween1bet\x00weenGObet\x00weenbet\x00weenbet\u0000weenbet\u0000ween bet\x00weenstring\x00with\x00...\x00"},
{nil, multiTypedArr, "20-6391GO string\x00with\x00...\x00"},
{-0, multiTypedArr, "2000-639010GO0000 0string\x00with\x00...\x00"},
{`\0`, multiTypedArr, "2\\00\\0-639\\01\\0GO\\0\\0\\0\\0 \\0string\x00with\x00...\x00"},
{[]any{1, "2", 3.45, true}, "sep", "1sep2sep3.45sep1"},
{"glue", nil, ""},
{"glue", 1234, ""},
{"glue", nil, ""},
{12, "pieces", ""},
{nil, "abcd", ""},
{", ", []any{Cat{"nabi", 3}}, "name is nabi and 3 years old"},
{", ", map[string]string{"foo": "bar"}, "bar"},
{", ", om, "value1, value2"},
{", ", *om, "value1, value2"},
}
for _, tc := range testCases {
testName := fmt.Sprintf("arg 1 : %v, arg2 : %v", tc.arg1, tc.arg2)
t.Run(testName, func(t *testing.T) {
if tc.arg2 == nil {
result, err := Implode(tc.arg1)
if err != nil {
if tc.string != "" {
t.Errorf("%s: expected : %s, got error %s", testName, tc.string, err.Error())
} else {
expectedErr := fmt.Errorf("argument must be one of array, slice, or ordered map, but got %v", reflect.TypeOf(tc.arg1))
if err.Error() != expectedErr.Error() {
t.Errorf("%s: expected error : %s, got %s", testName, expectedErr.Error(), err.Error())
}
}
} else {
if !reflect.DeepEqual(result, tc.string) {
t.Errorf("%s: expected : %s, got %s", testName, tc.string, result)
}
}
} else {
result, err := Implode(tc.arg1, tc.arg2)
if err != nil {
if tc.string != "" {
t.Errorf("%s: expected : %s, got error %s", testName, tc.string, err.Error())
} else {
expectedErr := fmt.Errorf("invalid arguments passed, got %v, %v", reflect.TypeOf(tc.arg1), reflect.TypeOf(tc.arg2))
if err.Error() != expectedErr.Error() {
t.Errorf("%s: expected error : %s, got %s", testName, expectedErr.Error(), err.Error())
}
}
} else {
if !reflect.DeepEqual(result, tc.string) {
t.Errorf("%s: expected : %s, got %s", testName, tc.string, result)
}
}
}
})
}
typeErrCase := struct {
arg1 any
arg2 any
}{"foo", []any{Dog{"choco", 5}, Cat{"nabi", 3}}}
testName := fmt.Sprintf("%v", typeErrCase)
t.Run(testName, func(t *testing.T) {
result, err := Implode(typeErrCase.arg1, typeErrCase.arg2)
if err != nil {
if !strings.Contains(err.Error(), "unsupported type in array") {
t.Errorf("%s: expected error : unsupported type in array, bug got %s", testName, err.Error())
}
} else {
t.Errorf("%s: error, but got %v", testName, result)
}
})
}
func BenchmarkImplode(b *testing.B) {
// Initialization
var (
// Small scale map
smallMap = orderedmap.NewOrderedMap[any, any]()
// Medium scale map
mediumMap = orderedmap.NewOrderedMap[any, any]()
// Large scale map
largeMap = orderedmap.NewOrderedMap[any, any]()
// String-based map
stringMap = orderedmap.NewOrderedMap[any, any]()
// Integer-based map
intMap = orderedmap.NewOrderedMap[any, any]()
)
// Initialize small scale map
smallMap.Set("key1", "value1")
smallMap.Set("key2", "value2")
// Initialize medium scale map
for i := 0; i < 50; i++ {
mediumMap.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
}
// Initialize large scale map
for i := 0; i < 1000; i++ {
largeMap.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
}
// Initialize string-based map
for i := 0; i < 100; i++ {
stringMap.Set(fmt.Sprintf("str%d", i), fmt.Sprintf("val%d", i))
}
// Initialize integer-based map
for i := 0; i < 100; i++ {
intMap.Set(i, i)
}
testCases := []struct {
name string
arg1 any
arg2 any
}{
{"SmallMap", smallMap, nil},
{"MediumMap", mediumMap, nil},
{"LargeMap", largeMap, nil},
{"StringKeysMap", stringMap, nil},
{"IntegerKeysMap", intMap, nil},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
var arg2Any []any
if tc.arg2 != nil {
switch v := tc.arg2.(type) {
case string:
arg2Any = append(arg2Any, v)
case []string:
for _, item := range v {
arg2Any = append(arg2Any, item)
}
default:
arg2Any = append(arg2Any, v)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if tc.arg2 == nil {
_, _ = Implode(tc.arg1)
} else {
_, _ = Implode(tc.arg1, arg2Any...)
}
}
})
}
}
func TestIsOrderedMap(t *testing.T) {
testCases := []struct {
any
}{
{orderedmap.NewOrderedMap[any, any]()},
{*orderedmap.NewOrderedMap[any, any]()},
}
for _, tc := range testCases {
testName := fmt.Sprintf("%v", reflect.TypeOf(tc.any).Kind())
t.Run(testName, func(t *testing.T) {
if !isOrderedMap(tc.any) {
t.Errorf("expected result was true but got false")
}
})
}
}