-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_test.go
184 lines (142 loc) · 3.01 KB
/
avl_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package avl
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
type myNode struct {
avlHeader AvlNode
hash string
id int32
deleted bool
}
const maxNodes = 1000000
const hashLen = 32
var root *AvlNode
var p interface{}
var myp, mypp *myNode
var nodes [maxNodes]myNode
func cmpNameKey(key interface{}, node interface{}) int {
myName1 := key.(string)
myName2 := node.(*myNode).hash
if myName1 < myName2 {
return -1
} else if myName1 > myName2 {
return 1
} else {
return 0
}
}
func cmpNameNode(node1 interface{}, node2 interface{}) int {
myName1 := node1.(*myNode).hash
myName2 := node2.(*myNode).hash
if myName1 < myName2 {
return -1
} else if myName1 > myName2 {
return 1
} else {
return 0
}
}
func generateHash(i int32) string {
hash := md5.Sum([]byte(fmt.Sprintf("%0d", i)))
return hex.EncodeToString(hash[:])
}
func TestAvlInit(t *testing.T) {
var i int32
for i = 0; i < maxNodes; i++ {
nodes[i].id = i
nodes[i].hash = generateHash(i)
}
}
func TestAvlTreeInsert(t *testing.T) {
var it interface{}
var i int32
for i = 0; i < maxNodes; i++ {
it = AvlTreeInsert(&root, &nodes[i].avlHeader, &nodes[i], cmpNameNode)
assert.Nil(t, it, "node already in tree!")
if it != nil {
// if the insert failed, mark the node as deleted to ensure
// we don't try to remove it later
nodes[i].deleted = true
}
}
}
func TestAvlTreeFirstInOrder_1st(t *testing.T) {
p = AvlTreeFirstInOrder(root)
if p != nil {
myp = p.(*myNode)
}
}
func TestAvlTreeNextInOrder_1st(t *testing.T) {
for p != nil {
p = AvlTreeNextInOrder(&myp.avlHeader)
if p != nil {
myp = p.(*myNode)
}
}
}
func TestAvlTreeLastInOrder(t *testing.T) {
p = AvlTreeLastInOrder(root)
if p != nil {
myp = p.(*myNode)
}
}
func TestAvlTreePrevInOrder(t *testing.T) {
for p != nil {
p = AvlTreePrevInOrder(&myp.avlHeader)
if p != nil {
myp = p.(*myNode)
}
}
}
func TestAvlTreeFirstInPostOrder(t *testing.T) {
p = AvlTreeFirstInPostOrder(root)
if p != nil {
myp = p.(*myNode)
}
}
func TestAvlTreeNextInPostOrder(t *testing.T) {
for p != nil {
p = AvlGetParent(&myp.avlHeader)
if p != nil {
mypp = p.(*myNode)
p = AvlTreeNextInPostOrder(&myp.avlHeader, &mypp.avlHeader)
myp = p.(*myNode)
}
}
}
func TestAvlTreeRemove(t *testing.T) {
for i := 0; i < maxNodes; i += 2 {
AvlTreeRemove(&root, &nodes[i].avlHeader)
nodes[i].deleted = true
}
}
func TestAvlTreeFirstInOrder_2nd(t *testing.T) {
p = AvlTreeFirstInOrder(root)
if p != nil {
myp = p.(*myNode)
}
}
func TestAvlTreeNextInOrder_2nd(t *testing.T) {
for p != nil {
p = AvlTreeNextInOrder(&myp.avlHeader)
if p != nil {
myp = p.(*myNode)
}
}
}
func TestAvlTreeLookup(t *testing.T) {
for i := 0; i < maxNodes; i++ {
p = AvlTreeLookup(root, nodes[i].hash, cmpNameKey)
if p != nil {
assert.False(t, nodes[i].deleted,
fmt.Sprintf("Node %d is NOT in tree!", i))
} else {
assert.True(t, nodes[i].deleted,
fmt.Sprintf("Node %d IS in tree!", i))
}
}
}