(filter): add integer for filter test

This commit is contained in:
bfu4 2021-09-06 14:53:28 -04:00
parent 93af011977
commit 99026bd939
No known key found for this signature in database
GPG Key ID: FD1D952871D22043

View File

@ -24,6 +24,22 @@ func TestFilter(t *testing.T) {
}
}
func TestIntFilter(t *testing.T) {
intMap := gomap.New(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
values := []int{2, 4, 6, 8, 10}
mappable := intMap.Mappable()
filtered := mappable.Filter(func(i interface{}) bool {
if value, ok := i.(int); ok {
// Filter even numbers.
return value%2 == 0
}
return false
})
if !reflect.DeepEqual(mapToInt(filtered.Items()), values) {
t.Fail()
}
}
// Benchmark the filtering function.
func BenchmarkFilter(b *testing.B) {
stringMap := gomap.New("test", "test1", "test2", "not_applicable", "test3")
@ -47,8 +63,16 @@ func BenchmarkFilter(b *testing.B) {
// Map an interface array to a string.
func mapToString(i []interface{}) []string {
var strArr []string
for _, v := range i {
for _, v := range i {
strArr = append(strArr, v.(string))
}
return strArr
}
}
func mapToInt(i []interface{}) []int {
var ia []int
for _, v := range i {
ia = append(ia, v.(int))
}
return ia
}