-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunkby_test.go
91 lines (70 loc) · 1.57 KB
/
chunkby_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
package ranges
import "testing"
func TestChunkBy(t *testing.T) {
t.Parallel()
result := Slice(
Map(
ChunkBy(
Only(1, 2, 3, 3, 4, 5, 6, 6, 7, 8),
func(a, b int) bool { return a == b },
),
Slice[int],
),
)
assertEqual(t, result, [][]int{{1}, {2}, {3, 3}, {4}, {5}, {6, 6}, {7}, {8}})
result2 := Slice(
Map(
ChunkBy(
Only(1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 8, 9, 9),
func(a, b int) bool { return a < b },
),
Slice[int],
),
)
assertEqual(t, result2, [][]int{{1, 2, 3}, {3, 4, 5, 6}, {6, 7, 8}, {8, 9}, {9}})
}
func TestChunkByRepeatedFront(t *testing.T) {
t.Parallel()
result := ChunkBy(
Only(1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 8, 9, 9),
func(a, b int) bool { return a < b },
)
assertHasFront(t, result.Front(), 1)
assertHasFront(t, result.Front(), 1)
result.PopFront()
assertHasFront(t, result.Front(), 3)
assertHasFront(t, result.Front(), 3)
result.PopFront()
assertHasFront(t, result.Front(), 6)
assertHasFront(t, result.Front(), 6)
result.PopFront()
result.PopFront()
assertHasFront(t, result.Front(), 9)
result.PopFront()
assertEmpty(t, result)
}
type chunkByValueTestStruct struct {
x int
}
func TestChunkByValue(t *testing.T) {
t.Parallel()
result := Slice(
Map(
ChunkByValue(
SliceRange([]chunkByValueTestStruct{{1}, {2}, {3}, {3}, {4}, {5}, {6}, {6}, {7}, {8}}),
func(a chunkByValueTestStruct) int { return a.x },
),
Slice[chunkByValueTestStruct],
),
)
assertEqual(t, result, [][]chunkByValueTestStruct{
{{1}},
{{2}},
{{3}, {3}},
{{4}},
{{5}},
{{6}, {6}},
{{7}},
{{8}},
})
}