-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstepper_test.go
102 lines (94 loc) · 2.23 KB
/
stepper_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
package radixtree
import (
"testing"
)
func TestStepper(t *testing.T) {
rt := new(Tree[string])
rt.Put("tom", "TOM")
rt.Put("tomato", "TOMATO")
rt.Put("torn", "TORN")
// (root) t-> ("o", _) m-> ("", TOM) a-> ("to", TOMATO)
// r-> ("n", TORN)
iter := rt.NewStepper()
if iter.Next('x') {
t.Fatal("'x' should not have advanced iterator")
}
if !iter.Next('t') {
t.Fatal("'t' should have advanced iterator")
}
val, ok := iter.Value()
if ok || val != "" {
t.Fatal("should not have value at 't'")
}
item := iter.Item()
if item != nil {
t.Fatal("should not have item at 't'")
}
if !iter.Next('o') {
t.Fatal("'o' should have advanced iterator")
}
if _, ok = iter.Value(); ok {
t.Fatal("should not have value at 'o'")
}
if iter.Next('o') {
t.Fatal("'o' should not have advanced iterator")
}
// branch iterator
iterR := iter.Copy()
if !iter.Next('m') {
t.Fatal("'m' should have advanced iterator")
}
val, ok = iter.Value()
if !ok || val != "TOM" {
t.Fatalf("expected \"TOM\" at 'm', got %q", val)
}
item = iter.Item()
if item == nil || item.Value() != "TOM" {
t.Fatalf("expected value \"TOM\" at 'm'")
}
if item.Key() != "tom" {
t.Fatalf("expected key \"tom\" at 'm'")
}
if !iter.Next('a') {
t.Fatal("'a' should have advanced iterator")
}
if _, ok = iter.Value(); ok {
t.Fatal("should not have value at 'a'")
}
if !iter.Next('t') {
t.Fatal("'t' should have advanced iterator")
}
if _, ok = iter.Value(); ok {
t.Fatal("should not have value at 't'")
}
if !iter.Next('o') {
t.Fatal("'o' should have advanced iterator")
}
val, ok = iter.Value()
if !ok || val != "TOMATO" {
t.Fatal("expected \"TOMATO\" 'o'")
}
if !iterR.Next('r') {
t.Fatal("'r' should have advanced iterator")
}
if val, ok = iterR.Value(); ok {
t.Fatal("should not have value at 'r', got ", val)
}
if !iterR.Next('n') {
t.Fatal("'n' should have advanced iterator")
}
val, ok = iterR.Value()
if !ok || val != "TORN" {
t.Fatal("expected \"TORN\" 'n'")
}
if iterR.Next('n') {
t.Fatal("'n' should not have advanced iterator")
}
iter = rt.NewStepper()
if !iter.Next('t') {
t.Fatal("'t' should have advanced iterator")
}
if iter.Next('x') {
t.Fatal("'x' should not have advanced iterator")
}
}