-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk_test.go
146 lines (125 loc) · 2.95 KB
/
chunk_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package generics_test
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeroflucs-given/generics"
"testing"
)
func TestChunk(t *testing.T) {
items := []int{
1, 1, 2, 3, 5, 8, 13, 21,
}
type testDef struct {
Buckets int
Expected [][]int
}
tests := []testDef{
{
Buckets: 0,
Expected: nil,
},
{
Buckets: 1,
Expected: [][]int{{1, 1, 2, 3, 5, 8, 13, 21}},
},
{
Buckets: 2,
Expected: [][]int{{1, 1, 2, 3}, {5, 8, 13, 21}},
},
{
Buckets: 3,
Expected: [][]int{{1, 1, 2}, {3, 5, 8}, {13, 21}},
},
{
Buckets: 4,
Expected: [][]int{{1, 1}, {2, 3}, {5, 8}, {13, 21}},
},
{
Buckets: 5,
Expected: [][]int{{1, 1}, {2, 3}, {5, 8}, {13}, {21}},
},
{
Buckets: 6,
Expected: [][]int{{1, 1}, {2, 3}, {5}, {8}, {13}, {21}},
},
{
Buckets: 7,
Expected: [][]int{{1, 1}, {2}, {3}, {5}, {8}, {13}, {21}},
},
{
Buckets: 8,
Expected: [][]int{{1}, {1}, {2}, {3}, {5}, {8}, {13}, {21}},
},
{
Buckets: 9,
Expected: [][]int{{1}, {1}, {2}, {3}, {5}, {8}, {13}, {21}, {}},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%v", test.Buckets), func(t *testing.T) {
r := generics.Chunk(items, test.Buckets)
require.Equal(t, test.Expected, r)
})
}
}
var benchArray []int
func init() {
benchArray = make([]int, 1_000_000_000)
for i := range benchArray {
benchArray[i] = i
}
}
func BenchmarkChunk(b *testing.B) {
_ = generics.Chunk(benchArray, b.N)
b.StopTimer()
b.Logf("%v buckets: %v", b.N, b.Elapsed())
}
func TestChunkMap(t *testing.T) {
items := map[int]int{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
type testDef struct {
ExpectedCounts []int
}
tests := []testDef{
{ExpectedCounts: []int{}},
{ExpectedCounts: []int{8}},
{ExpectedCounts: []int{4, 4}},
{ExpectedCounts: []int{3, 3, 2}},
{ExpectedCounts: []int{2, 2, 2, 2}},
{ExpectedCounts: []int{2, 2, 2, 1, 1}},
{ExpectedCounts: []int{2, 2, 1, 1, 1, 1}},
{ExpectedCounts: []int{2, 1, 1, 1, 1, 1, 1}},
{ExpectedCounts: []int{1, 1, 1, 1, 1, 1, 1, 1}},
{ExpectedCounts: []int{1, 1, 1, 1, 1, 1, 1, 1, 0}},
}
// Map iteration order is non-deterministic, so combine them all and make sure
// we have the input.
for _, test := range tests {
t.Run(fmt.Sprintf("%v", len(test.ExpectedCounts)), func(t *testing.T) {
r := generics.ChunkMap(items, len(test.ExpectedCounts))
require.Len(t, r, len(test.ExpectedCounts))
comb := make(map[int]int, len(items))
for i, m := range r {
assert.Len(t, m, test.ExpectedCounts[i])
for k, v := range m {
comb[k] = v
}
}
if len(r) > 0 {
require.Equal(t, items, comb)
}
})
}
}
var chunkBenchMap map[int]int
func init() {
chunkBenchMap = make(map[int]int, 1_000_000_000)
for i := range chunkBenchMap {
chunkBenchMap[i] = i
}
}
func BenchmarkChunkMap(b *testing.B) {
_ = generics.ChunkMap(chunkBenchMap, b.N)
b.StopTimer()
b.Logf("%v buckets: %v", b.N, b.Elapsed())
}