| 1 |
package trie |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
|
| 9 |
func TestPathSegmenter(t *testing.T) { |
| 10 |
cases := []struct { |
| 11 |
key string |
| 12 |
parts []string |
| 13 |
indices []int |
| 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 := PathSegmenter(c.key, 0); ; prefix, i = PathSegmenter(c.key, i) { |
| 29 |
if prefix != c.parts[partNum] { |
| 30 |
t.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.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.Errorf("expected '%s' to have %d parts, got %d", c.key, len(c.parts), partNum) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestPathSegmenterEdgeCases(t *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 := PathSegmenter(c.path, c.start) |
| 68 |
if segment != c.segment { |
| 69 |
t.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.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 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 := 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 TestCustomPathSegmenter(t *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 := NewPathTrieWithConfig(&PathTrieConfig{Segmenter: testPathSegmenterDot}) |
| 103 |
for k := range depthTest { |
| 104 |
tie.Put(k, true) |
| 105 |
} |
| 106 |
|
| 107 |
for k := range depthTest { |
| 108 |
tie.WalkPath(k, func(k string, v interface{}) error { |
| 109 |
out := tie.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.Errorf("did not walk thru %s node", k) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
} |