Skip to content

refactor: Add more tests in HashMap #5974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ public void display() {
}
}

/**
* Clears the contents of the hash map by reinitializing each bucket.
*/
public void clear() {
for (int i = 0; i < hashSize; i++) {
buckets[i] = new LinkedList<>();
}
}

/**
* Gets the number of key-value pairs in the hash map.
*
* @return the number of key-value pairs in the hash map
*/
public int size() {
int size = 0;
for (int i = 0; i < hashSize; i++) {
size += buckets[i].isEmpty() ? 0 : 1;
}
return size;
}

/**
* A nested static class that represents a linked list used for separate chaining in the hash map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void testInsertAndSearch() {
assertEquals("Value15", hashMap.search(15));
assertEquals("Value25", hashMap.search(25));
assertEquals("Value35", hashMap.search(35));
assertNull(hashMap.search(45));
assertNull(hashMap.search(45)); // Test for non-existent key
}

@Test
Expand All @@ -29,7 +29,7 @@ public void testDelete() {

assertEquals("Value25", hashMap.search(25));
hashMap.delete(25);
assertNull(hashMap.search(25));
assertNull(hashMap.search(25)); // Confirm deletion
}

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

@Test
public void testInsertNullKey() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(null, "NullValue");
assertEquals("NullValue", hashMap.search(null));
assertEquals("NullValue", hashMap.search(null)); // Verify null key handling
}

@Test
public void testInsertNullValue() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(15, null);
assertNull(hashMap.search(15));
assertNull(hashMap.search(15)); // Verify null value handling
}

@Test
Expand All @@ -61,12 +62,12 @@ public void testUpdateExistingKey() {
hashMap.insert(15, "Value15");
hashMap.insert(15, "UpdatedValue15");

assertEquals("UpdatedValue15", hashMap.search(15));
assertEquals("UpdatedValue15", hashMap.search(15)); // Verify update
}

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

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

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

@Test
Expand All @@ -101,7 +102,7 @@ public void testInsertLargeNumberOfElements() {
}

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

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

hashMap.delete(1);
assertNull(hashMap.search(1));
assertNull(hashMap.search(1)); // Verify head deletion
assertEquals("Value4", hashMap.search(4));
assertEquals("Value7", hashMap.search(7));
}
Expand All @@ -126,7 +127,7 @@ public void testDeleteTailOfBucket() {
hashMap.insert(7, "Value7");

hashMap.delete(7);
assertNull(hashMap.search(7));
assertNull(hashMap.search(7)); // Verify tail deletion
assertEquals("Value1", hashMap.search(1));
assertEquals("Value4", hashMap.search(4));
}
Expand All @@ -139,8 +140,45 @@ public void testDeleteMiddleElementOfBucket() {
hashMap.insert(7, "Value7");

hashMap.delete(4);
assertNull(hashMap.search(4));
assertNull(hashMap.search(4)); // Verify middle element deletion
assertEquals("Value1", hashMap.search(1));
assertEquals("Value7", hashMap.search(7));
}

@Test
public void testResizeHashMap() {
HashMap<Integer, String> hashMap = new HashMap<>(2); // Small initial size to force rehashing
for (int i = 0; i < 10; i++) {
hashMap.insert(i, "Value" + i);
}

// Verify all values after resizing
for (int i = 0; i < 10; i++) {
assertEquals("Value" + i, hashMap.search(i));
}
}

@Test
public void testCollisionResolution() {
HashMap<String, String> hashMap = new HashMap<>(3);
hashMap.insert("abc", "Value1"); // Hash index 0
hashMap.insert("cab", "Value2"); // Hash index 0 (collision)
hashMap.insert("bac", "Value3"); // Hash index 0 (collision)

assertEquals("Value1", hashMap.search("abc"));
assertEquals("Value2", hashMap.search("cab"));
assertEquals("Value3", hashMap.search("bac"));
}

@Test
public void testClearHashMap() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(1, "Value1");
hashMap.insert(2, "Value2");

hashMap.clear(); // Assuming clear method resets the hash map
assertNull(hashMap.search(1));
assertNull(hashMap.search(2));
assertEquals(0, hashMap.size()); // Verify size is reset
}
}