Enhance docs, fix & add tests in `GenericHashMapUsingArrayL… (#5973)

This commit is contained in:
Hardik Pawar
2024-10-26 22:15:16 +05:30
committed by GitHub
parent f5bc2c807d
commit bc6ea1ec87
2 changed files with 128 additions and 10 deletions

View File

@@ -50,4 +50,47 @@ class GenericHashMapUsingArrayListTest {
assertEquals("Washington DC", map.get(101));
assertTrue(map.containsKey(46));
}
@Test
void testRemoveNonExistentKey() {
GenericHashMapUsingArrayList<String, String> map = new GenericHashMapUsingArrayList<>();
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() {
GenericHashMapUsingArrayList<String, String> map = new GenericHashMapUsingArrayList<>();
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() {
GenericHashMapUsingArrayList<String, String> map = new GenericHashMapUsingArrayList<>();
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() {
GenericHashMapUsingArrayList<String, String> map = new GenericHashMapUsingArrayList<>();
map.put("USA", "Washington DC");
map.put("Nepal", "Kathmandu");
String expected = "{USA : Washington DC, Nepal : Kathmandu}";
assertEquals(expected, map.toString());
}
@Test
void testContainsKey() {
GenericHashMapUsingArrayList<String, String> map = new GenericHashMapUsingArrayList<>();
map.put("USA", "Washington DC");
assertTrue(map.containsKey("USA"));
assertFalse(map.containsKey("Nepal"));
}
}