-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_test.go
110 lines (83 loc) · 1.95 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package radix
import (
"testing"
)
func TestSet(t *testing.T) {
tree := NewTree()
tree.Set("abc", "1")
tree.Set("def", "2")
tree.Set("abd", "3")
}
func TestLen(t *testing.T) {
tree := NewTree()
tree.Set("abc", "1")
tree.Set("def", "2")
tree.Set("abd", "3")
s := tree.Size()
if s != 3 {
t.Fatalf("tree.Size() failed, expected %d, have %d", 3, s)
}
}
func TestGet(t *testing.T) {
tree := NewTree()
tree.Set("abc", "1")
tree.Set("def", "2")
tree.Set("abd", "3")
if v, found := tree.Get("def"); !found {
t.Fatalf("tree.Get() failed, no results returned")
} else {
if v != "2" {
t.Fatalf("tree.Get() failed, expected %v, have %v", "2", v)
}
}
if v, found := tree.Get("abd"); !found {
t.Fatalf("tree.Get() failed, no results returned")
} else {
if v != "3" {
t.Fatalf("tree.Get() failed, expected %v, have %v", "3", v)
}
}
if v, found := tree.Get("ab"); found {
t.Fatalf("tree.Get() failed, no results expected, have %v", v)
}
}
func TestDelete(t *testing.T) {
tree := NewTree()
tree.Set("a", "1")
tree.Set("ab", "2")
tree.Set("acd", "3")
tree.Set("acdx", "4")
err := tree.Delete("acd")
if err != nil {
t.Fatalf("tree.Delete() failed: %v", err)
}
if s := tree.Size(); s != 3 {
t.Fatalf("tree.Delete() failed, expected %d, have %d", 3, s)
}
}
func TestGetClosest(t *testing.T) {
tree := NewTree()
tree.Set("a", "1")
tree.Set("ab", "2")
tree.Set("acd", "3")
tree.Set("acdx", "4")
longest, value, found := tree.GetClosest("acde")
if !found {
t.Fatalf("tree.GetClosest() failed: no results found")
}
if longest != "acd" {
t.Fatalf("tree.GetClosest() failed, expected longest prefix to be %v, have %v", "acd", longest)
}
if value != "3" {
t.Fatalf("tree.GetClosest() failed, expected closest value to be %v, have %v", "3", value)
}
}
func TestString(t *testing.T) {
tree := NewTree()
tree.Set("a", "1")
tree.Set("ab", "2")
tree.Set("acd", "3")
tree.Set("acdx", "4")
s := tree.String()
t.Log(s)
}