Debian Codegraph @

trie_test.go

raw symbols SCIP
1 package triescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/
2
3 import (
4 "errorsscip-go gomod github.com/golang/go/src go1.19 errors/"
5 "testingscip-go gomod github.com/golang/go/src go1.19 testing/"
6 )
7
8 // RuneTrie
9
10 func TestRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrie().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
11 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
12 testTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrie().(t, trie)
13 }
14
15 func TestRuneTrieNilBehaviorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrieNilBehavior().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
16 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
17 testNilBehaviorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testNilBehavior().(t, trie)
18 }
19
20 func TestRuneTrieRootscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrieRoot().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
21 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
22 testTrieRootscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieRoot().(t, trie)
23
24 trie = NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
25 if !trie.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#isLeaf().() {
26 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("root of empty tree should be leaf")
27 }
28 trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#Put().("", "root")
29 if !trie.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#isLeaf().() {
30 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("root should not have children, only value")
31 }
32 }
33
34 func TestRuneTrieWalkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrieWalk().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
35 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
36 testTrieWalkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalk().(t, trie)
37 }
38
39 func TestRuneTrieWalkErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrieWalkError().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
40 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
41 testTrieWalkErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkError().(t, trie)
42 }
43
44 func TestRuneTrieWalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrieWalkPath().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
45 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
46 testTrieWalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkPath().(t, trie)
47 }
48
49 func TestRuneTrieWalkPathErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestRuneTrieWalkPathError().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
50 trie := NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().()
51 testTrieWalkPathErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkPathError().(t, trie)
52 }
53
54 // PathTrie
55
56 func TestPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrie().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
57 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
58 testTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrie().(t, trie)
59 }
60
61 func TestPathTrieWithNilConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWithNilConfig().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
62 trie := NewPathTrieWithConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrieWithConfig().(nil)
63 testTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrie().(t, trie)
64 }
65
66 func TestPathTrieWithEmptyConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWithEmptyConfig().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
67 trie := NewPathTrieWithConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrieWithConfig().(&PathTrieConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#{})
68 testTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrie().(t, trie)
69 }
70
71 func TestPathTrieWithConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWithConfig().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
72 trie := NewPathTrieWithConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrieWithConfig().(&PathTrieConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#{PathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathSegmenter().})
73 testTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrie().(t, trie)
74 }
75
76 func TestPathTrieNilBehaviorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieNilBehavior().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
77 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
78 testNilBehaviorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testNilBehavior().(t, trie)
79 }
80
81 func TestPathTrieRootscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieRoot().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
82 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
83 testTrieRootscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieRoot().(t, trie)
84
85 trie = NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
86 if !trie.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#isLeaf().() {
87 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("root of empty tree should be leaf")
88 }
89 trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Put().("", "root")
90 if !trie.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#isLeaf().() {
91 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("root should not have children, only value")
92 }
93 }
94
95 func TestPathTrieWalkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWalk().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
96 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
97 testTrieWalkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalk().(t, trie)
98 }
99
100 func TestPathTrieWalkErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWalkError().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
101 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
102 testTrieWalkErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkError().(t, trie)
103 }
104
105 func TestPathTrieWalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWalkPath().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
106 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
107 testTrieWalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkPath().(t, trie)
108 }
109
110 func TestPathTrieWalkPathErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathTrieWalkPathError().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
111 trie := NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().()
112 testTrieWalkPathErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkPathError().(t, trie)
113 }
114
115 func testTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrie().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
116 const firstPutValue = "first put"
117 cases := []struct {
118 key string
119 value interface{}
120 }{
121 {"fish", 0},
122 {"/cat", 1},
123 {"/dog", 2},
124 {"/cats", 3},
125 {"/caterpillar", 4},
126 {"/cat/gideon", 5},
127 {"/cat/giddy", 6},
128 }
129
130 // get missing keys
131 for _, c := range cases {
132 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(c.key); value != nil {
133 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be missing, found value %v", c.key, value)
134 }
135 }
136
137 // initial put
138 for _, c := range cases {
139 if isNew := trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(c.key, firstPutValue); !isNew {
140 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be missing", c.key)
141 }
142 }
143
144 // subsequent put
145 for _, c := range cases {
146 if isNew := trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(c.key, c.value); isNew {
147 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to have a value already", c.key)
148 }
149 }
150
151 // get
152 for _, c := range cases {
153 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(c.key); value != c.value {
154 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to have value %v, got %v", c.key, c.value, value)
155 }
156 }
157
158 // delete, expect Delete to return true indicating a node was nil'd
159 for _, c := range cases {
160 if deleted := trie.Deletescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Delete.(c.key); !deleted {
161 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be deleted", c.key)
162 }
163 }
164
165 // delete cleaned all the way to the first character
166 // expect Delete to return false bc no node existed to nil
167 for _, c := range cases {
168 if deleted := trie.Deletescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Delete.(string(c.key[0])); deleted {
169 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be cleaned by delete", string(c.key[0]))
170 }
171 }
172
173 // get deleted keys
174 for _, c := range cases {
175 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(c.key); value != nil {
176 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be deleted, got value %v", c.key, value)
177 }
178 }
179 }
180
181 func testNilBehaviorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testNilBehavior().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
182 cases := []struct {
183 key string
184 value interface{}
185 }{
186 {"/cat", 1},
187 {"/catamaran", 2},
188 {"/caterpillar", nil},
189 }
190 expectNilValues := []string{"/", "/c", "/ca", "/caterpillar", "/other"}
191
192 // initial put
193 for _, c := range cases {
194 if isNew := trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(c.key, c.value); !isNew {
195 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be missing", c.key)
196 }
197 }
198
199 // get nil
200 for _, key := range expectNilValues {
201 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(key); value != nil {
202 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to have value nil, got %v", key, value)
203 }
204 }
205 }
206
207 func testTrieRootscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieRoot().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
208 const firstPutValue = "first put"
209 const putValue = "value"
210
211 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(""); value != nil {
212 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key '' to be missing, found value %v", value)
213 }
214 if !trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.("", firstPutValue) {
215 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("expected key '' to be missing")
216 }
217 if trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.("", putValue) {
218 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("expected key '' to have a value already")
219 }
220 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(""); value != putValue {
221 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key '' to have value %v, got %v", putValue, value)
222 }
223 if !trie.Deletescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Delete.("") {
224 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("expected key '' to be deleted")
225 }
226 if value := trie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Get.(""); value != nil {
227 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key '' to be deleted, got value %v", value)
228 }
229 }
230
231 func testTrieWalkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalk().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
232 table := map[string]interface{}{
233 "": -1,
234 "fish": 0,
235 "/cat": 1,
236 "/dog": 2,
237 "/cats": 3,
238 "/caterpillar": 4,
239 "/notes": 30,
240 "/notes/new": 31,
241 "/notes/:id": 32,
242 }
243 // key -> times walked
244 walked := make(map[string]int)
245 for key := range table {
246 walked[key] = 0
247 }
248
249 for key, value := range table {
250 if isNew := trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(key, value); !isNew {
251 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be missing", key)
252 }
253 }
254
255 walker := func(key string, value interface{}) error {
256 // value for each walked key is correct
257 if value != table[key] {
258 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to have value %v, got %v", key, table[key], value)
259 }
260 walked[key]++
261 return nil
262 }
263 if err := trie.Walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Walk.(walker); err != nil {
264 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected error nil, got %v", err)
265 }
266
267 // each key/value walked exactly once
268 for key, walkedCount := range walked {
269 if walkedCount != 1 {
270 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be walked exactly once, got %v", key, walkedCount)
271 }
272 }
273 }
274
275 func testTrieWalkErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkError().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
276 table := map[string]interface{}{
277 "/L1/L2A": 1,
278 "/L1/L2B/L3A": 2,
279 "/L1/L2B/L3B/L4": 42,
280 "/L1/L2B/L3C": 4,
281 "/L1/L2C": 5,
282 }
283
284 walkerError := errorsscip-go gomod github.com/golang/go/src go1.19 errors/.Newscip-go gomod github.com/golang/go/src go1.19 errors/New().("walker error")
285 walked := 0
286
287 for key, value := range table {
288 trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(key, value)
289 }
290 walker := func(key string, value interface{}) error {
291 if value == 42 {
292 return walkerError
293 }
294 walked++
295 return nil
296 }
297 if err := trie.Walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Walk.(walker); err != walkerError {
298 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected walker error, got %v", err)
299 }
300 if len(table) == walked {
301 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected nodes walked < %d, got %d", len(table), walked)
302 }
303 }
304
305 func testTrieWalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkPath().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
306 table := map[string]interface{}{
307 "fish": 0,
308 "/cat": 1,
309 "/dog": 2,
310 "/cats": 3,
311 "/caterpillar": 4,
312 "/notes": 30,
313 "/notes/new": 31,
314 "/notes/new/noise": 32,
315 }
316 // key -> times walked
317 walked := make(map[string]int)
318 for key := range table {
319 walked[key] = 0
320 }
321
322 for key, value := range table {
323 if isNew := trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(key, value); !isNew {
324 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be missing", key)
325 }
326 }
327
328 walker := func(key string, value interface{}) error {
329 // value for each walked key is correct
330 if value != table[key] {
331 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to have value %v, got %v", key, table[key], value)
332 }
333 walked[key]++
334 return nil
335 }
336 if err := trie.WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#WalkPath.("/notes/new/noise", walker); err != nil {
337 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected error nil, got %v", err)
338 }
339
340 // expect each key/value in path walked exactly once, and not other keys
341 for key, walkedCount := range walked {
342 switch key {
343 case "/notes", "/notes/new", "/notes/new/noise":
344 if walkedCount != 1 {
345 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be walked exactly once, got %v", key, walkedCount)
346 }
347 default:
348 if walkedCount != 0 {
349 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to not be walked, got %v", key, walkedCount)
350 }
351 }
352 }
353
354 for key := range table {
355 walked[key] = 0
356 }
357 if err := trie.WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#WalkPath.("/notes/new/nose", walker); err != nil {
358 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected error nil, got %v", err)
359 }
360 // expect each key/value in path walked exactly once, and not other keys
361 for key, walkedCount := range walked {
362 switch key {
363 case "/notes", "/notes/new":
364 if walkedCount != 1 {
365 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be walked exactly once, got %v", key, walkedCount)
366 }
367 default:
368 if walkedCount != 0 {
369 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to not be walked, got %v", key, walkedCount)
370 }
371 }
372 }
373
374 var foundRoot bool
375 trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.("", "ROOT")
376 trie.WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#WalkPath.("/notes/new/noise", func(key string, value interface{}) error {
377 if key == "" && value == "ROOT" {
378 foundRoot = true
379 }
380 return nil
381 })
382 if !foundRoot {
383 t.Errorscip-go gomod github.com/golang/go/src go1.19 testing/common#Error().("did not find root")
384 }
385 }
386
387 func testTrieWalkPathErrorscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testTrieWalkPathError().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#, trie Trierscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#) {
388 table := map[string]interface{}{
389 "/L1/L2A": 1,
390 "/L1/L2A/L3B": 99,
391 "/L1/L2A/L3B/L4": 2,
392 "/L1/L2B/L3B": 3,
393 "/L1/L2C": 4,
394 }
395
396 walkerError := errorsscip-go gomod github.com/golang/go/src go1.19 errors/.Newscip-go gomod github.com/golang/go/src go1.19 errors/New().("walker error")
397
398 walked := make(map[string]int)
399 for key := range table {
400 walked[key] = 0
401 }
402
403 for key, value := range table {
404 trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.(key, value)
405 }
406 walker := func(key string, value interface{}) error {
407 if value == 99 {
408 return walkerError
409 }
410 walked[key]++
411 return nil
412 }
413 if err := trie.WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#WalkPath.("/L1/L2A/L3B", walker); err != walkerError {
414 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected walker error, got %v", err)
415 }
416 // expect each key/value in path, up to error and not including key with
417 // value 99, walked exactly once, and not other keys
418 var walkedTotal int
419 for key, walkedCount := range walked {
420 switch key {
421 case "/L1/L2A":
422 if walkedCount != 1 {
423 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to be walked exactly once, got %v", key, walkedCount)
424 }
425 walkedTotal++
426 default:
427 if walkedCount != 0 {
428 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected key %s to not be walked, got %v", key, walkedCount)
429 }
430 }
431 }
432 if walkedTotal != 1 {
433 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected 1 nodes walked, got %d", walkedTotal)
434 }
435
436 rootError := errorsscip-go gomod github.com/golang/go/src go1.19 errors/.Newscip-go gomod github.com/golang/go/src go1.19 errors/New().("error at root")
437 trie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#Put.("", "ROOT")
438 err := trie.WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/Trier#WalkPath.("/L1/L2A/L3B/L4", func(key string, value interface{}) error {
439 if key == "" && value == "ROOT" {
440 return rootError
441 }
442 return nil
443 })
444 if err != rootError {
445 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected %s, got %s", rootError, err)
446 }
447 }