-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_test.go
More file actions
1757 lines (1510 loc) · 60.3 KB
/
Copy pathstream_test.go
File metadata and controls
1757 lines (1510 loc) · 60.3 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package streams
import (
"iter"
"slices"
"testing"
"github.com/stretchr/testify/assert"
)
// TestStream tests Stream constructors and basic operations.
func TestStream(t *testing.T) {
t.Parallel()
t.Run("Of", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input []int
expected []int
}{
{"SingleElement", []int{1}, []int{1}},
{"MultipleElements", []int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := Of(tt.input...).Collect()
assert.Equal(t, tt.expected, result, "Of should create stream from values")
})
}
// Test empty stream separately
t.Run("EmptyStream", func(t *testing.T) {
t.Parallel()
result := Of[int]().Collect()
assert.Empty(t, result, "Of with no values should create empty stream")
})
})
t.Run("FromSlice", func(t *testing.T) {
t.Parallel()
slice := []string{"a", "b", "c"}
result := FromSlice(slice).Collect()
assert.Equal(t, slice, result, "FromSlice should create stream from slice")
})
t.Run("From", func(t *testing.T) {
t.Parallel()
slice := []int{1, 2, 3}
seq := slices.Values(slice)
result := From(seq).Collect()
assert.Equal(t, slice, result, "From should wrap iter.Seq")
})
t.Run("FromChannel", func(t *testing.T) {
t.Parallel()
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
result := FromChannel(ch).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "FromChannel should create stream from channel")
})
t.Run("Range", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
start int
end int
expected []int
}{
{"NormalRange", 1, 5, []int{1, 2, 3, 4}},
{"ZeroToThree", 0, 3, []int{0, 1, 2}},
{"NegativeStart", -2, 2, []int{-2, -1, 0, 1}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
result := Range(tt.start, tt.end).Collect()
assert.Equal(t, tt.expected, result, "Range should generate [start, end)")
})
}
// Test empty ranges separately
t.Run("EmptyRange", func(t *testing.T) {
t.Parallel()
result := Range(5, 5).Collect()
assert.Empty(t, result, "Range with start==end should be empty")
})
t.Run("NegativeRange", func(t *testing.T) {
t.Parallel()
result := Range(5, 1).Collect()
assert.Empty(t, result, "Range with start>end should be empty")
})
})
t.Run("RangeClosed", func(t *testing.T) {
t.Parallel()
result := RangeClosed(1, 5).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result, "RangeClosed should generate [start, end]")
})
t.Run("Empty", func(t *testing.T) {
t.Parallel()
result := Empty[int]().Collect()
assert.Empty(t, result, "Empty should create empty stream")
})
t.Run("Repeat", func(t *testing.T) {
t.Parallel()
result := Repeat("x", 3).Collect()
assert.Equal(t, []string{"x", "x", "x"}, result, "Repeat should repeat value n times")
emptyResult := Repeat("x", 0).Collect()
assert.Empty(t, emptyResult, "Repeat with n=0 should be empty")
})
t.Run("Generate", func(t *testing.T) {
t.Parallel()
counter := 0
result := Generate(func() int {
counter++
return counter
}).Limit(5).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result, "Generate should produce infinite stream")
})
t.Run("Iterate", func(t *testing.T) {
t.Parallel()
result := Iterate(1, func(n int) int { return n * 2 }).Limit(5).Collect()
assert.Equal(t, []int{1, 2, 4, 8, 16}, result, "Iterate should apply function repeatedly")
})
t.Run("Concat", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 2)
s2 := Of(3, 4)
s3 := Of(5)
result := Concat(s1, s2, s3).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result, "Concat should join streams")
})
t.Run("Cycle", func(t *testing.T) {
t.Parallel()
result := Cycle(1, 2, 3).Limit(7).Collect()
assert.Equal(t, []int{1, 2, 3, 1, 2, 3, 1}, result, "Cycle should repeat values")
emptyResult := Cycle[int]().Limit(5).Collect()
assert.Empty(t, emptyResult, "Cycle with no values should be empty")
})
t.Run("FromMap", func(t *testing.T) {
t.Parallel()
m := map[string]int{"a": 1, "b": 2}
count := FromMap(m).Count()
assert.Equal(t, 2, count, "FromMap should create Stream2 from map")
})
t.Run("Seq", func(t *testing.T) {
t.Parallel()
s := Of(1, 2, 3)
seq := s.Seq()
var result []int
for v := range seq {
result = append(result, v)
}
assert.Equal(t, []int{1, 2, 3}, result, "Seq should return underlying iter.Seq")
})
}
// TestIntermediateOperations tests intermediate operations on Stream.
func TestIntermediateOperations(t *testing.T) {
t.Parallel()
t.Run("Filter", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).
Filter(func(n int) bool { return n%2 == 0 }).
Collect()
assert.Equal(t, []int{2, 4}, result, "Filter should keep matching elements")
})
t.Run("Map", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3).
Map(func(n int) int { return n * 2 }).
Collect()
assert.Equal(t, []int{2, 4, 6}, result, "Map should transform elements")
})
t.Run("Peek", func(t *testing.T) {
t.Parallel()
var peeked []int
result := Of(1, 2, 3).
Peek(func(n int) { peeked = append(peeked, n) }).
Collect()
assert.Equal(t, []int{1, 2, 3}, result, "Peek should not modify stream")
assert.Equal(t, []int{1, 2, 3}, peeked, "Peek should execute action")
})
t.Run("Limit", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).Limit(3).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "Limit should take first n elements")
emptyResult := Of(1, 2, 3).Limit(0).Collect()
assert.Empty(t, emptyResult, "Limit(0) should be empty")
})
t.Run("Skip", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).Skip(2).Collect()
assert.Equal(t, []int{3, 4, 5}, result, "Skip should skip first n elements")
fullResult := Of(1, 2, 3).Skip(0).Collect()
assert.Equal(t, []int{1, 2, 3}, fullResult, "Skip(0) should return all elements")
})
t.Run("TakeWhile", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 1, 2).
TakeWhile(func(n int) bool { return n < 4 }).
Collect()
assert.Equal(t, []int{1, 2, 3}, result, "TakeWhile should take while predicate is true")
})
t.Run("DropWhile", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 1, 2).
DropWhile(func(n int) bool { return n < 3 }).
Collect()
assert.Equal(t, []int{3, 4, 1, 2}, result, "DropWhile should drop while predicate is true")
})
t.Run("Sorted", func(t *testing.T) {
t.Parallel()
result := Of(3, 1, 4, 1, 5).
Sorted(func(a, b int) int { return a - b }).
Collect()
assert.Equal(t, []int{1, 1, 3, 4, 5}, result, "Sorted should sort elements")
})
t.Run("Reverse", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).Reverse().Collect()
assert.Equal(t, []int{5, 4, 3, 2, 1}, result, "Reverse should reverse order")
})
t.Run("Chunk", func(t *testing.T) {
t.Parallel()
result := Chunk(Of(1, 2, 3, 4, 5), 2).Collect()
assert.Equal(t, [][]int{{1, 2}, {3, 4}, {5}}, result, "Chunk should group elements")
emptyResult := Chunk(Of(1, 2, 3), 0).Collect()
assert.Empty(t, emptyResult, "Chunk(0) should be empty")
})
// Additional early termination tests
t.Run("PeekEarlyTermination", func(t *testing.T) {
t.Parallel()
var peeked []int
result := Of(1, 2, 3, 4, 5).
Peek(func(n int) { peeked = append(peeked, n) }).
Limit(2).
Collect()
assert.Equal(t, []int{1, 2}, result, "Peek then Limit(2) should collect first two values [1 2]")
// Peek sees the element before yield returns false, so it may see one more
assert.True(t, len(peeked) >= 2 && len(peeked) <= 3, "Peek should see at least limited elements")
})
t.Run("SkipEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).Skip(2).Limit(2).Collect()
assert.Equal(t, []int{3, 4}, result, "Skip(2) then Limit(2) should yield [3 4]")
})
t.Run("TakeWhileEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).TakeWhile(func(n int) bool { return n < 10 }).Limit(2).Collect()
assert.Equal(t, []int{1, 2}, result, "TakeWhile(n<10) then Limit(2) should yield [1 2]")
})
t.Run("DropWhileEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).DropWhile(func(n int) bool { return n < 2 }).Limit(2).Collect()
assert.Equal(t, []int{2, 3}, result, "DropWhile(n<2) then Limit(2) should yield [2 3]")
})
t.Run("SortedEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Of(5, 4, 3, 2, 1).Sorted(func(a, b int) int { return a - b }).Limit(2).Collect()
assert.Equal(t, []int{1, 2}, result, "Sorted(asc) then Limit(2) should yield [1 2]")
})
t.Run("ReverseEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).Reverse().Limit(2).Collect()
assert.Equal(t, []int{5, 4}, result, "Reverse then Limit(2) should yield [5 4]")
})
t.Run("ChunkEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Chunk(Of(1, 2, 3, 4, 5, 6), 2).Limit(2).Collect()
assert.Equal(t, [][]int{{1, 2}, {3, 4}}, result, "Chunk(size=2) then Limit(2) should yield [[1 2] [3 4]]")
})
}
// TestMapToAndFlatMap tests type-changing transformations.
func TestMapToAndFlatMap(t *testing.T) {
t.Parallel()
t.Run("MapTo", func(t *testing.T) {
t.Parallel()
type Person struct {
Name string
Age int
}
people := []Person{
{Name: "Alice", Age: 30},
{Name: "Bob", Age: 25},
}
names := MapTo(FromSlice(people), func(p Person) string {
return p.Name
}).Collect()
assert.Equal(t, []string{"Alice", "Bob"}, names, "MapTo should change element type")
})
t.Run("FlatMap", func(t *testing.T) {
t.Parallel()
result := FlatMap(Of(1, 2, 3), func(n int) Stream[int] {
return Of(n, n*10)
}).Collect()
assert.Equal(t, []int{1, 10, 2, 20, 3, 30}, result, "FlatMap should flatten results")
})
t.Run("FlatMapSeq", func(t *testing.T) {
t.Parallel()
result := FlatMapSeq(Of("ab", "cd"), func(s string) iter.Seq[rune] {
return func(yield func(rune) bool) {
for _, r := range s {
if !yield(r) {
return
}
}
}
}).Collect()
assert.Equal(t, []rune{'a', 'b', 'c', 'd'}, result, "FlatMapSeq over \"ab\",\"cd\" should yield runes [a b c d]")
})
// Early termination tests
t.Run("MapToEarlyTermination", func(t *testing.T) {
t.Parallel()
result := MapTo(Of(1, 2, 3, 4, 5), func(n int) string {
return string(rune('a' + n - 1))
}).Limit(2).Collect()
assert.Equal(t, []string{"a", "b"}, result, "MapTo int->letter then Limit(2) should yield [\"a\" \"b\"]")
})
t.Run("FlatMapEarlyTermination", func(t *testing.T) {
t.Parallel()
result := FlatMap(Of(1, 2, 3), func(n int) Stream[int] {
return Of(n*10, n*10+1)
}).Limit(3).Collect()
assert.Equal(t, []int{10, 11, 20}, result, "FlatMap n->[n*10,n*10+1] then Limit(3) should yield [10 11 20]")
})
t.Run("FlatMapSeqEarlyTermination", func(t *testing.T) {
t.Parallel()
result := FlatMapSeq(Of("abc", "def"), func(s string) iter.Seq[rune] {
return func(yield func(rune) bool) {
for _, r := range s {
if !yield(r) {
return
}
}
}
}).Limit(4).Collect()
assert.Equal(t, []rune{'a', 'b', 'c', 'd'}, result, "FlatMapSeq over \"abc\",\"def\" then Limit(4) should yield [a b c d]")
})
}
// TestZipOperations tests zip-related operations.
func TestZipOperations(t *testing.T) {
t.Parallel()
t.Run("Zip", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 2, 3)
s2 := Of("a", "b", "c")
result := Zip(s1, s2).Collect()
expected := []Pair[int, string]{
{First: 1, Second: "a"},
{First: 2, Second: "b"},
{First: 3, Second: "c"},
}
assert.Equal(t, expected, result, "Zip should combine streams")
})
t.Run("ZipUnequalLength", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 2, 3, 4)
s2 := Of("a", "b")
result := Zip(s1, s2).Collect()
assert.Len(t, result, 2, "Zip should stop at shorter stream")
})
t.Run("ZipWithIndex", func(t *testing.T) {
t.Parallel()
result := ZipWithIndex(Of("a", "b", "c")).CollectPairs()
expected := []Pair[int, string]{
{First: 0, Second: "a"},
{First: 1, Second: "b"},
{First: 2, Second: "c"},
}
assert.Equal(t, expected, result, "ZipWithIndex should add indices")
})
// Early termination tests
t.Run("ZipEarlyTermination", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 2, 3, 4, 5)
s2 := Of("a", "b", "c", "d", "e")
result := Zip(s1, s2).Limit(2).Collect()
assert.Len(t, result, 2, "Zip then Limit(2) should return exactly 2 pairs")
})
t.Run("ZipWithIndexEarlyTermination", func(t *testing.T) {
t.Parallel()
result := ZipWithIndex(Of("a", "b", "c", "d", "e")).Limit(2).CollectPairs()
assert.Len(t, result, 2, "ZipWithIndex then Limit(2) should return first 2 indexed pairs")
})
t.Run("Unzip", func(t *testing.T) {
t.Parallel()
pairs := Of(
NewPair(1, "a"),
NewPair(2, "b"),
NewPair(3, "c"),
)
firsts, seconds := Unzip(pairs)
assert.Equal(t, []int{1, 2, 3}, firsts, "Unzip should extract first elements")
assert.Equal(t, []string{"a", "b", "c"}, seconds, "Unzip should extract second elements")
})
t.Run("Zip3", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 2)
s2 := Of("a", "b")
s3 := Of(1.0, 2.0)
result := Zip3(s1, s2, s3).Collect()
assert.Len(t, result, 2, "Zip3 should combine three streams")
assert.Equal(t, 1, result[0].First, "First element should match")
assert.Equal(t, "a", result[0].Second, "Second element should match")
assert.Equal(t, 1.0, result[0].Third, "Third element should match")
})
}
// TestDistinctOperations tests distinct-related operations.
func TestDistinctOperations(t *testing.T) {
t.Parallel()
t.Run("Distinct", func(t *testing.T) {
t.Parallel()
result := Distinct(Of(1, 2, 2, 3, 1, 3)).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "Distinct should remove duplicates")
})
t.Run("DistinctBy", func(t *testing.T) {
t.Parallel()
type Person struct {
Name string
Age int
}
people := []Person{
{Name: "Alice", Age: 30},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 25},
}
result := DistinctBy(FromSlice(people), func(p Person) int {
return p.Age
}).Collect()
assert.Len(t, result, 2, "DistinctBy should remove duplicates by key")
})
}
// TestSortingOperations tests sorting-related operations.
func TestSortingOperations(t *testing.T) {
t.Parallel()
t.Run("SortedBy", func(t *testing.T) {
t.Parallel()
type Person struct {
Name string
Age int
}
people := []Person{
{Name: "Charlie", Age: 35},
{Name: "Alice", Age: 30},
{Name: "Bob", Age: 25},
}
result := SortedBy(FromSlice(people), func(p Person) int {
return p.Age
}).Collect()
assert.Equal(t, "Bob", result[0].Name, "SortedBy should sort by key")
assert.Equal(t, "Alice", result[1].Name, "SortedBy should sort by key")
assert.Equal(t, "Charlie", result[2].Name, "SortedBy should sort by key")
})
t.Run("SortedByEarlyTermination", func(t *testing.T) {
t.Parallel()
result := SortedBy(Of(5, 3, 1, 4, 2), func(n int) int { return n }).Limit(2).Collect()
assert.Equal(t, []int{1, 2}, result, "SortedBy(identity) then Limit(2) should yield [1 2]")
})
}
// TestWindowAndInterleave tests window and interleave operations.
func TestWindowAndInterleave(t *testing.T) {
t.Parallel()
t.Run("Window", func(t *testing.T) {
t.Parallel()
result := Window(Of(1, 2, 3, 4, 5), 3).Collect()
expected := [][]int{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}
assert.Equal(t, expected, result, "Window should create sliding windows")
})
t.Run("WindowSmallerThanSize", func(t *testing.T) {
t.Parallel()
result := Window(Of(1, 2), 5).Collect()
assert.Empty(t, result, "Window should be empty if size > stream length")
})
t.Run("Interleave", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 3, 5)
s2 := Of(2, 4)
result := Interleave(s1, s2).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result, "Interleave should alternate elements")
})
// Early termination tests
t.Run("WindowEarlyTermination", func(t *testing.T) {
t.Parallel()
result := Window(Of(1, 2, 3, 4, 5, 6, 7), 3).Limit(2).Collect()
assert.Len(t, result, 2, "Window(size=3) then Limit(2) should return first 2 windows")
})
t.Run("InterleaveEarlyTermination", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 3, 5, 7, 9)
s2 := Of(2, 4, 6, 8, 10)
result := Interleave(s1, s2).Limit(4).Collect()
assert.Len(t, result, 4, "Interleave then Limit(4) should return exactly 4 elements")
})
t.Run("InterleaveEarlyTerminationS1Side", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 3, 5, 7, 9, 11, 13)
s2 := Of(2, 4, 6)
result := Interleave(s1, s2).Limit(5).Collect()
assert.Len(t, result, 5, "Interleave(s2 shorter) then Limit(5) should return 5 elements")
})
}
// TestEarlyTermination tests that streams handle early termination correctly.
func TestEarlyTermination(t *testing.T) {
t.Parallel()
t.Run("FilterWithLimit", func(t *testing.T) {
t.Parallel()
// Filter should stop processing once limit is reached
result := Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).
Filter(func(n int) bool { return n%2 == 0 }).
Limit(2).
Collect()
assert.Equal(t, []int{2, 4}, result, "Filter evens then Limit(2) should yield [2 4]")
})
t.Run("MapWithLimit", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).
Map(func(n int) int { return n * 2 }).
Limit(2).
Collect()
assert.Equal(t, []int{2, 4}, result, "Map(*2) then Limit(2) should yield [2 4]")
})
t.Run("GenerateWithLimit", func(t *testing.T) {
t.Parallel()
counter := 0
result := Generate(func() int {
counter++
return counter
}).Limit(3).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "Generate(counter) then Limit(3) should yield [1 2 3]")
})
t.Run("InfiniteIterateWithTakeWhile", func(t *testing.T) {
t.Parallel()
result := Iterate(1, func(n int) int { return n + 1 }).
TakeWhile(func(n int) bool { return n <= 5 }).
Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result, "Iterate(+1) then TakeWhile(n<=5) should yield [1 2 3 4 5]")
})
t.Run("ChainedOperationsWithLimit", func(t *testing.T) {
t.Parallel()
result := Range(1, 100).
Filter(func(n int) bool { return n%3 == 0 }).
Map(func(n int) int { return n * 2 }).
Limit(3).
Collect()
assert.Equal(t, []int{6, 12, 18}, result, "Range(1,100)->Filter(n%3==0)->Map(*2) then Limit(3) should yield [6 12 18]")
})
t.Run("FindFirstStopsEarly", func(t *testing.T) {
t.Parallel()
// FindFirst should stop as soon as it finds a match
result := Range(1, 1000000).
FindFirst(func(n int) bool { return n > 100 })
assert.True(t, result.IsPresent(), "FindFirst should find element")
assert.Equal(t, 101, result.Get(), "FindFirst should return first match")
})
t.Run("AnyMatchStopsEarly", func(t *testing.T) {
t.Parallel()
result := Range(1, 1000000).
AnyMatch(func(n int) bool { return n == 50 })
assert.True(t, result, "AnyMatch should find match early")
})
// Additional early termination tests for previously uncovered code paths
t.Run("FromChannelWithLimit", func(t *testing.T) {
t.Parallel()
ch := make(chan int, 10)
for i := 1; i <= 10; i++ {
ch <- i
}
close(ch)
result := FromChannel(ch).Limit(3).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "FromChannel(1..10) then Limit(3) should yield [1 2 3]")
})
t.Run("RangeClosedWithLimit", func(t *testing.T) {
t.Parallel()
result := RangeClosed(1, 100).Limit(3).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "RangeClosed(1,100) then Limit(3) should yield [1 2 3]")
})
t.Run("ConcatWithLimit", func(t *testing.T) {
t.Parallel()
s1 := Of(1, 2, 3)
s2 := Of(4, 5, 6)
result := Concat(s1, s2).Limit(2).Collect()
assert.Equal(t, []int{1, 2}, result, "Concat([1 2 3],[4 5 6]) then Limit(2) should yield [1 2]")
})
t.Run("RepeatWithLimit", func(t *testing.T) {
t.Parallel()
result := Repeat("x", 100).Limit(3).Collect()
assert.Equal(t, []string{"x", "x", "x"}, result, "Repeat(\"x\",100) then Limit(3) should yield [x x x]")
})
t.Run("RepeatForeverWithLimit", func(t *testing.T) {
t.Parallel()
result := RepeatForever("x").Limit(3).Collect()
assert.Equal(t, []string{"x", "x", "x"}, result, "RepeatForever(\"x\") then Limit(3) should yield [x x x]")
})
}
// TestNewStreamOperations tests newly added stream operations.
func TestNewStreamOperations(t *testing.T) {
t.Parallel()
t.Run("Scan", func(t *testing.T) {
t.Parallel()
// Running sum using Scan
result := Scan(Of(1, 2, 3, 4, 5), 0, func(acc, v int) int { return acc + v }).Collect()
assert.Equal(t, []int{1, 3, 6, 10, 15}, result, "Scan should produce running totals")
// Running product
result2 := Scan(Of(1, 2, 3, 4), 1, func(acc, v int) int { return acc * v }).Collect()
assert.Equal(t, []int{1, 2, 6, 24}, result2, "Scan(product) over [1 2 3 4] should yield [1 2 6 24]")
// Empty stream
result3 := Scan(Empty[int](), 0, func(acc, v int) int { return acc + v }).Collect()
assert.Empty(t, result3, "Scan on empty stream should be empty")
})
t.Run("Step", func(t *testing.T) {
t.Parallel()
// Every 2nd element
result := Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).Step(2).Collect()
assert.Equal(t, []int{1, 3, 5, 7, 9}, result, "Step(2) should return every 2nd element")
// Every 3rd element
result2 := Of(1, 2, 3, 4, 5, 6, 7, 8, 9).Step(3).Collect()
assert.Equal(t, []int{1, 4, 7}, result2, "Step(3) should return every 3rd element")
// Step 1 should return all elements
result3 := Of(1, 2, 3).Step(1).Collect()
assert.Equal(t, []int{1, 2, 3}, result3, "Step(1) should return all elements")
// Step 0 should return all elements
result4 := Of(1, 2, 3).Step(0).Collect()
assert.Equal(t, []int{1, 2, 3}, result4, "Step(0) should return all elements")
})
t.Run("DistinctUntilChanged", func(t *testing.T) {
t.Parallel()
result := DistinctUntilChanged(Of(1, 1, 2, 2, 2, 3, 1, 1)).Collect()
assert.Equal(t, []int{1, 2, 3, 1}, result, "DistinctUntilChanged should remove consecutive duplicates")
// All same
result2 := DistinctUntilChanged(Of(1, 1, 1, 1)).Collect()
assert.Equal(t, []int{1}, result2, "DistinctUntilChanged should collapse all same values to one")
// All different
result3 := DistinctUntilChanged(Of(1, 2, 3, 4)).Collect()
assert.Equal(t, []int{1, 2, 3, 4}, result3, "DistinctUntilChanged should keep all different values")
// Empty stream
result4 := DistinctUntilChanged(Empty[int]()).Collect()
assert.Empty(t, result4, "DistinctUntilChanged on empty stream should be empty")
})
t.Run("DistinctUntilChangedBy", func(t *testing.T) {
t.Parallel()
type item struct {
id int
name string
}
items := Of(
item{1, "a"}, item{1, "b"}, item{2, "c"}, item{2, "d"}, item{1, "e"},
)
result := DistinctUntilChangedBy(items, func(a, b item) bool { return a.id == b.id }).Collect()
assert.Len(t, result, 3, "DistinctUntilChangedBy should remove consecutive duplicates by key")
assert.Equal(t, 1, result[0].id, "DistinctUntilChangedBy result[0].id should be 1")
assert.Equal(t, 2, result[1].id, "DistinctUntilChangedBy result[1].id should be 2")
assert.Equal(t, 1, result[2].id, "DistinctUntilChangedBy result[2].id should be 1")
})
t.Run("TakeLast", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).TakeLast(3).Collect()
assert.Equal(t, []int{3, 4, 5}, result, "TakeLast should return last 3 elements")
// TakeLast more than available
result2 := Of(1, 2).TakeLast(5).Collect()
assert.Equal(t, []int{1, 2}, result2, "TakeLast should return all if n > length")
// TakeLast 0
result3 := Of(1, 2, 3).TakeLast(0).Collect()
assert.Empty(t, result3, "TakeLast(0) should return empty")
// TakeLast negative
result4 := Of(1, 2, 3).TakeLast(-1).Collect()
assert.Empty(t, result4, "TakeLast(-1) should return empty")
// Empty stream
result5 := Empty[int]().TakeLast(3).Collect()
assert.Empty(t, result5, "TakeLast on empty stream should be empty")
})
t.Run("DropLast", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3, 4, 5).DropLast(2).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "DropLast should remove last 2 elements")
// DropLast more than available
result2 := Of(1, 2).DropLast(5).Collect()
assert.Empty(t, result2, "DropLast should return empty if n >= length")
// DropLast 0
result3 := Of(1, 2, 3).DropLast(0).Collect()
assert.Equal(t, []int{1, 2, 3}, result3, "DropLast(0) should return all elements")
})
t.Run("WindowWithStep", func(t *testing.T) {
t.Parallel()
// Step 1 (sliding window)
result := WindowWithStep(Of(1, 2, 3, 4, 5), 3, 1, false).Collect()
assert.Equal(t, [][]int{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}, result, "WindowWithStep should create sliding windows")
// Step 2
result2 := WindowWithStep(Of(1, 2, 3, 4, 5, 6), 2, 2, false).Collect()
assert.Equal(t, [][]int{{1, 2}, {3, 4}, {5, 6}}, result2, "WindowWithStep with step=size should create chunks")
// With partial window allowed
result3 := WindowWithStep(Of(1, 2, 3, 4, 5), 3, 2, true).Collect()
assert.Equal(t, [][]int{{1, 2, 3}, {3, 4, 5}, {5}}, result3, "WindowWithStep with allowPartial should include partial")
// Without partial window
result4 := WindowWithStep(Of(1, 2, 3, 4, 5), 3, 2, false).Collect()
assert.Equal(t, [][]int{{1, 2, 3}, {3, 4, 5}}, result4, "WindowWithStep without allowPartial should exclude partial")
})
t.Run("Pairwise", func(t *testing.T) {
t.Parallel()
result := Pairwise(Of(1, 2, 3, 4)).Collect()
assert.Len(t, result, 3, "Pairwise should return n-1 pairs")
assert.Equal(t, Pair[int, int]{1, 2}, result[0], "Pairwise result[0] should be (1,2)")
assert.Equal(t, Pair[int, int]{2, 3}, result[1], "Pairwise result[1] should be (2,3)")
assert.Equal(t, Pair[int, int]{3, 4}, result[2], "Pairwise result[2] should be (3,4)")
// Single element
result2 := Pairwise(Of(1)).Collect()
assert.Empty(t, result2, "Pairwise with single element should be empty")
// Empty stream
result3 := Pairwise(Empty[int]()).Collect()
assert.Empty(t, result3, "Pairwise on empty stream should be empty")
})
t.Run("Triples", func(t *testing.T) {
t.Parallel()
result := Triples(Of(1, 2, 3, 4, 5)).Collect()
assert.Len(t, result, 3, "Triples should return n-2 triples")
assert.Equal(t, Triple[int, int, int]{1, 2, 3}, result[0], "Triples result[0] should be (1,2,3)")
assert.Equal(t, Triple[int, int, int]{2, 3, 4}, result[1], "Triples result[1] should be (2,3,4)")
assert.Equal(t, Triple[int, int, int]{3, 4, 5}, result[2], "Triples result[2] should be (3,4,5)")
})
t.Run("SortedStable", func(t *testing.T) {
t.Parallel()
type item struct {
key int
order int // original order
}
items := []item{{1, 1}, {2, 2}, {1, 3}, {2, 4}, {1, 5}}
result := Of(items...).SortedStable(func(a, b item) int {
return a.key - b.key
}).Collect()
// Items with key=1 should maintain their relative order
key1Items := []item{}
for _, it := range result {
if it.key == 1 {
key1Items = append(key1Items, it)
}
}
assert.Equal(t, []item{{1, 1}, {1, 3}, {1, 5}}, key1Items, "SortedStable should maintain relative order")
})
t.Run("Flatten", func(t *testing.T) {
t.Parallel()
nested := Of([]int{1, 2}, []int{3, 4, 5}, []int{6})
result := Flatten(nested).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5, 6}, result, "Flatten should flatten nested slices")
// Empty inner slices
nested2 := Of([]int{1}, []int{}, []int{2, 3})
result2 := Flatten(nested2).Collect()
assert.Equal(t, []int{1, 2, 3}, result2, "Flatten should handle empty inner slices")
})
t.Run("Intersperse", func(t *testing.T) {
t.Parallel()
result := Of(1, 2, 3).Intersperse(0).Collect()
assert.Equal(t, []int{1, 0, 2, 0, 3}, result, "Intersperse should insert separator between elements")
// Single element
result2 := Of(1).Intersperse(0).Collect()
assert.Equal(t, []int{1}, result2, "Intersperse with single element should not add separator")
// Empty stream
result3 := Empty[int]().Intersperse(0).Collect()
assert.Empty(t, result3, "Intersperse on empty stream should be empty")
})
}
// TestEdgeCases tests boundary conditions and edge cases.
func TestEdgeCases(t *testing.T) {
t.Parallel()
t.Run("TakeLastLargeN", func(t *testing.T) {
t.Parallel()
// n much larger than input
result := Of(1, 2, 3).TakeLast(1000).Collect()
assert.Equal(t, []int{1, 2, 3}, result, "TakeLast with n > len should return all elements")
// Large input with small n
input := Range(1, 10001).Collect() // 1 to 10000
result2 := FromSlice(input).TakeLast(5).Collect()
assert.Equal(t, []int{9996, 9997, 9998, 9999, 10000}, result2, "TakeLast should correctly return last 5 of large input")
})
t.Run("DropLastLargeN", func(t *testing.T) {
t.Parallel()
// n much larger than input
result := Of(1, 2, 3).DropLast(1000).Collect()
assert.Empty(t, result, "DropLast with n > len should return empty")
// Large input with small n
input := Range(1, 101).Collect() // 1 to 100
result2 := FromSlice(input).DropLast(3).Collect()
assert.Len(t, result2, 97, "DropLast should drop last 3 elements")
assert.Equal(t, 1, result2[0], "First element should be 1")
assert.Equal(t, 97, result2[96], "Last element should be 97")
})
t.Run("TakeLastRingBufferCorrectness", func(t *testing.T) {
t.Parallel()
// Test that ring buffer correctly wraps around
result := Range(1, 11).TakeLast(3).Collect() // 1-10, take last 3
assert.Equal(t, []int{8, 9, 10}, result, "Ring buffer should correctly track last elements")
// Edge case: n equals input length
result2 := Of(1, 2, 3, 4, 5).TakeLast(5).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result2, "TakeLast with n == len should return all")
})
t.Run("DistinctUntilChangedByAlwaysTrue", func(t *testing.T) {
t.Parallel()
// eq always returns true: only first element should be yielded
result := DistinctUntilChangedBy(Of(1, 2, 3, 4, 5), func(a, b int) bool { return true }).Collect()
assert.Equal(t, []int{1}, result, "DistinctUntilChangedBy with always-true eq should yield only first")
})
t.Run("DistinctUntilChangedByAlwaysFalse", func(t *testing.T) {
t.Parallel()
// eq always returns false: all elements should be yielded
result := DistinctUntilChangedBy(Of(1, 1, 1, 1, 1), func(a, b int) bool { return false }).Collect()
assert.Equal(t, []int{1, 1, 1, 1, 1}, result, "DistinctUntilChangedBy with always-false eq should yield all")
})
t.Run("WindowWithStepStepGreaterThanSize", func(t *testing.T) {
t.Parallel()
// Step > Size: windows should not overlap, with gaps
result := WindowWithStep(Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 2, 4, false).Collect()
assert.Equal(t, [][]int{{1, 2}, {5, 6}, {9, 10}}, result, "Windows with step>size should skip elements")
// Step > Size with allowPartial
result2 := WindowWithStep(Of(1, 2, 3, 4, 5, 6, 7), 2, 3, true).Collect()
assert.Equal(t, [][]int{{1, 2}, {4, 5}, {7}}, result2, "allowPartial should yield partial window at end")
})
t.Run("WindowWithStepStepEqualsSizeNoPartial", func(t *testing.T) {
t.Parallel()
// Step == Size: non-overlapping chunks
result := WindowWithStep(Of(1, 2, 3, 4, 5), 2, 2, false).Collect()
assert.Equal(t, [][]int{{1, 2}, {3, 4}}, result, "Step==Size without partial should act like Chunk")
})
t.Run("LimitSkipEdgeCases", func(t *testing.T) {
t.Parallel()
// Limit(0) returns empty
result := Of(1, 2, 3).Limit(0).Collect()
assert.Empty(t, result, "Limit(0) should return empty")
// Skip(0) returns all
result2 := Of(1, 2, 3).Skip(0).Collect()
assert.Equal(t, []int{1, 2, 3}, result2, "Skip(0) should return all elements")
// Negative values
result3 := Of(1, 2, 3).Limit(-1).Collect()
assert.Empty(t, result3, "Limit(-1) should return empty")
})
t.Run("StepEdgeCases", func(t *testing.T) {
t.Parallel()
// Step(1) returns all
result := Of(1, 2, 3, 4, 5).Step(1).Collect()
assert.Equal(t, []int{1, 2, 3, 4, 5}, result, "Step(1) should return all")
// Step(0) returns all
result2 := Of(1, 2, 3).Step(0).Collect()
assert.Equal(t, []int{1, 2, 3}, result2, "Step(0) should return all")
// Step(-1) returns all
result3 := Of(1, 2, 3).Step(-1).Collect()
assert.Equal(t, []int{1, 2, 3}, result3, "Step(-1) should return all")
// Step larger than input
result4 := Of(1, 2, 3).Step(10).Collect()
assert.Equal(t, []int{1}, result4, "Step(10) on 3 elements should return only first")
})
}
// TestRepeatForever tests RepeatForever function.
func TestRepeatForever(t *testing.T) {
t.Parallel()
t.Run("Basic", func(t *testing.T) {
t.Parallel()
result := RepeatForever("x").Limit(5).Collect()
assert.Equal(t, []string{"x", "x", "x", "x", "x"}, result, "RepeatForever(\"x\") Limit(5) should yield five \"x\" values")
})
t.Run("WithInt", func(t *testing.T) {
t.Parallel()