-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP25SubmissionChecker.java
More file actions
195 lines (173 loc) · 4.87 KB
/
Copy pathP25SubmissionChecker.java
File metadata and controls
195 lines (173 loc) · 4.87 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
185
186
187
188
189
190
191
192
193
194
195
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import java.util.NoSuchElementException;
/**
* This class extends the HashtableMap class to run submission checks on it.
*/
public class P25SubmissionChecker extends HashtableMap<Integer, Integer> {
/**
* Checks if hashtable resizes as expected.
*/
@Test
public void testTableResize() {
HashtableMap<String, String> map = new HashtableMap<>(8);
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");
map.put("e", "5");
// check capacity before resizing
Assertions.assertEquals(8, map.getCapacity());
map.put("f", "6");
map.put("g", "7");
map.put("h", "8");
map.put("i", "9");
// check capacity after resizing
Assertions.assertEquals(16, map.getCapacity());
}
/**
* Checks if hashtable resizes when it reaches the load factor threshold.
*/
@Test
public void testTableResizeAtThreshold() {
HashtableMap<String, String> map = new HashtableMap<>(12);
map.put("1", "a");
map.put("2", "b");
map.put("3", "c");
map.put("4", "d");
map.put("5", "e");
map.put("6", "f");
map.put("7", "g");
map.put("8", "h");
// check capacity before resizing
Assertions.assertEquals(12, map.getCapacity());
map.put("9", "i");
map.put("10", "j");
// check capacity after resizing
Assertions.assertEquals(24, map.getCapacity());
}
/**
* Tests putting duplicate "a" keys into the HashtableMap and checks for IllegalArgumentExceptions
* after the first insertion. Then checkes if calling get("a") returns the first value inserted.
*/
@Test
public void testDuplicateInsertions() {
HashtableMap<String, Integer> map = new HashtableMap<>();
map.put("a", 6);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> map.put("a", 7)
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> map.put("a", 8)
);
Assertions.assertEquals(6, map.get("a"));
}
/**
* Tests checks the difference between adding a unique and duplicate key into the
* HashtableMap by calling put.
*/
@Test
public void testMultipleDuplicateInsertions() {
HashtableMap<String, Integer> map = new HashtableMap<>();
map.put("a", 6);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> map.put("a", 7)
);
map.put("b", 5);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> map.put("a", 8)
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> map.put("b", 5)
);
}
/**
* Tests the get method on a small HashtableMap with keys 1, 2, and 3.
*/
@Test
public void testGetOnSmallMap123() {
HashtableMap<Integer, String> map = new HashtableMap<>();
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
Assertions.assertEquals("two", map.get(2));
}
/**
* Tests whether the get method throws a NoSuchElementException if key
* is not in table.
*/
@Test
public void testGetException() {
HashtableMap<Integer, String> map = new HashtableMap<>();
map.put(6, "one");
map.put(8, "two");
map.put(9, "three");
Assertions.assertThrows(
NoSuchElementException.class,
() -> map.get(7)
);
}
/**
* Tests get(13) and get(23) on a HashtableMap(10).
*/
@Test
public void testSimpleCollision() {
HashtableMap<Integer, String> map = new HashtableMap<>(10);
map.put(12, "one");
map.put(13, "two");
map.put(23, "three");
Assertions.assertEquals("two", map.get(13));
Assertions.assertEquals("three", map.get(23));
}
/**
* Tests if get(27) on a HashtableMap(10) with keys: 17, 28, 77
* throws a NoSuchElementException.
*/
@Test
public void testCollisionException() {
HashtableMap<Integer, String> map = new HashtableMap<>(10);
map.put(17, "one");
map.put(28, "two");
map.put(77, "three");
Assertions.assertThrows(
NoSuchElementException.class,
() -> map.get(27)
);
}
/**
* Tests get for a key that has been removed.
*/
@Test
public void testGetAfterRemove() {
HashtableMap<Integer, Integer> map = new HashtableMap<>();
map.put(2, 6);
map.put(3, 7);
map.put(4, 8);
map.get(3);
map.remove(3);
Assertions.assertThrows(
NoSuchElementException.class,
() -> map.get(3)
);
}
/**
* Tests removal of a key that is not contained in the HashtableMap.
*/
@Test
public void testRemoveThrowsException() {
HashtableMap<Integer, Integer> map = new HashtableMap<>(10);
map.put(2, 0);
map.put(22, 0);
map.put(32, 1);
map.put(42, 1);
Assertions.assertThrows(
NoSuchElementException.class,
() -> map.remove(12)
);
}
}