-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk_test.go
207 lines (167 loc) · 4.76 KB
/
chunk_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
package enumerators_test
import (
"errors"
"testing"
"github.com/fgrzl/enumerators"
"github.com/stretchr/testify/assert"
)
func TestChunk(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{1, 2, 3, 4, 5, 6})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{
{1, 2}, // 1 + 2 = 3 (less than target 5)
{3}, // 3 (exactly hits target 5)
{4}, // 4 (less than target 5, starts new chunk)
{5}, // 5 (exactly hits target 5)
{6}, // 6 (new chunk)
}
assert.Equal(t, expected, result)
}
func TestChunk_EmptyInput(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
assert.EqualValues(t, 0, len(result))
}
func TestChunk_SingleLargeItem(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{10, 2, 3})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{
{10}, // Exceeds target, starts a new chunk
{2, 3}, // Fits within target
}
assert.Equal(t, expected, result)
}
func TestChunk_LargeTarget(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{1, 2, 3, 4, 5, 6})
chunks := enumerators.Chunk(source, 50, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{{1, 2, 3, 4, 5, 6}}
assert.Equal(t, expected, result)
}
func TestChunk_Skip(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{1, 2, 3, 4, 5, 6})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) { return item, nil })
// Act
chunks.MoveNext()
// Assert
result, err := enumerators.Collect(chunks)
assert.Nil(t, err)
expected := [][]int{
{3}, // 3 (exactly hits target 5)
{4}, // 4 (less than target 5, starts new chunk)
{5}, // 5 (exactly hits target 5)
{6}, // 6 (new chunk)
}
assert.Equal(t, expected, result)
}
func TestChunk_NonUniformSizes(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{1, 2, 3, 4, 5})
chunks := enumerators.Chunk(source, 7, func(item int) (int, error) { return item * 2, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{
{1, 2}, // 1*2 + 2*2 = 6 (less than 7)
{3}, // 3*2 = 6 (new chunk)
{4}, // 4*2 = 8 (new chunk exceeds 7)
{5}, // 5*2 = 10 (new chunk exceeds 7)
}
assert.Equal(t, expected, result)
}
func TestChunk_ItemsEqualTarget(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{5, 5, 5})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{{5}, {5}, {5}}
assert.Equal(t, expected, result)
}
func TestChunk_TargetSmallerThanItems(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{10, 20, 30})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{{10}, {20}, {30}}
assert.Equal(t, expected, result)
}
func TestChunk_ItemsMatchMultipleTargets(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{1, 2, 2, 5, 5})
chunks := enumerators.Chunk(source, 10, func(item int) (int, error) { return item, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]int{
{1, 2, 2, 5}, // 1 + 2 + 2 + 5 = 10
{5}, // New chunk
}
assert.Equal(t, expected, result)
}
type Item struct {
Weight int
}
func TestChunk_CustomStruct(t *testing.T) {
// Arrange
source := enumerators.Slice([]Item{
{Weight: 3}, {Weight: 4}, {Weight: 2}, {Weight: 1},
})
chunks := enumerators.Chunk(source, 5, func(item Item) (int, error) { return item.Weight, nil })
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Nil(t, err)
expected := [][]Item{
{{Weight: 3}},
{{Weight: 4}},
{{Weight: 2}, {Weight: 1}},
}
assert.Equal(t, expected, result)
}
func TestChunk_BubbleError(t *testing.T) {
// Arrange
source := enumerators.Slice([]int{1, 2, 3, 4, 5})
chunks := enumerators.Chunk(source, 5, func(item int) (int, error) {
if item == 4 {
return 0, errors.New("bubble error")
}
return item, nil
})
// Act
result, err := enumerators.Collect(chunks)
// Assert
assert.Equal(t, "bubble error", err.Error())
expected := [][]int{
{1, 2},
{3},
}
assert.Equal(t, expected, result)
}