-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathllrb_tree_test.go
82 lines (76 loc) · 1.78 KB
/
llrb_tree_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
package main
import "testing"
import "strconv"
import "math/rand"
import "time"
// Test never fails, just use it with `go test -v`, it prints some values, looks
// like everything is fine.
func TestLLRBTree(t *testing.T) {
var tree llrb_tree
rand.Seed(time.Now().UnixNano())
p := rand.Perm(1024)
// insert 1024 different numbers
for _, v := range p {
var x []byte
x = strconv.AppendInt(x, int64(v), 10)
tree.insert_maybe(x)
}
tree.clear()
// try inserting twice
for _, v := range p {
var x []byte
x = strconv.AppendInt(x, int64(v), 10)
tree.insert_maybe(x)
}
for _, v := range p {
var x []byte
x = strconv.AppendInt(x, int64(v), 10)
tree.insert_maybe(x)
}
t.Logf("Length: %d\n", tree.count)
// should be near 1/2
t.Logf("Root: %s\n", string(tree.root.value))
// should be near 1/4
t.Logf("Left: %s\n", string(tree.root.left.value))
// should be near 3/4
t.Logf("Right: %s\n", string(tree.root.right.value))
contains := func(n int) {
var x []byte
x = strconv.AppendInt(x, int64(n), 10)
t.Logf("Contains: %d, %v\n", n, tree.contains(x))
}
contains(10)
contains(0)
contains(999)
contains(54400)
max_h := 0
var traverse func(n *llrb_node, h int)
traverse = func(n *llrb_node, h int) {
if h > max_h {
max_h = h
}
if n.left != nil {
traverse(n.left, h+1)
}
if n.right != nil {
traverse(n.right, h+1)
}
}
traverse(tree.root, 0)
// from what I've tested, max height seems to be 12 or 13, which is nice
t.Logf("Max height: %d\n", max_h)
// check if it's sorted correctly
/*
var printnodes func(n *llrb_node)
printnodes = func(n *llrb_node) {
if n == nil {
return
}
printnodes(n.left)
t.Logf("Node: %s\n", string(n.value))
printnodes(n.right)
}
printnodes(tree.root)
*/
// seems correct, the order is lexicographic
}