forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapTest.java
More file actions
184 lines (153 loc) · 6.09 KB
/
HashMapTest.java
File metadata and controls
184 lines (153 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.thealgorithms.datastructures.hashmap.hashing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
public class HashMapTest {
@Test
public void testInsertAndSearch() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(15, "Value15");
hashMap.insert(25, "Value25");
hashMap.insert(35, "Value35");
assertEquals("Value15", hashMap.search(15));
assertEquals("Value25", hashMap.search(25));
assertEquals("Value35", hashMap.search(35));
assertNull(hashMap.search(45)); // Test for non-existent key
}
@Test
public void testDelete() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(15, "Value15");
hashMap.insert(25, "Value25");
hashMap.insert(35, "Value35");
assertEquals("Value25", hashMap.search(25));
hashMap.delete(25);
assertNull(hashMap.search(25)); // Confirm deletion
}
@Test
public void testDisplay() {
HashMap<Integer, String> hashMap = new HashMap<>(5);
hashMap.insert(15, "Value15");
hashMap.insert(25, "Value25");
hashMap.insert(35, "Value35");
// Optionally verify display functionality if it returns a string
hashMap.display(); // Manual check during test execution
}
@Test
public void testInsertNullKey() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(null, "NullValue");
assertEquals("NullValue", hashMap.search(null)); // Verify null key handling
}
@Test
public void testInsertNullValue() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(15, null);
assertNull(hashMap.search(15)); // Verify null value handling
}
@Test
public void testUpdateExistingKey() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(15, "Value15");
hashMap.insert(15, "UpdatedValue15");
assertEquals("UpdatedValue15", hashMap.search(15)); // Verify update
}
@Test
public void testHandleCollisions() {
HashMap<Integer, String> hashMap = new HashMap<>(3); // Create a small bucket size to force collisions
// These keys should collide if the hash function is modulo 3
hashMap.insert(1, "Value1");
hashMap.insert(4, "Value4");
hashMap.insert(7, "Value7");
assertEquals("Value1", hashMap.search(1));
assertEquals("Value4", hashMap.search(4));
assertEquals("Value7", hashMap.search(7));
}
@Test
public void testSearchInEmptyHashMap() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
assertNull(hashMap.search(10)); // Confirm search returns null in empty map
}
@Test
public void testDeleteNonExistentKey() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(15, "Value15");
hashMap.delete(25); // Delete non-existent key
assertEquals("Value15", hashMap.search(15)); // Ensure existing key remains
assertNull(hashMap.search(25)); // Confirm non-existent key remains null
}
@Test
public void testInsertLargeNumberOfElements() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
for (int i = 0; i < 100; i++) {
hashMap.insert(i, "Value" + i);
}
for (int i = 0; i < 100; i++) {
assertEquals("Value" + i, hashMap.search(i)); // Verify all inserted values
}
}
@Test
public void testDeleteHeadOfBucket() {
HashMap<Integer, String> hashMap = new HashMap<>(3);
hashMap.insert(1, "Value1");
hashMap.insert(4, "Value4");
hashMap.insert(7, "Value7");
hashMap.delete(1);
assertNull(hashMap.search(1)); // Verify head deletion
assertEquals("Value4", hashMap.search(4));
assertEquals("Value7", hashMap.search(7));
}
@Test
public void testDeleteTailOfBucket() {
HashMap<Integer, String> hashMap = new HashMap<>(3);
hashMap.insert(1, "Value1");
hashMap.insert(4, "Value4");
hashMap.insert(7, "Value7");
hashMap.delete(7);
assertNull(hashMap.search(7)); // Verify tail deletion
assertEquals("Value1", hashMap.search(1));
assertEquals("Value4", hashMap.search(4));
}
@Test
public void testDeleteMiddleElementOfBucket() {
HashMap<Integer, String> hashMap = new HashMap<>(3);
hashMap.insert(1, "Value1");
hashMap.insert(4, "Value4");
hashMap.insert(7, "Value7");
hashMap.delete(4);
assertNull(hashMap.search(4)); // Verify middle element deletion
assertEquals("Value1", hashMap.search(1));
assertEquals("Value7", hashMap.search(7));
}
@Test
public void testResizeHashMap() {
HashMap<Integer, String> hashMap = new HashMap<>(2); // Small initial size to force rehashing
for (int i = 0; i < 10; i++) {
hashMap.insert(i, "Value" + i);
}
// Verify all values after resizing
for (int i = 0; i < 10; i++) {
assertEquals("Value" + i, hashMap.search(i));
}
}
@Test
public void testCollisionResolution() {
HashMap<String, String> hashMap = new HashMap<>(3);
hashMap.insert("abc", "Value1"); // Hash index 0
hashMap.insert("cab", "Value2"); // Hash index 0 (collision)
hashMap.insert("bac", "Value3"); // Hash index 0 (collision)
assertEquals("Value1", hashMap.search("abc"));
assertEquals("Value2", hashMap.search("cab"));
assertEquals("Value3", hashMap.search("bac"));
}
@Test
public void testClearHashMap() {
HashMap<Integer, String> hashMap = new HashMap<>(10);
hashMap.insert(1, "Value1");
hashMap.insert(2, "Value2");
hashMap.clear(); // Assuming clear method resets the hash map
assertNull(hashMap.search(1));
assertNull(hashMap.search(2));
assertEquals(0, hashMap.size()); // Verify size is reset
}
}