Skip to content

Commit 9c3f2fa

Browse files
committed
refactor: Add more tests in HashMap
1 parent 5246f63 commit 9c3f2fa

File tree

2 files changed

+76
-17
lines changed
  • src
    • main/java/com/thealgorithms/datastructures/hashmap/hashing
    • test/java/com/thealgorithms/datastructures/hashmap/hashing

2 files changed

+76
-17
lines changed

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMap.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ public void display() {
8585
}
8686
}
8787

88+
/**
89+
* Clears the contents of the hash map by reinitializing each bucket.
90+
*/
91+
public void clear() {
92+
for (int i = 0; i < hashSize; i++) {
93+
buckets[i] = new LinkedList<>();
94+
}
95+
}
96+
97+
/**
98+
* Gets the number of key-value pairs in the hash map.
99+
*
100+
* @return the number of key-value pairs in the hash map
101+
*/
102+
public int size() {
103+
int size = 0;
104+
for (int i = 0; i < hashSize; i++) {
105+
size += buckets[i].isEmpty() ? 0 : 1;
106+
}
107+
return size;
108+
}
109+
88110
/**
89111
* A nested static class that represents a linked list used for separate chaining in the hash map.
90112
*

src/test/java/com/thealgorithms/datastructures/hashmap/hashing/HashMapTest.java

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.datastructures.hashmap.hashing;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertNull;
3+
import static org.junit.jupiter.api.Assertions.*;
54

65
import org.junit.jupiter.api.Test;
76

@@ -17,7 +16,7 @@ public void testInsertAndSearch() {
1716
assertEquals("Value15", hashMap.search(15));
1817
assertEquals("Value25", hashMap.search(25));
1918
assertEquals("Value35", hashMap.search(35));
20-
assertNull(hashMap.search(45));
19+
assertNull(hashMap.search(45)); // Test for non-existent key
2120
}
2221

2322
@Test
@@ -29,7 +28,7 @@ public void testDelete() {
2928

3029
assertEquals("Value25", hashMap.search(25));
3130
hashMap.delete(25);
32-
assertNull(hashMap.search(25));
31+
assertNull(hashMap.search(25)); // Confirm deletion
3332
}
3433

3534
@Test
@@ -38,21 +37,22 @@ public void testDisplay() {
3837
hashMap.insert(15, "Value15");
3938
hashMap.insert(25, "Value25");
4039
hashMap.insert(35, "Value35");
41-
hashMap.display();
40+
// Optionally verify display functionality if it returns a string
41+
hashMap.display(); // Manual check during test execution
4242
}
4343

4444
@Test
4545
public void testInsertNullKey() {
4646
HashMap<Integer, String> hashMap = new HashMap<>(10);
4747
hashMap.insert(null, "NullValue");
48-
assertEquals("NullValue", hashMap.search(null));
48+
assertEquals("NullValue", hashMap.search(null)); // Verify null key handling
4949
}
5050

5151
@Test
5252
public void testInsertNullValue() {
5353
HashMap<Integer, String> hashMap = new HashMap<>(10);
5454
hashMap.insert(15, null);
55-
assertNull(hashMap.search(15));
55+
assertNull(hashMap.search(15)); // Verify null value handling
5656
}
5757

5858
@Test
@@ -61,12 +61,12 @@ public void testUpdateExistingKey() {
6161
hashMap.insert(15, "Value15");
6262
hashMap.insert(15, "UpdatedValue15");
6363

64-
assertEquals("UpdatedValue15", hashMap.search(15));
64+
assertEquals("UpdatedValue15", hashMap.search(15)); // Verify update
6565
}
6666

6767
@Test
6868
public void testHandleCollisions() {
69-
HashMap<Integer, String> hashMap = new HashMap<>(3);
69+
HashMap<Integer, String> hashMap = new HashMap<>(3); // Create a small bucket size to force collisions
7070
// These keys should collide if the hash function is modulo 3
7171
hashMap.insert(1, "Value1");
7272
hashMap.insert(4, "Value4");
@@ -80,17 +80,17 @@ public void testHandleCollisions() {
8080
@Test
8181
public void testSearchInEmptyHashMap() {
8282
HashMap<Integer, String> hashMap = new HashMap<>(10);
83-
assertNull(hashMap.search(10));
83+
assertNull(hashMap.search(10)); // Confirm search returns null in empty map
8484
}
8585

8686
@Test
8787
public void testDeleteNonExistentKey() {
8888
HashMap<Integer, String> hashMap = new HashMap<>(10);
8989
hashMap.insert(15, "Value15");
90-
hashMap.delete(25);
90+
hashMap.delete(25); // Delete non-existent key
9191

92-
assertEquals("Value15", hashMap.search(15));
93-
assertNull(hashMap.search(25));
92+
assertEquals("Value15", hashMap.search(15)); // Ensure existing key remains
93+
assertNull(hashMap.search(25)); // Confirm non-existent key remains null
9494
}
9595

9696
@Test
@@ -101,7 +101,7 @@ public void testInsertLargeNumberOfElements() {
101101
}
102102

103103
for (int i = 0; i < 100; i++) {
104-
assertEquals("Value" + i, hashMap.search(i));
104+
assertEquals("Value" + i, hashMap.search(i)); // Verify all inserted values
105105
}
106106
}
107107

@@ -113,7 +113,7 @@ public void testDeleteHeadOfBucket() {
113113
hashMap.insert(7, "Value7");
114114

115115
hashMap.delete(1);
116-
assertNull(hashMap.search(1));
116+
assertNull(hashMap.search(1)); // Verify head deletion
117117
assertEquals("Value4", hashMap.search(4));
118118
assertEquals("Value7", hashMap.search(7));
119119
}
@@ -126,7 +126,7 @@ public void testDeleteTailOfBucket() {
126126
hashMap.insert(7, "Value7");
127127

128128
hashMap.delete(7);
129-
assertNull(hashMap.search(7));
129+
assertNull(hashMap.search(7)); // Verify tail deletion
130130
assertEquals("Value1", hashMap.search(1));
131131
assertEquals("Value4", hashMap.search(4));
132132
}
@@ -139,8 +139,45 @@ public void testDeleteMiddleElementOfBucket() {
139139
hashMap.insert(7, "Value7");
140140

141141
hashMap.delete(4);
142-
assertNull(hashMap.search(4));
142+
assertNull(hashMap.search(4)); // Verify middle element deletion
143143
assertEquals("Value1", hashMap.search(1));
144144
assertEquals("Value7", hashMap.search(7));
145145
}
146+
147+
@Test
148+
public void testResizeHashMap() {
149+
HashMap<Integer, String> hashMap = new HashMap<>(2); // Small initial size to force rehashing
150+
for (int i = 0; i < 10; i++) {
151+
hashMap.insert(i, "Value" + i);
152+
}
153+
154+
// Verify all values after resizing
155+
for (int i = 0; i < 10; i++) {
156+
assertEquals("Value" + i, hashMap.search(i));
157+
}
158+
}
159+
160+
@Test
161+
public void testCollisionResolution() {
162+
HashMap<String, String> hashMap = new HashMap<>(3);
163+
hashMap.insert("abc", "Value1"); // Hash index 0
164+
hashMap.insert("cab", "Value2"); // Hash index 0 (collision)
165+
hashMap.insert("bac", "Value3"); // Hash index 0 (collision)
166+
167+
assertEquals("Value1", hashMap.search("abc"));
168+
assertEquals("Value2", hashMap.search("cab"));
169+
assertEquals("Value3", hashMap.search("bac"));
170+
}
171+
172+
@Test
173+
public void testClearHashMap() {
174+
HashMap<Integer, String> hashMap = new HashMap<>(10);
175+
hashMap.insert(1, "Value1");
176+
hashMap.insert(2, "Value2");
177+
178+
hashMap.clear(); // Assuming clear method resets the hash map
179+
assertNull(hashMap.search(1));
180+
assertNull(hashMap.search(2));
181+
assertEquals(0, hashMap.size()); // Verify size is reset
182+
}
146183
}

0 commit comments

Comments
 (0)