-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaggregate_test.go
44 lines (37 loc) · 1003 Bytes
/
aggregate_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
package collection_test
import (
"fmt"
"testing"
"github.com/sergeydobrodey/collection"
)
func TestAggregate(t *testing.T) {
cases := []struct {
name string
source []int
aggregator func(int, int) int
want int
}{
{name: "sum", source: []int{1, 2, 3, 4, 5}, aggregator: func(s int, v int) int { return s + v }, want: 15},
{name: "sum even", source: []int{1, 2, 3, 4, 5}, aggregator: func(s int, v int) int {
if v%2 != 0 {
return s
}
return s + v
}, want: 6},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := collection.Aggregate(tc.source, tc.aggregator)
if got != tc.want {
t.Errorf("Aggregate(%v) = %v; want %v", tc.source, got, tc.want)
}
})
}
}
// ExampleAggregate: Example function demonstrating the use of the Aggregate function.
func ExampleAggregate() {
sum := func(s int, v int) int { return s + v }
result := collection.Aggregate([]int{1, 2, 3, 4, 5}, sum)
fmt.Println(result)
// Output: 15
}