-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator_test.go
104 lines (90 loc) · 1.75 KB
/
iterator_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
package treap_test
import (
"testing"
"github.com/lthibault/treap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIter_Empty(t *testing.T) {
t.Parallel()
var empty *treap.Node
assert.Nil(t, handle.Iter(empty).Node,
"iterator for empty root should have nil node")
var i int
for it := handle.Iter(empty); it.Node != nil; it.Next() {
i++
}
assert.Zero(t, i, "empty iterator shoudl not loop")
}
func TestIter_SingleEntry(t *testing.T) {
t.Parallel()
var root = &treap.Node{
Key: 1,
Value: 1,
Weight: 1,
}
assert.NotNil(t, handle.Iter(root).Node,
"iterator for non-empty root should have non-nil node")
var i int
for it := handle.Iter(root); it.Node != nil; it.Next() {
i++
}
assert.Equal(t, 1, i, "iterator should loop one time")
}
func TestIter_MultiEntry(t *testing.T) {
t.Parallel()
var (
root *treap.Node
ok bool
tt = []treap.Node{
{
Key: 0,
Value: 0,
Weight: 0,
},
{
Key: 1,
Value: 1,
Weight: 1,
},
{
Key: 2,
Value: 2,
Weight: 2,
},
{
Key: 3,
Value: 3,
Weight: 2,
},
{
Key: 4,
Value: 4,
Weight: 2,
},
{
Key: 5,
Value: 5,
Weight: 1,
},
{
Key: 6,
Value: 6,
Weight: 2,
},
}
)
for _, n := range tt {
root, ok = handle.Insert(root, n.Key, n.Value, n.Weight)
require.True(t, ok, "precondition failed: insert must succeed")
}
var ns []treap.Node
for it := handle.Iter(root); it.Node != nil; it.Next() {
t.Log(it.Node.Key)
ns = append(ns, *it.Node)
}
assert.Len(t, ns, len(tt), "iterator should loop %d times", len(tt))
for i, n := range tt {
assert.Equal(t, n.Key, ns[i].Key, "iterator should traverse in key order")
}
}