Debian Codegraph @

patricia/patricia_test.go

raw symbols SCIP
1 // Copyright (c) 2014 The go-patricia AUTHORS
2 //
3 // Use of this source code is governed by The MIT License
4 // that can be found in the LICENSE file.
5
6 package patricia
7
8 import (
9 "crypto/rand"
10 mrand "math/rand"
11 "reflect"
12 "testing"
13 )
14
15 // Tests -----------------------------------------------------------------------
16
17 func TestTrie_GetNonexistentPrefix(t *testing.T) {
18 trie := NewTrie()
19
20 data := []testData{
21 {"aba", 0, success},
22 }
23
24 for _, v := range data {
25 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
26 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
27 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
28 }
29 }
30
31 t.Logf("GET prefix=baa, expect item=nil")
32 if item := trie.Get(Prefix("baa")); item != nil {
33 t.Errorf("Unexpected return value, expected=<nil>, got=%v", item)
34 }
35 }
36
37 func TestTrie_RandomKitchenSink(t *testing.T) {
38 if testing.Short() {
39 t.Skip()
40 }
41 const count, size = 750000, 16
42 b := make([]byte, count+size+1)
43 if _, err := rand.Read(b); err != nil {
44 t.Fatal("error generating random bytes", err)
45 }
46 m := make(map[string]string)
47 for i := 0; i < count; i++ {
48 m[string(b[i:i+size])] = string(b[i+1 : i+size+1])
49 }
50 trie := NewTrie()
51 getAndDelete := func(k, v string) {
52 i := trie.Get(Prefix(k))
53 if i == nil {
54 t.Fatalf("item not found, prefix=%v", []byte(k))
55 } else if s, ok := i.(string); !ok {
56 t.Fatalf("unexpected item type, expecting=%v, got=%v", reflect.TypeOf(k), reflect.TypeOf(i))
57 } else if s != v {
58 t.Fatalf("unexpected item, expecting=%v, got=%v", []byte(k), []byte(s))
59 } else if !trie.Delete(Prefix(k)) {
60 t.Fatalf("delete failed, prefix=%v", []byte(k))
61 } else if i = trie.Get(Prefix(k)); i != nil {
62 t.Fatalf("unexpected item, expecting=<nil>, got=%v", i)
63 } else if trie.Delete(Prefix(k)) {
64 t.Fatalf("extra delete succeeded, prefix=%v", []byte(k))
65 }
66 }
67 for k, v := range m {
68 if !trie.Insert(Prefix(k), v) {
69 t.Fatalf("insert failed, prefix=%v", []byte(k))
70 }
71 if byte(k[size/2]) < 128 {
72 getAndDelete(k, v)
73 delete(m, k)
74 }
75 }
76 for k, v := range m {
77 getAndDelete(k, v)
78 }
79 }
80
81 // Make sure Delete that affects the root node works.
82 // This was panicking when Delete was broken.
83 func TestTrie_DeleteRoot(t *testing.T) {
84 trie := NewTrie()
85
86 v := testData{"aba", 0, success}
87
88 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
89 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
90 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
91 }
92
93 t.Logf("DELETE prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
94 if ok := trie.Delete(Prefix(v.key)); ok != v.retVal {
95 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
96 }
97 }
98
99 func TestTrie_DeleteAbsentPrefix(t *testing.T) {
100 trie := NewTrie()
101
102 v := testData{"a", 0, success}
103
104 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
105 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
106 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
107 }
108
109 d := "ab"
110 t.Logf("DELETE prefix=%v, success=%v", d, failure)
111 if ok := trie.Delete(Prefix(d)); ok != failure {
112 t.Errorf("Unexpected return value, expected=%v, got=%v", failure, ok)
113 }
114 t.Logf("GET prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
115 if i := trie.Get(Prefix(v.key)); i != v.value {
116 t.Errorf("Unexpected item, expected=%v, got=%v", v.value, i)
117 }
118 }
119
120 func reverse(s string) string {
121 runes := []rune(s)
122 for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
123 runes[i], runes[j] = runes[j], runes[i]
124 }
125 return string(runes)
126 }
127
128 func checkMasksRecursive(t *testing.T, root *Trie) {
129 for _, child := range root.children.getChildren() {
130 if child.mask & ^root.mask != 0 {
131 t.Errorf("\ninvalid mask at prefix %s\nchild prefix: %s\ncharmap: \t%s\nmask: \t%064b\n"+
132 "child mask: \t%064b\ndiff:\t%064b\n",
133 root.prefix,
134 child.prefix,
135 reverse(charmap),
136 root.mask,
137 child.mask,
138 child.mask & ^root.mask,
139 )
140 }
141 checkMasksRecursive(t, child)
142 }
143 }
144
145 func TestTrie_AddCorrectMasks(t *testing.T) {
146 trie := NewTrie()
147 data := []testData{
148 {"Pepan", "Pepan Zdepan", success},
149 {"Pepin", "Pepin Omacka", success},
150 {"Honza", "Honza Novak", success},
151 {"Jenik", "Jenik Poustevnicek", success},
152 {"Pepan", "Pepan Dupan", failure},
153 {"Karel", "Karel Pekar", success},
154 {"Jenak", "Jenak Poustevnicek", success},
155 {"Pepanek", "Pepanek Zemlicka", success},
156 }
157
158 for _, v := range data {
159 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
160 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
161 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
162 }
163 checkMasksRecursive(t, trie)
164 }
165 }
166
167 func TestTrie_DeleteCorrectMasks(t *testing.T) {
168 data := []testData{
169 {"Pepan", "Pepan Zdepan", success},
170 {"Pepin", "Pepin Omacka", success},
171 {"Honza", "Honza Novak", success},
172 {"Jenik", "Jenik Poustevnicek", success},
173 {"Karel", "Karel Pekar", success},
174 {"Jenak", "Jenak Poustevnicek", success},
175 {"Pepanek", "Pepanek Zemlicka", success},
176 }
177
178 deleteData := [][]testData{
179 {
180 {"Honza", "Honza Novak", success},
181 {"Jenik", "Jenik Poustevnicek", success},
182 {"Pepan", "Pepan Dupan", success},
183 },
184 {
185 {"Pepan", "Pepan Dupan", success},
186 },
187 {
188 {"Jenak", "Jenak Poustevnicek", success},
189 {"Pepanek", "Pepanek Zemlicka", success},
190 {"Pepin", "Pepin Omacka", success},
191 {"Honza", "Honza Novak", success},
192 {"Jenik", "Jenik Poustevnicek", success},
193 },
194 }
195
196 for _, d := range deleteData {
197 trie := NewTrie()
198 for _, v := range data {
199 t.Logf("INSERT prefix=%v, item=%v, success=%v", v.key, v.value, v.retVal)
200 if ok := trie.Insert(Prefix(v.key), v.value); ok != v.retVal {
201 t.Errorf("Unexpected return value, expected=%v, got=%v", v.retVal, ok)
202 }
203 }
204
205 for _, record := range d {
206 trie.Delete(Prefix(record.key))
207 }
208
209 checkMasksRecursive(t, trie)
210 }
211
212 }
213
214 func populateTrie(t *testing.T) *Trie {
215 data := []string{
216 "Pepan",
217 "Pepin",
218 "Honza",
219 "Jenik",
220 "Karel",
221 "Jenak",
222 "Pepanek",
223 }
224
225 trie := NewTrie()
226 for _, v := range data {
227 if ok := trie.Insert(Prefix(v), struct{}{}); !ok {
228 t.Errorf("Couldn't insert item %s", v)
229 }
230 }
231
232 return trie
233 }
234
235 func TestTrie_FuzzyCollect(t *testing.T) {
236 trie := populateTrie(t)
237
238 type testResult struct {
239 wantKey string
240 wantSkipped int
241 }
242
243 type testData struct {
244 query string
245 caseInsensitive bool
246 wantResults []testResult
247 }
248
249 testQueries := []testData{
250 {
251 "Ppn",
252 false,
253 []testResult{
254 {"Pepan", 2},
255 {"Pepin", 2},
256 {"Pepanek", 2},
257 },
258 },
259 {
260 "Ha",
261 false,
262 []testResult{
263 {"Honza", 3},
264 },
265 },
266 {
267 "nza",
268 false,
269 []testResult{
270 {"Honza", 0},
271 },
272 },
273 {
274 "eni",
275 false,
276 []testResult{
277 {"Jenik", 0},
278 },
279 },
280 {
281 "jk",
282 true,
283 []testResult{
284 {"Jenik", 3},
285 {"Jenak", 3},
286 },
287 },
288 {
289 "ppn",
290 true,
291 []testResult{
292 {"Pepan", 2},
293 {"Pepin", 2},
294 {"Pepanek", 2},
295 },
296 },
297 }
298
299 for _, data := range testQueries {
300 resultMap := make(map[string]int)
301 t.Logf("QUERY %s", data.query)
302 trie.VisitFuzzy(Prefix(data.query), data.caseInsensitive, func(prefix Prefix, item Item, skipped int) error {
303 // result := testResult{string(prefix), skipped}
304 resultMap[string(prefix)] = skipped
305 return nil
306 })
307 t.Logf("got result set %v\n", resultMap)
308
309 for _, want := range data.wantResults {
310 got, ok := resultMap[want.wantKey]
311 if !ok {
312 t.Errorf("item %s not found in result set\n", want.wantKey)
313 continue
314 }
315
316 if got != want.wantSkipped {
317 t.Errorf("got wrong skipped value, wanted %d, got %d\n",
318 want.wantSkipped, got)
319 }
320 }
321 }
322 }
323
324 func TestTrie_SubstringCollect(t *testing.T) {
325 trie := populateTrie(t)
326
327 type testData struct {
328 query string
329 caseInsensitive bool
330 wantResults []string
331 }
332
333 testQueries := []testData{
334 {
335 "epa",
336 false,
337 []string{
338 "Pepan",
339 "Pepanek",
340 },
341 },
342 {
343 "onza",
344 false,
345 []string{
346 "Honza",
347 },
348 },
349 {
350 "nza",
351 false,
352 []string{
353 "Honza",
354 },
355 },
356 {
357 "l",
358 false,
359 []string{
360 "Karel",
361 },
362 },
363 {
364 "a",
365 false,
366 []string{
367 "Pepan",
368 "Honza",
369 "Pepan",
370 "Karel",
371 "Jenak",
372 "Pepanek",
373 },
374 },
375 {
376 "pep",
377 true,
378 []string{
379 "Pepin",
380 "Pepan",
381 },
382 },
383 {
384 "kar",
385 true,
386 []string{
387 "Karel",
388 },
389 },
390 {
391 "",
392 false,
393 []string{
394 "Pepan",
395 "Pepin",
396 "Honza",
397 "Jenik",
398 "Karel",
399 "Jenak",
400 "Pepanek",
401 },
402 },
403 }
404
405 for _, data := range testQueries {
406 resultMap := make(map[string]bool)
407 t.Logf("QUERY %s", data.query)
408 trie.VisitSubstring(Prefix(data.query), true, func(prefix Prefix, item Item) error {
409 // result := testResult{string(prefix), skipped}
410 resultMap[string(prefix)] = true
411 return nil
412 })
413 t.Logf("got result set %v\n", resultMap)
414
415 for _, want := range data.wantResults {
416 if _, ok := resultMap[want]; !ok {
417 t.Errorf("item %s not found in result set\n", want)
418 continue
419 }
420 }
421 }
422 }
423
424 func Test_makePrefixMask(t *testing.T) {
425 type testData struct {
426 key Prefix
427 wanted uint64
428 }
429
430 data := []testData{
431 {
432 Prefix("0123456789"),
433 0x3FF,
434 },
435 {
436 Prefix("AAAA"),
437 0x400,
438 },
439 {
440 Prefix(""),
441 0,
442 },
443 {
444 Prefix("abc"),
445 0x7000000000,
446 },
447 {
448 Prefix(".-"),
449 0xc000000000000000,
450 },
451 }
452
453 for _, d := range data {
454 got := makePrefixMask(d.key)
455 if got != d.wanted {
456 t.Errorf("Unexpected bitmask, wanted: %b, got %b\n", d.wanted, got)
457 }
458 }
459 }
460
461 const (
462 amountWords = 100000
463 wordLength = 10
464 queryLength = 10
465 )
466
467 var benchmarkTrie *Trie
468
469 func populateBenchmarkTrie(superDenseChildList bool) {
470 benchmarkTrie = NewTrie()
471
472 for i := 0; i < amountWords; i++ {
473 benchmarkTrie.Insert(Prefix(mrandBytes(wordLength)), struct{}{})
474 }
475 }
476
477 type visitFunc func(prefix Prefix, caseInsensitive bool, visitor VisitorFunc) error
478
479 func benchmarkVisit(caseInsensitive bool, visitor visitFunc, b *testing.B) {
480 b.ResetTimer()
481
482 for i := 0; i < b.N; i++ {
483 visitor(Prefix(mrandBytes(queryLength)), caseInsensitive, func(prefix Prefix, item Item) error {
484 return nil
485 })
486 }
487 }
488
489 func benchmarkVisitFuzzy(caseInsensitive bool, b *testing.B) {
490 b.ResetTimer()
491
492 for i := 0; i < b.N; i++ {
493 benchmarkTrie.VisitFuzzy(Prefix(mrandBytes(queryLength)), caseInsensitive, func(prefix Prefix, item Item, skipped int) error {
494 return nil
495 })
496 }
497 }
498
499 func BenchmarkPrefix(b *testing.B) {
500 populateBenchmarkTrie(false)
501 benchmarkVisit(false, benchmarkTrie.VisitPrefixes, b)
502 }
503 func BenchmarkPrefixCaseInsensitive(b *testing.B) {
504 populateBenchmarkTrie(false)
505 benchmarkVisit(true, benchmarkTrie.VisitPrefixes, b)
506 }
507 func BenchmarkPrefixSuperDense(b *testing.B) {
508 populateBenchmarkTrie(true)
509 benchmarkVisit(false, benchmarkTrie.VisitPrefixes, b)
510 }
511 func BenchmarkPrefixCaseInsensitiveSuperDense(b *testing.B) {
512 populateBenchmarkTrie(true)
513 benchmarkVisit(true, benchmarkTrie.VisitPrefixes, b)
514 }
515 func BenchmarkSubstring(b *testing.B) {
516 populateBenchmarkTrie(false)
517 benchmarkVisit(false, benchmarkTrie.VisitSubstring, b)
518 }
519 func BenchmarkSubstringCaseInsensitive(b *testing.B) {
520 populateBenchmarkTrie(false)
521 benchmarkVisit(true, benchmarkTrie.VisitSubstring, b)
522 }
523 func BenchmarkSubstringSuperDense(b *testing.B) {
524 populateBenchmarkTrie(true)
525 benchmarkVisit(false, benchmarkTrie.VisitSubstring, b)
526 }
527 func BenchmarkSubstringCaseInsensitiveSuperDense(b *testing.B) {
528 populateBenchmarkTrie(true)
529 benchmarkVisit(true, benchmarkTrie.VisitSubstring, b)
530 }
531
532 func BenchmarkFuzzy(b *testing.B) {
533 populateBenchmarkTrie(false)
534 benchmarkVisitFuzzy(false, b)
535 }
536 func BenchmarkFuzzyCaseInsensitive(b *testing.B) {
537 populateBenchmarkTrie(false)
538 benchmarkVisitFuzzy(true, b)
539 }
540 func BenchmarkFuzzySuperDense(b *testing.B) {
541 populateBenchmarkTrie(true)
542 benchmarkVisitFuzzy(false, b)
543 }
544 func BenchmarkFuzzyCaseInsensitiveSuperDense(b *testing.B) {
545 populateBenchmarkTrie(true)
546 benchmarkVisitFuzzy(true, b)
547 }
548
549 func mrandBytes(length int) []byte {
550 bytes := make([]byte, length)
551 for i := 0; i < length; i++ {
552 bytes = append(bytes, byte(mrand.Intn(75)+'0'))
553 }
554
555 return bytes
556 }