-
Notifications
You must be signed in to change notification settings - Fork 35
/
reduce_test.go
59 lines (53 loc) · 1.46 KB
/
reduce_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
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bigslice_test
import (
"fmt"
"testing"
"github.com/grailbio/bigslice"
"github.com/grailbio/bigslice/slicetest"
)
func TestReduce(t *testing.T) {
const N = 100
ints := make([]int, N)
for i := range ints {
ints[i] = i
}
for m := 1; m < 5; m++ {
slice := bigslice.Const(m, ints)
slice = bigslice.Map(slice, func(x int) (string, int) {
return fmt.Sprint(x%3) + "x", x
})
slice = bigslice.Reduce(slice, func(x, y int) int { return x + y })
assertEqual(t, slice, true, []string{"0x", "1x", "2x"}, []int{1683, 1617, 1650})
}
}
func TestReducePrefix(t *testing.T) {
const N = 100
ints := make([]int, N)
for i := range ints {
ints[i] = i
}
for m := 1; m < 5; m++ {
slice := bigslice.Const(m, ints)
slice = bigslice.Map(slice, func(x int) (string, int, int) {
return "x", x % 3, x
})
slice = bigslice.Prefixed(slice, 2)
slice = bigslice.Reduce(slice, func(x, y int) int { return x + y })
assertEqual(t, slice, true, []string{"x", "x", "x"}, []int{0, 1, 2}, []int{1683, 1617, 1650})
}
}
func ExampleReduce() {
slice := bigslice.Const(2,
[]string{"c", "a", "b", "c", "c", "b", "a", "a", "a", "a", "c"},
[]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
)
slice = bigslice.Reduce(slice, func(a, b int) int { return a + b })
slicetest.Print(slice)
// Output:
// a 5
// b 2
// c 4
}