/
CuckooTests.java
195 lines (166 loc) · 4.92 KB
/
CuckooTests.java
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
185
186
187
188
189
190
191
192
193
194
195
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CuckooTests {
CuckooHashTable<Integer, String> test;
MapEntry<Integer, String>[] table;
MapEntry<Integer, String>[] tableTwo;
// Test add to removed spot
//Test collide to removed spot
// Put to empty hashtable
@Test
public void testPut1() {
test = new CuckooHashTable<Integer, String>();
test.put(5, "Hi");
table = test.getTable();
int key = table[5%table.length].getKey();
String value = table[5%table.length].getValue();
assertEquals(key, 5);
assertEquals(value, "Hi");
// Single collision
test.put(14, "get kicked out lulz");
table = test.getTable();
tableTwo = test.getTableTwo();
assertEquals((int)table[5%table.length].getKey(), 14);
assertEquals(table[5%table.length].getValue(), "get kicked out lulz");
//Check second table
assertEquals((int)tableTwo[5%tableTwo.length].getKey(), 5);
assertEquals(tableTwo[5%tableTwo.length].getValue(), "Hi");
}
// Tests multiple collisions
@Test
public void testPut2() {
test = new CuckooHashTable<Integer, String>();
test.put(15, "15"); // 6 % 5 = 1
test.put(10, "10"); // 1 % 5 = 1 they collide, 15 now in tableTwo at 5 % 5 = 0
test.put(24, "24");
table = test.getTable();
tableTwo = test.getTableTwo();
assertEquals(15, (int)table[1].getKey());
assertEquals(10, (int)tableTwo[0].getKey());
assertEquals(24, (int)tableTwo[3].getKey());
}
// Tests resize, going past max load factor
@Test
public void testPut3() {
test = new CuckooHashTable<Integer, String>();
test.put(15, "15"); // 6 % 5 = 1
test.put(10, "10"); // 1 % 5 = 1 they collide, 15 now in tableTwo at 5 % 5 = 0
test.put(24, "24");
test.put(3, "3");
test.put(4, "4");
test.put(0, "0");
test.put(1, "1");
test.put(2, "2");
table = test.getTable();
tableTwo = test.getTableTwo();
assertEquals(10, table.length);
assertEquals(10, tableTwo.length);
assertEquals(0, (int)table[0].getKey());
assertEquals(1, (int)table[1].getKey());
assertEquals(2, (int)table[2].getKey());
assertEquals(3, (int)table[3].getKey());
assertEquals(4, (int)table[4].getKey());
assertEquals(24, (int)table[6].getKey());
assertEquals(10, (int)tableTwo[0].getKey());
assertEquals(15, (int)tableTwo[5].getKey());
}
// Test infinite loop & rehash
@Test
public void testPut4() {
test = new CuckooHashTable<Integer, String>();
test.put(21, "21");
System.out.println(test);
test.put(17, "17");
System.out.println(test);
test.put(12, "12");
System.out.println(test);
assertEquals(3, test.size());
}
// Add existing entry
@Test
public void testPut5() {
test = new CuckooHashTable<Integer, String>();
test.put(21, "21");
table = test.getTable();
assertEquals("21", table[3].getValue());
test.put(21, "twenty-one");
table = test.getTable();
assertEquals("twenty-one", table[3].getValue());
assertEquals(1, test.size());
}
// Add existing entry after collision
@Test
public void testPut6() {
test = new CuckooHashTable<Integer, String>();
test.put(5, "5");
test.put(0, "0");
//System.out.println(test);
test.put(5, "five");
table = test.getTable();
tableTwo = test.getTableTwo();
//System.out.println(test);
assertEquals("five", tableTwo[0].getValue());
assertEquals(2, test.size());
}
// Remove an add collision and add after
@Test
public void testPut7() {
test = new CuckooHashTable<Integer, String>();
test.put(5, "5");
test.put(500, "500");
test.remove(5);
test.put(50, "50");
table = test.getTable();
tableTwo = test.getTableTwo();
assertEquals(50, (int)table[0].getKey());
assertEquals(500, (int)tableTwo[0].getKey());
}
// Simple add after remove
@Test
public void testPut8() {
test = new CuckooHashTable<Integer, String>();
test.put(5, "5");
test.remove(5);
test.put(0, "0");
table = test.getTable();
assertEquals(0, (int)table[0].getKey());
}
@Test
public void testHashOne() {
test = new CuckooHashTable<Integer, String>();
assertEquals(test.hashOne(54), 9);
}
@Test
public void testHashTwo() {
test = new CuckooHashTable<Integer, String>();
assertEquals(test.hashTwo(54), 20);
}
// Remove from hashtable
@Test
public void testRemove1() {
CuckooHashTable<Integer, String> test = new CuckooHashTable<Integer, String>();
test.put(5, "hi");
String look = test.remove(5);
assertEquals(look, "hi");
}
@Test
public void testRemove2() {
CuckooHashTable<Integer, String> test = new CuckooHashTable<Integer, String>();
test.put(6, "hi");
test.put(5, "hello");
String look = test.remove(5);
assertEquals(look, "hello");
String look2 = test.remove(6);
assertEquals(look2, "hi");
}
@Test
public void testRemove3() {
CuckooHashTable<Integer, String> test = new CuckooHashTable<Integer, String>();
test.put(5, "hi");
test.put(10, "hello");
String look = test.remove(5);
assertEquals(look, "hi");
String look2 = test.remove(10);
assertEquals(look2, "hello");
}
}