Skip to content

Commit

Permalink
Multi-find function bug fixes
Browse files Browse the repository at this point in the history
When a terminal tree node is unambiguously found in the tree, multi-find
functions do not return additional child nodes.
  • Loading branch information
beevik committed Jun 27, 2023
1 parent 1430afe commit 739c7ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
9 changes: 9 additions & 0 deletions prefixtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func (t *Tree) FindKeys(prefix string) (keys []string) {
if err == ErrPrefixNotFound {
return []string{}
}
if st.isTerminal() && err != ErrPrefixAmbiguous {
return []string{st.key}
}
return appendDescendantKeys(st, nil)
}

Expand All @@ -127,6 +130,9 @@ func (t *Tree) FindKeyValues(prefix string) (values []KeyValue) {
if err == ErrPrefixNotFound {
return []KeyValue{}
}
if st.isTerminal() && err != ErrPrefixAmbiguous {
return []KeyValue{{st.key, st.value}}
}
return appendDescendantKeyValues(st, nil)
}

Expand All @@ -137,6 +143,9 @@ func (t *Tree) FindValues(prefix string) (values []any) {
if err == ErrPrefixNotFound {
return []any{}
}
if st.isTerminal() && err != ErrPrefixAmbiguous {
return []any{st.value}
}
return appendDescendantValues(st, nil)
}

Expand Down
36 changes: 18 additions & 18 deletions prefixtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func TestFindKeys(t *testing.T) {
keys []string
}{
{"", []string{"a", "apple", "applepie", "arm", "bee", "bog"}},
{"a", []string{"a", "apple", "applepie", "arm"}},
{"a", []string{"a"}},
{"ap", []string{"apple", "applepie"}},
{"app", []string{"apple", "applepie"}},
{"appl", []string{"apple", "applepie"}},
{"apple", []string{"apple", "applepie"}},
{"apple", []string{"apple"}},
{"applep", []string{"applepie"}},
{"applepi", []string{"applepie"}},
{"applepie", []string{"applepie"}},
Expand All @@ -221,7 +221,7 @@ func TestFindKeys(t *testing.T) {
{"c", []string{}},
}

for _, c := range cases {
for i, c := range cases {
keys := tree.FindKeys(c.prefix)
match := false
if len(keys) == len(c.keys) {
Expand All @@ -235,13 +235,13 @@ func TestFindKeys(t *testing.T) {
}

if !match {
t.Errorf("FindAllValues(\"%s\") returned %v, expected %v.\n",
c.prefix, keys, c.keys)
t.Errorf("Case %d: FindKeys(\"%s\") returned %v, expected %v.\n",
i, c.prefix, keys, c.keys)
}
}
}

func TestFindAll(t *testing.T) {
func TestFindValues(t *testing.T) {
entries := []entry{
{"apple", 1},
{"applepie", 2},
Expand All @@ -260,11 +260,11 @@ func TestFindAll(t *testing.T) {
values []any
}{
{"", []any{3, 1, 2, 4, 5}},
{"a", []any{3, 1, 2, 4}},
{"a", []any{3}},
{"ap", []any{1, 2}},
{"app", []any{1, 2}},
{"appl", []any{1, 2}},
{"apple", []any{1, 2}},
{"apple", []any{1}},
{"applep", []any{2}},
{"applepi", []any{2}},
{"applepie", []any{2}},
Expand All @@ -279,7 +279,7 @@ func TestFindAll(t *testing.T) {
{"c", []any{}},
}

for _, c := range cases {
for i, c := range cases {
values := tree.FindValues(c.key)
match := false
if len(values) == len(c.values) {
Expand All @@ -293,8 +293,8 @@ func TestFindAll(t *testing.T) {
}

if !match {
t.Errorf("FindAllValues(\"%s\") returned %v, expected %v.\n",
c.key, values, c.values)
t.Errorf("Case %d: FindValues(\"%s\") returned %v, expected %v.\n",
i, c.key, values, c.values)
}
}
}
Expand All @@ -314,11 +314,11 @@ func TestMatchingChars(t *testing.T) {
{"apple", "a", 1},
{"apple", "bag", 0},
}
for _, c := range cases {
for i, c := range cases {
r := matchingChars(c.s1, c.s2)
if r != c.result {
t.Errorf("matchingChars(\"%s\", \"%s\") returned %d, expected %d\n",
c.s1, c.s2, r, c.result)
t.Errorf("Case %d: matchingChars(\"%s\", \"%s\") returned %d, expected %d\n",
i, c.s1, c.s2, r, c.result)
}
}
}
Expand Down Expand Up @@ -351,10 +351,10 @@ func TestDictionary(t *testing.T) {
"diametricall",
"diametrically",
}
for _, key := range keys {
for i, key := range keys {
_, err := tree.Find(key)
if err != nil {
t.Errorf("Find(\"%s\") encountered error: %v\n", key, err)
t.Errorf("Case %d: Find(\"%s\") encountered error: %v\n", i, key, err)
}
}

Expand All @@ -365,10 +365,10 @@ func TestDictionary(t *testing.T) {
"de",
"dea",
}
for _, key := range keys {
for i, key := range keys {
_, err := tree.Find(key)
if err != ErrPrefixAmbiguous {
t.Errorf("Find(\"%s\") should have been ambiguous\n", key)
t.Errorf("Case %d: Find(\"%s\") should have been ambiguous\n", i, key)
}
}
}
Expand Down

0 comments on commit 739c7ef

Please sign in to comment.