Skip to content

Commit

Permalink
Merge pull request #198 from dzyp/unit-tests
Browse files Browse the repository at this point in the history
RM-30900 Ctrie hash collisions
  • Loading branch information
dustinhiatt-wf authored Aug 29, 2018
2 parents 8ba3709 + 8816099 commit f07cbe3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion trie/ctrie/ctrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (l *lNode) lookup(e *Entry) (interface{}, bool) {

// inserted creates a new L-node with the added entry.
func (l *lNode) inserted(entry *Entry) *lNode {
return &lNode{l.Add(&sNode{entry})}
return &lNode{l.removed(entry).Add(&sNode{entry})}
}

// removed creates a new L-node with the entry removed.
Expand Down
45 changes: 45 additions & 0 deletions trie/ctrie/ctrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,51 @@ func TestClear(t *testing.T) {
assert.Equal(uint(10), snapshot.Size())
}

type fakehash struct{}

func (h *fakehash) Sum32() uint32 {
return 42
}

func (h *fakehash) Sum(b []byte) []byte {
return nil
}

func (h *fakehash) Size() int {
return 0
}

func (h *fakehash) BlockSize() int {
return 0
}

func (h *fakehash) Reset() {

}

func (h *fakehash) Write(b []byte) (int, error) {
return 0, nil
}

func factory() hash.Hash32 {
return &fakehash{}
}

func TestHashCollision(t *testing.T) {
trie := New(factory)
trie.Insert([]byte("foobar"), 1)
trie.Insert([]byte("zogzog"), 2)
trie.Insert([]byte("foobar"), 3)
val, exists := trie.Lookup([]byte("foobar"))
assert.True(t, exists)
assert.Equal(t, 3, val)

trie.Remove([]byte("foobar"))

_, exists = trie.Lookup([]byte("foobar"))
assert.False(t, exists)
}

func BenchmarkInsert(b *testing.B) {
ctrie := New(nil)
b.ResetTimer()
Expand Down

0 comments on commit f07cbe3

Please sign in to comment.