forked from sauerbraten/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_test.go
93 lines (83 loc) · 1.6 KB
/
radix_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
package radix
import (
"fmt"
"testing"
)
func printit(r *Radix, level int) {
for i := 0; i < level; i++ {
fmt.Print("\t")
}
fmt.Printf("'%v' value: %v\n", r.key, r.value)
println()
for _, child := range r.children {
printit(child, level+1)
}
}
// None of the children must have a prefix in common with r.key
func validate(r *Radix) bool {
for _, child := range r.children {
_, i := longestCommonPrefix(r.key, child.key)
if i != 0 {
return false
}
validate(child)
}
return true
}
func TestInsert(t *testing.T) {
r := New()
if !validate(r) {
t.Log("Tree does not validate")
t.Fail()
}
if r.Len() != 0 {
t.Log("Len should be 0", r.Len())
}
r.Set("test", nil)
r.Set("slow", nil)
r.Set("water", nil)
r.Set("tester", nil)
r.Set("testering", nil)
r.Set("rewater", nil)
r.Set("waterrat", nil)
if !validate(r) {
t.Log("Tree does not validate")
t.Fail()
}
}
func TestRemove(t *testing.T) {
r := New()
r.Set("test", "aa")
r.Set("slow", "bb")
if k := r.Remove("slow").value; k != "bb" {
t.Log("should be bb", k)
t.Fail()
}
if r.Remove("slow") != nil {
t.Log("should be nil")
t.Fail()
}
}
func ExampleSubTree() {
r := New()
r.Set("tester", nil)
r.Set("testering", nil)
r.Set("te", nil)
r.Set("testeringandmore", nil)
f := r.SubTree("tester")
fmt.Printf("'%v' value: %v\n", f.key, f.value)
// Output:
// 'ster' value: <nil>
}
func BenchmarkSubTree(b *testing.B) {
b.StopTimer()
r := New()
r.Set("tester", nil)
r.Set("testering", nil)
r.Set("te", nil)
r.Set("testeringandmore", nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
_ = r.SubTree("tester")
}
}