|
@@ -2,6 +2,7 @@ package org.insa.algo.utils;
|
2
|
2
|
|
3
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
4
|
import static org.junit.Assert.assertTrue;
|
|
5
|
+import static org.junit.Assert.fail;
|
5
|
6
|
|
6
|
7
|
import java.util.ArrayList;
|
7
|
8
|
import java.util.Arrays;
|
|
@@ -69,6 +70,11 @@ public abstract class PriorityQueueTest {
|
69
|
70
|
return Integer.compare(this.value, other.value);
|
70
|
71
|
}
|
71
|
72
|
|
|
73
|
+ @Override
|
|
74
|
+ public String toString() {
|
|
75
|
+ return Integer.toString(get());
|
|
76
|
+ }
|
|
77
|
+
|
72
|
78
|
};
|
73
|
79
|
|
74
|
80
|
protected static class TestParameters<E extends Comparable<E>> {
|
|
@@ -108,6 +114,11 @@ public abstract class PriorityQueueTest {
|
108
|
114
|
Arrays.stream(new int[]{ 1, 7, 4, 8, 9, 6, 5 })
|
109
|
115
|
.mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
|
110
|
116
|
new int[]{ 2, 0, 1, 3, 4, 5, 6 }));
|
|
117
|
+
|
|
118
|
+ objects.add(new TestParameters<>(
|
|
119
|
+ Arrays.stream(new int[]{ 1, 7, 2, 8, 9, 3, 4, 10, 11, 12, 13, 5, 6 })
|
|
120
|
+ .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
|
|
121
|
+ new int[]{ 3, 4, 0, 2, 5, 6, 1, 7, 8, 9, 10, 11, 12 }));
|
111
|
122
|
return objects;
|
112
|
123
|
}
|
113
|
124
|
|
|
@@ -194,26 +205,57 @@ public abstract class PriorityQueueTest {
|
194
|
205
|
queue.remove(new MutableInteger(0));
|
195
|
206
|
}
|
196
|
207
|
|
197
|
|
- @Test(expected = ElementNotFoundException.class)
|
|
208
|
+ @Test
|
198
|
209
|
public void testRemoveNotFound() {
|
199
|
210
|
Assume.assumeFalse(queue.isEmpty());
|
200
|
211
|
List<MutableInteger> data = Arrays.asList(parameters.data);
|
201
|
|
- queue.remove(new MutableInteger(Collections.min(data).get() - 1));
|
202
|
|
- queue.remove(new MutableInteger(Collections.max(data).get() + 1));
|
|
212
|
+ MutableInteger min = new MutableInteger(Collections.min(data).get() - 1),
|
|
213
|
+ max = new MutableInteger(Collections.max(data).get() + 1);
|
|
214
|
+ try {
|
|
215
|
+ queue.remove(min);
|
|
216
|
+ fail("Expected exception " + ElementNotFoundException.class.getName());
|
|
217
|
+ }
|
|
218
|
+ catch (ElementNotFoundException e) {
|
|
219
|
+ assertEquals(e.getElement(), min);
|
|
220
|
+ }
|
|
221
|
+ try {
|
|
222
|
+ queue.remove(max);
|
|
223
|
+ fail("Expected exception " + ElementNotFoundException.class.getName());
|
|
224
|
+ }
|
|
225
|
+ catch (ElementNotFoundException e) {
|
|
226
|
+ assertEquals(e.getElement(), max);
|
|
227
|
+ }
|
203
|
228
|
}
|
204
|
229
|
|
205
|
|
- @Test(expected = ElementNotFoundException.class)
|
|
230
|
+ @Test
|
206
|
231
|
public void testDeleteThenRemove() {
|
207
|
232
|
Assume.assumeFalse(queue.isEmpty());
|
208
|
|
- MutableInteger min = queue.deleteMin();
|
209
|
|
- queue.remove(min);
|
|
233
|
+ while (!queue.isEmpty()) {
|
|
234
|
+ MutableInteger min = queue.deleteMin();
|
|
235
|
+ try {
|
|
236
|
+ queue.remove(min);
|
|
237
|
+ fail("Expected exception " + ElementNotFoundException.class.getName());
|
|
238
|
+ }
|
|
239
|
+ catch (ElementNotFoundException e) {
|
|
240
|
+ assertEquals(e.getElement(), min);
|
|
241
|
+ }
|
|
242
|
+ }
|
210
|
243
|
}
|
211
|
244
|
|
212
|
|
- @Test(expected = ElementNotFoundException.class)
|
|
245
|
+ @Test
|
213
|
246
|
public void testRemoveTwice() {
|
214
|
247
|
Assume.assumeFalse(queue.isEmpty());
|
215
|
|
- queue.remove(parameters.data[4 % parameters.data.length]);
|
216
|
|
- queue.remove(parameters.data[4 % parameters.data.length]);
|
|
248
|
+ for (MutableInteger data: parameters.data) {
|
|
249
|
+ PriorityQueue<MutableInteger> copyQueue = this.createQueue(this.queue);
|
|
250
|
+ copyQueue.remove(data);
|
|
251
|
+ try {
|
|
252
|
+ copyQueue.remove(data);
|
|
253
|
+ fail("Expected exception " + ElementNotFoundException.class.getName());
|
|
254
|
+ }
|
|
255
|
+ catch (ElementNotFoundException e) {
|
|
256
|
+ assertEquals(e.getElement(), data);
|
|
257
|
+ }
|
|
258
|
+ }
|
217
|
259
|
}
|
218
|
260
|
|
219
|
261
|
@Test
|
|
@@ -233,6 +275,7 @@ public abstract class PriorityQueueTest {
|
233
|
275
|
remains_in.add(parameters.data[parameters.deleteOrder[j]]);
|
234
|
276
|
remains_cp.add(copyTree.deleteMin());
|
235
|
277
|
}
|
|
278
|
+
|
236
|
279
|
Collections.sort(remains_in);
|
237
|
280
|
|
238
|
281
|
// Check that the copy is now empty, and that both list contains all
|