Skip to content

Commit

Permalink
Merge pull request #127 from Workiva/fix_avl
Browse files Browse the repository at this point in the history
Fix deletion bug due to incorrect node copy.
  • Loading branch information
dustinhiatt-wf committed Jan 5, 2016
2 parents 371ee25 + d50a4a7 commit 6519136
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tree/avl/avl.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (immutable *Immutable) delete(entry Entry) Entry {
}
it = it.copy() // the node we found needs to be copied

oldTop := top
if it.children[0] == nil || it.children[1] == nil { // need to set children on parent, splicing out
dir = intFromBool(it.children[0] == nil)
if top != 0 {
Expand All @@ -272,6 +273,11 @@ func (immutable *Immutable) delete(entry Entry) Entry {
}

it.entry = heir.entry
if oldTop != 0 {
cache[oldTop-1].children[dirs[oldTop-1]] = it
} else {
immutable.root = it
}
cache[top-1].children[intFromBool(cache[top-1] == it)] = heir.children[1]
}

Expand Down
23 changes: 23 additions & 0 deletions tree/avl/avl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,29 @@ func TestAVLDeleteReplay(t *testing.T) {
assert.Equal(t, uint64(4), i2.Len())
}

func TestAVLFails(t *testing.T) {
keys := []mockEntry{
mockEntry(0),
mockEntry(1),
mockEntry(3),
mockEntry(4),
mockEntry(5),
mockEntry(6),
mockEntry(7),
mockEntry(2),
}
i1 := NewImmutable()
for _, k := range keys {
i1, _ = i1.Insert(k)
}

for _, k := range keys {
var deleted Entries
i1, deleted = i1.Delete(k)
assert.Equal(t, Entries{k}, deleted)
}
}

func BenchmarkImmutableInsert(b *testing.B) {
numItems := b.N
sl := NewImmutable()
Expand Down

0 comments on commit 6519136

Please sign in to comment.