Debian Codegraph @

tpl/collections/collections_test.go

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