Skip to content

Commit 90f13b3

Browse files
authored
Refactor assertions to use Assertions class
1 parent ae32765 commit 90f13b3

File tree

1 file changed

+64
-65
lines changed

1 file changed

+64
-65
lines changed
Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.thealgorithms.datastructures.hashmap.hashing;
22

3-
import static org.junit.jupiter.api.Assertions.*;
4-
3+
import org.junit.jupiter.api.Assertions;
54
import org.junit.jupiter.api.Test;
65
import org.junit.jupiter.params.ParameterizedTest;
76
import org.junit.jupiter.params.provider.ValueSource;
@@ -15,10 +14,10 @@ void testGenericHashmapWhichUsesArrayAndBothKeyAndValueAreStrings() {
1514
map.put("Nepal", "Kathmandu");
1615
map.put("India", "New Delhi");
1716
map.put("Australia", "Sydney");
18-
assertNotNull(map);
19-
assertEquals(4, map.size());
20-
assertEquals("Kathmandu", map.get("Nepal"));
21-
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"));
2221
}
2322

2423
@Test
@@ -28,12 +27,12 @@ void testGenericHashmapWhichUsesArrayAndKeyIsStringValueIsInteger() {
2827
map.put("Nepal", 25);
2928
map.put("India", 101);
3029
map.put("Australia", 99);
31-
assertNotNull(map);
32-
assertEquals(4, map.size());
33-
assertEquals(25, map.get("Nepal"));
34-
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"));
3534
map.remove("Nepal");
36-
assertFalse(map.containsKey("Nepal"));
35+
Assertions.assertFalse(map.containsKey("Nepal"));
3736
}
3837

3938
@Test
@@ -43,19 +42,19 @@ void testGenericHashmapWhichUsesArrayAndKeyIsIntegerValueIsString() {
4342
map.put(34, "Kathmandu");
4443
map.put(46, "New Delhi");
4544
map.put(89, "Sydney");
46-
assertNotNull(map);
47-
assertEquals(4, map.size());
48-
assertEquals("Sydney", map.get(89));
49-
assertEquals("Washington DC", map.get(101));
50-
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));
5150
}
5251

5352
@Test
5453
void testRemoveNonExistentKey() {
5554
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
5655
map.put("USA", "Washington DC");
5756
map.remove("Nepal"); // Attempting to remove a non-existent key
58-
assertEquals(1, map.size()); // Size should remain the same
57+
Assertions.assertEquals(1, map.size()); // Size should remain the same
5958
}
6059

6160
@Test
@@ -64,16 +63,16 @@ void testRehashing() {
6463
for (int i = 0; i < 20; i++) {
6564
map.put("Key" + i, "Value" + i);
6665
}
67-
assertEquals(20, map.size()); // Ensure all items were added
68-
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
6968
}
7069

7170
@Test
7271
void testUpdateValueForExistingKey() {
7372
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
7473
map.put("USA", "Washington DC");
7574
map.put("USA", "New Washington DC"); // Updating value for existing key
76-
assertEquals("New Washington DC", map.get("USA"));
75+
Assertions.assertEquals("New Washington DC", map.get("USA"));
7776
}
7877

7978
@Test
@@ -82,33 +81,33 @@ void testToStringMethod() {
8281
map.put("USA", "Washington DC");
8382
map.put("Nepal", "Kathmandu");
8483
String expected = "{USA : Washington DC, Nepal : Kathmandu}";
85-
assertEquals(expected, map.toString());
84+
Assertions.assertEquals(expected, map.toString());
8685
}
8786

8887
@Test
8988
void testContainsKey() {
9089
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
9190
map.put("USA", "Washington DC");
92-
assertTrue(map.containsKey("USA"));
93-
assertFalse(map.containsKey("Nepal"));
91+
Assertions.assertTrue(map.containsKey("USA"));
92+
Assertions.assertFalse(map.containsKey("Nepal"));
9493
}
9594

9695
// ======= Added tests from the new version =======
9796

9897
@Test
9998
void shouldThrowNullPointerExceptionForNullKey() {
10099
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
101-
assertThrows(NullPointerException.class, () -> map.put(null, "value"));
100+
Assertions.assertThrows(NullPointerException.class, () -> map.put(null, "value"));
102101
}
103102

104103
@Test
105104
void shouldStoreNullValueForKey() {
106105
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
107106
map.put("keyWithNullValue", null);
108-
assertEquals(1, map.size());
109-
assertNull(map.get("keyWithNullValue"));
107+
Assertions.assertEquals(1, map.size());
108+
Assertions.assertNull(map.get("keyWithNullValue"));
110109
// Note: containsKey returns false for null values due to implementation
111-
assertFalse(map.containsKey("keyWithNullValue"));
110+
Assertions.assertFalse(map.containsKey("keyWithNullValue"));
112111
}
113112

114113
@Test
@@ -118,59 +117,59 @@ void shouldHandleCollisionWhenKeysHashToSameBucket() {
118117
Integer key2 = 17;
119118
map.put(key1, 100);
120119
map.put(key2, 200);
121-
assertEquals(2, map.size());
122-
assertEquals(100, map.get(key1));
123-
assertEquals(200, map.get(key2));
124-
assertTrue(map.containsKey(key1));
125-
assertTrue(map.containsKey(key2));
120+
Assertions.assertEquals(2, map.size());
121+
Assertions.assertEquals(100, map.get(key1));
122+
Assertions.assertEquals(200, map.get(key2));
123+
Assertions.assertTrue(map.containsKey(key1));
124+
Assertions.assertTrue(map.containsKey(key2));
126125
}
127126

