-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_test.go
108 lines (87 loc) · 1.99 KB
/
transform_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
package giter
import (
"reflect"
"testing"
)
func TestMap(t *testing.T) {
xs := []int{1, 2, 3, 4, 5}
want := make([]int, 0, len(xs))
f := func(x int) int { return 2 * x }
for _, x := range xs {
want = append(want, f(x))
}
mapped := Map(f, Slice(xs))
defer mapped.Close()
out := []int{}
for x := range mapped.Each {
out = append(out, x)
}
if !reflect.DeepEqual(out, want) {
t.Errorf("TestMap: Map(2*x, xs) = %v, want = %v", out, want)
}
}
func TestFilter(t *testing.T) {
xs := []int{1, 2, 3, 4, 5}
want := make([]int, 0, len(xs))
f := func(x int) bool { return x%2 == 0 }
for _, x := range xs {
if f(x) {
want = append(want, x)
}
}
mapped := Filter(f, Slice(xs))
defer mapped.Close()
out := []int{}
for x := range mapped.Each {
out = append(out, x)
}
if !reflect.DeepEqual(out, want) {
t.Errorf("TestFilter: Map(!x%%2, xs) = %v, want = %v", out, want)
}
}
func TestFlatMap(t *testing.T) {
xs := []int{1, 2, 3, 4, 5}
want := make([]int, 0, len(xs))
f := func(x int) []int { return []int{x, x / 2} }
for _, x := range xs {
want = append(want, f(x)...)
}
out := ToSlice(FlatMap(f, Slice(xs)))
if !reflect.DeepEqual(out, want) {
t.Errorf("TestFlatMap: FlatMap(x -> [ x, x / 2 ], xs) = %v, want = %v", out, want)
}
}
func TestChunk(t *testing.T) {
xs := []int{1, 2, 3, 4, 5}
want := [][]int{
[]int{xs[0], xs[1]},
[]int{xs[2], xs[3]},
[]int{xs[4]},
}
out := ToSlice(Chunk(2, Slice(xs)))
if !reflect.DeepEqual(out, want) {
t.Errorf("TestChunk: Chunk(2, {1..=5}) = %v, want = %v", out, want)
}
}
func TestChunkedFlatMap(t *testing.T) {
xs := []int{1, 2, 3, 4, 5}
want := make([]int, 0, len(xs))
for _, x := range xs {
want = append(want, 2*x)
}
out := ToSlice(
ChunkedFlatMap(
2,
func(in, out []int) []int {
for _, x := range in {
out = append(out, 2*x)
}
return out
},
Slice(xs)))
if !reflect.DeepEqual(out, want) {
t.Errorf(
"TestChunkedFlatMap: ChunkedFlatMap(2, 2*x, {1..=5}) = %v, want = %v",
out, want)
}
}