Debian Codegraph @

exp/slice/slice_test.go

raw
1 package slice_test
2
3 import (
4 "reflect"
5 "testing"
6
7 "github.com/charmbracelet/x/exp/slice"
8 )
9
10 func TestGroupBy(t *testing.T) {
11 expected := map[string][]string{
12 "a": {"andrey", "ayman"},
13 "b": {"bash"},
14 "c": {"carlos", "christian"},
15 "r": {"raphael"},
16 }
17 input := []string{
18 "andrey",
19 "ayman",
20 "bash",
21 "carlos",
22 "christian",
23 "raphael",
24 }
25 output := slice.GroupBy(input, func(s string) string { return string(s[0]) })
26
27 if !reflect.DeepEqual(expected, output) {
28 t.Errorf("Expected %v, got %v", expected, output)
29 }
30 }
31
32 func TestTake(t *testing.T) {
33 for i, tc := range []struct {
34 input []int
35 take int
36 expected []int
37 }{
38 {
39 input: []int{1, 2, 3, 4, 5},
40 take: 3,
41 expected: []int{1, 2, 3},
42 },
43 {
44 input: []int{1, 2, 3},
45 take: 5,
46 expected: []int{1, 2, 3},
47 },
48 {
49 input: []int{},
50 take: 2,
51 expected: []int{},
52 },
53 {
54 input: []int{1, 2, 3},
55 take: 0,
56 expected: []int{},
57 },
58 {
59 input: nil,
60 take: 2,
61 expected: []int{},
62 },
63 } {
64 actual := slice.Take(tc.input, tc.take)
65 if len(actual) != len(tc.expected) {
66 t.Errorf("Test %d: Expected %v, got %v", i, tc.expected, actual)
67 }
68 }
69 }
70
71 func TestLast(t *testing.T) {
72 for i, tc := range []struct {
73 input []int
74 ok bool
75 expected int
76 }{
77 {
78 input: []int{1, 2, 3, 4, 5},
79 ok: true,
80 expected: 5,
81 },
82 {
83 input: []int{1, 2, 3},
84 ok: true,
85 expected: 3,
86 },
87 {
88 input: []int{1},
89 ok: true,
90 expected: 1,
91 },
92 {
93 input: []int{},
94 ok: false,
95 expected: 0,
96 },
97 } {
98 actual, ok := slice.Last(tc.input)
99 if ok != tc.ok {
100 t.Errorf("Test %d: Expected ok %v, got %v", i, tc.ok, ok)
101 }
102 if actual != tc.expected {
103 t.Errorf("Test %d: Expected %v, got %v", i, tc.expected, actual)
104 }
105 }
106 }
107
108 func TestUniq(t *testing.T) {
109 for i, tc := range []struct {
110 input []int
111 expected []int
112 }{
113 {
114 input: []int{1, 2, 3, 4, 5},
115 expected: []int{1, 2, 3, 4, 5},
116 },
117 {
118 input: []int{1, 2, 2, 3, 4, 4, 5},
119 expected: []int{1, 2, 3, 4, 5},
120 },
121 {
122 input: []int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5},
123 expected: []int{1, 2, 3, 4, 5},
124 },
125 {
126 input: []int{},
127 expected: []int{},
128 },
129 } {
130 actual := slice.Uniq(tc.input)
131 if !reflect.DeepEqual(actual, tc.expected) {
132 t.Errorf("Test %d: Expected %v, got %v", i, tc.expected, actual)
133 }
134 }
135 }
136
137 func TestIntersperse(t *testing.T) {
138 for i, tc := range []struct {
139 input []string
140 insert string
141 expected []string
142 }{
143 {
144 input: []string{},
145 insert: "-",
146 expected: []string{},
147 },
148 {
149 input: []string{"a"},
150 insert: "-",
151 expected: []string{"a"},
152 },
153 {
154 input: []string{"a", "b"},
155 insert: "-",
156 expected: []string{"a", "-", "b"},
157 },
158 {
159 input: []string{"a", "b", "c"},
160 insert: "-",
161 expected: []string{"a", "-", "b", "-", "c"},
162 },
163 } {
164 actual := slice.Intersperse(tc.input, tc.insert)
165 if !reflect.DeepEqual(actual, tc.expected) {
166 t.Errorf("Test %d: Expected %v, got %v", i, tc.expected, actual)
167 }
168 }
169 }
170
171 func TestContainsAny(t *testing.T) {
172 for i, tc := range []struct {
173 input []string
174 values []string
175 expected bool
176 }{
177 {
178 input: []string{"a", "b", "c"},
179 values: []string{"a", "b"},
180 expected: true,
181 },
182 {
183 input: []string{"a", "b", "c"},
184 values: []string{"d", "e"},
185 expected: false,
186 },
187 {
188 input: []string{"a", "b", "c"},
189 values: []string{"c", "d"},
190 expected: true,
191 },
192 {
193 input: []string{},
194 values: []string{"d", "e"},
195 expected: false,
196 },
197 } {
198 actual := slice.ContainsAny(tc.input, tc.values...)
199 if actual != tc.expected {
200 t.Errorf("Test %d: Expected %v, got %v", i, tc.expected, actual)
201 }
202 }
203 }
204
205 func TestShift(t *testing.T) {
206 for i, tc := range []struct {
207 input []int
208 ok bool
209 expectedVal int
210 expectedSlice []int
211 }{
212 {
213 input: []int{1, 2, 3, 4, 5},
214 ok: true,
215 expectedVal: 1,
216 expectedSlice: []int{2, 3, 4, 5},
217 },
218 {
219 input: []int{1, 2, 3},
220 ok: true,
221 expectedVal: 1,
222 expectedSlice: []int{2, 3},
223 },
224 {
225 input: []int{1},
226 ok: true,
227 expectedVal: 1,
228 expectedSlice: []int{},
229 },
230 {
231 input: []int{},
232 ok: false,
233 expectedVal: 0,
234 expectedSlice: []int{},
235 },
236 } {
237 actual, newSlice, ok := slice.Shift(tc.input)
238 if ok != tc.ok {
239 t.Errorf("test %d: expected ok %v, got %v", i, tc.ok, ok)
240 }
241 if actual != tc.expectedVal {
242 t.Errorf("test %d: expected val %v, got %v", i, tc.expectedVal, actual)
243 }
244 if !reflect.DeepEqual(newSlice, tc.expectedSlice) {
245 t.Errorf("test %d: expected slice %v, got %v", i, tc.expectedSlice, newSlice)
246 }
247 }
248 }
249
250 func TestPop(t *testing.T) {
251 for i, tc := range []struct {
252 input []int
253 ok bool
254 expectedVal int
255 expectedSlice []int
256 }{
257 {
258 input: []int{1, 2, 3, 4, 5},
259 ok: true,
260 expectedVal: 5,
261 expectedSlice: []int{1, 2, 3, 4},
262 },
263 {
264 input: []int{1, 2, 3},
265 ok: true,
266 expectedVal: 3,
267 expectedSlice: []int{1, 2},
268 },
269 {
270 input: []int{1},
271 ok: true,
272 expectedVal: 1,
273 expectedSlice: []int{},
274 },
275 {
276 input: []int{},
277 ok: false,
278 expectedVal: 0,
279 expectedSlice: []int{},
280 },
281 } {
282 actual, newSlice, ok := slice.Pop(tc.input)
283 if ok != tc.ok {
284 t.Errorf("test %d: expected ok %v, got %v", i, tc.ok, ok)
285 }
286 if actual != tc.expectedVal {
287 t.Errorf("test %d: expected val %v, got %v", i, tc.expectedVal, actual)
288 }
289 if !reflect.DeepEqual(newSlice, tc.expectedSlice) {
290 t.Errorf("test %d: expected slice %v, got %v", i, tc.expectedSlice, newSlice)
291 }
292 }
293 }
294
295 func TestDeleteAt(t *testing.T) {
296 for i, tc := range []struct {
297 input []int
298 index int
299 ok bool
300 expectedVal int
301 expectedSlice []int
302 }{
303 {
304 input: []int{1, 2, 3, 4, 5},
305 index: 2,
306 ok: true,
307 expectedVal: 3,
308 expectedSlice: []int{1, 2, 4, 5},
309 },
310 {
311 input: []int{1, 2, 3},
312 index: 0,
313 ok: true,
314 expectedVal: 1,
315 expectedSlice: []int{2, 3},
316 },
317 {
318 input: []int{1, 2, 3},
319 index: 2,
320 ok: true,
321 expectedVal: 3,
322 expectedSlice: []int{1, 2},
323 },
324 {
325 input: []int{1},
326 index: 0,
327 ok: true,
328 expectedVal: 1,
329 expectedSlice: []int{},
330 },
331 {
332 input: []int{},
333 index: 0,
334 ok: false,
335 expectedVal: 0,
336 expectedSlice: []int{},
337 },
338 } {
339 actual, newSlice, ok := slice.DeleteAt(tc.input, tc.index)
340 if ok != tc.ok {
341 t.Errorf("test %d: expected ok %v, got %v", i, tc.ok, ok)
342 }
343 if actual != tc.expectedVal {
344 t.Errorf("test %d: expected val %v, got %v", i, tc.expectedVal, actual)
345 }
346 if !reflect.DeepEqual(newSlice, tc.expectedSlice) {
347 t.Errorf("test %d: expected slice %v, got %v", i, tc.expectedSlice, newSlice)
348 }
349 }
350 }
351
352 func TestIsSubset(t *testing.T) {
353 tests := []struct {
354 name string
355 a []string
356 b []string
357 expected bool
358 }{
359 // Basic subset cases
360 {
361 name: "empty subset of empty",
362 a: []string{},
363 b: []string{},
364 expected: true,
365 },
366 {
367 name: "empty subset of non-empty",
368 a: []string{},
369 b: []string{"a", "b", "c"},
370 expected: true,
371 },
372 {
373 name: "non-empty not subset of empty",
374 a: []string{"a"},
375 b: []string{},
376 expected: false,
377 },
378 {
379 name: "single element subset",
380 a: []string{"b"},
381 b: []string{"a", "b", "c"},
382 expected: true,
383 },
384 {
385 name: "single element not subset",
386 a: []string{"d"},
387 b: []string{"a", "b", "c"},
388 expected: false,
389 },
390 {
391 name: "multiple elements subset",
392 a: []string{"a", "c"},
393 b: []string{"a", "b", "c", "d"},
394 expected: true,
395 },
396 {
397 name: "multiple elements not subset",
398 a: []string{"a", "e"},
399 b: []string{"a", "b", "c", "d"},
400 expected: false,
401 },
402 {
403 name: "equal sets are subsets",
404 a: []string{"a", "b", "c"},
405 b: []string{"a", "b", "c"},
406 expected: true,
407 },
408 {
409 name: "larger set not subset of smaller",
410 a: []string{"a", "b", "c", "d"},
411 b: []string{"a", "b"},
412 expected: false,
413 },
414
415 // Order independence
416 {
417 name: "subset with different order",
418 a: []string{"c", "a"},
419 b: []string{"b", "a", "d", "c"},
420 expected: true,
421 },
422
423 // Duplicate handling
424 {
425 name: "duplicates in subset",
426 a: []string{"a", "a", "b"},
427 b: []string{"a", "b", "c"},
428 expected: true,
429 },
430 {
431 name: "duplicates in superset",
432 a: []string{"a", "b"},
433 b: []string{"a", "a", "b", "b", "c"},
434 expected: true,
435 },
436 {
437 name: "duplicates in both",
438 a: []string{"a", "a", "b"},
439 b: []string{"a", "a", "b", "b", "c"},
440 expected: true,
441 },
442 }
443
444 for i, tt := range tests {
445 t.Run(tt.name, func(t *testing.T) {
446 actual := slice.IsSubset(tt.a, tt.b)
447 if actual != tt.expected {
448 t.Errorf("Test %d: Expected %v, got %v", i, tt.expected, actual)
449 }
450 })
451 }
452 }
453
454 func TestIsSubsetWithInts(t *testing.T) {
455 tests := []struct {
456 name string
457 a []int
458 b []int
459 expected bool
460 }{
461 {
462 name: "int subset",
463 a: []int{1, 3},
464 b: []int{1, 2, 3, 4},
465 expected: true,
466 },
467 {
468 name: "int not subset",
469 a: []int{1, 5},
470 b: []int{1, 2, 3, 4},
471 expected: false,
472 },
473 {
474 name: "empty int subset",
475 a: []int{},
476 b: []int{1, 2, 3},
477 expected: true,
478 },
479 }
480
481 for i, tt := range tests {
482 t.Run(tt.name, func(t *testing.T) {
483 actual := slice.IsSubset(tt.a, tt.b)
484 if actual != tt.expected {
485 t.Errorf("Test %d: Expected %v, got %v", i, tt.expected, actual)
486 }
487 })
488 }
489 }