Browse Source

Update tests for PriorityQueue.

Mikael Capelle 6 years ago
parent
commit
cf672f8cd4
1 changed files with 52 additions and 9 deletions
  1. 52
    9
      src/test/org/insa/algo/utils/PriorityQueueTest.java

+ 52
- 9
src/test/org/insa/algo/utils/PriorityQueueTest.java View File

2
 
2
 
3
 import static org.junit.Assert.assertEquals;
3
 import static org.junit.Assert.assertEquals;
4
 import static org.junit.Assert.assertTrue;
4
 import static org.junit.Assert.assertTrue;
5
+import static org.junit.Assert.fail;
5
 
6
 
6
 import java.util.ArrayList;
7
 import java.util.ArrayList;
7
 import java.util.Arrays;
8
 import java.util.Arrays;
69
             return Integer.compare(this.value, other.value);
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
     protected static class TestParameters<E extends Comparable<E>> {
80
     protected static class TestParameters<E extends Comparable<E>> {
108
                         Arrays.stream(new int[]{ 1, 7, 4, 8, 9, 6, 5 })
114
                         Arrays.stream(new int[]{ 1, 7, 4, 8, 9, 6, 5 })
109
                                 .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
115
                                 .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
110
                         new int[]{ 2, 0, 1, 3, 4, 5, 6 }));
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
         return objects;
122
         return objects;
112
     }
123
     }
113
 
124
 
194
         queue.remove(new MutableInteger(0));
205
         queue.remove(new MutableInteger(0));
195
     }
206
     }
196
 
207
 
197
-    @Test(expected = ElementNotFoundException.class)
208
+    @Test
198
     public void testRemoveNotFound() {
209
     public void testRemoveNotFound() {
199
         Assume.assumeFalse(queue.isEmpty());
210
         Assume.assumeFalse(queue.isEmpty());
200
         List<MutableInteger> data = Arrays.asList(parameters.data);
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
     public void testDeleteThenRemove() {
231
     public void testDeleteThenRemove() {
207
         Assume.assumeFalse(queue.isEmpty());
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
     public void testRemoveTwice() {
246
     public void testRemoveTwice() {
214
         Assume.assumeFalse(queue.isEmpty());
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
     @Test
261
     @Test
233
                 remains_in.add(parameters.data[parameters.deleteOrder[j]]);
275
                 remains_in.add(parameters.data[parameters.deleteOrder[j]]);
234
                 remains_cp.add(copyTree.deleteMin());
276
                 remains_cp.add(copyTree.deleteMin());
235
             }
277
             }
278
+
236
             Collections.sort(remains_in);
279
             Collections.sort(remains_in);
237
 
280
 
238
             // Check that the copy is now empty, and that both list contains all
281
             // Check that the copy is now empty, and that both list contains all

Loading…
Cancel
Save