-
Notifications
You must be signed in to change notification settings - Fork 1
/
iter_test.go
88 lines (73 loc) · 1.4 KB
/
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
package ef
import (
"fmt"
"math/rand"
"testing"
)
func TestIterator(t *testing.T) {
for i, tc := range testCases {
d, err := From(tc.in)
if err != nil {
t.Error(err)
}
it := d.Iterator()
for j := range tc.in {
got, gok := it.Next()
want, wok := d.Value(j)
if got != want || gok != wok {
t.Errorf(
"tc: %d, t: %d, got: %d, want: %d, gok: %v, wok: %v\n",
i, j, got, want, gok, wok,
)
}
}
for j := range tc.in {
got, gok := it.Value(j)
want, wok := d.Value(j)
if got != want || gok != wok {
t.Errorf(
"tc: %d, t: %d, got: %d, want: %d, gok: %v, wok: %v\n",
i, j, got, want, gok, wok,
)
}
for k := j + 1; k < len(tc.in); k++ {
got, gok := it.Next()
want, wok := d.Value(k)
if got != want || gok != wok {
t.Errorf(
"tc: %d, t: %d, k: %d, got: %d, want: %d, gok: %v, wok: %v\n",
i, j, k, got, want, gok, wok,
)
}
}
}
}
}
func BenchmarkIterator(b *testing.B) {
const (
n = 1_000_000
max = 100
)
in := make([]uint, n)
rand.Seed(18)
var prev uint
for i := range in {
prev += uint(rand.Intn(max))
in[i] = prev
}
d, err := From(in)
if err != nil {
b.Error(err)
}
it := d.Iterator()
b.Run(fmt.Sprintf("Next([%d])", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
v, ok := it.Value(0)
for i := 0; i < n; i++ {
v, ok = it.Next()
}
_ = v
_ = ok
}
})
}