Debian Codegraph @

tpl/collections/collections_test.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package collections
15
16 import (
17 "context"
18 "errors"
19 "fmt"
20 "html/template"
21 "math/rand"
22 "reflect"
23 "testing"
24 "time"
25
26 "github.com/gohugoio/hugo/common/hmaps"
27 "github.com/gohugoio/hugo/config/testconfig"
28
29 qt "github.com/frankban/quicktest"
30 )
31
32 type tstNoStringer struct{}
33
34 func TestAfter(t *testing.T) {
35 t.Parallel()
36 c := qt.New(t)
37
38 ns := newNs()
39
40 for i, test := range []struct {
41 index any
42 seq any
43 expect any
44 }{
45 {int(2), []string{"a", "b", "c", "d"}, []string{"c", "d"}},
46 {int32(3), []string{"a", "b"}, []string{}},
47 {int64(2), []int{100, 200, 300}, []int{300}},
48 {100, []int{100, 200}, []int{}},
49 {"1", []int{100, 200, 300}, []int{200, 300}},
50 {0, []int{100, 200, 300, 400, 500}, []int{100, 200, 300, 400, 500}},
51 {0, []string{"a", "b", "c", "d", "e"}, []string{"a", "b", "c", "d", "e"}},
52 {int64(-1), []int{100, 200, 300}, false},
53 {"noint", []int{100, 200, 300}, false},
54 {2, []string{}, []string{}},
55 {1, nil, false},
56 {nil, []int{100}, false},
57 {1, t, false},
58 {1, (*string)(nil), false},
59 } {
60 errMsg := qt.Commentf("[%d] %v", i, test)
61
62 result, err := ns.After(test.index, test.seq)
63
64 if b, ok := test.expect.(bool); ok && !b {
65 c.Assert(err, qt.Not(qt.IsNil), errMsg)
66 continue
67 }
68
69 c.Assert(err, qt.IsNil, errMsg)
70 c.Assert(result, qt.DeepEquals, test.expect, errMsg)
71 }
72 }
73
74 type tstGrouper struct{}
75
76 type tstGroupers []*tstGrouper
77
78 func (g tstGrouper) Group(key any, items any) (any, error) {
79 ilen := reflect.ValueOf(items).Len()
80 return fmt.Sprintf("%v(%d)", key, ilen), nil
81 }
82
83 type tstGrouper2 struct{}
84
85 func (g *tstGrouper2) Group(key any, items any) (any, error) {
86 ilen := reflect.ValueOf(items).Len()
87 return fmt.Sprintf("%v(%d)", key, ilen), nil
88 }
89
90 func TestGroup(t *testing.T) {
91 t.Parallel()
92 c := qt.New(t)
93 ns := newNs()
94
95 for i, test := range []struct {
96 key any
97 items any
98 expect any
99 }{
100 {"a", []*tstGrouper{{}, {}}, "a(2)"},
101 {"b", tstGroupers{&tstGrouper{}, &tstGrouper{}}, "b(2)"},
102 {"a", []tstGrouper{{}, {}}, "a(2)"},
103 {"a", []*tstGrouper2{{}, {}}, "a(2)"},
104 {"b", []tstGrouper2{{}, {}}, "b(2)"},
105 {"a", []*tstGrouper{}, "a(0)"},
106 {"a", []string{"a", "b"}, false},
107 {"a", "asdf", false},
108 {"a", nil, false},
109 {nil, []*tstGrouper{{}, {}}, false},
110 } {
111 errMsg := qt.Commentf("[%d] %v", i, test)
112
113 result, err := ns.Group(test.key, test.items)
114
115 if b, ok := test.expect.(bool); ok && !b {
116 c.Assert(err, qt.Not(qt.IsNil), errMsg)
117 continue
118 }
119
120 c.Assert(err, qt.IsNil, errMsg)
121 c.Assert(result, qt.Equals, test.expect, errMsg)
122 }
123 }
124
125 func TestDelimit(t *testing.T) {
126 t.Parallel()
127 c := qt.New(t)
128
129 ns := newNs()
130
131 for i, test := range []struct {
132 seq any
133 delimiter any
134 last any
135 expect string
136 }{
137 {[]string{"class1", "class2", "class3"}, " ", nil, "class1 class2 class3"},
138 {[]int{1, 2, 3, 4, 5}, ",", nil, "1,2,3,4,5"},
139 {[]int{1, 2, 3, 4, 5}, ", ", nil, "1, 2, 3, 4, 5"},
140 {[]string{"class1", "class2", "class3"}, " ", " and ", "class1 class2 and class3"},
141 {[]int{1, 2, 3, 4, 5}, ",", ",", "1,2,3,4,5"},
142 {[]int{1, 2, 3, 4, 5}, ", ", ", and ", "1, 2, 3, 4, and 5"},
143 // test maps with and without sorting required
144 {map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "--", nil, "10--20--30--40--50"},
145 {map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "--", nil, "30--20--10--40--50"},
146 {map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, "--", nil, "10--20--30--40--50"},
147 {map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, "--", nil, "30--20--10--40--50"},
148 {map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, "--", nil, "50--40--10--30--20"},
149 {map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, "--", nil, "10--20--30--40--50"},
150 {map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, "--", nil, "30--20--10--40--50"},
151 {map[float64]string{3.3: "10", 2.3: "20", 1.3: "30", 4.3: "40", 5.3: "50"}, "--", nil, "30--20--10--40--50"},
152 // test maps with a last delimiter
153 {map[string]int{"1": 10, "2": 20, "3": 30, "4": 40, "5": 50}, "--", "--and--", "10--20--30--40--and--50"},
154 {map[string]int{"3": 10, "2": 20, "1": 30, "4": 40, "5": 50}, "--", "--and--", "30--20--10--40--and--50"},
155 {map[string]string{"1": "10", "2": "20", "3": "30", "4": "40", "5": "50"}, "--", "--and--", "10--20--30--40--and--50"},
156 {map[string]string{"3": "10", "2": "20", "1": "30", "4": "40", "5": "50"}, "--", "--and--", "30--20--10--40--and--50"},
157 {map[string]string{"one": "10", "two": "20", "three": "30", "four": "40", "five": "50"}, "--", "--and--", "50--40--10--30--and--20"},
158 {map[int]string{1: "10", 2: "20", 3: "30", 4: "40", 5: "50"}, "--", "--and--", "10--20--30--40--and--50"},
159 {map[int]string{3: "10", 2: "20", 1: "30", 4: "40", 5: "50"}, "--", "--and--", "30--20--10--40--and--50"},
160 {map[float64]string{3.5: "10", 2.5: "20", 1.5: "30", 4.5: "40", 5.5: "50"}, "--", "--and--", "30--20--10--40--and--50"},
161 } {
162 errMsg := qt.Commentf("[%d] %v", i, test)
163
164 var result string
165 var err error
166
167 if test.last == nil {
168 result, err = ns.Delimit(context.Background(), test.seq, test.delimiter)
169 } else {
170 result, err = ns.Delimit(context.Background(), test.seq, test.delimiter, test.last)
171 }
172
173 c.Assert(err, qt.IsNil, errMsg)
174 c.Assert(result, qt.Equals, test.expect, errMsg)
175 }
176 }
177
178 func TestDictionary(t *testing.T) {
179 c := qt.New(t)
180
181 ns := newNs()
182
183 for i, test := range []struct {
184 values []any
185 expect any
186 }{
187 {[]any{"a", "b"}, map[string]any{"a": "b"}},
188 {[]any{[]string{"a", "b"}, "c"}, map[string]any{"a": map[string]any{"b": "c"}}},
189 {
190 []any{[]string{"a", "b"}, "c", []string{"a", "b2"}, "c2", "b", "c"},
191 map[string]any{"a": map[string]any{"b": "c", "b2": "c2"}, "b": "c"},
192 },
193 {[]any{"a", 12, "b", []int{4}}, map[string]any{"a": 12, "b": []int{4}}},
194 // errors
195 {[]any{5, "b"}, false},
196 {[]any{"a", "b", "c"}, false},
197 } {
198 c.Run(fmt.Sprint(i), func(c *qt.C) {
199 c.Parallel()
200 errMsg := qt.Commentf("[%d] %v", i, test.values)
201
202 result, err := ns.Dictionary(test.values...)
203
204 if b, ok := test.expect.(bool); ok && !b {
205 c.Assert(err, qt.Not(qt.IsNil), errMsg)
206 return
207 }
208
209 c.Assert(err, qt.IsNil, errMsg)
210 c.Assert(result, qt.DeepEquals, test.expect, qt.Commentf(fmt.Sprint(result)))
211 })
212 }
213 }
214
215 func TestReverse(t *testing.T) {
216 t.Parallel()
217 c := qt.New(t)
218 ns := newNs()
219
220 s := []string{"a", "b", "c"}
221 reversed, err := ns.Reverse(s)
222 c.Assert(err, qt.IsNil)
223 c.Assert(reversed, qt.DeepEquals, []string{"c", "b", "a"}, qt.Commentf(fmt.Sprint(reversed)))
224 c.Assert(s, qt.DeepEquals, []string{"a", "b", "c"})
225
226 reversed, err = ns.Reverse(nil)
227 c.Assert(err, qt.IsNil)
228 c.Assert(reversed, qt.IsNil)
229 _, err = ns.Reverse(43)
230 c.Assert(err, qt.Not(qt.IsNil))
231 }
232
233 func TestFirst(t *testing.T) {
234 t.Parallel()
235 c := qt.New(t)
236
237 ns := newNs()
238
239 for i, test := range []struct {
240 limit any
241 seq any
242 expect any
243 }{
244 {int(2), []string{"a", "b", "c"}, []string{"a", "b"}},
245 {int32(3), []string{"a", "b"}, []string{"a", "b"}},
246 {int64(2), []int{100, 200, 300}, []int{100, 200}},
247 {100, []int{100, 200}, []int{100, 200}},
248 {"1", []int{100, 200, 300}, []int{100}},
249 {0, []string{"h", "u", "g", "o"}, []string{}},
250 {int64(-1), []int{100, 200, 300}, false},
251 {"noint", []int{100, 200, 300}, false},
252 {1, nil, false},
253 {nil, []int{100}, false},
254 {1, t, false},
255 {1, (*string)(nil), false},
256 } {
257 errMsg := qt.Commentf("[%d] %v", i, test)
258
259 result, err := ns.First(test.limit, test.seq)
260
261 if b, ok := test.expect.(bool); ok && !b {
262 c.Assert(err, qt.Not(qt.IsNil), errMsg)
263 continue
264 }
265
266 c.Assert(err, qt.IsNil)
267 c.Assert(result, qt.DeepEquals, test.expect, errMsg)
268 }
269 }
270
271 func TestIn(t *testing.T) {
272 t.Parallel()
273 c := qt.New(t)
274 ns := newNs()
275
276 for i, test := range []struct {
277 l1 any
278 l2 any
279 expect bool
280 }{
281 {[]string{"a", "b", "c"}, "b", true},
282 {[]any{"a", "b", "c"}, "b", true},
283 {[]any{"a", "b", "c"}, "d", false},
284 {[]string{"a", "b", "c"}, "d", false},
285 {[]string{"a", "12", "c"}, 12, false},
286 {[]string{"a", "b", "c"}, nil, false},
287 {[]int{1, 2, 4}, 2, true},
288 {[]any{1, 2, 4}, 2, true},
289 {[]any{1, 2, 4}, nil, false},
290 {[]any{nil}, nil, false},
291 {[]int{1, 2, 4}, 3, false},
292 {[]float64{1.23, 2.45, 4.67}, 1.23, true},
293 {[]float64{1.234567, 2.45, 4.67}, 1.234568, false},
294 {[]float64{1, 2, 3}, 1, true},
295 {[]float32{1, 2, 3}, 1, true},
296 {"this substring should be found", "substring", true},
297 {"this substring should not be found", "subseastring", false},
298 {nil, "foo", false},
299 // Pointers
300 {pagesPtr{p1, p2, p3, p2}, p2, true},
301 {pagesPtr{p1, p2, p3, p2}, p4, false},
302 // Structs
303 {pagesVals{p3v, p2v, p3v, p2v}, p2v, true},
304 {pagesVals{p3v, p2v, p3v, p2v}, p4v, false},
305 // template.HTML
306 {template.HTML("this substring should be found"), "substring", true},
307 {template.HTML("this substring should not be found"), "subseastring", false},
308 // Uncomparable, use hashstructure
309 {[]string{"a", "b"}, []string{"a", "b"}, false},
310 {[][]string{{"a", "b"}}, []string{"a", "b"}, true},
311 } {
312
313 errMsg := qt.Commentf("[%d] %v", i, test)
314
315 result, err := ns.In(test.l1, test.l2)
316 c.Assert(err, qt.IsNil)
317 c.Assert(result, qt.Equals, test.expect, errMsg)
318 }
319 }
320
321 type testPage struct {
322 Title string
323 }
324
325 func (p testPage) String() string {
326 return "p-" + p.Title
327 }
328
329 type (
330 pagesPtr []*testPage
331 pagesVals []testPage
332 )
333
334 var (
335 p1 = &testPage{"A"}
336 p2 = &testPage{"B"}
337 p3 = &testPage{"C"}
338 p4 = &testPage{"D"}
339
340 p1v = testPage{"A"}
341 p2v = testPage{"B"}
342 p3v = testPage{"C"}
343 p4v = testPage{"D"}
344 )
345
346 func TestIntersect(t *testing.T) {
347 t.Parallel()
348 c := qt.New(t)
349
350 ns := newNs()
351
352 for i, test := range []struct {
353 l1, l2 any
354 expect any
355 }{
356 {[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b"}},
357 {[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b"}},
358 {[]string{"a", "b", "c"}, []string{"d", "e"}, []string{}},
359 {[]string{}, []string{}, []string{}},
360 {[]string{"a", "b"}, nil, []any{}},
361 {nil, []string{"a", "b"}, []any{}},
362 {nil, nil, []any{}},
363 {[]string{"1", "2"}, []int{1, 2}, []string{}},
364 {[]int{1, 2}, []string{"1", "2"}, []int{}},
365 {[]int{1, 2, 4}, []int{2, 4}, []int{2, 4}},
366 {[]int{2, 4}, []int{1, 2, 4}, []int{2, 4}},
367 {[]int{1, 2, 4}, []int{3, 6}, []int{}},
368 {[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4}},
369
370 // []interface{} ∩ []interface{}
371 {[]any{"a", "b", "c"}, []any{"a", "b", "b"}, []any{"a", "b"}},
372 {[]any{1, 2, 3}, []any{1, 2, 2}, []any{1, 2}},
373 {[]any{int8(1), int8(2), int8(3)}, []any{int8(1), int8(2), int8(2)}, []any{int8(1), int8(2)}},
374 {[]any{int16(1), int16(2), int16(3)}, []any{int16(1), int16(2), int16(2)}, []any{int16(1), int16(2)}},
375 {[]any{int32(1), int32(2), int32(3)}, []any{int32(1), int32(2), int32(2)}, []any{int32(1), int32(2)}},
376 {[]any{int64(1), int64(2), int64(3)}, []any{int64(1), int64(2), int64(2)}, []any{int64(1), int64(2)}},
377 {[]any{float32(1), float32(2), float32(3)}, []any{float32(1), float32(2), float32(2)}, []any{float32(1), float32(2)}},
378 {[]any{float64(1), float64(2), float64(3)}, []any{float64(1), float64(2), float64(2)}, []any{float64(1), float64(2)}},
379
380 // []interface{} ∩ []T
381 {[]any{"a", "b", "c"}, []string{"a", "b", "b"}, []any{"a", "b"}},
382 {[]any{1, 2, 3}, []int{1, 2, 2}, []any{1, 2}},
383 {[]any{int8(1), int8(2), int8(3)}, []int8{1, 2, 2}, []any{int8(1), int8(2)}},
384 {[]any{int16(1), int16(2), int16(3)}, []int16{1, 2, 2}, []any{int16(1), int16(2)}},
385 {[]any{int32(1), int32(2), int32(3)}, []int32{1, 2, 2}, []any{int32(1), int32(2)}},
386 {[]any{int64(1), int64(2), int64(3)}, []int64{1, 2, 2}, []any{int64(1), int64(2)}},
387 {[]any{uint(1), uint(2), uint(3)}, []uint{1, 2, 2}, []any{uint(1), uint(2)}},
388 {[]any{float32(1), float32(2), float32(3)}, []float32{1, 2, 2}, []any{float32(1), float32(2)}},
389 {[]any{float64(1), float64(2), float64(3)}, []float64{1, 2, 2}, []any{float64(1), float64(2)}},
390
391 // []T ∩ []interface{}
392 {[]string{"a", "b", "c"}, []any{"a", "b", "b"}, []string{"a", "b"}},
393 {[]int{1, 2, 3}, []any{1, 2, 2}, []int{1, 2}},
394 {[]int8{1, 2, 3}, []any{int8(1), int8(2), int8(2)}, []int8{1, 2}},
395 {[]int16{1, 2, 3}, []any{int16(1), int16(2), int16(2)}, []int16{1, 2}},
396 {[]int32{1, 2, 3}, []any{int32(1), int32(2), int32(2)}, []int32{1, 2}},
397 {[]int64{1, 2, 3}, []any{int64(1), int64(2), int64(2)}, []int64{1, 2}},
398 {[]float32{1, 2, 3}, []any{float32(1), float32(2), float32(2)}, []float32{1, 2}},
399 {[]float64{1, 2, 3}, []any{float64(1), float64(2), float64(2)}, []float64{1, 2}},
400
401 // Structs
402 {pagesPtr{p1, p4, p2, p3}, pagesPtr{p4, p2, p2}, pagesPtr{p4, p2}},
403 {pagesVals{p1v, p4v, p2v, p3v}, pagesVals{p1v, p3v, p3v}, pagesVals{p1v, p3v}},
404 {[]any{p1, p4, p2, p3}, []any{p4, p2, p2}, []any{p4, p2}},
405 {[]any{p1v, p4v, p2v, p3v}, []any{p1v, p3v, p3v}, []any{p1v, p3v}},
406 {pagesPtr{p1, p4, p2, p3}, pagesPtr{}, pagesPtr{}},
407 {pagesVals{}, pagesVals{p1v, p3v, p3v}, pagesVals{}},
408 {[]any{p1, p4, p2, p3}, []any{}, []any{}},
409 {[]any{}, []any{p1v, p3v, p3v}, []any{}},
410
411 // errors
412 {"not array or slice", []string{"a"}, false},
413 {[]string{"a"}, "not array or slice", false},
414
415 // uncomparable types - #3820
416 {[]map[int]int{{1: 1}, {2: 2}}, []map[int]int{{2: 2}, {3: 3}}, false},
417 {[][]int{{1, 1}, {1, 2}}, [][]int{{1, 2}, {1, 2}, {1, 3}}, false},
418 {[]int{1, 1}, [][]int{{1, 2}, {1, 2}, {1, 3}}, false},
419 } {
420
421 errMsg := qt.Commentf("[%d] %v", i, test)
422
423 result, err := ns.Intersect(test.l1, test.l2)
424
425 if b, ok := test.expect.(bool); ok && !b {
426 c.Assert(err, qt.Not(qt.IsNil), errMsg)
427 continue
428 }
429
430 c.Assert(err, qt.IsNil, errMsg)
431 if !reflect.DeepEqual(result, test.expect) {
432 t.Fatalf("[%d] Got\n%v expected\n%v", i, result, test.expect)
433 }
434 }
435 }
436
437 func TestIsSet(t *testing.T) {
438 t.Parallel()
439 c := qt.New(t)
440 ns := newNs()
441
442 for i, test := range []struct {
443 a any
444 key any
445 expect bool
446 isErr bool
447 }{
448 {[]any{1, 2, 3, 5}, 2, true, false},
449 {[]any{1, 2, 3, 5}, "2", true, false},
450 {[]any{1, 2, 3, 5}, 2.0, true, false},
451
452 {[]any{1, 2, 3, 5}, 22, false, false},
453
454 {map[string]any{"a": 1, "b": 2}, "b", true, false},
455 {map[string]any{"a": 1, "b": 2}, "bc", false, false},
456
457 {time.Now(), "Day", false, false},
458 {nil, "nil", false, false},
459 {[]any{1, 2, 3, 5}, TstX{}, false, true},
460 } {
461 errMsg := qt.Commentf("[%d] %v", i, test)
462
463 result, err := ns.IsSet(test.a, test.key)
464 if test.isErr {
465 continue
466 }
467
468 c.Assert(err, qt.IsNil, errMsg)
469 c.Assert(result, qt.Equals, test.expect, errMsg)
470 }
471 }
472
473 func TestLast(t *testing.T) {
474 t.Parallel()
475 c := qt.New(t)
476
477 ns := newNs()
478
479 for i, test := range []struct {
480 limit any
481 seq any
482 expect any
483 }{
484 {int(2), []string{"a", "b", "c"}, []string{"b", "c"}},
485 {int32(3), []string{"a", "b"}, []string{"a", "b"}},
486 {int64(2), []int{100, 200, 300}, []int{200, 300}},
487 {100, []int{100, 200}, []int{100, 200}},
488 {"1", []int{100, 200, 300}, []int{300}},
489 {"0", []int{100, 200, 300}, []int{}},
490 {"0", []string{"a", "b", "c"}, []string{}},
491 // errors
492 {int64(-1), []int{100, 200, 300}, false},
493 {"noint", []int{100, 200, 300}, false},
494 {1, nil, false},
495 {nil, []int{100}, false},
496 {1, t, false},
497 {1, (*string)(nil), false},
498 } {
499 errMsg := qt.Commentf("[%d] %v", i, test)
500
501 result, err := ns.Last(test.limit, test.seq)
502
503 if b, ok := test.expect.(bool); ok && !b {
504 c.Assert(err, qt.Not(qt.IsNil), errMsg)
505 continue
506 }
507
508 c.Assert(err, qt.IsNil, errMsg)
509 c.Assert(result, qt.DeepEquals, test.expect, errMsg)
510 }
511 }
512
513 func TestSeq(t *testing.T) {
514 t.Parallel()
515 c := qt.New(t)
516 ns := newNs()
517
518 for i, test := range []struct {
519 args []any
520 expect any
521 }{
522 {[]any{-2, 5}, []int{-2, -1, 0, 1, 2, 3, 4, 5}},
523 {[]any{1, 2, 4}, []int{1, 3}},
524 {[]any{1}, []int{1}},
525 {[]any{3}, []int{1, 2, 3}},
526 {[]any{3.2}, []int{1, 2, 3}},
527 {[]any{0}, []int{}},
528 {[]any{-1}, []int{-1}},
529 {[]any{-3}, []int{-1, -2, -3}},
530 {[]any{3, -2}, []int{3, 2, 1, 0, -1, -2}},
531 {[]any{6, -2, 2}, []int{6, 4, 2}},
532 // errors
533 {[]any{1, 0, 2}, false},
534 {[]any{1, -1, 2}, false},
535 {[]any{2, 1, 1}, false},
536 {[]any{2, 1, 1, 1}, false},
537 {[]any{-1000001}, false},
538 {[]any{}, false},
539 {[]any{0, -1000000}, false},
540 {[]any{tstNoStringer{}}, false},
541 {nil, false},
542 } {
543 errMsg := qt.Commentf("[%d] %v", i, test)
544
545 result, err := ns.Seq(test.args...)
546
547 if b, ok := test.expect.(bool); ok && !b {
548 c.Assert(err, qt.Not(qt.IsNil), errMsg)
549 continue
550 }
551
552 c.Assert(err, qt.IsNil, errMsg)
553 c.Assert(result, qt.DeepEquals, test.expect, errMsg)
554 }
555 }
556
557 func TestShuffle(t *testing.T) {
558 t.Parallel()
559 c := qt.New(t)
560 ns := newNs()
561
562 for i, test := range []struct {
563 seq any
564 success bool
565 }{
566 {[]string{"a", "b", "c", "d"}, true},
567 {[]int{100, 200, 300}, true},
568 {[]int{100, 200, 300}, true},
569 {[]int{100, 200}, true},
570 {[]string{"a", "b"}, true},
571 {[]int{100, 200, 300}, true},
572 {[]int{100, 200, 300}, true},
573 {[]int{100}, true},
574 // errors
575 {nil, false},
576 {t, false},
577 {(*string)(nil), false},
578 } {
579 errMsg := qt.Commentf("[%d] %v", i, test)
580
581 result, err := ns.Shuffle(test.seq)
582
583 if !test.success {
584 c.Assert(err, qt.Not(qt.IsNil), errMsg)
585 continue
586 }
587
588 c.Assert(err, qt.IsNil, errMsg)
589
590 resultv := reflect.ValueOf(result)
591 seqv := reflect.ValueOf(test.seq)
592
593 c.Assert(seqv.Len(), qt.Equals, resultv.Len(), errMsg)
594 }
595 }
596
597 func TestShuffleRandomising(t *testing.T) {
598 t.Parallel()
599 c := qt.New(t)
600 ns := newNs()
601
602 // Note that this test can fail with false negative result if the shuffle
603 // of the sequence happens to be the same as the original sequence. However
604 // the probability of the event is 10^-158 which is negligible.
605 seqLen := 100
606
607 for _, test := range []struct {
608 seq []int
609 }{
610 {rand.Perm(seqLen)},
611 } {
612 result, err := ns.Shuffle(test.seq)
613 resultv := reflect.ValueOf(result)
614
615 c.Assert(err, qt.IsNil)
616
617 allSame := true
618 for i, v := range test.seq {
619 allSame = allSame && (resultv.Index(i).Interface() == v)
620 }
621
622 c.Assert(allSame, qt.Equals, false)
623 }
624 }
625
626 // Also see tests in commons/collection.
627 func TestSlice(t *testing.T) {
628 t.Parallel()
629 c := qt.New(t)
630 ns := newNs()
631
632 for i, test := range []struct {
633 args []any
634 expected any
635 }{
636 {[]any{"a", "b"}, []string{"a", "b"}},
637 {[]any{}, []any{}},
638 {[]any{nil}, []any{nil}},
639 {[]any{5, "b"}, []any{5, "b"}},
640 {[]any{tstNoStringer{}}, []tstNoStringer{{}}},
641 } {
642 errMsg := qt.Commentf("[%d] %v", i, test.args)
643
644 result := ns.Slice(test.args...)
645
646 c.Assert(result, qt.DeepEquals, test.expected, errMsg)
647 }
648 }
649
650 func TestUnion(t *testing.T) {
651 t.Parallel()
652 c := qt.New(t)
653
654 ns := newNs()
655
656 for i, test := range []struct {
657 l1 any
658 l2 any
659 expect any
660 isErr bool
661 }{
662 {nil, nil, []any{}, false},
663 {nil, []string{"a", "b"}, []string{"a", "b"}, false},
664 {[]string{"a", "b"}, nil, []string{"a", "b"}, false},
665
666 // []A ∪ []B
667 {[]string{"1", "2"}, []int{3}, []string{}, false},
668 {[]int{1, 2}, []string{"1", "2"}, []int{}, false},
669
670 // []T ∪ []T
671 {[]string{"a", "b", "c", "c"}, []string{"a", "b", "b"}, []string{"a", "b", "c"}, false},
672 {[]string{"a", "b"}, []string{"a", "b", "c"}, []string{"a", "b", "c"}, false},
673 {[]string{"a", "b", "c"}, []string{"d", "e"}, []string{"a", "b", "c", "d", "e"}, false},
674 {[]string{}, []string{}, []string{}, false},
675 {[]int{1, 2, 3}, []int{3, 4, 5}, []int{1, 2, 3, 4, 5}, false},
676 {[]int{1, 2, 3}, []int{1, 2, 3}, []int{1, 2, 3}, false},
677 {[]int{1, 2, 4}, []int{2, 4}, []int{1, 2, 4}, false},
678 {[]int{2, 4}, []int{1, 2, 4}, []int{2, 4, 1}, false},
679 {[]int{1, 2, 4}, []int{3, 6}, []int{1, 2, 4, 3, 6}, false},
680 {[]float64{2.2, 4.4}, []float64{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false},
681 {[]any{"a", "b", "c", "c"}, []any{"a", "b", "b"}, []any{"a", "b", "c"}, false},
682
683 // []T ∪ []interface{}
684 {[]string{"1", "2"}, []any{"9"}, []string{"1", "2", "9"}, false},
685 {[]int{2, 4}, []any{1, 2, 4}, []int{2, 4, 1}, false},
686 {[]int8{2, 4}, []any{int8(1), int8(2), int8(4)}, []int8{2, 4, 1}, false},
687 {[]int8{2, 4}, []any{1, 2, 4}, []int8{2, 4, 1}, false},
688 {[]int16{2, 4}, []any{1, 2, 4}, []int16{2, 4, 1}, false},
689 {[]int32{2, 4}, []any{1, 2, 4}, []int32{2, 4, 1}, false},
690 {[]int64{2, 4}, []any{1, 2, 4}, []int64{2, 4, 1}, false},
691
692 {[]float64{2.2, 4.4}, []any{1.1, 2.2, 4.4}, []float64{2.2, 4.4, 1.1}, false},
693 {[]float32{2.2, 4.4}, []any{1.1, 2.2, 4.4}, []float32{2.2, 4.4, 1.1}, false},
694
695 // []interface{} ∪ []T
696 {[]any{"a", "b", "c", "c"}, []string{"a", "b", "d"}, []any{"a", "b", "c", "d"}, false},
697 {[]any{}, []string{}, []any{}, false},
698 {[]any{1, 2}, []int{2, 3}, []any{1, 2, 3}, false},
699 {[]any{1, 2}, []int8{2, 3}, []any{1, 2, 3}, false}, // 28
700 {[]any{uint(1), uint(2)}, []uint{2, 3}, []any{uint(1), uint(2), uint(3)}, false},
701 {[]any{1.1, 2.2}, []float64{2.2, 3.3}, []any{1.1, 2.2, 3.3}, false},
702
703 // Structs
704 {pagesPtr{p1, p4}, pagesPtr{p4, p2, p2}, pagesPtr{p1, p4, p2}, false},
705 {pagesVals{p1v}, pagesVals{p3v, p3v}, pagesVals{p1v, p3v}, false},
706 {[]any{p1, p4}, []any{p4, p2, p2}, []any{p1, p4, p2}, false},
707 {[]any{p1v}, []any{p3v, p3v}, []any{p1v, p3v}, false},
708 // #3686
709 {[]any{p1v}, []any{}, []any{p1v}, false},
710 {[]any{}, []any{p1v}, []any{p1v}, false},
711 {pagesPtr{p1}, pagesPtr{}, pagesPtr{p1}, false},
712 {pagesVals{p1v}, pagesVals{}, pagesVals{p1v}, false},
713 {pagesPtr{}, pagesPtr{p1}, pagesPtr{p1}, false},
714 {pagesVals{}, pagesVals{p1v}, pagesVals{p1v}, false},
715
716 // errors
717 {"not array or slice", []string{"a"}, false, true},
718 {[]string{"a"}, "not array or slice", false, true},
719
720 // uncomparable types - #3820
721 {[]map[string]int{{"K1": 1}}, []map[string]int{{"K2": 2}, {"K2": 2}}, false, true},
722 {[][]int{{1, 1}, {1, 2}}, [][]int{{2, 1}, {2, 2}}, false, true},
723 } {
724
725 errMsg := qt.Commentf("[%d] %v", i, test)
726
727 result, err := ns.Union(test.l1, test.l2)
728 if test.isErr {
729 c.Assert(err, qt.Not(qt.IsNil), errMsg)
730 continue
731 }
732
733 c.Assert(err, qt.IsNil, errMsg)
734 if !reflect.DeepEqual(result, test.expect) {
735 t.Fatalf("[%d] Got\n%v expected\n%v", i, result, test.expect)
736 }
737 }
738 }
739
740 func TestUniq(t *testing.T) {
741 t.Parallel()
742 c := qt.New(t)
743 ns := newNs()
744 for i, test := range []struct {
745 l any
746 expect any
747 isErr bool
748 }{
749 {[]string{"a", "b", "c"}, []string{"a", "b", "c"}, false},
750 {[]string{"a", "b", "c", "c"}, []string{"a", "b", "c"}, false},
751 {[]string{"a", "b", "b", "c"}, []string{"a", "b", "c"}, false},
752 {[]string{"a", "b", "c", "b"}, []string{"a", "b", "c"}, false},
753 {[]int{1, 2, 3}, []int{1, 2, 3}, false},
754 {[]int{1, 2, 3, 3}, []int{1, 2, 3}, false},
755 {[]int{1, 2, 2, 3}, []int{1, 2, 3}, false},
756 {[]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
757 {[4]int{1, 2, 3, 2}, []int{1, 2, 3}, false},
758 {nil, make([]any, 0), false},
759 // Pointers
760 {pagesPtr{p1, p2, p3, p2}, pagesPtr{p1, p2, p3}, false},
761 {pagesPtr{}, pagesPtr{}, false},
762 // Structs
763 {pagesVals{p3v, p2v, p3v, p2v}, pagesVals{p3v, p2v}, false},
764
765 // not Comparable(), use hashstructure
766 {[]map[string]int{
767 {"K1": 1}, {"K2": 2}, {"K1": 1}, {"K2": 1},
768 }, []map[string]int{
769 {"K1": 1}, {"K2": 2}, {"K2": 1},
770 }, false},
771
772 // should fail
773 {1, 1, true},
774 {"foo", "fo", true},
775 } {
776 errMsg := qt.Commentf("[%d] %v", i, test)
777
778 result, err := ns.Uniq(test.l)
779 if test.isErr {
780 c.Assert(err, qt.Not(qt.IsNil), errMsg)
781 continue
782 }
783
784 c.Assert(err, qt.IsNil, errMsg)
785 c.Assert(result, qt.DeepEquals, test.expect, errMsg)
786 }
787 }
788
789 func TestD(t *testing.T) {
790 t.Parallel()
791 ns := newNs()
792
793 const (
794 errNumberOfRequestedValuesRegexPattern = `.*the number of requested values.*`
795 errMaximumRequestedValueRegexPattern = `.*the maximum requested value.*`
796 errSeedValueRegexPattern = `.*the seed value.*`
797 )
798
799 tests := []struct {
800 name string
801 seed any
802 n any
803 hi any
804 wantResult []int
805 wantErrText string
806 }{
807 // n <= hi
808 {"seed_eq_42", 42, 5, 100, []int{24, 34, 66, 82, 96}, ""},
809 {"seed_eq_31", 31, 5, 100, []int{12, 37, 38, 69, 98}, ""},
810 {"n_lt_hi", 42, 9, 10, []int{0, 1, 2, 3, 4, 6, 7, 8, 9}, ""},
811 {"n_eq_hi", 42, 10, 10, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, ""},
812 {"hi_eq_max_size", 42, 5, maxSeqSize, []int{240121, 347230, 669726, 829701, 957989}, ""},
813 // n > hi
814 {"n_gt_hi", 42, 11, 10, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, ""},
815 // zero values
816 {"seed_eq_0", 0, 5, 100, []int{0, 2, 29, 50, 72}, ""},
817 {"n_eq_0", 42, 0, 100, []int{}, ""},
818 {"hi_eq_0", 42, 5, 0, []int{}, ""},
819 // errors: values < 0
820 {"seed_lt_0", -42, 5, 100, nil, errSeedValueRegexPattern},
821 {"n_lt_0", 42, -1, 100, nil, errNumberOfRequestedValuesRegexPattern},
822 {"hi_lt_0", 42, 5, -100, nil, errMaximumRequestedValueRegexPattern},
823 // errors: values that can't be cast to int
824 {"seed_invalid_type", "foo", 5, 100, nil, errSeedValueRegexPattern},
825 {"n_invalid_type", 42, "bar", 100, nil, errNumberOfRequestedValuesRegexPattern},
826 {"hi_invalid_type", 42, 5, "baz", nil, errMaximumRequestedValueRegexPattern},
827 // errors: values that exceed maxSeqSize
828 {"n_gt_max_size", 42, maxSeqSize + 1, 10, nil, errNumberOfRequestedValuesRegexPattern},
829 {"hi_gt_max_size", 42, 5, maxSeqSize + 1, nil, errMaximumRequestedValueRegexPattern},
830 }
831
832 for _, tt := range tests {
833 t.Run(tt.name, func(t *testing.T) {
834 t.Parallel()
835 c := qt.New(t)
836
837 got, err := ns.D(tt.seed, tt.n, tt.hi)
838
839 if tt.wantErrText != "" {
840 c.Assert(err, qt.ErrorMatches, tt.wantErrText, qt.Commentf("n=%d, hi=%d", tt.n, tt.hi))
841 c.Assert(got, qt.IsNil, qt.Commentf("Expected nil result on error"))
842 return
843 }
844
845 c.Assert(err, qt.IsNil, qt.Commentf("Did not expect an error, but got: %v", err))
846 c.Assert(got, qt.DeepEquals, tt.wantResult)
847 })
848 }
849 }
850
851 func BenchmarkD2(b *testing.B) {
852 ns := newNs()
853 runBenchmark := func(seed, n, max any) {
854 name := fmt.Sprintf("n=%d,max=%d", n, max)
855 b.Run(name, func(b *testing.B) {
856 for b.Loop() {
857 _, _ = ns.D(seed, n, max)
858 }
859 })
860 }
861
862 runBenchmark(32, 5, 100)
863 runBenchmark(32, 50, 1000)
864 runBenchmark(32, 10, 10000)
865 runBenchmark(32, 500, 10000)
866 runBenchmark(32, 10, 500000)
867 runBenchmark(32, 5000, 500000)
868 }
869
870 func (x *TstX) TstRp() string {
871 return "r" + x.A
872 }
873
874 func (x TstX) TstRv() string {
875 return "r" + x.B
876 }
877
878 func (x TstX) TstRv2() string {
879 return "r" + x.B
880 }
881
882 //lint:ignore U1000 reflect test
883 func (x TstX) unexportedMethod() string {
884 return x.unexported
885 }
886
887 func (x TstX) MethodWithArg(s string) string {
888 return s
889 }
890
891 func (x TstX) MethodReturnNothing() {}
892
893 func (x TstX) MethodReturnErrorOnly() error {
894 return errors.New("some error occurred")
895 }
896
897 func (x TstX) MethodReturnTwoValues() (string, string) {
898 return "foo", "bar"
899 }
900
901 func (x TstX) MethodReturnValueWithError() (string, error) {
902 return "", errors.New("some error occurred")
903 }
904
905 func (x TstX) String() string {
906 return fmt.Sprintf("A: %s, B: %s", x.A, x.B)
907 }
908
909 type TstX struct {
910 A, B string
911 unexported string //lint:ignore U1000 reflect test
912 }
913
914 type TstParams struct {
915 params hmaps.Params
916 }
917
918 func (x TstParams) Params() hmaps.Params {
919 return x.params
920 }
921
922 type TstXIHolder struct {
923 XI TstXI
924 }
925
926 // Partially implemented by the TstX struct.
927 type TstXI interface {
928 TstRv2() string
929 }
930
931 func ToTstXIs(slice any) []TstXI {
932 s := reflect.ValueOf(slice)
933 if s.Kind() != reflect.Slice {
934 return nil
935 }
936 tis := make([]TstXI, s.Len())
937
938 for i := range s.Len() {
939 tsti, ok := s.Index(i).Interface().(TstXI)
940 if !ok {
941 return nil
942 }
943 tis[i] = tsti
944 }
945
946 return tis
947 }
948
949 func newNs() *Namespace {
950 return New(testconfig.GetTestDeps(nil, nil))
951 }