From c8fd59fb6a1876d40d258d9f6a317ad2daa7174d Mon Sep 17 00:00:00 2001 From: lmj798 <2757400745@qq.com> Date: Thu, 5 Mar 2026 21:29:42 +0800 Subject: [PATCH 1/5] test: enhance GenericHashMapUsingArrayTest with additional edge case coverage --- .../hashing/GenericHashMapUsingArrayTest.java | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java index 5d1733a3e97c..a341edb80c80 100644 --- a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java +++ b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java @@ -3,9 +3,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; class GenericHashMapUsingArrayTest { @@ -93,4 +98,143 @@ void testContainsKey() { assertTrue(map.containsKey("USA")); assertFalse(map.containsKey("Nepal")); } + + // ======= Added tests from the new version ======= + + @Test + void shouldThrowNullPointerExceptionForNullKey() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + assertThrows(NullPointerException.class, () -> map.put(null, "value")); + } + + @Test + void shouldStoreNullValueForKey() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put("keyWithNullValue", null); + assertEquals(1, map.size()); + assertNull(map.get("keyWithNullValue")); + // Note: containsKey returns false for null values due to implementation + assertFalse(map.containsKey("keyWithNullValue")); + } + + @Test + void shouldHandleCollisionWhenKeysHashToSameBucket() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + Integer key1 = 1; + Integer key2 = 17; + map.put(key1, 100); + map.put(key2, 200); + assertEquals(2, map.size()); + assertEquals(100, map.get(key1)); + assertEquals(200, map.get(key2)); + assertTrue(map.containsKey(key1)); + assertTrue(map.containsKey(key2)); + } + + @Test + void shouldHandleEmptyStringAsKey() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put("", "valueForEmptyKey"); + assertEquals(1, map.size()); + assertEquals("valueForEmptyKey", map.get("")); + assertTrue(map.containsKey("")); + } + + @Test + void shouldHandleEmptyStringAsValue() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put("keyForEmptyValue", ""); + assertEquals(1, map.size()); + assertEquals("", map.get("keyForEmptyValue")); + assertTrue(map.containsKey("keyForEmptyValue")); + } + + @Test + void shouldHandleNegativeIntegerKeys() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put(-1, 100); + map.put(-100, 200); + assertEquals(2, map.size()); + assertEquals(100, map.get(-1)); + assertEquals(200, map.get(-100)); + assertTrue(map.containsKey(-1)); + assertTrue(map.containsKey(-100)); + } + + @Test + void shouldHandleZeroAsKey() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put(0, 100); + assertEquals(1, map.size()); + assertEquals(100, map.get(0)); + assertTrue(map.containsKey(0)); + } + + @Test + void shouldHandleStringWithSpecialCharacters() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put("key!@#$%^&*()", "value<>?/\\|"); + assertEquals(1, map.size()); + assertEquals("value<>?/\\|", map.get("key!@#$%^&*()")); + assertTrue(map.containsKey("key!@#$%^&*()")); + } + + @Test + void shouldHandleLongStrings() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + StringBuilder longKey = new StringBuilder(); + StringBuilder longValue = new StringBuilder(); + for (int i = 0; i < 1000; i++) { + longKey.append("a"); + longValue.append("b"); + } + String key = longKey.toString(); + String value = longValue.toString(); + map.put(key, value); + assertEquals(1, map.size()); + assertEquals(value, map.get(key)); + assertTrue(map.containsKey(key)); + } + + @ParameterizedTest + @ValueSource(strings = {"a", "ab", "abc", "test", "longerString"}) + void shouldHandleKeysOfDifferentLengths(String key) { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + map.put(key, "value"); + assertEquals(1, map.size()); + assertEquals("value", map.get(key)); + assertTrue(map.containsKey(key)); + } + + @Test + void shouldHandleUpdateOnExistingKeyInCollisionBucket() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + Integer key1 = 1; + Integer key2 = 17; + map.put(key1, 100); + map.put(key2, 200); + assertEquals(2, map.size()); + map.put(key2, 999); + assertEquals(2, map.size()); + assertEquals(100, map.get(key1)); + assertEquals(999, map.get(key2)); + assertTrue(map.containsKey(key1)); + assertTrue(map.containsKey(key2)); + } + + @Test + void shouldHandleExactlyLoadFactorBoundary() { + GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); + // Fill exactly to load factor (12 items with capacity 16 and 0.75 load factor) + for (int i = 0; i < 12; i++) { + map.put(i, i * 10); + } + assertEquals(12, map.size()); + // Act - This should trigger rehash on 13th item + map.put(12, 120); + // Assert - Rehash should have happened + assertEquals(13, map.size()); + assertEquals(120, map.get(12)); + assertTrue(map.containsKey(12)); + } } From f018971e29c4ea3a874fefac01f44ce5125c0a8b Mon Sep 17 00:00:00 2001 From: lmj798 <2757400745@qq.com> Date: Thu, 5 Mar 2026 21:36:33 +0800 Subject: [PATCH 2/5] Removed unused assertion 'assertNotEquals' from imports. --- .../hashmap/hashing/GenericHashMapUsingArrayTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java index a341edb80c80..5ba17b4ef931 100644 --- a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java +++ b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java @@ -3,7 +3,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; From ae32765ae0d646f8b5070589eda1b9b1b77b388b Mon Sep 17 00:00:00 2001 From: lmj798 <2757400745@qq.com> Date: Tue, 10 Mar 2026 17:23:12 +0800 Subject: [PATCH 3/5] Simplify import statements in GenericHashMapUsingArrayTest --- .../hashmap/hashing/GenericHashMapUsingArrayTest.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java index 5ba17b4ef931..b529e76894a5 100644 --- a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java +++ b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java @@ -1,11 +1,6 @@ package com.thealgorithms.datastructures.hashmap.hashing; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; From 90f13b3b237bc019337d9f850cbea9cd4f6cb2fc Mon Sep 17 00:00:00 2001 From: lmj798 <2757400745@qq.com> Date: Tue, 10 Mar 2026 17:29:55 +0800 Subject: [PATCH 4/5] Refactor assertions to use Assertions class --- .../hashing/GenericHashMapUsingArrayTest.java | 129 +++++++++--------- 1 file changed, 64 insertions(+), 65 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java index b529e76894a5..17048f9f117e 100644 --- a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java +++ b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java @@ -1,7 +1,6 @@ package com.thealgorithms.datastructures.hashmap.hashing; -import static org.junit.jupiter.api.Assertions.*; - +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -15,10 +14,10 @@ void testGenericHashmapWhichUsesArrayAndBothKeyAndValueAreStrings() { map.put("Nepal", "Kathmandu"); map.put("India", "New Delhi"); map.put("Australia", "Sydney"); - assertNotNull(map); - assertEquals(4, map.size()); - assertEquals("Kathmandu", map.get("Nepal")); - assertEquals("Sydney", map.get("Australia")); + Assertions.assertNotNull(map); + Assertions.assertEquals(4, map.size()); + Assertions.assertEquals("Kathmandu", map.get("Nepal")); + Assertions.assertEquals("Sydney", map.get("Australia")); } @Test @@ -28,12 +27,12 @@ void testGenericHashmapWhichUsesArrayAndKeyIsStringValueIsInteger() { map.put("Nepal", 25); map.put("India", 101); map.put("Australia", 99); - assertNotNull(map); - assertEquals(4, map.size()); - assertEquals(25, map.get("Nepal")); - assertEquals(99, map.get("Australia")); + Assertions.assertNotNull(map); + Assertions.assertEquals(4, map.size()); + Assertions.assertEquals(25, map.get("Nepal")); + Assertions.assertEquals(99, map.get("Australia")); map.remove("Nepal"); - assertFalse(map.containsKey("Nepal")); + Assertions.assertFalse(map.containsKey("Nepal")); } @Test @@ -43,11 +42,11 @@ void testGenericHashmapWhichUsesArrayAndKeyIsIntegerValueIsString() { map.put(34, "Kathmandu"); map.put(46, "New Delhi"); map.put(89, "Sydney"); - assertNotNull(map); - assertEquals(4, map.size()); - assertEquals("Sydney", map.get(89)); - assertEquals("Washington DC", map.get(101)); - assertTrue(map.containsKey(46)); + Assertions.assertNotNull(map); + Assertions.assertEquals(4, map.size()); + Assertions.assertEquals("Sydney", map.get(89)); + Assertions.assertEquals("Washington DC", map.get(101)); + Assertions.assertTrue(map.containsKey(46)); } @Test @@ -55,7 +54,7 @@ void testRemoveNonExistentKey() { GenericHashMapUsingArray 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 + Assertions.assertEquals(1, map.size()); // Size should remain the same } @Test @@ -64,8 +63,8 @@ void testRehashing() { 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 + Assertions.assertEquals(20, map.size()); // Ensure all items were added + Assertions.assertEquals("Value5", map.get("Key5")); // Check retrieval after rehash } @Test @@ -73,7 +72,7 @@ void testUpdateValueForExistingKey() { GenericHashMapUsingArray 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")); + Assertions.assertEquals("New Washington DC", map.get("USA")); } @Test @@ -82,15 +81,15 @@ void testToStringMethod() { map.put("USA", "Washington DC"); map.put("Nepal", "Kathmandu"); String expected = "{USA : Washington DC, Nepal : Kathmandu}"; - assertEquals(expected, map.toString()); + Assertions.assertEquals(expected, map.toString()); } @Test void testContainsKey() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put("USA", "Washington DC"); - assertTrue(map.containsKey("USA")); - assertFalse(map.containsKey("Nepal")); + Assertions.assertTrue(map.containsKey("USA")); + Assertions.assertFalse(map.containsKey("Nepal")); } // ======= Added tests from the new version ======= @@ -98,17 +97,17 @@ void testContainsKey() { @Test void shouldThrowNullPointerExceptionForNullKey() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); - assertThrows(NullPointerException.class, () -> map.put(null, "value")); + Assertions.assertThrows(NullPointerException.class, () -> map.put(null, "value")); } @Test void shouldStoreNullValueForKey() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put("keyWithNullValue", null); - assertEquals(1, map.size()); - assertNull(map.get("keyWithNullValue")); + Assertions.assertEquals(1, map.size()); + Assertions.assertNull(map.get("keyWithNullValue")); // Note: containsKey returns false for null values due to implementation - assertFalse(map.containsKey("keyWithNullValue")); + Assertions.assertFalse(map.containsKey("keyWithNullValue")); } @Test @@ -118,29 +117,29 @@ void shouldHandleCollisionWhenKeysHashToSameBucket() { Integer key2 = 17; map.put(key1, 100); map.put(key2, 200); - assertEquals(2, map.size()); - assertEquals(100, map.get(key1)); - assertEquals(200, map.get(key2)); - assertTrue(map.containsKey(key1)); - assertTrue(map.containsKey(key2)); + Assertions.assertEquals(2, map.size()); + Assertions.assertEquals(100, map.get(key1)); + Assertions.assertEquals(200, map.get(key2)); + Assertions.assertTrue(map.containsKey(key1)); + Assertions.assertTrue(map.containsKey(key2)); } @Test void shouldHandleEmptyStringAsKey() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put("", "valueForEmptyKey"); - assertEquals(1, map.size()); - assertEquals("valueForEmptyKey", map.get("")); - assertTrue(map.containsKey("")); + Assertions.assertEquals(1, map.size()); + Assertions.assertEquals("valueForEmptyKey", map.get("")); + Assertions.assertTrue(map.containsKey("")); } @Test void shouldHandleEmptyStringAsValue() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put("keyForEmptyValue", ""); - assertEquals(1, map.size()); - assertEquals("", map.get("keyForEmptyValue")); - assertTrue(map.containsKey("keyForEmptyValue")); + Assertions.assertEquals(1, map.size()); + Assertions.assertEquals("", map.get("keyForEmptyValue")); + Assertions.assertTrue(map.containsKey("keyForEmptyValue")); } @Test @@ -148,29 +147,29 @@ void shouldHandleNegativeIntegerKeys() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put(-1, 100); map.put(-100, 200); - assertEquals(2, map.size()); - assertEquals(100, map.get(-1)); - assertEquals(200, map.get(-100)); - assertTrue(map.containsKey(-1)); - assertTrue(map.containsKey(-100)); + Assertions.assertEquals(2, map.size()); + Assertions.assertEquals(100, map.get(-1)); + Assertions.assertEquals(200, map.get(-100)); + Assertions.assertTrue(map.containsKey(-1)); + Assertions.assertTrue(map.containsKey(-100)); } @Test void shouldHandleZeroAsKey() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put(0, 100); - assertEquals(1, map.size()); - assertEquals(100, map.get(0)); - assertTrue(map.containsKey(0)); + Assertions.assertEquals(1, map.size()); + Assertions.assertEquals(100, map.get(0)); + Assertions.assertTrue(map.containsKey(0)); } @Test void shouldHandleStringWithSpecialCharacters() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put("key!@#$%^&*()", "value<>?/\\|"); - assertEquals(1, map.size()); - assertEquals("value<>?/\\|", map.get("key!@#$%^&*()")); - assertTrue(map.containsKey("key!@#$%^&*()")); + Assertions.assertEquals(1, map.size()); + Assertions.assertEquals("value<>?/\\|", map.get("key!@#$%^&*()")); + Assertions.assertTrue(map.containsKey("key!@#$%^&*()")); } @Test @@ -185,9 +184,9 @@ void shouldHandleLongStrings() { String key = longKey.toString(); String value = longValue.toString(); map.put(key, value); - assertEquals(1, map.size()); - assertEquals(value, map.get(key)); - assertTrue(map.containsKey(key)); + Assertions.assertEquals(1, map.size()); + Assertions.assertEquals(value, map.get(key)); + Assertions.assertTrue(map.containsKey(key)); } @ParameterizedTest @@ -195,9 +194,9 @@ void shouldHandleLongStrings() { void shouldHandleKeysOfDifferentLengths(String key) { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); map.put(key, "value"); - assertEquals(1, map.size()); - assertEquals("value", map.get(key)); - assertTrue(map.containsKey(key)); + Assertions.assertEquals(1, map.size()); + Assertions.assertEquals("value", map.get(key)); + Assertions.assertTrue(map.containsKey(key)); } @Test @@ -207,13 +206,13 @@ void shouldHandleUpdateOnExistingKeyInCollisionBucket() { Integer key2 = 17; map.put(key1, 100); map.put(key2, 200); - assertEquals(2, map.size()); + Assertions.assertEquals(2, map.size()); map.put(key2, 999); - assertEquals(2, map.size()); - assertEquals(100, map.get(key1)); - assertEquals(999, map.get(key2)); - assertTrue(map.containsKey(key1)); - assertTrue(map.containsKey(key2)); + Assertions.assertEquals(2, map.size()); + Assertions.assertEquals(100, map.get(key1)); + Assertions.assertEquals(999, map.get(key2)); + Assertions.assertTrue(map.containsKey(key1)); + Assertions.assertTrue(map.containsKey(key2)); } @Test @@ -223,12 +222,12 @@ void shouldHandleExactlyLoadFactorBoundary() { for (int i = 0; i < 12; i++) { map.put(i, i * 10); } - assertEquals(12, map.size()); + Assertions.assertEquals(12, map.size()); // Act - This should trigger rehash on 13th item map.put(12, 120); // Assert - Rehash should have happened - assertEquals(13, map.size()); - assertEquals(120, map.get(12)); - assertTrue(map.containsKey(12)); + Assertions.assertEquals(13, map.size()); + Assertions.assertEquals(120, map.get(12)); + Assertions.assertTrue(map.containsKey(12)); } } From 216be0984894f2fc5b19a83800a75b3063046091 Mon Sep 17 00:00:00 2001 From: lmj798 <2757400745@qq.com> Date: Tue, 10 Mar 2026 19:42:35 +0800 Subject: [PATCH 5/5] Refactor null key test to use variable --- .../hashmap/hashing/GenericHashMapUsingArrayTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java index 17048f9f117e..6b6e670a258b 100644 --- a/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java +++ b/src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java @@ -97,7 +97,8 @@ void testContainsKey() { @Test void shouldThrowNullPointerExceptionForNullKey() { GenericHashMapUsingArray map = new GenericHashMapUsingArray<>(); - Assertions.assertThrows(NullPointerException.class, () -> map.put(null, "value")); + String nullKey = null; // Use variable to avoid static analysis false positive + Assertions.assertThrows(NullPointerException.class, () -> map.put(nullKey, "value")); } @Test