-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_test.go
67 lines (55 loc) · 1.28 KB
/
stream_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
package gg
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestBaseStream(t *testing.T) {
src := []int{1, 2, 3, 4, 5}
want := 5
got := Reduce(0, func(acc, it int) int {
return acc + it
}, Map(func(it int) int {
return it * it
}, Filter(func(it int) bool {
return it < 3
}, FromSlice(src))))
assert.Equal(t, want, got)
}
func TestStreamToSLice(t *testing.T) {
src := []int{1, 2, 3, 4, 5}
want := []int{9, 16, 25}
got := ToSlice(
Filter(func(it int) bool {
return it > 5
}, Map(func(it int) int {
return it * it
}, FromSlice(src))))
assert.Equal(t, want, got)
}
func TestStreamToMap(t *testing.T) {
src := []int{1, 2, 3, 4, 5}
want := map[string]int{"1": 1, "2": 2}
got := ToMap(
Map(func(it int) KeyValue[string, int] {
return KeyValue[string, int]{
Key: fmt.Sprintf("%v", it),
Value: it,
}
}, Filter(func(it int) bool {
return it < 3
}, FromSlice(src))))
assert.Equal(t, want, got)
}
func TestFromMapToMap(t *testing.T) {
src := map[string]int{"foo": 1, "bar": 2}
want := map[int]string{1: "foo", 2: "bar"}
got := ToMap(
Map(func(kv KeyValue[string, int]) KeyValue[int, string] {
return KeyValue[int, string]{
Key: kv.Value,
Value: kv.Key,
}
}, FromMap(src)))
assert.Equal(t, want, got)
}