This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgroup_by_test.go
325 lines (255 loc) · 6.85 KB
/
group_by_test.go
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
package iter
import (
"math/rand"
"testing"
"github.com/barweiss/go-tuple"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"mtoohey.com/iter/v2/testutils"
)
func groupBySlice[K comparable, V any](s []V, f func(V) K) []tuple.T2[K, []V] {
res := []tuple.T2[K, []V]{}
if len(s) == 0 {
return res
}
currentKey := f(s[0])
groupSoFar := []V{s[0]}
for _, v := range s[1:] {
nextKey := f(v)
if nextKey != currentKey {
res = append(res, tuple.T2[K, []V]{V1: currentKey, V2: groupSoFar})
currentKey = nextKey
groupSoFar = nil
}
groupSoFar = append(groupSoFar, v)
}
return append(res, tuple.T2[K, []V]{V1: currentKey, V2: groupSoFar})
}
// smush sticks the two slices together, preserving the original orderings of
// elements as they are arranged in the input slices, but interleaving elements
// of the two slices amongst each other randomly.
func smush[T any](a []T, b []T) []T {
res := make([]T, len(a)+len(b))
for i := 0; ; i++ {
if len(a) == 0 {
copy(res[i:], b)
break
}
if len(b) == 0 {
copy(res[i:], a)
break
}
if rand.Int()%2 == 0 {
res[i] = a[0]
a = a[1:]
} else {
res[i] = b[0]
b = b[1:]
}
}
return res
}
func FuzzGroupBy(f *testing.F) {
testutils.AddByteSlices(f)
f.Fuzz(func(t *testing.T, b []byte) {
groupFunc := func(b byte) bool { return b%2 == 0 }
expectedPartitions := groupBySlice(b, groupFunc)
t.Run("sub-iterators evaluated as we go", func(t *testing.T) {
oi := GroupBy(Elems(b), groupFunc)
for _, expectedPartition := range expectedPartitions {
p, ok := oi()
require.True(t, ok)
assert.Equal(t, expectedPartition.V1, p.V1)
assert.Equal(t, expectedPartition.V2, p.V2.Collect())
}
p, ok := oi()
if assert.False(t, ok) {
assert.Zero(t, p)
}
})
t.Run("sub-iterators evaluated later", func(t *testing.T) {
actualSubIterators := []Iter[byte]{}
oi := GroupBy(Elems(b), groupFunc)
for _, expectedPartition := range expectedPartitions {
p, ok := oi()
require.True(t, ok)
assert.Equal(t, expectedPartition.V1, p.V1)
actualSubIterators = append(actualSubIterators, p.V2)
}
p, ok := oi()
if assert.False(t, ok) {
assert.Zero(t, p)
}
require.Len(t, actualSubIterators, len(expectedPartitions))
for i, expectedPartition := range expectedPartitions {
assert.Equal(t, expectedPartition.V2, actualSubIterators[i].Collect())
}
})
t.Run("sub-iterators evaluated ぐちゃぐちゃ", func(t *testing.T) {
operations := []func(){}
oi := GroupBy(Elems(b), groupFunc)
for i, expectedPartition := range expectedPartitions {
i := i
expectedPartition := expectedPartition
operations = append(operations, func() {
t.Logf("evaluating oi index %d, expecting: %#v", i, expectedPartition)
p, ok := oi()
require.True(t, ok)
assert.Equal(t, expectedPartition.V1, p.V1)
subIteratorOperations := []func(){}
for j, expectedNext := range expectedPartition.V2 {
j := j
expectedNext := expectedNext
subIteratorOperations = append(subIteratorOperations, func() {
t.Logf("evaluating si index %d,%d, expecting: %#v", i, j, expectedNext)
next, ok := p.V2()
if assert.True(t, ok) {
assert.Equal(t, expectedNext, next)
}
})
}
subIteratorOperations = append(subIteratorOperations, func() {
t.Logf("evaluating si index %d,%d, expecting nothing", i, len(expectedPartition.V2))
next, ok := p.V2()
if assert.False(t, ok) {
assert.Zero(t, next)
}
})
operations = smush(operations, subIteratorOperations)
})
}
operations = append(operations, func() {
t.Logf("evaluating oi index %d, expecting nothing", len(expectedPartitions))
next, ok := oi()
if assert.False(t, ok) {
assert.Zero(t, next)
}
})
for len(operations) > 0 {
operation := operations[0]
operations = operations[1:]
operation()
}
})
})
}
func BenchmarkGroupBy_asWeGo_1(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return v%2 == 0
})
for next, ok := oi(); ok; next, ok = oi() {
next.V2.Consume()
}
}
func BenchmarkGroupBy_asWeGo_10(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v/10)%2 == 0
})
for next, ok := oi(); ok; next, ok = oi() {
next.V2.Consume()
}
}
func BenchmarkGroupBy_asWeGo_100(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v/100)%2 == 0
})
for next, ok := oi(); ok; next, ok = oi() {
next.V2.Consume()
}
}
func BenchmarkGroupBy_asWeGo_quarter(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v*4/b.N)%2 == 0
})
for next, ok := oi(); ok; next, ok = oi() {
next.V2.Consume()
}
}
func BenchmarkGroupBy_asWeGo_half(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v*2/b.N)%2 == 0
})
for next, ok := oi(); ok; next, ok = oi() {
next.V2.Consume()
}
}
func BenchmarkGroupBy_asWeGo_full(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) struct{} {
return struct{}{}
})
for next, ok := oi(); ok; next, ok = oi() {
next.V2.Consume()
}
}
func BenchmarkGroupBy_later_1(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return v%2 == 0
})
sis := []Iter[int]{}
for next, ok := oi(); ok; next, ok = oi() {
sis = append(sis, next.V2)
}
for _, si := range sis {
si.Consume()
}
}
func BenchmarkGroupBy_later_10(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v/10)%2 == 0
})
sis := []Iter[int]{}
for next, ok := oi(); ok; next, ok = oi() {
sis = append(sis, next.V2)
}
for _, si := range sis {
si.Consume()
}
}
func BenchmarkGroupBy_later_100(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v/100)%2 == 0
})
sis := []Iter[int]{}
for next, ok := oi(); ok; next, ok = oi() {
sis = append(sis, next.V2)
}
for _, si := range sis {
si.Consume()
}
}
func BenchmarkGroupBy_later_quarter(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v*4/b.N)%2 == 0
})
sis := []Iter[int]{}
for next, ok := oi(); ok; next, ok = oi() {
sis = append(sis, next.V2)
}
for _, si := range sis {
si.Consume()
}
}
func BenchmarkGroupBy_later_half(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) bool {
return (v*2/b.N)%2 == 0
})
sis := []Iter[int]{}
for next, ok := oi(); ok; next, ok = oi() {
sis = append(sis, next.V2)
}
for _, si := range sis {
si.Consume()
}
}
func BenchmarkGroupBy_later_full(b *testing.B) {
oi := GroupBy(Ints[int]().Take(uint(b.N)), func(v int) struct{} {
return struct{}{}
})
sis := []Iter[int]{}
for next, ok := oi(); ok; next, ok = oi() {
sis = append(sis, next.V2)
}
for _, si := range sis {
si.Consume()
}
}