-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitertools_test.go
More file actions
365 lines (290 loc) · 9.04 KB
/
itertools_test.go
File metadata and controls
365 lines (290 loc) · 9.04 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
package itertools_test
import (
"fmt"
"runtime"
"testing"
"time"
"github.com/amjadjibon/itertools"
"github.com/stretchr/testify/assert"
)
func TestNewIterator(t *testing.T) {
iter1 := itertools.NewIterator(1, 2, 3, 4, 5)
collected1 := iter1.Collect()
assert.Equal(t, 5, len(collected1))
assert.Equal(t, []int{1, 2, 3, 4, 5}, collected1)
iter2 := itertools.NewIterator("a", "b", "c", "d", "e")
collected2 := iter2.Collect()
assert.Equal(t, 5, len(collected2))
assert.Equal(t, []string{"a", "b", "c", "d", "e"}, collected2)
}
func TestZip(t *testing.T) {
slice1 := []int{1, 2, 3, 4, 5, 6}
slice2 := []string{"a", "b", "c", "d", "e"}
iter1 := itertools.ToIter(slice1)
iter2 := itertools.ToIter(slice2)
zip := itertools.Zip(iter1, iter2).Collect()
if len(zip) != 5 {
t.Errorf("expected 5 elements, got %d", len(zip))
}
for i, v := range zip {
if slice1[i] != v.First {
t.Errorf("expected %d, got %d", v.First, slice1[i])
}
if slice2[i] != v.Second {
t.Errorf("expected %s, got %s", v.Second, slice2[i])
}
}
}
func TestZip2(t *testing.T) {
slice1 := []int{1, 2, 3, 4, 5}
slice2 := []string{"a", "b", "c"}
iter1 := itertools.ToIter(slice1)
iter2 := itertools.ToIter(slice2)
zip := itertools.Zip2(iter1, iter2, struct {
First int
Second string
}{0, ""}).Collect()
if len(zip) != 5 {
t.Errorf("expected 5 elements, got %d", len(zip))
}
for i, v := range zip {
if i < len(slice1) {
if slice1[i] != v.First {
t.Errorf("expected %d, got %d", v.First, slice1[i])
}
} else {
if v.First != 0 {
t.Errorf("expected 0, got %d", v.First)
}
}
if i < len(slice2) {
if slice2[i] != v.Second {
t.Errorf("expected %s, got %s", v.Second, slice2[i])
}
} else {
if v.Second != "" {
t.Errorf("expected \"\", got %s", v.Second)
}
}
}
}
func TestSum(t *testing.T) {
slice := []int{1, 2, 3, 4, 5}
iter := itertools.ToIter(slice)
sum := itertools.Sum(iter, func(v int) int { return v }, 0)
assert.Equal(t, 15, sum)
}
func TestSumFloat(t *testing.T) {
slice := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
iter := itertools.ToIter(slice)
sum := itertools.Sum(iter, func(v float64) float64 { return v }, 0)
assert.Equal(t, 16.5, sum)
}
func TestSumComplex(t *testing.T) {
type Complex struct {
A int
B int
}
slice := []Complex{{1, 2}, {3, 4}, {5, 6}}
iter := itertools.ToIter(slice)
sum := itertools.Sum(iter, func(v Complex) int { return v.A + v.B }, 0)
assert.Equal(t, 21, sum)
}
func TestFoldSum(t *testing.T) {
slice := []int{1, 2, 3, 4, 5}
iter := itertools.ToIter(slice)
sum := itertools.Fold(iter, func(acc, v int) int { return acc + v }, 0)
assert.Equal(t, 15, sum)
}
func TestFoldProduct(t *testing.T) {
slice := []int{1, 2, 3, 4, 5}
iter := itertools.ToIter(slice)
product := itertools.Fold(iter, func(acc, v int) int { return acc * v }, 1)
assert.Equal(t, 120, product)
}
func TestFoldConcat(t *testing.T) {
slice := []string{"a", "b", "c", "d", "e"}
iter := itertools.ToIter(slice)
concat := itertools.Fold(iter, func(acc, v string) string { return acc + v }, "")
assert.Equal(t, "abcde", concat)
}
func TestProduct(t *testing.T) {
slice := []int{1, 2, 3, 4, 5}
iter := itertools.ToIter(slice)
product := itertools.Product(iter, func(v int) int { return v }, 1)
assert.Equal(t, 120, product)
}
func TestProductFloat(t *testing.T) {
slice := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
iter := itertools.ToIter(slice)
product := itertools.Product(iter, func(v float64) float64 { return v }, 1)
assert.Equal(t, fmt.Sprintf("%.2f", 1.1*2.2*3.3*4.4*5.5), fmt.Sprintf("%.2f", product))
}
func TestProductComplex(t *testing.T) {
type Complex struct {
A int
B int
}
slice := []Complex{{1, 2}, {3, 4}, {5, 6}}
iter := itertools.ToIter(slice)
product := itertools.Product(iter, func(v Complex) int { return v.A * v.B }, 1)
assert.Equal(t, 720, product)
}
func TestChunkSlice(t *testing.T) {
slice := []int{1, 2, 3, 4, 5, 6, 7, 8}
iter := itertools.ToIter(slice)
chunks := itertools.ChunkSlice(iter, 3)
expected := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8}}
for i := 0; chunks.Next(); i++ {
chunk := itertools.ToIter(chunks.Current())
for j := 0; chunk.Next(); j++ {
assert.Equal(t, expected[i][j], chunk.Current())
}
}
}
func TestLazyChunks(t *testing.T) {
slice := []int{1, 2, 3, 4, 5, 6, 7, 8}
iter := itertools.ToIter(slice)
chunks := itertools.Chunks(iter, 3)
expected := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8}}
for i := 0; chunks.Next(); i++ {
chunk := chunks.Current()
for j := 0; chunk.Next(); j++ {
assert.Equal(t, expected[i][j], chunk.Current())
}
}
}
func TestFlatten(t *testing.T) {
slice := []int{1, 2, 3, 4, 5, 6, 7, 8}
iter := itertools.ToIter(slice)
chunkList := itertools.ChunkList(iter, 3)
flatten := itertools.Flatten(chunkList...)
assert.Equal(t, slice, flatten.Collect())
}
func TestCartesianProduct(t *testing.T) {
slice1 := []int{1, 2, 3}
slice2 := []string{"a", "b", "c"}
iter1 := itertools.ToIter(slice1)
iter2 := itertools.ToIter(slice2)
cartesianProduct := itertools.CartesianProduct(iter1, iter2).Collect()
expected := []struct {
X int
Y string
}{
{1, "a"},
{1, "b"},
{1, "c"},
{2, "a"},
{2, "b"},
{2, "c"},
{3, "a"},
{3, "b"},
{3, "c"},
}
for i, v := range cartesianProduct {
assert.Equal(t, expected[i].X, v.X)
assert.Equal(t, expected[i].Y, v.Y)
}
}
func TestCartesianProductEmpty(t *testing.T) {
slice1 := []int{}
slice2 := []string{}
iter1 := itertools.ToIter(slice1)
iter2 := itertools.ToIter(slice2)
cartesianProduct := itertools.CartesianProduct(iter1, iter2).Collect()
assert.Empty(t, cartesianProduct)
}
// TestZip_NoGoroutineLeak verifies that Zip properly cleans up goroutines
// when the iterator is stopped early
func TestZip_NoGoroutineLeak(t *testing.T) {
before := runtime.NumGoroutine()
// Create large iterators but only consume 5 elements
iter1 := itertools.Range(0, 1000000)
iter2 := itertools.Range(0, 1000000)
zipped := itertools.Zip(iter1, iter2).Take(5).Collect()
assert.Equal(t, 5, len(zipped))
// Give time for goroutines to clean up
time.Sleep(100 * time.Millisecond)
runtime.GC()
after := runtime.NumGoroutine()
// Should not have leaked goroutines
assert.LessOrEqual(t, after, before+1, "Goroutine leak detected")
}
// TestZip2_NoGoroutineLeak verifies that Zip2 properly cleans up goroutines
func TestZip2_NoGoroutineLeak(t *testing.T) {
before := runtime.NumGoroutine()
iter1 := itertools.Range(0, 1000000)
iter2 := itertools.Range(0, 1000000)
fill := struct {
First int
Second int
}{-1, -1}
zipped := itertools.Zip2(iter1, iter2, fill).Take(5).Collect()
assert.Equal(t, 5, len(zipped))
time.Sleep(100 * time.Millisecond)
runtime.GC()
after := runtime.NumGoroutine()
assert.LessOrEqual(t, after, before+1, "Goroutine leak detected")
}
// TestZip2_FillValues verifies that Zip2 actually uses fill values
func TestZip2_FillValues(t *testing.T) {
iter1 := itertools.ToIter([]int{1, 2, 3, 4, 5})
iter2 := itertools.ToIter([]string{"a", "b"})
fill := struct {
First int
Second string
}{-1, "FILL"}
result := itertools.Zip2(iter1, iter2, fill).Collect()
assert.Equal(t, 5, len(result))
assert.Equal(t, 1, result[0].First)
assert.Equal(t, "a", result[0].Second)
assert.Equal(t, 2, result[1].First)
assert.Equal(t, "b", result[1].Second)
// After iter2 ends, should use fill value
assert.Equal(t, 3, result[2].First)
assert.Equal(t, "FILL", result[2].Second)
assert.Equal(t, 4, result[3].First)
assert.Equal(t, "FILL", result[3].Second)
assert.Equal(t, 5, result[4].First)
assert.Equal(t, "FILL", result[4].Second)
}
// TestZip2_FillBothSides verifies fill works when iter1 is shorter
func TestZip2_FillBothSides(t *testing.T) {
iter1 := itertools.ToIter([]int{1, 2})
iter2 := itertools.ToIter([]string{"a", "b", "c", "d"})
fill := struct {
First int
Second string
}{-99, "EMPTY"}
result := itertools.Zip2(iter1, iter2, fill).Collect()
assert.Equal(t, 4, len(result))
assert.Equal(t, 1, result[0].First)
assert.Equal(t, "a", result[0].Second)
assert.Equal(t, 2, result[1].First)
assert.Equal(t, "b", result[1].Second)
// After iter1 ends, should use fill value
assert.Equal(t, -99, result[2].First)
assert.Equal(t, "c", result[2].Second)
assert.Equal(t, -99, result[3].First)
assert.Equal(t, "d", result[3].Second)
}
// TestFlatten_EarlyTermination verifies that Flatten stops properly
func TestFlatten_EarlyTermination(t *testing.T) {
iter1 := itertools.Range(0, 100)
iter2 := itertools.Range(100, 200)
iter3 := itertools.Range(200, 300)
// Take only 5 elements - should not process iter2 or iter3
result := itertools.Flatten(iter1, iter2, iter3).Take(5).Collect()
assert.Equal(t, 5, len(result))
assert.Equal(t, []int{0, 1, 2, 3, 4}, result)
}
// TestChunkList_Functional verifies ChunkList works correctly
func TestChunkList_Functional(t *testing.T) {
iter := itertools.Range(0, 10)
chunks := itertools.ChunkList(iter, 3)
assert.Equal(t, 4, len(chunks))
assert.Equal(t, []int{0, 1, 2}, chunks[0].Collect())
assert.Equal(t, []int{3, 4, 5}, chunks[1].Collect())
assert.Equal(t, []int{6, 7, 8}, chunks[2].Collect())
assert.Equal(t, []int{9}, chunks[3].Collect())
}