mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Enhance docs, fix & add tests in `GenericHashMapUsingArrayL… (#5973)
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user