Enhance docs, add tests in GenericHashMapUsingArray (#5972)

This commit is contained in:
Hardik Pawar
2024-10-30 00:21:27 +05:30
committed by GitHub
parent fd14016c86
commit 63d13b6f3a
2 changed files with 140 additions and 21 deletions

View File

@@ -50,4 +50,47 @@ class GenericHashMapUsingArrayTest {
assertEquals("Washington DC", map.get(101));
assertTrue(map.containsKey(46));
}
@Test
void testRemoveNonExistentKey() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("USA", "Washington DC");
map.remove("Nepal"); // Attempting to remove a non-existent key
assertEquals(1, map.size()); // Size should remain the same
}
@Test
void testRehashing() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
for (int i = 0; i < 20; i++) {
map.put("Key" + i, "Value" + i);
}
assertEquals(20, map.size()); // Ensure all items were added
assertEquals("Value5", map.get("Key5")); // Check retrieval after rehash
}
@Test
void testUpdateValueForExistingKey() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("USA", "Washington DC");
map.put("USA", "New Washington DC"); // Updating value for existing key
assertEquals("New Washington DC", map.get("USA"));
}
@Test
void testToStringMethod() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("USA", "Washington DC");
map.put("Nepal", "Kathmandu");
String expected = "{USA : Washington DC, Nepal : Kathmandu}";
assertEquals(expected, map.toString());
}
@Test
void testContainsKey() {
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
map.put("USA", "Washington DC");
assertTrue(map.containsKey("USA"));
assertFalse(map.containsKey("Nepal"));
}
}