11package com .thealgorithms .datastructures .hashmap .hashing ;
22
3- import static org .junit .jupiter .api .Assertions .assertEquals ;
4- import static org .junit .jupiter .api .Assertions .assertFalse ;
5- import static org .junit .jupiter .api .Assertions .assertNotNull ;
6- import static org .junit .jupiter .api .Assertions .assertTrue ;
7-
3+ import org .junit .jupiter .api .Assertions ;
84import org .junit .jupiter .api .Test ;
5+ import org .junit .jupiter .params .ParameterizedTest ;
6+ import org .junit .jupiter .params .provider .ValueSource ;
97
108class GenericHashMapUsingArrayTest {
119
@@ -16,10 +14,10 @@ void testGenericHashmapWhichUsesArrayAndBothKeyAndValueAreStrings() {
1614 map .put ("Nepal" , "Kathmandu" );
1715 map .put ("India" , "New Delhi" );
1816 map .put ("Australia" , "Sydney" );
19- assertNotNull (map );
20- assertEquals (4 , map .size ());
21- assertEquals ("Kathmandu" , map .get ("Nepal" ));
22- assertEquals ("Sydney" , map .get ("Australia" ));
17+ Assertions . assertNotNull (map );
18+ Assertions . assertEquals (4 , map .size ());
19+ Assertions . assertEquals ("Kathmandu" , map .get ("Nepal" ));
20+ Assertions . assertEquals ("Sydney" , map .get ("Australia" ));
2321 }
2422
2523 @ Test
@@ -29,12 +27,12 @@ void testGenericHashmapWhichUsesArrayAndKeyIsStringValueIsInteger() {
2927 map .put ("Nepal" , 25 );
3028 map .put ("India" , 101 );
3129 map .put ("Australia" , 99 );
32- assertNotNull (map );
33- assertEquals (4 , map .size ());
34- assertEquals (25 , map .get ("Nepal" ));
35- assertEquals (99 , map .get ("Australia" ));
30+ Assertions . assertNotNull (map );
31+ Assertions . assertEquals (4 , map .size ());
32+ Assertions . assertEquals (25 , map .get ("Nepal" ));
33+ Assertions . assertEquals (99 , map .get ("Australia" ));
3634 map .remove ("Nepal" );
37- assertFalse (map .containsKey ("Nepal" ));
35+ Assertions . assertFalse (map .containsKey ("Nepal" ));
3836 }
3937
4038 @ Test
@@ -44,19 +42,19 @@ void testGenericHashmapWhichUsesArrayAndKeyIsIntegerValueIsString() {
4442 map .put (34 , "Kathmandu" );
4543 map .put (46 , "New Delhi" );
4644 map .put (89 , "Sydney" );
47- assertNotNull (map );
48- assertEquals (4 , map .size ());
49- assertEquals ("Sydney" , map .get (89 ));
50- assertEquals ("Washington DC" , map .get (101 ));
51- assertTrue (map .containsKey (46 ));
45+ Assertions . assertNotNull (map );
46+ Assertions . assertEquals (4 , map .size ());
47+ Assertions . assertEquals ("Sydney" , map .get (89 ));
48+ Assertions . assertEquals ("Washington DC" , map .get (101 ));
49+ Assertions . assertTrue (map .containsKey (46 ));
5250 }
5351
5452 @ Test
5553 void testRemoveNonExistentKey () {
5654 GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
5755 map .put ("USA" , "Washington DC" );
5856 map .remove ("Nepal" ); // Attempting to remove a non-existent key
59- assertEquals (1 , map .size ()); // Size should remain the same
57+ Assertions . assertEquals (1 , map .size ()); // Size should remain the same
6058 }
6159
6260 @ Test
@@ -65,16 +63,16 @@ void testRehashing() {
6563 for (int i = 0 ; i < 20 ; i ++) {
6664 map .put ("Key" + i , "Value" + i );
6765 }
68- assertEquals (20 , map .size ()); // Ensure all items were added
69- assertEquals ("Value5" , map .get ("Key5" )); // Check retrieval after rehash
66+ Assertions . assertEquals (20 , map .size ()); // Ensure all items were added
67+ Assertions . assertEquals ("Value5" , map .get ("Key5" )); // Check retrieval after rehash
7068 }
7169
7270 @ Test
7371 void testUpdateValueForExistingKey () {
7472 GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
7573 map .put ("USA" , "Washington DC" );
7674 map .put ("USA" , "New Washington DC" ); // Updating value for existing key
77- assertEquals ("New Washington DC" , map .get ("USA" ));
75+ Assertions . assertEquals ("New Washington DC" , map .get ("USA" ));
7876 }
7977
8078 @ Test
@@ -83,14 +81,154 @@ void testToStringMethod() {
8381 map .put ("USA" , "Washington DC" );
8482 map .put ("Nepal" , "Kathmandu" );
8583 String expected = "{USA : Washington DC, Nepal : Kathmandu}" ;
86- assertEquals (expected , map .toString ());
84+ Assertions . assertEquals (expected , map .toString ());
8785 }
8886
8987 @ Test
9088 void testContainsKey () {
9189 GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
9290 map .put ("USA" , "Washington DC" );
93- assertTrue (map .containsKey ("USA" ));
94- assertFalse (map .containsKey ("Nepal" ));
91+ Assertions .assertTrue (map .containsKey ("USA" ));
92+ Assertions .assertFalse (map .containsKey ("Nepal" ));
93+ }
94+
95+ // ======= Added tests from the new version =======
96+
97+ @ Test
98+ void shouldThrowNullPointerExceptionForNullKey () {
99+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
100+ String nullKey = null ; // Use variable to avoid static analysis false positive
101+ Assertions .assertThrows (NullPointerException .class , () -> map .put (nullKey , "value" ));
102+ }
103+
104+ @ Test
105+ void shouldStoreNullValueForKey () {
106+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
107+ map .put ("keyWithNullValue" , null );
108+ Assertions .assertEquals (1 , map .size ());
109+ Assertions .assertNull (map .get ("keyWithNullValue" ));
110+ // Note: containsKey returns false for null values due to implementation
111+ Assertions .assertFalse (map .containsKey ("keyWithNullValue" ));
112+ }
113+
114+ @ Test
115+ void shouldHandleCollisionWhenKeysHashToSameBucket () {
116+ GenericHashMapUsingArray <Integer , Integer > map = new GenericHashMapUsingArray <>();
117+ Integer key1 = 1 ;
118+ Integer key2 = 17 ;
119+ map .put (key1 , 100 );
120+ map .put (key2 , 200 );
121+ Assertions .assertEquals (2 , map .size ());
122+ Assertions .assertEquals (100 , map .get (key1 ));
123+ Assertions .assertEquals (200 , map .get (key2 ));
124+ Assertions .assertTrue (map .containsKey (key1 ));
125+ Assertions .assertTrue (map .containsKey (key2 ));
126+ }
127+
128+ @ Test
129+ void shouldHandleEmptyStringAsKey () {
130+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
131+ map .put ("" , "valueForEmptyKey" );
132+ Assertions .assertEquals (1 , map .size ());
133+ Assertions .assertEquals ("valueForEmptyKey" , map .get ("" ));
134+ Assertions .assertTrue (map .containsKey ("" ));
135+ }
136+
137+ @ Test
138+ void shouldHandleEmptyStringAsValue () {
139+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
140+ map .put ("keyForEmptyValue" , "" );
141+ Assertions .assertEquals (1 , map .size ());
142+ Assertions .assertEquals ("" , map .get ("keyForEmptyValue" ));
143+ Assertions .assertTrue (map .containsKey ("keyForEmptyValue" ));
144+ }
145+
146+ @ Test
147+ void shouldHandleNegativeIntegerKeys () {
148+ GenericHashMapUsingArray <Integer , Integer > map = new GenericHashMapUsingArray <>();
149+ map .put (-1 , 100 );
150+ map .put (-100 , 200 );
151+ Assertions .assertEquals (2 , map .size ());
152+ Assertions .assertEquals (100 , map .get (-1 ));
153+ Assertions .assertEquals (200 , map .get (-100 ));
154+ Assertions .assertTrue (map .containsKey (-1 ));
155+ Assertions .assertTrue (map .containsKey (-100 ));
156+ }
157+
158+ @ Test
159+ void shouldHandleZeroAsKey () {
160+ GenericHashMapUsingArray <Integer , Integer > map = new GenericHashMapUsingArray <>();
161+ map .put (0 , 100 );
162+ Assertions .assertEquals (1 , map .size ());
163+ Assertions .assertEquals (100 , map .get (0 ));
164+ Assertions .assertTrue (map .containsKey (0 ));
165+ }
166+
167+ @ Test
168+ void shouldHandleStringWithSpecialCharacters () {
169+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
170+ map .put ("key!@#$%^&*()" , "value<>?/\\ |" );
171+ Assertions .assertEquals (1 , map .size ());
172+ Assertions .assertEquals ("value<>?/\\ |" , map .get ("key!@#$%^&*()" ));
173+ Assertions .assertTrue (map .containsKey ("key!@#$%^&*()" ));
174+ }
175+
176+ @ Test
177+ void shouldHandleLongStrings () {
178+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
179+ StringBuilder longKey = new StringBuilder ();
180+ StringBuilder longValue = new StringBuilder ();
181+ for (int i = 0 ; i < 1000 ; i ++) {
182+ longKey .append ("a" );
183+ longValue .append ("b" );
184+ }
185+ String key = longKey .toString ();
186+ String value = longValue .toString ();
187+ map .put (key , value );
188+ Assertions .assertEquals (1 , map .size ());
189+ Assertions .assertEquals (value , map .get (key ));
190+ Assertions .assertTrue (map .containsKey (key ));
191+ }
192+
193+ @ ParameterizedTest
194+ @ ValueSource (strings = {"a" , "ab" , "abc" , "test" , "longerString" })
195+ void shouldHandleKeysOfDifferentLengths (String key ) {
196+ GenericHashMapUsingArray <String , String > map = new GenericHashMapUsingArray <>();
197+ map .put (key , "value" );
198+ Assertions .assertEquals (1 , map .size ());
199+ Assertions .assertEquals ("value" , map .get (key ));
200+ Assertions .assertTrue (map .containsKey (key ));
201+ }
202+
203+ @ Test
204+ void shouldHandleUpdateOnExistingKeyInCollisionBucket () {
205+ GenericHashMapUsingArray <Integer , Integer > map = new GenericHashMapUsingArray <>();
206+ Integer key1 = 1 ;
207+ Integer key2 = 17 ;
208+ map .put (key1 , 100 );
209+ map .put (key2 , 200 );
210+ Assertions .assertEquals (2 , map .size ());
211+ map .put (key2 , 999 );
212+ Assertions .assertEquals (2 , map .size ());
213+ Assertions .assertEquals (100 , map .get (key1 ));
214+ Assertions .assertEquals (999 , map .get (key2 ));
215+ Assertions .assertTrue (map .containsKey (key1 ));
216+ Assertions .assertTrue (map .containsKey (key2 ));
217+ }
218+
219+ @ Test
220+ void shouldHandleExactlyLoadFactorBoundary () {
221+ GenericHashMapUsingArray <Integer , Integer > map = new GenericHashMapUsingArray <>();
222+ // Fill exactly to load factor (12 items with capacity 16 and 0.75 load factor)
223+ for (int i = 0 ; i < 12 ; i ++) {
224+ map .put (i , i * 10 );
225+ }
226+ Assertions .assertEquals (12 , map .size ());
227+ // Act - This should trigger rehash on 13th item
228+ map .put (12 , 120 );
229+ // Assert - Rehash should have happened
230+ Assertions .assertEquals (13 , map .size ());
231+ Assertions .assertEquals (120 , map .get (12 ));
232+ Assertions .assertTrue (map .containsKey (12 ));
95233 }
96234}
0 commit comments