From 9107eb4b32adee9ee32b1ca9e94bb14384a2f8e9 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 25 Aug 2022 14:04:26 +0800 Subject: [PATCH] fix: fix resize bug in HashMap --- datastructure/hashmap/hashmap.go | 2 ++ datastructure/hashmap/hashmap_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/datastructure/hashmap/hashmap.go b/datastructure/hashmap/hashmap.go index 12fd9b5..a7e5cd7 100644 --- a/datastructure/hashmap/hashmap.go +++ b/datastructure/hashmap/hashmap.go @@ -98,6 +98,8 @@ func (hm *HashMap) resize() { tempTable := hm.table + hm.table = make([]*mapNode, hm.capacity) + for i := 0; i < len(tempTable); i++ { node := tempTable[i] if node == nil { diff --git a/datastructure/hashmap/hashmap_test.go b/datastructure/hashmap/hashmap_test.go index e057faf..1994f6c 100644 --- a/datastructure/hashmap/hashmap_test.go +++ b/datastructure/hashmap/hashmap_test.go @@ -19,6 +19,18 @@ func TestHashMap_PutAndGet(t *testing.T) { assert.Equal(4, hm.Get("abc")) } +func TestHashMap_Resize(t *testing.T) { + assert := internal.NewAssert(t, "TestHashMap_Resize") + + hm := NewHashMapWithCapacity(3, 3) + + for i := 0; i < 20; i++ { + hm.Put(i, 10) + } + + assert.Equal(10, hm.Get(5)) +} + func TestHashMap_Delete(t *testing.T) { assert := internal.NewAssert(t, "TestHashMap_Delete")