-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
filter_test.go
116 lines (98 loc) · 2.83 KB
/
filter_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
package gogu
import (
"fmt"
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilter(t *testing.T) {
assert := assert.New(t)
// Obtain all the values which satisfies the callback function condition.
items1 := []int{1, 2, 3, 4, 5, 10, 20, 30, 40, 50}
assert.Equal([]int{10, 20, 30, 40, 50}, Filter(items1, func(val int) bool {
return val >= 10
}))
items2 := []float64{12.2, 22.1, 10.01, 1, 20, 50}
assert.Equal([]float64{1}, Filter(items2, func(val float64) bool {
return val == 1.0
}))
items3 := []float64{2.0, 4.0, 6.0}
assert.Equal([]float64{4.0, 6.0}, Filter(items3, func(val float64) bool {
return math.Sqrt(val) >= 2
}))
// Reject all the values which doesn't satisfies the callback function condition.
items4 := []int{1, 2, 3, 4, 5, 10, 20, 30, 40, 50}
assert.Equal([]int{10, 20, 30, 40, 50}, Reject(items4, func(val int) bool {
return val < 10
}))
assert.NotEmpty(Filter(items4, func(val int) bool {
return val%2 == 0
}))
// Map filter test cases.
items5 := map[int]string{1: "John", 2: "Doe", 3: "Fred"}
assert.Equal(map[int]string{1: "John"}, FilterMap(items5, func(v string) bool {
return v == "John"
}))
assert.Len(FilterMap(items5, func(v string) bool {
return v == "Bernie"
}), 0)
input1 := []map[string]int{
{"bernie": 22},
{"robert": 30},
}
expected1 := []map[string]int{
{"robert": 30},
}
assert.Equal(expected1, FilterMapCollection(input1, func(val int) bool {
return val > 22
}))
assert.Len(FilterMapCollection(input1, func(val int) bool {
return val == 30
}), 1)
input2 := []map[string]map[string]int{
{"bernie": {"age": 30, "ranking": 1}},
{"robert": {"age": 20, "ranking": 5}},
}
expected := []map[string]map[string]int{
{"bernie": {"age": 30, "ranking": 1}},
}
assert.Equal(expected, Filter2DMapCollection(input2, func(v map[string]int) bool {
return v["age"] > 20 && v["ranking"] < 5
}))
assert.Len(Filter2DMapCollection(input2, func(v map[string]int) bool {
return v["age"] > 20 && v["ranking"] < 5
}), 1)
assert.NotEqual(expected, Filter2DMapCollection(input2, func(v map[string]int) bool {
return v["age"] > 20 && v["ranking"] > 1
}))
}
func ExampleFilter() {
items1 := []int{1, 2, 3, 4, 5, 10, 20, 30, 40, 50}
res := Filter(items1, func(val int) bool {
return val >= 10
})
fmt.Println(res)
items2 := []int{1, 2, 3, 4, 5, 6, 10, 20, 30, 40, 50}
res = Reject(items2, func(val int) bool {
return val >= 10
})
fmt.Println(res)
items3 := map[int]string{1: "John", 2: "Doe", 3: "Fred"}
resMap := FilterMap(items3, func(v string) bool {
return v == "John"
})
fmt.Println(resMap)
input1 := []map[string]int{
{"bernie": 22},
{"robert": 30},
}
resMapCol := FilterMapCollection(input1, func(val int) bool {
return val > 22
})
fmt.Println(resMapCol)
// Output:
// [10 20 30 40 50]
// [1 2 3 4 5 6]
// map[1:John]
// [map[robert:30]]
}