-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsync_trie_test.go
158 lines (123 loc) · 3.23 KB
/
sync_trie_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
package trie
import (
"github.com/stretchr/testify/assert"
"testing"
)
import (
"fmt"
"strings"
)
func TestNewSyncTrie(t *testing.T) {
// 假装是一个Web路由
trie := NewSync[func() error](func(s string) ([]string, error) {
slice := make([]string, 0)
for _, x := range strings.Split(s, "/") {
if x != "" {
slice = append(slice, x)
}
}
return slice, nil
})
// 增加一个路由
err := trie.Add("/foo/bar", func() error {
fmt.Println("路由到了/foo/bar")
return nil
})
assert.Nil(t, err)
// 尝试寻找不存在的路由
value, err := trie.Query("/foo")
assert.NotNil(t, err)
assert.Nil(t, value)
// 尝试寻找存在的路由
handler, err := trie.Query("//foo//bar/")
assert.Nil(t, err)
assert.NotNil(t, handler)
err = handler()
assert.Nil(t, err)
}
func TestSyncTrieAdd(t *testing.T) {
trie := NewSync[string]()
err := trie.Add("test", "测试")
assert.Nil(t, err)
slice := trie.ToSlice()
assert.Equal(t, 1, len(slice))
}
func TestSyncTrieFindTrieNode(t *testing.T) {
}
func TestSyncTrieQuery(t *testing.T) {
trie := NewSync[string]()
err := trie.Add("test", "测试")
assert.Nil(t, err)
value, err := trie.Query("test")
assert.Nil(t, err)
assert.Equal(t, "测试", value)
value, err = trie.Query("test-001")
assert.ErrorIs(t, ErrNotFound, err)
}
func TestSyncTrieQueryOrDefault(t *testing.T) {
trie := NewSync[string]()
err := trie.Add("test", "测试")
assert.Nil(t, err)
value, err := trie.QueryOrDefault("test", "策士")
assert.Nil(t, err)
assert.Equal(t, "测试", value)
value, err = trie.QueryOrDefault("test-001", "策士")
assert.Nil(t, err)
assert.Equal(t, "策士", value)
}
func TestSyncTrieRemove(t *testing.T) {
trie := NewSync[string]()
err := trie.Upsert("china", "瓷器")
assert.Nil(t, err)
err = trie.Upsert("chinese", "中国人")
assert.Nil(t, err)
slice := trie.ToSlice()
assert.Equal(t, 2, len(slice))
err = trie.Remove("china")
assert.Nil(t, err)
slice = trie.ToSlice()
assert.Equal(t, 1, len(slice))
}
func TestSyncTrieUpsert(t *testing.T) {
trie := NewSync[string]()
_ = trie.Upsert("china", "瓷器")
value, err := trie.Query("china")
assert.Nil(t, err)
assert.Equal(t, "瓷器", value)
_ = trie.Upsert("china", "中国")
value, err = trie.Query("china")
assert.Nil(t, err)
assert.Equal(t, "中国", value)
}
func TestSyncTrieToSlice(t *testing.T) {
trie := NewSync[string]()
err := trie.Add("china", "中国")
assert.Nil(t, err)
err = trie.Add("chinese", "中国人")
assert.Nil(t, err)
slice := trie.ToSlice("")
assert.Equal(t, 2, len(slice))
}
func TestSyncTrieFindByPrefix(t *testing.T) {
trie := NewSync[string]()
_ = trie.Upsert("china", "china")
_ = trie.Upsert("chinese", "chinese")
_ = trie.Upsert("channel", "channel")
_ = trie.Upsert("chan", "chan")
_ = trie.Upsert("boy", "boy")
_ = trie.Upsert("CC11001100", "CC11001100")
slice := trie.QueryByPrefix("chan")
//t.Log(slice)
assert.Equal(t, 2, len(slice))
}
func TestSyncTrieContains(t *testing.T) {
trie := NewSync[string]()
_ = trie.Upsert("china", "china")
_ = trie.Upsert("chinese", "chinese")
exists, err := trie.Contains("china")
assert.Nil(t, err)
assert.True(t, exists)
exists, err = trie.Contains("chi")
assert.Nil(t, err)
assert.False(t, exists)
}