Skip to content

Commit f5bc2c8

Browse files
authored
Add more tests in HashMap (#5974)
1 parent cd40dfb commit f5bc2c8

File tree

2 files changed

+75
-15
lines changed
  • src
    • main/java/com/thealgorithms/datastructures/hashmap/hashing
    • test/java/com/thealgorithms/datastructures/hashmap/hashing

2 files changed

+75
-15
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: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void testInsertAndSearch() {
1717
assertEquals("Value15", hashMap.search(15));
1818
assertEquals("Value25", hashMap.search(25));
1919
assertEquals("Value35", hashMap.search(35));
20-
assertNull(hashMap.search(45));
20+
assertNull(hashMap.search(45)); // Test for non-existent key
2121
}
2222

2323
@Test
@@ -29,7 +29,7 @@ public void testDelete() {
2929

3030
assertEquals("Value25", hashMap.search(25));
3131
hashMap.delete(25);
32-
assertNull(hashMap.search(25));
32+
assertNull(hashMap.search(25)); // Confirm deletion
3333
}
3434

3535
@Test
@@ -38,21 +38,22 @@ public void testDisplay() {
3838
hashMap.insert(15, "Value15");
3939
hashMap.insert(25, "Value25");
4040
hashMap.insert(35, "Value35");
41-
hashMap.display();
41+
// Optionally verify display functionality if it returns a string
42+
hashMap.display(); // Manual check during test execution
4243
}
4344

4445
@Test
4546
public void testInsertNullKey() {
4647
HashMap<Integer, String> hashMap = new HashMap<>(10);
4748
hashMap.insert(null, "NullValue");
48-
assertEquals("NullValue", hashMap.search(null));
49+
assertEquals("NullValue", hashMap.search(null)); // Verify null key handling
4950
}
5051

5152
@Test
5253
public void testInsertNullValue() {
5354
HashMap<Integer, String> hashMap = new HashMap<>(10);
5455
hashMap.insert(15, null);
55-
assertNull(hashMap.search(15));
56+
assertNull(hashMap.search(15)); // Verify null value handling
5657
}
5758

5859
@Test
@@ -61,12 +62,12 @@ public void testUpdateExistingKey() {
6162
hashMap.insert(15, "Value15");
6263
hashMap.insert(15, "UpdatedValue15");
6364

64-
assertEquals("UpdatedValue15", hashMap.search(15));
65+
assertEquals("UpdatedValue15", hashMap.search(15)); // Verify update
6566
}
6667

6768
@Test
6869
public void testHandleCollisions() {
69-
HashMap<Integer, String> hashMap = new HashMap<>(3);
70+
HashMap<Integer, String> hashMap = new HashMap<>(3); // Create a small bucket size to force collisions
7071
// These keys should collide if the hash function is modulo 3
7172
hashMap.insert(1, "Value1");
7273
hashMap.insert(4, "Value4");
@@ -80,17 +81,17 @@ public void testHandleCollisions() {
8081
@Test
8182
public void testSearchInEmptyHashMap() {
8283
HashMap<Integer, String> hashMap = new HashMap<>(10);
83-
assertNull(hashMap.search(10));
84+
assertNull(hashMap.search(10)); // Confirm search returns null in empty map
8485
}
8586

8687
@Test
8788
public void testDeleteNonExistentKey() {
8889
HashMap<Integer, String> hashMap = new HashMap<>(10);
8990
hashMap.insert(15, "Value15");
90-
hashMap.delete(25);
91+
hashMap.delete(25); // Delete non-existent key
9192

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

9697
@Test
@@ -101,7 +102,7 @@ public void testInsertLargeNumberOfElements() {
101102
}
102103

103104
for (int i = 0; i < 100; i++) {
104-
assertEquals("Value" + i, hashMap.search(i));
105+
assertEquals("Value" + i, hashMap.search(i)); // Verify all inserted values
105106
}
106107
}
107108

@@ -113,7 +114,7 @@ public void testDeleteHeadOfBucket() {
113114
hashMap.insert(7, "Value7");
114115

115116
hashMap.delete(1);
116-
assertNull(hashMap.search(1));
117+
assertNull(hashMap.search(1)); // Verify head deletion
117118
assertEquals("Value4", hashMap.search(4));
118119
assertEquals("Value7", hashMap.search(7));
119120
}
@@ -126,7 +127,7 @@ public void testDeleteTailOfBucket() {
126127
hashMap.insert(7, "Value7");
127128

128129
hashMap.delete(7);
129-
assertNull(hashMap.search(7));
130+
assertNull(hashMap.search(7)); // Verify tail deletion
130131
assertEquals("Value1", hashMap.search(1));
131132
assertEquals("Value4", hashMap.search(4));
132133
}
@@ -139,8 +140,45 @@ public void testDeleteMiddleElementOfBucket() {
139140
hashMap.insert(7, "Value7");
140141

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

0 commit comments

Comments
 (0)