-
Notifications
You must be signed in to change notification settings - Fork 0
/
treeiterator_test.go
162 lines (149 loc) · 3.87 KB
/
treeiterator_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package btree
import (
"testing"
)
func TestTreeIterator_HasNext(t *testing.T) {
degree := 3
bt := createTestTree(degree, 0)
// empty node == zero depth
walker := bt.Iterate()
if walker.HasNext() {
t.Error("Expected false HasNext on empty node")
}
if walker.Next() != nil {
t.Error("Expected Next as nil on empty node")
}
bt = createTestTree(degree, 1)
walker = bt.Iterate()
if !walker.HasNext() {
t.Error("Expected true HasNext on single entry node before next")
}
_ = walker.Next()
if walker.HasNext() {
t.Error("Expected false HasNext on single entry node after next")
}
// test tree node tree
testCount := 3
bt = createTestTree(degree, testCount)
walker = bt.Iterate()
for i := 0; i < testCount; i++ {
if !walker.HasNext() {
t.Errorf("Expected HasNext true with test tree before %d iteration", i+1)
}
_ = walker.Next()
}
_ = walker.Next()
if walker.HasNext() {
t.Error("Expected HasNext true after first next")
}
}
func TestTreeIterator_Depth(t *testing.T) {
degree := 3
bt := createTestTree(degree, 0)
walker := bt.Iterate()
depth := walker.Depth()
if depth != 0 {
t.Errorf("Expected 0 on Depth with nil root, got %v", depth)
}
testCount := 1
bt = createTestTree(degree, testCount)
walker = bt.Iterate()
depth = walker.Depth()
if depth != 1 {
t.Errorf("Expected 1 on Depth with one node, got %v", depth)
}
testCount = 3
bt = createTestTree(degree, testCount)
walker = bt.Iterate()
depth = walker.Depth()
if depth != 2 {
t.Errorf("Expected 2 on Depth with three nodes, got %v", depth)
}
_ = walker.Next()
depth = walker.Depth()
if depth != 1 {
t.Errorf("Expected 1 on Depth with three nodes, got %v", depth)
}
_ = walker.Next()
depth = walker.Depth()
if depth != 2 {
t.Errorf("Expected 2 on Depth with three nodes, got %v", depth)
}
_ = walker.Next()
depth = walker.Depth()
if depth != 0 {
t.Errorf("Expected 0 on Depth with three nodes, got %v", depth)
}
}
func TestTreeIterator_Next(t *testing.T) {
degree := 3
testCount := 0
bt := createTestTree(degree, testCount)
walker := bt.Iterate()
// check nil and empty tree
next := walker.Next()
if next != nil {
t.Errorf("Expected nil on Next with nil root, got %v", next)
}
testCount = 1
bt = createTestTree(degree, testCount)
walker = bt.Iterate()
next = walker.Next()
if next == nil {
t.Errorf("Expected non nil result on Next on single entry node")
}
if len(next) != 1 {
t.Errorf("Expected 1 element returned, got %d", len(next))
}
if next[0].Key != 0 {
t.Errorf("Expected returned key 0, got %d", next[0].Key)
}
if next[0].Value == nil {
t.Errorf("Expected non nil value for key %v", next[0].Key)
}
expect := "-0-"
if *next[0].Value != expect {
t.Errorf("Expected returned value '%s', got '%s'", expect, *next[0].Value)
}
next = walker.Next()
if next != nil {
t.Errorf("Expected nil on Next with empty node, got %v", next)
}
if walker.HasNext() {
t.Error("Expected false HasNext on single entry node after next")
}
testCount = 15
bt = createTestTree(degree, testCount)
walker = bt.Iterate()
var total []NodeEntry[int, string]
for walker.HasNext() {
next = walker.Next()
if len(next) < 1 {
t.Error("Expected non empty entires for next iteration")
}
total = append(total, next...)
next = walker.Next()
if next == nil {
break
}
if len(next) != 1 {
t.Error("Expected 1 element returned, got ", len(next))
}
total = append(total, next...)
}
if len(total) != testCount {
t.Errorf("Expected %d elements returned from test tree, got %d", testCount, len(total))
}
lastKey := -1
for _, entry := range total {
if entry.Key <= lastKey {
t.Errorf("unexpected entry out of order. previous key was %d and found key %d following", lastKey, entry.Key)
}
lastKey = entry.Key
}
}
func createTestTree(degree, count int) *bTree[int, string] {
bt := NewBTree[int, string](degree)
fillTree(bt, count)
return bt.(*bTree[int, string])
}