Skip to content

Commit

Permalink
test: improve coverage on hashmap package (#435)
Browse files Browse the repository at this point in the history
* test: improve coverage on hashmap package

* Updated Documentation in README.md

Co-authored-by: Michele Caci <[email protected]>
Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 12, 2021
1 parent eb39cb7 commit 28b4df5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.

##### Functions:

1. [`New`](./structure/hashmap/hashmap.go#L24): New return new HashMap instance
1. [`Make`](./structure/hashmap/hashmap.go#L32): Make creates a new HashMap instance with input size and capacity
2. [`New`](./structure/hashmap/hashmap.go#L24): New return new HashMap instance

---
##### Types
Expand Down
9 changes: 9 additions & 0 deletions structure/hashmap/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ func New() *HashMap {
}
}

// Make creates a new HashMap instance with input size and capacity
func Make(size, capacity uint64) HashMap {
return HashMap{
size: size,
capacity: capacity,
table: make([]*node, capacity),
}
}

// Get returns value associated with given key
func (hm *HashMap) Get(key interface{}) interface{} {
node := hm.getNodeByHash(hm.hash(key))
Expand Down
30 changes: 26 additions & 4 deletions structure/hashmap/hashmap_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package hashmap
package hashmap_test

import (
"testing"

"github.com/TheAlgorithms/Go/structure/hashmap"
)

func TestHashMap(t *testing.T) {

mp := New()
mp := hashmap.New()

t.Run("Test 1: Put(10) and checking if Get() is correct", func(t *testing.T) {
mp.Put("test", 10)
Expand Down Expand Up @@ -48,12 +50,32 @@ func TestHashMap(t *testing.T) {
}
})

t.Run("Test 6: Checking if the key that doesn't exists returns false", func(t *testing.T) {
t.Run("Test 6: Checking if the key that does not exist returns false", func(t *testing.T) {
want := false
got := mp.Contains(2)
if got != want {
t.Errorf("Key '2' doesn't exists in the map but it says otherwise")
t.Errorf("Key '2' does not exist in the map but it says otherwise")
}
})

t.Run("Test 7: Checking if the key does not exist Get func returns nil", func(t *testing.T) {
want := interface{}(nil)
got := mp.Get(2)
if got != want {
t.Errorf("Key '2' does not exists in the map but it says otherwise")
}
})

t.Run("Test 8: Resizing a map", func(t *testing.T) {
mp := hashmap.Make(4, 4)

for i := 0; i < 20; i++ {
mp.Put(i, 40)
}

got := mp.Get(5)
if got != 40 {
t.Errorf("Put: %v, Got: %v", got, 40)
}
})
}

0 comments on commit 28b4df5

Please sign in to comment.