This repository has been 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
/
ordered_iter_test.go
142 lines (119 loc) · 2.47 KB
/
ordered_iter_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package iter
import (
"testing"
"github.com/stretchr/testify/assert"
"mtoohey.com/iter/v2/testutils"
)
func FuzzMin(f *testing.F) {
f.Add([]byte{2, 9, 1, 5, 0})
f.Add([]byte{})
f.Add([]byte{9, 4, 1})
f.Add([]byte{30})
f.Fuzz(func(t *testing.T, b []byte) {
foundMin := false
var min byte
for _, v := range b {
if !foundMin || v < min {
foundMin = true
min = v
}
}
actual, ok := Min(Elems(b))
if assert.Equal(t, foundMin, ok) && foundMin {
assert.Equal(t, min, actual)
}
})
}
func BenchmarkMin(b *testing.B) {
Min(Ints[int]().Take(uint(b.N)))
}
func FuzzMinByKey(f *testing.F) {
f.Add([]byte{2, 9, 1, 5, 0})
f.Add([]byte{})
f.Add([]byte{9, 4, 1})
f.Add([]byte{30})
f.Fuzz(func(t *testing.T, b []byte) {
foundMax := false
var max byte
for _, v := range b {
if !foundMax || v > max {
foundMax = true
max = v
}
}
actual, ok := MinByKey(Elems(b), func(v byte) int {
return -int(v)
})
if assert.Equal(t, foundMax, ok) && foundMax {
assert.Equal(t, max, actual)
}
})
}
func BenchmarkMinByKey(b *testing.B) {
MinByKey(Ints[int]().Take(uint(b.N)), func(n int) int {
return n
})
}
func FuzzMax(f *testing.F) {
f.Add([]byte{2, 9, 1, 5, 0})
f.Add([]byte{})
f.Add([]byte{9, 4, 1})
f.Add([]byte{30})
f.Fuzz(func(t *testing.T, b []byte) {
foundMax := false
var max byte
for _, v := range b {
if !foundMax || v > max {
foundMax = true
max = v
}
}
actual, ok := Max(Elems(b))
if assert.Equal(t, foundMax, ok) && foundMax {
assert.Equal(t, max, actual)
}
})
}
func BenchmarkMax(b *testing.B) {
Max(Ints[int]().Take(uint(b.N)))
}
func FuzzMaxByKey(f *testing.F) {
f.Add([]byte{2, 9, 1, 5, 0})
f.Add([]byte{})
f.Add([]byte{9, 4, 1})
f.Add([]byte{30})
f.Fuzz(func(t *testing.T, b []byte) {
foundMin := false
var min byte
for _, v := range b {
if !foundMin || v < min {
foundMin = true
min = v
}
}
actual, ok := MaxByKey(Elems(b), func(v byte) int {
return -int(v)
})
if assert.Equal(t, foundMin, ok) && foundMin {
assert.Equal(t, min, actual)
}
})
}
func BenchmarkMaxByKey(b *testing.B) {
MaxByKey(Ints[int]().Take(uint(b.N)), func(n int) int {
return n
})
}
func FuzzSum(f *testing.F) {
testutils.AddUints(f)
f.Fuzz(func(t *testing.T, n uint) {
expected := uint(0)
for i := uint(0); i < n; i++ {
expected += i
}
assert.Equal(t, expected, Sum(Ints[uint]().Take(n)))
})
}
func BenchmarkSum(b *testing.B) {
Sum(Ints[int]().Take(uint(b.N)))
}