-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsumaverage_test.go
431 lines (414 loc) · 9.67 KB
/
sumaverage_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
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
package go2linq
import (
"errors"
"fmt"
"iter"
"math"
"reflect"
"slices"
"strconv"
"testing"
"github.com/solsw/iterhelper"
)
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/SumTest.cs
// https://github.com/jskeet/edulinq/blob/master/src/Edulinq.Tests/AverageTest.cs
func TestSum(t *testing.T) {
type args struct {
source iter.Seq[float64]
}
tests := []struct {
name string
args args
want bool
}{
{name: "OverflowToNegInfinityFloat64",
args: args{
source: iterhelper.VarSeq(-math.MaxFloat64, -math.MaxFloat64),
},
want: true,
},
{name: "OverflowToInfinityFloat64",
args: args{
source: iterhelper.VarSeq(math.MaxFloat64, math.MaxFloat64),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Sum(tt.args.source)
var want bool
switch tt.name {
case "OverflowToNegInfinityFloat64":
want = math.IsInf(got, -1)
case "OverflowToInfinityFloat64":
want = math.IsInf(got, +1)
}
if want != tt.want {
t.Errorf("IsInf(Sum()) = %v, want %v", want, tt.want)
}
})
}
}
func TestSumSel_string_int(t *testing.T) {
type args struct {
source iter.Seq[string]
selector func(string) int
}
tests := []struct {
name string
args args
want int
}{
{name: "EmptySequenceIntWithSelector",
args: args{
source: Empty[string](),
selector: func(s string) int { return len(s) },
},
want: 0,
},
{name: "SimpleSumIntWithSelector",
args: args{
source: iterhelper.VarSeq("x", "abc", "de"),
selector: func(s string) int { return len(s) },
},
want: 6,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := SumSel(tt.args.source, tt.args.selector)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SumSel() = %v, want %v", got, tt.want)
}
})
}
}
func TestSumSel_string_float64(t *testing.T) {
type args struct {
source iter.Seq[string]
selector func(string) float64
}
tests := []struct {
name string
args args
want float64
}{
{name: "EmptySequenceFloat64WithSelector",
args: args{
source: Empty[string](),
selector: func(s string) float64 { return float64(len(s)) },
},
want: 0,
},
{name: "SimpleSumFloat64WithSelector",
args: args{
source: iterhelper.VarSeq("x", "abc", "de"),
selector: func(s string) float64 { return float64(len(s)) },
},
want: 6,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := SumSel(tt.args.source, tt.args.selector)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("SumSel() = %v, want %v", got, tt.want)
}
})
}
}
func TestSumSel_string_float64IsNaN(t *testing.T) {
type args struct {
source iter.Seq[string]
selector func(string) float64
}
tests := []struct {
name string
args args
want bool
}{
{name: "SimpleSumFloat64WithSelectorWithNan",
args: args{
source: iterhelper.VarSeq("x", "abc", "de"),
selector: func(s string) float64 {
l := len(s)
if l == 3 {
return math.NaN()
}
return float64(l)
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sumSel, _ := SumSel(tt.args.source, tt.args.selector)
want := math.IsNaN(sumSel)
if want != tt.want {
t.Errorf("IsNaN(SumSel()) = %v, want %v", want, tt.want)
}
})
}
}
func TestSumSel_string_float64IsInf(t *testing.T) {
type args struct {
source iter.Seq[string]
selector func(string) float64
}
tests := []struct {
name string
args args
want bool
}{
{name: "OverflowToInfinityFloat64WithSelector",
args: args{
source: iterhelper.VarSeq("x", "y"),
selector: func(string) float64 { return math.MaxFloat64 },
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := SumSel(tt.args.source, tt.args.selector)
want := math.IsInf(got, +1)
if want != tt.want {
t.Errorf("IsInf(SumSel()) = %v, want %v", want, tt.want)
}
})
}
}
func TestAverage_int(t *testing.T) {
type args struct {
source iter.Seq[int]
}
tests := []struct {
name string
args args
want float64
wantErr bool
expectedErr error
}{
{name: "EmptySequenceIntNoSelector",
args: args{
source: Empty[int](),
},
wantErr: true,
expectedErr: ErrEmptySource,
},
{name: "SimpleAverageInt",
args: args{
source: iterhelper.VarSeq(5, 10, 0, 15),
},
want: 7.5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Average(tt.args.source)
if (err != nil) != tt.wantErr {
t.Errorf("Average() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("Average() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
if got != tt.want {
t.Errorf("Average() = %v, want %v", got, tt.want)
}
})
}
}
func TestAverage_float64IsInf(t *testing.T) {
type args struct {
source iter.Seq[float64]
}
tests := []struct {
name string
args args
want bool
}{
{name: "Float64OverflowsToInfinity",
args: args{
source: iterhelper.VarSeq(math.MaxFloat64, math.MaxFloat64, -math.MaxFloat64, -math.MaxFloat64),
},
want: true,
},
{name: "Float64OverflowsToNegInfinity",
args: args{
source: iterhelper.VarSeq(-math.MaxFloat64, -math.MaxFloat64, math.MaxFloat64, math.MaxFloat64),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := Average(tt.args.source)
var want bool
switch tt.name {
case "Float64OverflowsToInfinity":
want = math.IsInf(got, +1)
case "Float64OverflowsToNegInfinity":
want = math.IsInf(got, -1)
}
if want != tt.want {
t.Errorf("IsInf(Average()) = %v, want %v", want, tt.want)
}
})
}
}
func TestAverageSel_string_int(t *testing.T) {
type args struct {
source iter.Seq[string]
selector func(string) int
}
tests := []struct {
name string
args args
want float64
wantErr bool
expectedErr error
}{
{name: "SourceStrNilSelector",
args: args{
source: iterhelper.VarSeq("one", "two", "three", "four"),
},
wantErr: true,
expectedErr: ErrNilSelector,
},
{name: "EmptySequenceIntWithSelector",
args: args{
source: Empty[string](),
selector: func(s string) int { return len(s) },
},
wantErr: true,
expectedErr: ErrEmptySource,
},
{name: "SimpleAverageIntWithSelector",
args: args{
source: iterhelper.VarSeq("", "abcd", "a", "b"),
selector: func(s string) int { return len(s) },
},
want: 1.5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := AverageSel(tt.args.source, tt.args.selector)
if (err != nil) != tt.wantErr {
t.Errorf("AverageSel() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
if !errors.Is(err, tt.expectedErr) {
t.Errorf("AverageSel() error = %v, expectedErr %v", err, tt.expectedErr)
}
return
}
if got != tt.want {
t.Errorf("AverageSel() = %v, want %v", got, tt.want)
}
})
}
}
func TestAverageSel_string_float64IsNaN(t *testing.T) {
type args struct {
source iter.Seq[string]
selector func(string) float64
}
tests := []struct {
name string
args args
want bool
}{
{name: "SequenceContainingNan",
args: args{
source: iterhelper.VarSeq("x", "abc", "de"),
selector: func(s string) float64 {
l := len(s)
if l == 3 {
return math.NaN()
}
return float64(l)
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := AverageSel(tt.args.source, tt.args.selector)
want := math.IsNaN(got)
if want != tt.want {
t.Errorf("IsNaN(AverageSel()) = %v, want %v", want, tt.want)
}
})
}
}
// example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.sum
func ExampleSum() {
numbers := []float64{43.68, 1.25, 583.7, 6.5}
sum, _ := Sum(slices.Values(numbers))
fmt.Printf("The sum of the numbers is %g.\n", sum)
// Output:
// The sum of the numbers is 635.13.
}
// SumEx1 example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.sum
func ExampleSumSel() {
packages := []Package{
{Company: "Coho Vineyard", Weight: 25.2},
{Company: "Lucerne Publishing", Weight: 18.7},
{Company: "Wingtip Toys", Weight: 6.0},
{Company: "Adventure Works", Weight: 33.8},
}
totalWeight, _ := SumSel(
slices.Values(packages),
func(pkg Package) float64 { return pkg.Weight },
)
fmt.Printf("The total weight of the packages is: %.1f\n", totalWeight)
// Output:
// The total weight of the packages is: 83.7
}
// example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.average
func ExampleAverage_ex1() {
grades := []int{78, 92, 100, 37, 81}
average, _ := Average(slices.Values(grades))
fmt.Printf("The average grade is %g.\n", average)
// Output:
// The average grade is 77.6.
}
// example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.average
func ExampleAverage_ex2() {
numbers := []string{"10007", "37", "299846234235"}
average, _ := AverageSel(
slices.Values(numbers),
func(e string) int {
r, _ := strconv.Atoi(e)
return r
},
)
fmt.Printf("The average is %.f.\n", average)
// Output:
// The average is 99948748093.
}
// example from
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.average
func ExampleAverageSel() {
fruits := []string{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
average, _ := AverageSel(
slices.Values(fruits),
func(e string) int { return len(e) },
)
fmt.Printf("The average string length is %g.\n", average)
// Output:
// The average string length is 6.5.
}