Skip to content

Commit

Permalink
Fix null as a key (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: plar <[email protected]>
  • Loading branch information
plar authored Dec 8, 2022
1 parent 8c34d05 commit 154dc06
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ func (an *artNode) shrink() *artNode {

// Leaf methods
func (l *leaf) match(key Key) bool {
if len(key) == 0 && len(l.key) == 0 {
return true
}

if key == nil || len(l.key) != len(key) {
return false
}
Expand Down
34 changes: 32 additions & 2 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,9 +1076,7 @@ func TestTreeAPI(t *testing.T) {
assert.True(t, found)
}

//
// Benchmarks
//
func BenchmarkWordsTreeInsert(b *testing.B) {
words := loadTestFile("test/assets/words.txt")
b.ResetTimer()
Expand Down Expand Up @@ -1495,3 +1493,35 @@ func TestTreeInsertAndSearchKeyWithUnicodeAccentChar(t *testing.T) {
assert.True(t, found)
assert.Equal(t, string(accent), v)
}

func TestTreeInsertNilKeyTwice(t *testing.T) {
tree := newTree()

kk := Key("key")
kv := "kk-value"
old, updated := tree.Insert(kk, kv)
assert.Nil(t, old)
assert.False(t, updated)
v, found := tree.Search(kk)
assert.Equal(t, kv, v)
assert.True(t, found)

knil := Key(nil)
knilv0 := "knil-value-0"
old, updated = tree.Insert(knil, knilv0)
assert.Nil(t, old)
assert.False(t, updated)

v, found = tree.Search(knil)
assert.Equal(t, knilv0, v)
assert.True(t, found)

knilv1 := "knil-value-1"
old, updated = tree.Insert(knil, knilv1)
assert.Equal(t, knilv0, old)
assert.True(t, updated)

v, found = tree.Search(knil)
assert.Equal(t, knilv1, v)
assert.True(t, found)
}

0 comments on commit 154dc06

Please sign in to comment.