Skip to content

Commit 925fe1b

Browse files
author
Dustin Hiatt
committed
Add unit tests for hash collision.
1 parent 3a90759 commit 925fe1b

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

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)