Skip to content

Commit c8fd59f

Browse files
authored
test: enhance GenericHashMapUsingArrayTest with additional edge case coverage
1 parent 4b04ad4 commit c8fd59f

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

src/test/java/com/thealgorithms/datastructures/hashmap/hashing/GenericHashMapUsingArrayTest.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNull;
68
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
710

811
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.ValueSource;
914

1015
class GenericHashMapUsingArrayTest {
1116

@@ -93,4 +98,143 @@ void testContainsKey() {
9398
assertTrue(map.containsKey("USA"));
9499
assertFalse(map.containsKey("Nepal"));
95100
}
101+
102+
// ======= Added tests from the new version =======
103+
104+
@Test
105+
void shouldThrowNullPointerExceptionForNullKey() {
106+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
107+
assertThrows(NullPointerException.class, () -> map.put(null, "value"));
108+
}
109+
110+
@Test
111+
void shouldStoreNullValueForKey() {
112+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
113+
map.put("keyWithNullValue", null);
114+
assertEquals(1, map.size());
115+
assertNull(map.get("keyWithNullValue"));
116+
// Note: containsKey returns false for null values due to implementation
117+
assertFalse(map.containsKey("keyWithNullValue"));
118+
}
119+
120+
@Test
121+
void shouldHandleCollisionWhenKeysHashToSameBucket() {
122+
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
123+
Integer key1 = 1;
124+
Integer key2 = 17;
125+
map.put(key1, 100);
126+
map.put(key2, 200);
127+
assertEquals(2, map.size());
128+
assertEquals(100, map.get(key1));
129+
assertEquals(200, map.get(key2));
130+
assertTrue(map.containsKey(key1));
131+
assertTrue(map.containsKey(key2));
132+
}
133+
134+
@Test
135+
void shouldHandleEmptyStringAsKey() {
136+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
137+
map.put("", "valueForEmptyKey");
138+
assertEquals(1, map.size());
139+
assertEquals("valueForEmptyKey", map.get(""));
140+
assertTrue(map.containsKey(""));
141+
}
142+
143+
@Test
144+
void shouldHandleEmptyStringAsValue() {
145+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
146+
map.put("keyForEmptyValue", "");
147+
assertEquals(1, map.size());
148+
assertEquals("", map.get("keyForEmptyValue"));
149+
assertTrue(map.containsKey("keyForEmptyValue"));
150+
}
151+
152+
@Test
153+
void shouldHandleNegativeIntegerKeys() {
154+
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
155+
map.put(-1, 100);
156+
map.put(-100, 200);
157+
assertEquals(2, map.size());
158+
assertEquals(100, map.get(-1));
159+
assertEquals(200, map.get(-100));
160+
assertTrue(map.containsKey(-1));
161+
assertTrue(map.containsKey(-100));
162+
}
163+
164+
@Test
165+
void shouldHandleZeroAsKey() {
166+
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
167+
map.put(0, 100);
168+
assertEquals(1, map.size());
169+
assertEquals(100, map.get(0));
170+
assertTrue(map.containsKey(0));
171+
}
172+
173+
@Test
174+
void shouldHandleStringWithSpecialCharacters() {
175+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
176+
map.put("key!@#$%^&*()", "value<>?/\\|");
177+
assertEquals(1, map.size());
178+
assertEquals("value<>?/\\|", map.get("key!@#$%^&*()"));
179+
assertTrue(map.containsKey("key!@#$%^&*()"));
180+
}
181+
182+
@Test
183+
void shouldHandleLongStrings() {
184+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
185+
StringBuilder longKey = new StringBuilder();
186+
StringBuilder longValue = new StringBuilder();
187+
for (int i = 0; i < 1000; i++) {
188+
longKey.append("a");
189+
longValue.append("b");
190+
}
191+
String key = longKey.toString();
192+
String value = longValue.toString();
193+
map.put(key, value);
194+
assertEquals(1, map.size());
195+
assertEquals(value, map.get(key));
196+
assertTrue(map.containsKey(key));
197+
}
198+
199+
@ParameterizedTest
200+
@ValueSource(strings = {"a", "ab", "abc", "test", "longerString"})
201+
void shouldHandleKeysOfDifferentLengths(String key) {
202+
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
203+
map.put(key, "value");
204+
assertEquals(1, map.size());
205+
assertEquals("value", map.get(key));
206+
assertTrue(map.containsKey(key));
207+
}
208+
209+
@Test
210+
void shouldHandleUpdateOnExistingKeyInCollisionBucket() {
211+
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
212+
Integer key1 = 1;
213+
Integer key2 = 17;
214+
map.put(key1, 100);
215+
map.put(key2, 200);
216+
assertEquals(2, map.size());
217+
map.put(key2, 999);
218+
assertEquals(2, map.size());
219+
assertEquals(100, map.get(key1));
220+
assertEquals(999, map.get(key2));
221+
assertTrue(map.containsKey(key1));
222+
assertTrue(map.containsKey(key2));
223+
}
224+
225+
@Test
226+
void shouldHandleExactlyLoadFactorBoundary() {
227+
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
228+
// Fill exactly to load factor (12 items with capacity 16 and 0.75 load factor)
229+
for (int i = 0; i < 12; i++) {
230+
map.put(i, i * 10);
231+
}
232+
assertEquals(12, map.size());
233+
// Act - This should trigger rehash on 13th item
234+
map.put(12, 120);
235+
// Assert - Rehash should have happened
236+
assertEquals(13, map.size());
237+
assertEquals(120, map.get(12));
238+
assertTrue(map.containsKey(12));
239+
}
96240
}

0 commit comments

Comments
 (0)