-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.go
68 lines (58 loc) · 1.41 KB
/
core_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
package chains
import (
"slices"
"testing"
)
func TestGroupBy(t *testing.T) {
intSeq := Each([]int{100, 101, 200, 202, 203, 225, 201, 300, 303, 399, 199})
keys := []int{100, 200, 300, 100}
vals := [][]int{{100, 101}, {200, 202, 203, 225, 201}, {300, 303, 399}, {199}}
index := 0
for key, items := range GroupBy(func(i int) int { return i - (i % 100) }, intSeq) {
if key != keys[index] {
t.Fatalf("Items not equal: %v != %v", key, keys[index])
}
computedVals := []int{}
for item := range items {
computedVals = append(computedVals, item)
}
if !All(
Map2(
ZipLongest(
Each(computedVals),
Each(vals[index]),
-1,
-1,
),
func(a, b int) bool {
return a == b
},
),
func(f bool) bool {
return f
},
) {
t.Fatalf("Values not equal: %v != %v", computedVals, vals[index])
}
index += 1
}
}
func TestWindows(t *testing.T) {
items := []int{1, 2, 3, 4}
expectedSlidingWindows := [][]int{{1, 2, 3}, {2, 3, 4}}
expectedWindows := [][]int{{1, 2, 3}, {4}}
index := 0
for window := range SlidingWindows(Each(items), 3) {
if !slices.Equal(window, expectedSlidingWindows[index]) {
t.Fatalf("%v != %v", window, expectedSlidingWindows[index])
}
index += 1
}
index = 0
for window := range Windows(Each(items), 3) {
if !slices.Equal(window, expectedWindows[index]) {
t.Fatalf("%v != %v", window, expectedWindows[index])
}
index += 1
}
}