forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashMapCuckooHashingTest.java
More file actions
140 lines (116 loc) · 4.75 KB
/
HashMapCuckooHashingTest.java
File metadata and controls
140 lines (116 loc) · 4.75 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
package com.thealgorithms.datastructures.hashmap.hashing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class HashMapCuckooHashingTest {
@Test
void insertKey() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
assertEquals(0, hashTable.getNumberOfKeysInTable());
hashTable.insertKey2HashTable(3);
assertEquals(1, hashTable.getNumberOfKeysInTable());
}
@Test
void getKeyIndex() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(8);
hashTable.insertKey2HashTable(4);
assertNotEquals(-1, hashTable.findKeyInTable(8));
}
@Test
void containsKey() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(8);
boolean contains = hashTable.checkTableContainsKey(8);
assertTrue(contains);
}
@Test
void removeKey() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(3);
int initialSize = hashTable.getNumberOfKeysInTable();
hashTable.deleteKeyFromHashTable(3);
assertEquals(initialSize - 1, hashTable.getNumberOfKeysInTable());
}
@Test
void removeNone() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
try {
hashTable.deleteKeyFromHashTable(3);
} catch (Exception e) {
assertTrue(true);
return;
}
Assertions.fail("Expected exception when trying to delete a non-existing key.");
}
@Test
void reHashTableIncreasesTableSize() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(1);
hashTable.insertKey2HashTable(2);
hashTable.insertKey2HashTable(3);
hashTable.insertKey2HashTable(4);
hashTable.insertKey2HashTable(5);
hashTable.insertKey2HashTable(6);
hashTable.insertKey2HashTable(7);
hashTable.insertKey2HashTable(8);
hashTable.insertKey2HashTable(9);
hashTable.insertKey2HashTable(10); // This should trigger rehashing
assertEquals(10, hashTable.getNumberOfKeysInTable());
}
@Test
void hashFunctionsAreDifferent() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(33);
assertNotEquals(hashTable.hashFunction1(3), hashTable.hashFunction2(3), "Hash functions should produce different indices.");
}
@Test
void avoidInfiniteLoops() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(0);
hashTable.insertKey2HashTable(10);
hashTable.insertKey2HashTable(100);
assertTrue(hashTable.checkTableContainsKey(0));
assertTrue(hashTable.checkTableContainsKey(10));
assertTrue(hashTable.checkTableContainsKey(100));
}
@Test
void testLoadFactor() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
for (int i = 1; i <= 8; i++) { // Insert 8 keys to test load factor
hashTable.insertKey2HashTable(i);
}
assertEquals(8, hashTable.getNumberOfKeysInTable());
// Check that rehashing occurs when a 9th key is added
hashTable.insertKey2HashTable(9);
assertTrue(hashTable.getNumberOfKeysInTable() > 8, "Load factor exceeded, table should have been resized.");
}
@Test
void testDeleteNonExistentKey() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(1);
hashTable.insertKey2HashTable(2);
Exception exception = null;
try {
hashTable.deleteKeyFromHashTable(3); // Try deleting a non-existent key
} catch (IllegalArgumentException e) {
exception = e; // Capture the exception
}
assertNotNull(exception, "Expected an IllegalArgumentException when deleting a non-existent key.");
}
@Test
void testInsertDuplicateKey() {
HashMapCuckooHashing hashTable = new HashMapCuckooHashing(10);
hashTable.insertKey2HashTable(1);
Exception exception = null;
try {
hashTable.insertKey2HashTable(1); // Attempt to insert duplicate key
} catch (IllegalArgumentException e) {
exception = e; // Capture the exception
}
assertNotNull(exception, "Expected an IllegalArgumentException for duplicate key insertion.");
}
}