Skip to content

Commit

Permalink
Merge pull request #44 from semihbkgr/fix-new-function
Browse files Browse the repository at this point in the history
Use only positive size value in the new function to prevent panics
  • Loading branch information
alphadose authored Dec 19, 2023
2 parents d41ce81 + 33dd687 commit 9300b14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9300b14

Please sign in to comment.