From 249e2f60913f016230fd8efc1a9571224339cac2 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Mon, 18 Dec 2023 20:36:47 +0300 Subject: [PATCH 1/2] check size parameter if positive value in new function --- map.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/map.go b/map.go index bf27f8d..99b69fd 100644 --- a/map.go +++ b/map.go @@ -64,7 +64,7 @@ type ( func New[K hashable, V any](size ...uintptr) *Map[K, V] { m := &Map[K, V]{listHead: newListHead[K, V]()} m.numItems.Store(0) - if len(size) > 0 { + if len(size) > 0 && size[0] > 0 { m.allocate(size[0]) } else { m.allocate(defaultSize) From 33dd687afd9934d23ece21df1fdda669cab9d2f5 Mon Sep 17 00:00:00 2001 From: semihbkgr Date: Mon, 18 Dec 2023 20:52:31 +0300 Subject: [PATCH 2/2] add test for creating map when zero size is provided --- e2e_test.go | 8 ++++++++ map.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/e2e_test.go b/e2e_test.go index a0c59fa..cbe71d2 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -19,6 +19,14 @@ func TestMapCreation(t *testing.T) { if m.Len() != 0 { t.Errorf("new map should be empty but has %d items.", m.Len()) } + + t.Run("default size is used when zero is provided", func(t *testing.T) { + m := New[int, int](0) + index := m.metadata.Load().index + if len(index) != defaultSize { + t.Error("map index size is not as expected") + } + }) } func TestOverwrite(t *testing.T) { diff --git a/map.go b/map.go index 99b69fd..dd2ff2a 100644 --- a/map.go +++ b/map.go @@ -64,7 +64,7 @@ type ( func New[K hashable, V any](size ...uintptr) *Map[K, V] { m := &Map[K, V]{listHead: newListHead[K, V]()} m.numItems.Store(0) - if len(size) > 0 && size[0] > 0 { + if len(size) > 0 && size[0] != 0 { m.allocate(size[0]) } else { m.allocate(defaultSize)