-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunks_test.go
98 lines (69 loc) · 1.99 KB
/
chunks_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
package ranges
import "testing"
func TestChunks(t *testing.T) {
t.Parallel()
input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
chunks := Chunks[int](SliceRange(input), 2)
allValues := make([][]int, 0, len(input)/2)
for !chunks.Empty() {
c := chunks.Front()
cValues := make([]int, 0, 2)
for !c.Empty() {
cValues = append(cValues, c.Front())
c.PopFront()
}
allValues = append(allValues, cValues)
chunks.PopFront()
}
assertEqual(t, allValues, [][]int{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9}})
}
func TestChunksNotEmptyAfterChunkExhausted(t *testing.T) {
t.Parallel()
input := []int{1, 2, 3, 4}
chunks := Chunks[int](SliceRange(input), 2)
firstChunk := chunks.Front()
firstChunk.PopFront()
firstChunk.PopFront()
assertNotEmpty(t, chunks)
}
func TestChunksModifiedByReference(t *testing.T) {
t.Parallel()
input := []int{1, 2, 3, 4}
chunks := Chunks[int](SliceRange(input), 2)
sliceCopy := Slice(chunks.Front())
// The chunk should be correct
assertEqual(t, sliceCopy, []int{1, 2})
// The current chunk should be empty when we ask for it again.
assertEmpty(t, chunks.Front())
chunks.PopFront()
secondChunk := chunks.Front()
chunks.Front().PopFront()
// The previously loaded chunk reference should be advanced when we pop a new value.
assertHasFront(t, secondChunk, 4)
}
func TestChunkF(t *testing.T) {
t.Parallel()
input := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
chunks := ChunksF(SliceRange(input), 2)
allValues := make([][]int, 0, len(input)/2)
const saveAt = 2
i := 0
var savedChunk ForwardRange[ForwardRange[int]]
for !chunks.Empty() {
if i == saveAt {
savedChunk = chunks.Save()
}
c := chunks.Front()
cValues := make([]int, 0, 2)
for !c.Empty() {
cValues = append(cValues, c.Front())
c.PopFront()
}
allValues = append(allValues, cValues)
chunks.PopFront()
i++
}
assertEqual(t, allValues, [][]int{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9}})
savedResult := SliceF(MapF(savedChunk, SliceF[int]))
assertEqual(t, savedResult, [][]int{{5, 6}, {7, 8}, {9}})
}