-
Notifications
You must be signed in to change notification settings - Fork 2
/
gopart_test.go
52 lines (46 loc) · 1.35 KB
/
gopart_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
package gopart
import (
"reflect"
"testing"
)
func TestPartition(t *testing.T) {
var partitionTests = []struct {
collectionLen int
partitionSize int
expRanges []IdxRange
}{
// evenly split
{9, 3, []IdxRange{{0, 3}, {3, 6}, {6, 9}}},
// uneven partition
{13, 5, []IdxRange{{0, 5}, {5, 10}, {10, 13}}},
// large partition size
{13, 19, []IdxRange{{0, 13}}},
// nonpositive partiition size
{7, 0, nil},
// same size
{19, 19, []IdxRange{{0, 19}}},
// zero collection length
{0, 19, nil},
}
for _, tt := range partitionTests {
actChannel := Partition(tt.collectionLen, tt.partitionSize)
var actRange []IdxRange
for idxRange := range actChannel {
actRange = append(actRange, idxRange)
}
if !reflect.DeepEqual(actRange, tt.expRanges) {
t.Errorf("expected %d, actual %d", actRange, tt.expRanges)
}
}
}
func Benchmark100kPartitions(b *testing.B) { benchmarkParition(17*1e5+11, b) }
func Benchmark10kPartitions(b *testing.B) { benchmarkParition(17*1e4+11, b) }
func Benchmark1kPartitions(b *testing.B) { benchmarkParition(17*1e3+11, b) }
func Benchmark100Partitions(b *testing.B) { benchmarkParition(17*1e2+11, b) }
func Benchmark10Partitions(b *testing.B) { benchmarkParition(17*1e1+11, b) }
func benchmarkParition(length int, b *testing.B) {
for n := 0; n < b.N; n++ {
for _ = range Partition(length, 17) {
}
}
}