128127
@Test
129128
void shouldHandleEmptyStringAsKey() {
130129
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
131130
map.put("", "valueForEmptyKey");
132-
assertEquals(1, map.size());
133-
assertEquals("valueForEmptyKey", map.get(""));
134-
assertTrue(map.containsKey(""));
131+
Assertions.assertEquals(1, map.size());
132+
Assertions.assertEquals("valueForEmptyKey", map.get(""));
133+
Assertions.assertTrue(map.containsKey(""));
135134
}
136135

137136
@Test
138137
void shouldHandleEmptyStringAsValue() {
139138
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
140139
map.put("keyForEmptyValue", "");
141-
assertEquals(1, map.size());
142-
assertEquals("", map.get("keyForEmptyValue"));
143-
assertTrue(map.containsKey("keyForEmptyValue"));
140+
Assertions.assertEquals(1, map.size());
141+
Assertions.assertEquals("", map.get("keyForEmptyValue"));
142+
Assertions.assertTrue(map.containsKey("keyForEmptyValue"));
144143
}
145144

146145
@Test
147146
void shouldHandleNegativeIntegerKeys() {
148147
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
149148
map.put(-1, 100);
150149
map.put(-100, 200);
151-
assertEquals(2, map.size());
152-
assertEquals(100, map.get(-1));
153-
assertEquals(200, map.get(-100));
154-
assertTrue(map.containsKey(-1));
155-
assertTrue(map.containsKey(-100));
150+
Assertions.assertEquals(2, map.size());
151+
Assertions.assertEquals(100, map.get(-1));
152+
Assertions.assertEquals(200, map.get(-100));
153+
Assertions.assertTrue(map.containsKey(-1));
154+
Assertions.assertTrue(map.containsKey(-100));
156155
}
157156

158157
@Test
159158
void shouldHandleZeroAsKey() {
160159
GenericHashMapUsingArray<Integer, Integer> map = new GenericHashMapUsingArray<>();
161160
map.put(0, 100);
162-
assertEquals(1, map.size());
163-
assertEquals(100, map.get(0));
164-
assertTrue(map.containsKey(0));
161+
Assertions.assertEquals(1, map.size());
162+
Assertions.assertEquals(100, map.get(0));
163+
Assertions.assertTrue(map.containsKey(0));
165164
}
166165

167166
@Test
168167
void shouldHandleStringWithSpecialCharacters() {
169168
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
170169
map.put("key!@#$%^&*()", "value<>?/\\|");
171-
assertEquals(1, map.size());
172-
assertEquals("value<>?/\\|", map.get("key!@#$%^&*()"));
173-
assertTrue(map.containsKey("key!@#$%^&*()"));
170+
Assertions.assertEquals(1, map.size());
171+
Assertions.assertEquals("value<>?/\\|", map.get("key!@#$%^&*()"));
172+
Assertions.assertTrue(map.containsKey("key!@#$%^&*()"));
174173
}
175174

176175
@Test
@@ -185,19 +184,19 @@ void shouldHandleLongStrings() {
185184
String key = longKey.toString();
186185
String value = longValue.toString();
187186
map.put(key, value);
188-
assertEquals(1, map.size());
189-
assertEquals(value, map.get(key));
190-
assertTrue(map.containsKey(key));
187+
Assertions.assertEquals(1, map.size());
188+
Assertions.assertEquals(value, map.get(key));
189+
Assertions.assertTrue(map.containsKey(key));
191190
}
192191

193192
@ParameterizedTest
194193
@ValueSource(strings = {"a", "ab", "abc", "test", "longerString"})
195194
void shouldHandleKeysOfDifferentLengths(String key) {
196195
GenericHashMapUsingArray<String, String> map = new GenericHashMapUsingArray<>();
197196
map.put(key, "value");
198-
assertEquals(1, map.size());
199-
assertEquals("value", map.get(key));
200-
assertTrue(map.containsKey(key));
197+
Assertions.assertEquals(1, map.size());
198+
Assertions.assertEquals("value", map.get(key));
199+
Assertions.assertTrue(map.containsKey(key));
201200
}
202201

203202
@Test
@@ -207,13 +206,13 @@ void shouldHandleUpdateOnExistingKeyInCollisionBucket() {
207206
Integer key2 = 17;
208207
map.put(key1, 100);
209208
map.put(key2, 200);
210-
assertEquals(2, map.size());
209+
Assertions.assertEquals(2, map.size());
211210
map.put(key2, 999);
212-
assertEquals(2, map.size());
213-
assertEquals(100, map.get(key1));
214-
assertEquals(999, map.get(key2));
215-
assertTrue(map.containsKey(key1));
216-
assertTrue(map.containsKey(key2));
211+
Assertions.assertEquals(2, map.size());
212+
Assertions.assertEquals(100, map.get(key1));
213+
Assertions.assertEquals(999, map.get(key2));
214+
Assertions.assertTrue(map.containsKey(key1));
215+
Assertions.assertTrue(map.containsKey(key2));
217216
}
218217

219218
@Test
@@ -223,12 +222,12 @@ void shouldHandleExactlyLoadFactorBoundary() {
223222
for (int i = 0; i < 12; i++) {
224223
map.put(i, i * 10);
225224
}
226-
assertEquals(12, map.size());
225+
Assertions.assertEquals(12, map.size());
227226
// Act - This should trigger rehash on 13th item
228227
map.put(12, 120);
229228
// Assert - Rehash should have happened
230-
assertEquals(13, map.size());
231-
assertEquals(120, map.get(12));
232-
assertTrue(map.containsKey(12));
229+
Assertions.assertEquals(13, map.size());
230+
Assertions.assertEquals(120, map.get(12));
231+
Assertions.assertTrue(map.containsKey(12));
233232
}
234233
}

0 commit comments

Comments
 (0)