From 2acb6d16e408346d2d7e0147c8eef0cb9bf3fa45 Mon Sep 17 00:00:00 2001 From: rench1988 Date: Fri, 22 Jan 2016 10:54:25 +0800 Subject: [PATCH 1/2] crash test --- trie_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/trie_test.go b/trie_test.go index 290773d..e4fbdfe 100644 --- a/trie_test.go +++ b/trie_test.go @@ -61,6 +61,25 @@ func TestTrieFindMissing(t *testing.T) { } } +func TestRemove(t *testing.T) { + trie := New() + initial := []string{"football"} + + for _, key := range initial { + trie.Add(key, nil) + } + + trie.Remove("football") + keys := trie.Keys() + + if len(keys) != 0 { + t.Errorf("Expected 0 keys got %d", len(keys)) + } + + trie.Remove("aabb") //this will core dump +} + +/* func TestRemove(t *testing.T) { trie := New() initial := []string{"football", "foostar", "foosball"} @@ -93,6 +112,7 @@ func TestRemove(t *testing.T) { } } } +*/ func TestTrieKeys(t *testing.T) { trie := New() From 9db3ab5ddde90f3d46c6ae5565bf3ac8122dee1c Mon Sep 17 00:00:00 2001 From: rench1988 Date: Mon, 25 Jan 2016 10:06:11 +0800 Subject: [PATCH 2/2] bug modify --- trie.go | 13 ++++++++++--- trie_test.go | 20 -------------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/trie.go b/trie.go index cfcddc9..db2d8c7 100644 --- a/trie.go +++ b/trie.go @@ -78,7 +78,7 @@ func (t *Trie) Find(key string) (*Node, bool) { } node = node.Children()[nul] - if !node.term { + if node == nil || !node.term { return nil, false } @@ -87,13 +87,17 @@ func (t *Trie) Find(key string) (*Node, bool) { // Removes a key from the trie, ensuring that // all bitmasks up to root are appropriately recalculated. -func (t *Trie) Remove(key string) { +func (t *Trie) Remove(key string) bool { var ( i int rs = []rune(key) node = findNode(t.Root(), []rune(key)) ) + if node == nil { + return false + } + t.size-- for n := node.Parent(); n != nil; n = n.Parent() { i++ @@ -103,6 +107,8 @@ func (t *Trie) Remove(key string) { break } } + + return true } // Returns all the keys currently stored in the trie. @@ -145,7 +151,8 @@ func (n *Node) NewChild(val rune, bitmask uint64, meta interface{}, term bool) * func (n *Node) RemoveChild(r rune) { delete(n.children, r) - for nd := n.parent; nd != nil; nd = nd.parent { + + for nd := n; nd != nil; nd = nd.parent { nd.mask ^= nd.mask nd.mask |= uint64(1) << uint64(nd.val-'a') for _, c := range nd.children { diff --git a/trie_test.go b/trie_test.go index e4fbdfe..290773d 100644 --- a/trie_test.go +++ b/trie_test.go @@ -61,25 +61,6 @@ func TestTrieFindMissing(t *testing.T) { } } -func TestRemove(t *testing.T) { - trie := New() - initial := []string{"football"} - - for _, key := range initial { - trie.Add(key, nil) - } - - trie.Remove("football") - keys := trie.Keys() - - if len(keys) != 0 { - t.Errorf("Expected 0 keys got %d", len(keys)) - } - - trie.Remove("aabb") //this will core dump -} - -/* func TestRemove(t *testing.T) { trie := New() initial := []string{"football", "foostar", "foosball"} @@ -112,7 +93,6 @@ func TestRemove(t *testing.T) { } } } -*/ func TestTrieKeys(t *testing.T) { trie := New()