-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpartition_test.go
More file actions
60 lines (47 loc) · 1.36 KB
/
partition_test.go
File metadata and controls
60 lines (47 loc) · 1.36 KB
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
package underscore_test
import (
"testing"
"github.com/stretchr/testify/assert"
u "github.com/rjNemo/underscore"
)
func TestPartition(t *testing.T) {
nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
isEven := func(n int) bool { return n%2 == 0 }
wantEvens := []int{0, 2, 4, 6, 8}
wantOdds := []int{1, 3, 5, 7, 9}
evens, odds := u.Partition(nums, isEven)
assert.Equal(t, wantEvens, evens)
assert.Equal(t, wantOdds, odds)
}
func TestPartitionEmpty(t *testing.T) {
keep, reject := u.Partition([]int{}, func(n int) bool { return n > 0 })
assert.Empty(t, keep)
assert.Empty(t, reject)
}
func TestPartitionSingleElement(t *testing.T) {
keep, reject := u.Partition([]int{5}, func(n int) bool { return n > 3 })
assert.Equal(t, []int{5}, keep)
assert.Empty(t, reject)
}
func TestPartitionAllPass(t *testing.T) {
nums := []int{2, 4, 6, 8}
keep, reject := u.Partition(nums, func(n int) bool { return n%2 == 0 })
assert.Equal(t, nums, keep)
assert.Empty(t, reject)
}
func TestPartitionAllReject(t *testing.T) {
nums := []int{1, 3, 5, 7}
keep, reject := u.Partition(nums, func(n int) bool { return n%2 == 0 })
assert.Empty(t, keep)
assert.Equal(t, nums, reject)
}
func BenchmarkPartition(b *testing.B) {
data := make([]int, 1000)
for i := range data {
data[i] = i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
u.Partition(data, func(n int) bool { return n%2 == 0 })
}
}