This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter_test.go
76 lines (60 loc) · 1.4 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
package iter
import (
"testing"
"github.com/stretchr/testify/assert"
"mtoohey.com/iter/v2/testutils"
)
func FuzzIter_Filter(f *testing.F) {
testutils.AddByteSlices(f)
f.Fuzz(func(t *testing.T, b []byte) {
expected := []byte{}
for _, v := range b {
if v%2 == 0 {
expected = append(expected, v)
}
}
assert.Equal(t, expected, Elems(b).Filter(func(v byte) bool {
return v%2 == 0
}).Collect())
})
}
func BenchmarkIter_Filter(b *testing.B) {
Ints[int]().Filter(func(i int) bool {
return i%2 == 0
}).Take(uint(b.N)).Consume()
}
func FuzzFilterMap(f *testing.F) {
testutils.AddByteSlices(f)
f.Fuzz(func(t *testing.T, b []byte) {
expected := []byte{}
for _, v := range b {
if v%2 == 0 {
expected = append(expected, v*2)
}
}
predicate := func(v byte) (byte, error) {
if v%2 != 0 {
return 0, assert.AnError
}
return v * 2, nil
}
assert.Equal(t, expected, Elems(b).FilterMap(predicate).Collect())
assert.Equal(t, expected, FilterMap(Elems(b), predicate).Collect())
})
}
func BenchmarkIter_FilterMap(b *testing.B) {
Ints[int]().FilterMap(func(i int) (int, error) {
if i%2 == 0 {
return i * 2, nil
}
return 0, assert.AnError
}).Take(uint(b.N)).Consume()
}
func BenchmarkFilterMap(b *testing.B) {
FilterMap(Ints[int](), func(i int) (int, error) {
if i%2 == 0 {
return i * 2, nil
}
return 0, assert.AnError
}).Take(uint(b.N)).Consume()
}