@@ -2,6 +2,7 @@ package go2linq
2
2
3
3
import (
4
4
"fmt"
5
+ "iter"
5
6
"reflect"
6
7
"strings"
7
8
"testing"
@@ -11,7 +12,7 @@ import (
11
12
12
13
func TestAggregate_int (t * testing.T ) {
13
14
type args struct {
14
- source Enumerable [int ]
15
+ source iter. Seq [int ]
15
16
accumulator func (int , int ) int
16
17
}
17
18
tests := []struct {
@@ -31,15 +32,15 @@ func TestAggregate_int(t *testing.T) {
31
32
},
32
33
{name : "NullFuncUnseeded" ,
33
34
args : args {
34
- source : NewEnSlice (1 , 3 ),
35
+ source : VarAll (1 , 3 ),
35
36
accumulator : nil ,
36
37
},
37
38
wantErr : true ,
38
39
expectedErr : ErrNilAccumulator ,
39
40
},
40
41
{name : "UnseededAggregation" ,
41
42
args : args {
42
- source : NewEnSlice (1 , 4 , 5 ),
43
+ source : VarAll (1 , 4 , 5 ),
43
44
accumulator : func (ag , el int ) int { return ag * 2 + el },
44
45
},
45
46
want : 17 ,
@@ -54,14 +55,14 @@ func TestAggregate_int(t *testing.T) {
54
55
},
55
56
{name : "UnseededSingleElementAggregation" ,
56
57
args : args {
57
- source : NewEnSlice (1 ),
58
+ source : VarAll (1 ),
58
59
accumulator : func (ag , el int ) int { return ag * 2 + el },
59
60
},
60
61
want : 1 ,
61
62
},
62
63
{name : "FirstElementOfInputIsUsedAsSeedForUnseededOverload" ,
63
64
args : args {
64
- source : NewEnSlice (5 , 3 , 2 ),
65
+ source : VarAll (5 , 3 , 2 ),
65
66
accumulator : func (ag , el int ) int { return ag * el },
66
67
},
67
68
want : 30 ,
@@ -89,7 +90,7 @@ func TestAggregate_int(t *testing.T) {
89
90
90
91
func TestAggregateSeed_int_int (t * testing.T ) {
91
92
type args struct {
92
- source Enumerable [int ]
93
+ source iter. Seq [int ]
93
94
seed int
94
95
accumulator func (int , int ) int
95
96
}
@@ -111,7 +112,7 @@ func TestAggregateSeed_int_int(t *testing.T) {
111
112
},
112
113
{name : "NullFuncSeeded" ,
113
114
args : args {
114
- source : NewEnSlice (1 , 3 ),
115
+ source : VarAll (1 , 3 ),
115
116
seed : 5 ,
116
117
accumulator : nil ,
117
118
},
@@ -120,7 +121,7 @@ func TestAggregateSeed_int_int(t *testing.T) {
120
121
},
121
122
{name : "SeededAggregation" ,
122
123
args : args {
123
- source : NewEnSlice (1 , 4 , 5 ),
124
+ source : VarAll (1 , 4 , 5 ),
124
125
seed : 5 ,
125
126
accumulator : func (ac , el int ) int { return ac * 2 + el },
126
127
},
@@ -155,20 +156,21 @@ func TestAggregateSeed_int_int(t *testing.T) {
155
156
}
156
157
}
157
158
158
- func TestAggregateSeedMust_int32_int64 (t * testing.T ) {
159
+ func TestAggregateSeed_int32_int64 (t * testing.T ) {
159
160
type args struct {
160
- source Enumerable [int32 ]
161
+ source iter. Seq [int32 ]
161
162
seed int64
162
163
accumulator func (int64 , int32 ) int64
163
164
}
164
165
tests := []struct {
165
- name string
166
- args args
167
- want int64
166
+ name string
167
+ args args
168
+ want int64
169
+ wantErr bool
168
170
}{
169
171
{name : "DifferentSourceAndAccumulatorTypes" ,
170
172
args : args {
171
- source : NewEnSlice (int32 (2000000000 ), int32 (2000000000 ), int32 (2000000000 )),
173
+ source : VarAll (int32 (2000000000 ), int32 (2000000000 ), int32 (2000000000 )),
172
174
seed : int64 (0 ),
173
175
accumulator : func (ac int64 , el int32 ) int64 { return ac + int64 (el ) },
174
176
},
@@ -177,17 +179,21 @@ func TestAggregateSeedMust_int32_int64(t *testing.T) {
177
179
}
178
180
for _ , tt := range tests {
179
181
t .Run (tt .name , func (t * testing.T ) {
180
- got := AggregateSeedMust (tt .args .source , tt .args .seed , tt .args .accumulator )
182
+ got , err := AggregateSeed (tt .args .source , tt .args .seed , tt .args .accumulator )
183
+ if (err != nil ) != tt .wantErr {
184
+ t .Errorf ("AggregateSeed() error = %v, wantErr %v" , err , tt .wantErr )
185
+ return
186
+ }
181
187
if ! reflect .DeepEqual (got , tt .want ) {
182
- t .Errorf ("AggregateSeedMust () = %v, want %v" , got , tt .want )
188
+ t .Errorf ("AggregateSeed () = %v, want %v" , got , tt .want )
183
189
}
184
190
})
185
191
}
186
192
}
187
193
188
194
func TestAggregateSeedSel_int_int_string (t * testing.T ) {
189
195
type args struct {
190
- source Enumerable [int ]
196
+ source iter. Seq [int ]
191
197
seed int
192
198
accumulator func (int , int ) int
193
199
resultSelector func (int ) string
@@ -211,7 +217,7 @@ func TestAggregateSeedSel_int_int_string(t *testing.T) {
211
217
},
212
218
{name : "NullFuncSeededWithResultSelector" ,
213
219
args : args {
214
- source : NewEnSlice (1 , 3 ),
220
+ source : VarAll (1 , 3 ),
215
221
seed : 5 ,
216
222
accumulator : nil ,
217
223
resultSelector : func (r int ) string { return fmt .Sprint (r ) },
@@ -221,7 +227,7 @@ func TestAggregateSeedSel_int_int_string(t *testing.T) {
221
227
},
222
228
{name : "NullProjectionSeededWithResultSelector" ,
223
229
args : args {
224
- source : NewEnSlice (1 , 3 ),
230
+ source : VarAll (1 , 3 ),
225
231
seed : 5 ,
226
232
accumulator : func (ac , el int ) int { return ac + el },
227
233
resultSelector : nil ,
@@ -231,7 +237,7 @@ func TestAggregateSeedSel_int_int_string(t *testing.T) {
231
237
},
232
238
{name : "SeededAggregationWithResultSelector" ,
233
239
args : args {
234
- source : NewEnSlice (1 , 4 , 5 ),
240
+ source : VarAll (1 , 4 , 5 ),
235
241
seed : 5 ,
236
242
accumulator : func (ac , el int ) int { return ac * 2 + el },
237
243
resultSelector : func (r int ) string { return fmt .Sprint (r ) },
@@ -268,29 +274,29 @@ func TestAggregateSeedSel_int_int_string(t *testing.T) {
268
274
}
269
275
}
270
276
271
- // see the last example from Enumerable.Aggregate help
277
+ // last example from
272
278
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.aggregate
273
- func ExampleAggregateMust () {
279
+ func ExampleAggregate () {
274
280
sentence := "the quick brown fox jumps over the lazy dog"
275
281
// Split the string into individual words.
276
282
words := strings .Fields (sentence )
277
283
// Prepend each word to the beginning of the new sentence to reverse the word order.
278
- reversed := AggregateMust (
279
- NewEnSlice (words ... ),
284
+ reversed , _ := Aggregate (
285
+ SliceAll (words ),
280
286
func (workingSentence , next string ) string { return next + " " + workingSentence },
281
287
)
282
288
fmt .Println (reversed )
283
289
// Output:
284
290
// dog lazy the over jumps fox brown quick the
285
291
}
286
292
287
- // see the second example from Enumerable.Aggregate help
293
+ // second example from
288
294
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.aggregate
289
- func ExampleAggregateSeedMust () {
295
+ func ExampleAggregateSeed () {
290
296
ints := []int {4 , 8 , 8 , 3 , 9 , 0 , 7 , 8 , 2 }
291
297
// Count the even numbers in the array, using a seed value of 0.
292
- numEven := AggregateSeedMust (
293
- NewEnSlice (ints ... ),
298
+ numEven , _ := AggregateSeed (
299
+ SliceAll (ints ),
294
300
0 ,
295
301
func (total , next int ) int {
296
302
if next % 2 == 0 {
@@ -304,13 +310,13 @@ func ExampleAggregateSeedMust() {
304
310
// The number of even integers is: 6
305
311
}
306
312
307
- // see the first example from Enumerable.Aggregate help
313
+ // first example from
308
314
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.aggregate
309
- func ExampleAggregateSeedSelMust () {
315
+ func ExampleAggregateSeedSel () {
310
316
fruits := []string {"apple" , "mango" , "orange" , "passionfruit" , "grape" }
311
317
// Determine whether any string in the array is longer than "banana".
312
- longestName := AggregateSeedSelMust (
313
- NewEnSlice (fruits ... ),
318
+ longestName , _ := AggregateSeedSel (
319
+ SliceAll (fruits ),
314
320
"banana" ,
315
321
func (longest , next string ) string {
316
322
if len (next ) > len (longest ) {
0 commit comments