-
Notifications
You must be signed in to change notification settings - Fork 32
/
aggregate_test.go
53 lines (48 loc) · 948 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
45
46
47
48
49
50
51
52
53
package underscore
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Benchmark_Aggregate(b *testing.B) {
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
total := 0
Range(1, 100, 1).Aggregate(
func(memo []int, r, _ int) []int {
memo = append(memo, r)
memo = append(memo, -r)
return memo
},
make([]int, 0),
).Value(&total)
}
})
b.Run("no value", func(b *testing.B) {
for n := 0; n < b.N; n++ {
Range(1, 100, 1).Aggregate(
func(memo []int, r, _ int) []int {
memo = append(memo, r)
memo = append(memo, -r)
return memo
},
make([]int, 0),
)
}
})
}
func Test_Aggregate(t *testing.T) {
var res []int
Chain([]int{1, 2}).Aggregate(
func(memo []int, n, _ int) []int {
memo = append(memo, n)
memo = append(memo, n+10)
return memo
},
make([]int, 0),
).Value(&res)
assert.EqualValues(
t,
res,
[]int{1, 11, 2, 12},
)
}