Skip to content

Commit f07cbe3

Browse files
Merge pull request #198 from dzyp/unit-tests
RM-30900 Ctrie hash collisions
2 parents 8ba3709 + 8816099 commit f07cbe3

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

trie/ctrie/ctrie.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (l *lNode) lookup(e *Entry) (interface{}, bool) {
229229

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

235235
// removed creates a new L-node with the entry removed.

trie/ctrie/ctrie_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,51 @@ func TestClear(t *testing.T) {
429429
assert.Equal(uint(10), snapshot.Size())
430430
}
431431

432+
type fakehash struct{}
433+
434+
func (h *fakehash) Sum32() uint32 {
435+
return 42
436+
}
437+
438+
func (h *fakehash) Sum(b []byte) []byte {
439+
return nil
440+
}
441+
442+
func (h *fakehash) Size() int {
443+
return 0
444+
}
445+
446+
func (h *fakehash) BlockSize() int {
447+
return 0
448+
}
449+
450+
func (h *fakehash) Reset() {
451+
452+
}
453+
454+
func (h *fakehash) Write(b []byte) (int, error) {
455+
return 0, nil
456+
}
457+
458+
func factory() hash.Hash32 {
459+
return &fakehash{}
460+
}
461+
462+
func TestHashCollision(t *testing.T) {
463+
trie := New(factory)
464+
trie.Insert([]byte("foobar"), 1)
465+
trie.Insert([]byte("zogzog"), 2)
466+
trie.Insert([]byte("foobar"), 3)
467+
val, exists := trie.Lookup([]byte("foobar"))
468+
assert.True(t, exists)
469+
assert.Equal(t, 3, val)
470+
471+
trie.Remove([]byte("foobar"))
472+
473+
_, exists = trie.Lookup([]byte("foobar"))
474+
assert.False(t, exists)
475+
}
476+
432477
func BenchmarkInsert(b *testing.B) {
433478
ctrie := New(nil)
434479
b.ResetTimer()

0 commit comments

Comments
 (0)