Debian Codegraph @

segmenter_test.go

raw symbols SCIP
1 package triescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/
2
3 import (
4 "stringsscip-go gomod github.com/golang/go/src go1.19 strings/"
5 "testingscip-go gomod github.com/golang/go/src go1.19 testing/"
6 )
7
8 // test splitting /path/keys/ into parts (e.g. /path, /keys, /)
9 func TestPathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathSegmenter().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
10 cases := []struct {
11 key string
12 parts []string
13 indices []int // indexes to use as next start, in order
14 }{
15 {"", []string{""}, []int{-1}},
16 {"/", []string{"/"}, []int{-1}},
17 {"static_file", []string{"static_file"}, []int{-1}},
18 {"/users/scott", []string{"/users", "/scott"}, []int{6, -1}},
19 {"users/scott", []string{"users", "/scott"}, []int{5, -1}},
20 {"/users/ramona/", []string{"/users", "/ramona", "/"}, []int{6, 13, -1}},
21 {"users/ramona/", []string{"users", "/ramona", "/"}, []int{5, 12, -1}},
22 {"//", []string{"/", "/"}, []int{1, -1}},
23 {"/a/b/c", []string{"/a", "/b", "/c"}, []int{2, 4, -1}},
24 }
25
26 for _, c := range cases {
27 partNum := 0
28 for prefix, i := PathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathSegmenter().(c.key, 0); ; prefix, i = PathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathSegmenter().(c.key, i) {
29 if prefix != c.parts[partNum] {
30 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected part %d of key '%s' to be '%s', got '%s'", partNum, c.key, c.parts[partNum], prefix)
31 }
32 if i != c.indices[partNum] {
33 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("in iteration %d, expected next index of key '%s' to be '%d', got '%d'", partNum, c.key, c.indices[partNum], i)
34 }
35 partNum++
36 if i == -1 {
37 break
38 }
39 }
40 if partNum != len(c.parts) {
41 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected '%s' to have %d parts, got %d", c.key, len(c.parts), partNum)
42 }
43 }
44 }
45
46 func TestPathSegmenterEdgeCasesscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestPathSegmenterEdgeCases().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
47 cases := []struct {
48 path string
49 start int
50 segment string
51 nextIndex int
52 }{
53 {"", 0, "", -1},
54 {"", 10, "", -1},
55 {"/", 0, "/", -1},
56 {"/", 10, "", -1},
57 {"/", -10, "", -1},
58 {"/", 1, "", -1},
59 {"//", 0, "/", 1},
60 {"//", 1, "/", -1},
61 {"//", 2, "", -1},
62 {" /", 0, " ", 1},
63 {" /", 1, "/", -1},
64 }
65
66 for _, c := range cases {
67 segment, nextIndex := PathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathSegmenter().(c.path, c.start)
68 if segment != c.segment {
69 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected segment %s starting at %d in path %s, got %s", c.segment, c.start, c.path, segment)
70 }
71 if nextIndex != c.nextIndex {
72 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("expected nextIndex %d starting at %d in path %s, got %d", c.nextIndex, c.start, c.path, nextIndex)
73 }
74 }
75 }
76
77 func testPathSegmenterDotscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testPathSegmenterDot().(path string, start int) (segment string, next int) {
78 if len(path) == 0 || start < 0 || start > len(path)-1 {
79 return "", -1
80 }
81 end := stringsscip-go gomod github.com/golang/go/src go1.19 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.19 strings/IndexRune().(path[start+1:], '.')
82 if end == -1 {
83 return path[start:], -1
84 }
85 return path[start : start+end+1], start + end + 1
86 }
87
88 func TestCustomPathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/TestCustomPathSegmenter().(t *testingscip-go gomod github.com/golang/go/src go1.19 testing/.Tscip-go gomod github.com/golang/go/src go1.19 testing/T#) {
89 depthTest := map[string]bool{
90 "a": false,
91 "a.b": false,
92 "a.b.c": false,
93 "a.b.c.d": false,
94 "a.b.c.d.e": false,
95 ".a": false,
96 ".a.b": false,
97 ".a.b.c": false,
98 ".a.b.c.d": false,
99 ".a.b.c.d.e": false,
100 }
101
102 tie := NewPathTrieWithConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrieWithConfig().(&PathTrieConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#{Segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#Segmenter.: testPathSegmenterDotscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/testPathSegmenterDot().})
103 for k := range depthTest {
104 tie.Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Put().(k, true)
105 }
106
107 for k := range depthTest {
108 tie.WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#WalkPath().(k, func(k string, v interface{}) error {
109 out := tie.Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Get().(k)
110 if out != nil {
111 depthTest[k] = true
112 }
113 return nil
114 })
115 }
116 for k, ok := range depthTest {
117 if !ok {
118 t.Errorfscip-go gomod github.com/golang/go/src go1.19 testing/common#Errorf().("did not walk thru %s node", k)
119 }
120 }
121
122 }