Browse Source

Update tests for PriorityQueue.

Mikaël Capelle 3 years ago
parent
commit
d05bc92689

+ 7
- 228
src/test/org/insa/algo/utils/BinaryHeapTest.java View File

@@ -1,236 +1,15 @@
1 1
 package org.insa.algo.utils;
2 2
 
3
-import static org.junit.Assert.assertEquals;
4
-import static org.junit.Assert.assertFalse;
5
-import static org.junit.Assert.assertTrue;
3
+public class BinaryHeapTest extends PriorityQueueTest {
6 4
 
7
-import java.util.Arrays;
8
-import java.util.HashSet;
9
-import java.util.stream.IntStream;
10
-
11
-import org.junit.Before;
12
-import org.junit.Test;
13
-
14
-public class BinaryHeapTest {
15
-
16
-    class MutableInteger implements Comparable<MutableInteger> {
17
-
18
-        // Actual value
19
-        private int value;
20
-
21
-        public MutableInteger(int value) {
22
-            this.value = value;
23
-        }
24
-
25
-        /**
26
-         * @return The integer value stored inside this MutableInteger.
27
-         */
28
-        public int get() {
29
-            return this.value;
30
-        }
31
-
32
-        /**
33
-         * Update the integer value stored inside this MutableInteger.
34
-         * 
35
-         * @param value New value to set.
36
-         */
37
-        public void set(int value) {
38
-            this.value = value;
39
-        }
40
-
41
-        @Override
42
-        public int compareTo(MutableInteger other) {
43
-            return Integer.compare(this.value, other.value);
44
-        }
45
-
46
-    };
47
-
48
-    // Raw data arrays.
49
-    private MutableInteger[] data1 = IntStream.range(0, 20).mapToObj(MutableInteger::new)
50
-            .toArray(MutableInteger[]::new);
51
-    private MutableInteger[] data2 = Arrays.stream(new int[] { 8, 1, 6, 3, 4, 5, 9 })
52
-            .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new);
53
-
54
-    // Actual heap.
55
-    private BinaryHeap<MutableInteger> heap1, heap2;
56
-
57
-    @Before
58
-    public void init() {
59
-        // Create the range heap
60
-        this.heap1 = new BinaryHeap<>();
61
-        this.heap2 = new BinaryHeap<>();
62
-
63
-        for (MutableInteger v: data1) {
64
-            this.heap1.insert(v);
65
-        }
66
-
67
-        for (MutableInteger v: data2) {
68
-            this.heap2.insert(v);
69
-        }
70
-    }
71
-
72
-    @Test
73
-    public void testIsEmpty() {
74
-        BinaryHeap<MutableInteger> tree = new BinaryHeap<>();
75
-        assertTrue(tree.isEmpty());
76
-        assertFalse(this.heap1.isEmpty());
77
-        assertFalse(this.heap2.isEmpty());
78
-    }
79
-
80
-    @Test
81
-    public void testSize() {
82
-        BinaryHeap<MutableInteger> tree = new BinaryHeap<>();
83
-        assertEquals(0, tree.size());
84
-        assertEquals(20, this.heap1.size());
85
-        assertEquals(7, this.heap2.size());
86
-    }
87
-
88
-    @Test
89
-    public void testInsert() {
90
-        BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
91
-        int size = 0;
92
-        for (MutableInteger x: data1) {
93
-            heap.insert(x);
94
-            assertEquals(++size, heap.size());
95
-        }
96
-        assertEquals(data1.length, heap.size());
97
-
98
-        heap = new BinaryHeap<>();
99
-        size = 0;
100
-        for (MutableInteger x: data2) {
101
-            heap.insert(x);
102
-            assertEquals(++size, heap.size());
103
-        }
104
-        assertEquals(data2.length, heap.size());
105
-    }
106
-
107
-    @Test(expected = EmptyPriorityQueueException.class)
108
-    public void testEmptyFindMin() {
109
-        BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
110
-        heap.findMin();
111
-    }
112
-
113
-    @Test
114
-    public void testFindMin() {
115
-        assertEquals(0, heap1.findMin().get());
116
-        assertEquals(1, heap2.findMin().get());
117
-    }
118
-
119
-    @Test(expected = EmptyPriorityQueueException.class)
120
-    public void testEmptyDeleteMin() {
121
-        BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
122
-        heap.deleteMin();
123
-    }
124
-
125
-    @Test
126
-    public void testDeleteMin() {
127
-        // range 1 (sorted)
128
-        int size = data1.length;
129
-        assertEquals(heap1.size(), size);
130
-        for (MutableInteger x: data1) {
131
-            assertEquals(x, heap1.deleteMin());
132
-            size -= 1;
133
-            assertEquals(size, heap1.size());
134
-        }
135
-        assertEquals(0, heap1.size());
136
-        assertTrue(heap1.isEmpty());
137
-
138
-        // range 2 (was not sorted)
139
-        MutableInteger[] range2 = Arrays.copyOf(data2, data2.length);
140
-        Arrays.sort(range2);
141
-        size = range2.length;
142
-        assertEquals(heap2.size(), size);
143
-        for (MutableInteger x: range2) {
144
-            assertEquals(x.get(), heap2.deleteMin().get());
145
-            size -= 1;
146
-            assertEquals(size, heap2.size());
147
-        }
148
-        assertEquals(0, heap2.size());
149
-        assertTrue(heap2.isEmpty());
150
-    }
151
-
152
-    @Test(expected = ElementNotFoundException.class)
153
-    public void testRemoveEmpty() {
154
-        BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
155
-        heap.remove(new MutableInteger(0));
156
-    }
157
-
158
-    @Test(expected = ElementNotFoundException.class)
159
-    public void testRemoveNotFound() {
160
-        heap1.remove(new MutableInteger(20));
161
-    }
162
-
163
-    @Test
164
-    public void testRemove() {
165
-        // heap 1
166
-        int size1 = heap1.size();
167
-        int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
168
-                6, 1, 11 };
169
-        for (int i = 0; i < deleteOrder1.length; ++i) {
170
-            // Remove from structure
171
-            heap1.remove(this.data1[deleteOrder1[i]]);
172
-
173
-            // Copy the remaining elements
174
-            BinaryHeap<MutableInteger> copyTree = new BinaryHeap<>(heap1);
175
-
176
-            // Retrieve all remaining elements in both structures
177
-            MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1],
178
-                    remains_cp = new MutableInteger[deleteOrder1.length - i - 1];
179
-            for (int j = 0; j < remains_cp.length; ++j) {
180
-                remains_in[j] = this.data1[deleteOrder1[i + j + 1]];
181
-                remains_cp[j] = copyTree.deleteMin();
182
-            }
183
-
184
-            // Check that the copy is now empty, and that both list contains all
185
-            // elements.
186
-            assertTrue(copyTree.isEmpty());
187
-            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
188
-                    new HashSet<>(Arrays.asList(remains_cp)));
189
-
190
-            // Check that the size of the original tree is correct.
191
-            assertEquals(--size1, heap1.size());
192
-        }
193
-        assertTrue(heap1.isEmpty());
194
-
195
-        // heap 2
196
-        int size2 = heap2.size();
197
-        int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
198
-        for (int i = 0; i < deleteOrder2.length; ++i) {
199
-            // Remove from structure
200
-            heap2.remove(this.data2[deleteOrder2[i]]);
201
-
202
-            // Copy the remaining elements
203
-            BinaryHeap<MutableInteger> copyTree = new BinaryHeap<>(heap2);
204
-
205
-            // Retrieve all remaining elements in both structures
206
-            MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1],
207
-                    remains_cp = new MutableInteger[deleteOrder2.length - i - 1];
208
-            for (int j = 0; j < remains_cp.length; ++j) {
209
-                remains_in[j] = this.data2[deleteOrder2[i + j + 1]];
210
-                remains_cp[j] = copyTree.deleteMin();
211
-            }
212
-
213
-            // Check that the copy is now empty, and that both list contains all
214
-            // elements.
215
-            assertTrue(copyTree.isEmpty());
216
-            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
217
-                    new HashSet<>(Arrays.asList(remains_cp)));
218
-
219
-            // Check that the size of the original tree is correct.
220
-            assertEquals(--size2, heap2.size());
221
-        }
222
-        assertTrue(heap2.isEmpty());
5
+    @Override
6
+    public PriorityQueue<MutableInteger> createQueue() {
7
+        return new BinaryHeap<>();
223 8
     }
224 9
 
225
-    @Test
226
-    public void testRemoveThenAdd() {
227
-        MutableInteger mi5 = this.data1[6];
228
-        heap1.remove(mi5);
229
-        assertEquals(19, heap1.size());
230
-        mi5.set(-20);
231
-        heap1.insert(mi5);
232
-        assertEquals(20, heap1.size());
233
-        assertEquals(-20, heap1.findMin().get());
10
+    @Override
11
+    public PriorityQueue<MutableInteger> createQueue(PriorityQueue<MutableInteger> queue) {
12
+        return new BinaryHeap<>((BinaryHeap<MutableInteger>) queue);
234 13
     }
235 14
 
236 15
 }

+ 7
- 228
src/test/org/insa/algo/utils/BinarySearchTreeTest.java View File

@@ -1,236 +1,15 @@
1 1
 package org.insa.algo.utils;
2 2
 
3
-import static org.junit.Assert.assertEquals;
4
-import static org.junit.Assert.assertFalse;
5
-import static org.junit.Assert.assertTrue;
3
+public class BinarySearchTreeTest extends PriorityQueueTest {
6 4
 
7
-import java.util.Arrays;
8
-import java.util.HashSet;
9
-import java.util.stream.IntStream;
10
-
11
-import org.junit.Before;
12
-import org.junit.Test;
13
-
14
-public class BinarySearchTreeTest {
15
-
16
-    class MutableInteger implements Comparable<MutableInteger> {
17
-
18
-        // Actual value
19
-        private int value;
20
-
21
-        public MutableInteger(int value) {
22
-            this.value = value;
23
-        }
24
-
25
-        /**
26
-         * @return The integer value stored inside this MutableInteger.
27
-         */
28
-        public int get() {
29
-            return this.value;
30
-        }
31
-
32
-        /**
33
-         * Update the integer value stored inside this MutableInteger.
34
-         * 
35
-         * @param value New value to set.
36
-         */
37
-        public void set(int value) {
38
-            this.value = value;
39
-        }
40
-
41
-        @Override
42
-        public int compareTo(MutableInteger other) {
43
-            return Integer.compare(this.value, other.value);
44
-        }
45
-
46
-    };
47
-
48
-    // Raw data arrays.
49
-    private MutableInteger[] data1 = IntStream.range(0, 20).mapToObj(MutableInteger::new)
50
-            .toArray(MutableInteger[]::new);
51
-    private MutableInteger[] data2 = Arrays.stream(new int[] { 8, 1, 6, 3, 4, 5, 9 })
52
-            .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new);
53
-
54
-    // Actual searchTree.
55
-    private BinarySearchTree<MutableInteger> searchTree1, searchTree2;
56
-
57
-    @Before
58
-    public void init() {
59
-        // Create the range searchTree
60
-        this.searchTree1 = new BinarySearchTree<>();
61
-        this.searchTree2 = new BinarySearchTree<>();
62
-
63
-        for (MutableInteger v: data1) {
64
-            this.searchTree1.insert(v);
65
-        }
66
-
67
-        for (MutableInteger v: data2) {
68
-            this.searchTree2.insert(v);
69
-        }
70
-    }
71
-
72
-    @Test
73
-    public void testIsEmpty() {
74
-        BinarySearchTree<MutableInteger> tree = new BinarySearchTree<>();
75
-        assertTrue(tree.isEmpty());
76
-        assertFalse(this.searchTree1.isEmpty());
77
-        assertFalse(this.searchTree2.isEmpty());
78
-    }
79
-
80
-    @Test
81
-    public void testSize() {
82
-        BinarySearchTree<MutableInteger> tree = new BinarySearchTree<>();
83
-        assertEquals(0, tree.size());
84
-        assertEquals(20, this.searchTree1.size());
85
-        assertEquals(7, this.searchTree2.size());
86
-    }
87
-
88
-    @Test
89
-    public void testInsert() {
90
-        BinarySearchTree<MutableInteger> searchTree = new BinarySearchTree<>();
91
-        int size = 0;
92
-        for (MutableInteger x: data1) {
93
-            searchTree.insert(x);
94
-            assertEquals(++size, searchTree.size());
95
-        }
96
-        assertEquals(data1.length, searchTree.size());
97
-
98
-        searchTree = new BinarySearchTree<>();
99
-        size = 0;
100
-        for (MutableInteger x: data2) {
101
-            searchTree.insert(x);
102
-            assertEquals(++size, searchTree.size());
103
-        }
104
-        assertEquals(data2.length, searchTree.size());
105
-    }
106
-
107
-    @Test(expected = EmptyPriorityQueueException.class)
108
-    public void testEmptyFindMin() {
109
-        BinarySearchTree<MutableInteger> searchTree = new BinarySearchTree<>();
110
-        searchTree.findMin();
111
-    }
112
-
113
-    @Test
114
-    public void testFindMin() {
115
-        assertEquals(0, searchTree1.findMin().get());
116
-        assertEquals(1, searchTree2.findMin().get());
117
-    }
118
-
119
-    @Test(expected = EmptyPriorityQueueException.class)
120
-    public void testEmptyDeleteMin() {
121
-        BinarySearchTree<MutableInteger> searchTree = new BinarySearchTree<>();
122
-        searchTree.deleteMin();
123
-    }
124
-
125
-    @Test
126
-    public void testDeleteMin() {
127
-        // range 1 (sorted)
128
-        int size = data1.length;
129
-        assertEquals(searchTree1.size(), size);
130
-        for (MutableInteger x: data1) {
131
-            assertEquals(x, searchTree1.deleteMin());
132
-            size -= 1;
133
-            assertEquals(size, searchTree1.size());
134
-        }
135
-        assertEquals(0, searchTree1.size());
136
-        assertTrue(searchTree1.isEmpty());
137
-
138
-        // range 2 (was not sorted)
139
-        MutableInteger[] range2 = Arrays.copyOf(data2, data2.length);
140
-        Arrays.sort(range2);
141
-        size = range2.length;
142
-        assertEquals(searchTree2.size(), size);
143
-        for (MutableInteger x: range2) {
144
-            assertEquals(x.get(), searchTree2.deleteMin().get());
145
-            size -= 1;
146
-            assertEquals(size, searchTree2.size());
147
-        }
148
-        assertEquals(0, searchTree2.size());
149
-        assertTrue(searchTree2.isEmpty());
150
-    }
151
-
152
-    @Test(expected = ElementNotFoundException.class)
153
-    public void testRemoveEmpty() {
154
-        BinarySearchTree<MutableInteger> searchTree = new BinarySearchTree<>();
155
-        searchTree.remove(new MutableInteger(0));
156
-    }
157
-
158
-    @Test(expected = ElementNotFoundException.class)
159
-    public void testRemoveNotFound() {
160
-        searchTree1.remove(new MutableInteger(20));
161
-    }
162
-
163
-    @Test
164
-    public void testRemove() {
165
-        // searchTree 1
166
-        int size1 = searchTree1.size();
167
-        int[] deleteOrder1 = new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7,
168
-                6, 1, 11 };
169
-        for (int i = 0; i < deleteOrder1.length; ++i) {
170
-            // Remove from structure
171
-            searchTree1.remove(this.data1[deleteOrder1[i]]);
172
-
173
-            // Copy the remaining elements
174
-            BinarySearchTree<MutableInteger> copyTree = new BinarySearchTree<>(searchTree1);
175
-
176
-            // Retrieve all remaining elements in both structures
177
-            MutableInteger[] remains_in = new MutableInteger[deleteOrder1.length - i - 1],
178
-                    remains_cp = new MutableInteger[deleteOrder1.length - i - 1];
179
-            for (int j = 0; j < remains_cp.length; ++j) {
180
-                remains_in[j] = this.data1[deleteOrder1[i + j + 1]];
181
-                remains_cp[j] = copyTree.deleteMin();
182
-            }
183
-
184
-            // Check that the copy is now empty, and that both list contains all
185
-            // elements.
186
-            assertTrue(copyTree.isEmpty());
187
-            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
188
-                    new HashSet<>(Arrays.asList(remains_cp)));
189
-
190
-            // Check that the size of the original tree is correct.
191
-            assertEquals(--size1, searchTree1.size());
192
-        }
193
-        assertTrue(searchTree1.isEmpty());
194
-
195
-        // searchTree 2
196
-        int size2 = searchTree2.size();
197
-        int[] deleteOrder2 = new int[] { 6, 5, 0, 1, 4, 2, 3 };
198
-        for (int i = 0; i < deleteOrder2.length; ++i) {
199
-            // Remove from structure
200
-            searchTree2.remove(this.data2[deleteOrder2[i]]);
201
-
202
-            // Copy the remaining elements
203
-            BinarySearchTree<MutableInteger> copyTree = new BinarySearchTree<>(searchTree2);
204
-
205
-            // Retrieve all remaining elements in both structures
206
-            MutableInteger[] remains_in = new MutableInteger[deleteOrder2.length - i - 1],
207
-                    remains_cp = new MutableInteger[deleteOrder2.length - i - 1];
208
-            for (int j = 0; j < remains_cp.length; ++j) {
209
-                remains_in[j] = this.data2[deleteOrder2[i + j + 1]];
210
-                remains_cp[j] = copyTree.deleteMin();
211
-            }
212
-
213
-            // Check that the copy is now empty, and that both list contains all
214
-            // elements.
215
-            assertTrue(copyTree.isEmpty());
216
-            assertEquals(new HashSet<>(Arrays.asList(remains_in)),
217
-                    new HashSet<>(Arrays.asList(remains_cp)));
218
-
219
-            // Check that the size of the original tree is correct.
220
-            assertEquals(--size2, searchTree2.size());
221
-        }
222
-        assertTrue(searchTree2.isEmpty());
5
+    @Override
6
+    public PriorityQueue<MutableInteger> createQueue() {
7
+        return new BinarySearchTree<>();
223 8
     }
224 9
 
225
-    @Test
226
-    public void testRemoveThenAdd() {
227
-        MutableInteger mi5 = this.data1[6];
228
-        searchTree1.remove(mi5);
229
-        assertEquals(19, searchTree1.size());
230
-        mi5.set(-20);
231
-        searchTree1.insert(mi5);
232
-        assertEquals(20, searchTree1.size());
233
-        assertEquals(-20, searchTree1.findMin().get());
10
+    @Override
11
+    public PriorityQueue<MutableInteger> createQueue(PriorityQueue<MutableInteger> queue) {
12
+        return new BinarySearchTree<>((BinarySearchTree<MutableInteger>) queue);
234 13
     }
235 14
 
236 15
 }

+ 259
- 0
src/test/org/insa/algo/utils/PriorityQueueTest.java View File

@@ -0,0 +1,259 @@
1
+package org.insa.algo.utils;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+import static org.junit.Assert.assertTrue;
5
+
6
+import java.util.ArrayList;
7
+import java.util.Arrays;
8
+import java.util.Collection;
9
+import java.util.Collections;
10
+import java.util.List;
11
+import java.util.stream.IntStream;
12
+
13
+import org.junit.Assume;
14
+import org.junit.Before;
15
+import org.junit.Test;
16
+import org.junit.runner.RunWith;
17
+import org.junit.runners.Parameterized;
18
+import org.junit.runners.Parameterized.Parameter;
19
+import org.junit.runners.Parameterized.Parameters;
20
+
21
+@RunWith(Parameterized.class)
22
+public abstract class PriorityQueueTest {
23
+
24
+    /**
25
+     * Needs to be implemented by child class to actually provide priority queue
26
+     * implementation.
27
+     * 
28
+     * @return A new instance of a PriorityQueue implementation.
29
+     */
30
+    public abstract PriorityQueue<MutableInteger> createQueue();
31
+
32
+    /**
33
+     * Needs to be implemented by child class to actually provide priority queue
34
+     * implementation.
35
+     * 
36
+     * @param queue Queue to copy.
37
+     * 
38
+     * @return Copy of the given queue.
39
+     */
40
+    public abstract PriorityQueue<MutableInteger> createQueue(PriorityQueue<MutableInteger> queue);
41
+
42
+    protected static class MutableInteger implements Comparable<MutableInteger> {
43
+
44
+        // Actual value
45
+        private int value;
46
+
47
+        public MutableInteger(int value) {
48
+            this.value = value;
49
+        }
50
+
51
+        /**
52
+         * @return The integer value stored inside this MutableInteger.
53
+         */
54
+        public int get() {
55
+            return this.value;
56
+        }
57
+
58
+        /**
59
+         * Update the integer value stored inside this MutableInteger.
60
+         * 
61
+         * @param value New value to set.
62
+         */
63
+        public void set(int value) {
64
+            this.value = value;
65
+        }
66
+
67
+        @Override
68
+        public int compareTo(MutableInteger other) {
69
+            return Integer.compare(this.value, other.value);
70
+        }
71
+
72
+    };
73
+
74
+    protected static class TestParameters<E extends Comparable<E>> {
75
+
76
+        // Data to insert
77
+        public final E[] data;
78
+        public final int[] deleteOrder;
79
+
80
+        public TestParameters(E[] data, int[] deleteOrder) {
81
+            this.data = data;
82
+            this.deleteOrder = deleteOrder;
83
+        }
84
+
85
+    };
86
+
87
+    /**
88
+     * Set of parameters.
89
+     * 
90
+     */
91
+    @Parameters
92
+    public static Collection<Object> data() {
93
+        Collection<Object> objects = new ArrayList<>();
94
+        objects.add(new TestParameters<>(new MutableInteger[0], new int[0]));
95
+        objects.add(new TestParameters<>(
96
+                IntStream.range(0, 50).mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
97
+                IntStream.range(0, 50).toArray()));
98
+        objects.add(new TestParameters<>(
99
+                IntStream.range(0, 20).mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
100
+                new int[] { 12, 17, 18, 19, 4, 5, 3, 2, 0, 9, 10, 16, 8, 14, 13, 15, 7, 6, 1,
101
+                        11 }));
102
+        objects.add(
103
+                new TestParameters<>(
104
+                        Arrays.stream(new int[] { 8, 1, 6, 3, 4, 5, 9 })
105
+                                .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new),
106
+                        new int[] { 6, 5, 0, 1, 4, 2, 3 }));
107
+        return objects;
108
+    }
109
+
110
+    @Parameter
111
+    public TestParameters<MutableInteger> parameters;
112
+
113
+    // Actual queue.
114
+    private PriorityQueue<MutableInteger> queue;
115
+
116
+    @Before
117
+    public void init() {
118
+        // Create the range queue
119
+        this.queue = createQueue();
120
+
121
+        for (MutableInteger v: parameters.data) {
122
+            this.queue.insert(v);
123
+        }
124
+    }
125
+
126
+    @Test
127
+    public void testIsEmpty() {
128
+        assertEquals(parameters.data.length == 0, this.queue.isEmpty());
129
+    }
130
+
131
+    @Test
132
+    public void testSize() {
133
+        assertEquals(parameters.data.length, this.queue.size());
134
+    }
135
+
136
+    @Test
137
+    public void testInsert() {
138
+        PriorityQueue<MutableInteger> queue = createQueue();
139
+        int size = 0;
140
+        for (MutableInteger x: parameters.data) {
141
+            queue.insert(x);
142
+            assertEquals(++size, queue.size());
143
+        }
144
+        assertEquals(parameters.data.length, queue.size());
145
+        MutableInteger[] range = Arrays.copyOf(parameters.data, parameters.data.length);
146
+        Arrays.sort(range);
147
+
148
+        for (MutableInteger mi: range) {
149
+            assertEquals(mi.get(), queue.deleteMin().value);
150
+            assertEquals(--size, queue.size());
151
+        }
152
+    }
153
+
154
+    @Test(expected = EmptyPriorityQueueException.class)
155
+    public void testEmptyFindMin() {
156
+        Assume.assumeTrue(queue.isEmpty());
157
+        queue.findMin();
158
+    }
159
+
160
+    @Test
161
+    public void testFindMin() {
162
+        Assume.assumeFalse(queue.isEmpty());
163
+        assertEquals(Collections.min(Arrays.asList(parameters.data)).get(), queue.findMin().get());
164
+    }
165
+
166
+    @Test(expected = EmptyPriorityQueueException.class)
167
+    public void testEmptyDeleteMin() {
168
+        Assume.assumeTrue(queue.isEmpty());
169
+        queue.deleteMin();
170
+    }
171
+
172
+    @Test
173
+    public void testDeleteMin() {
174
+        int size = parameters.data.length;
175
+        assertEquals(queue.size(), size);
176
+        MutableInteger[] range = Arrays.copyOf(parameters.data, parameters.data.length);
177
+        Arrays.sort(range);
178
+        for (MutableInteger x: range) {
179
+            assertEquals(x, queue.deleteMin());
180
+            size -= 1;
181
+            assertEquals(size, queue.size());
182
+        }
183
+        assertEquals(0, queue.size());
184
+        assertTrue(queue.isEmpty());
185
+    }
186
+
187
+    @Test(expected = ElementNotFoundException.class)
188
+    public void testRemoveEmpty() {
189
+        Assume.assumeTrue(queue.isEmpty());
190
+        queue.remove(new MutableInteger(0));
191
+    }
192
+
193
+    @Test(expected = ElementNotFoundException.class)
194
+    public void testRemoveNotFound() {
195
+        Assume.assumeFalse(queue.isEmpty());
196
+        List<MutableInteger> data = Arrays.asList(parameters.data);
197
+        queue.remove(new MutableInteger(Collections.min(data).get() - 1));
198
+        queue.remove(new MutableInteger(Collections.max(data).get() + 1));
199
+    }
200
+
201
+    @Test(expected = ElementNotFoundException.class)
202
+    public void testDeleteThenRemove() {
203
+        Assume.assumeFalse(queue.isEmpty());
204
+        MutableInteger min = queue.deleteMin();
205
+        queue.remove(min);
206
+    }
207
+
208
+    @Test(expected = ElementNotFoundException.class)
209
+    public void testRemoveTwice() {
210
+        Assume.assumeFalse(queue.isEmpty());
211
+        queue.remove(parameters.data[4 % parameters.data.length]);
212
+        queue.remove(parameters.data[4 % parameters.data.length]);
213
+    }
214
+
215
+    @Test
216
+    public void testRemove() {
217
+        int size1 = queue.size();
218
+        for (int i = 0; i < parameters.deleteOrder.length; ++i) {
219
+            // Remove from structure
220
+            queue.remove(parameters.data[parameters.deleteOrder[i]]);
221
+
222
+            // Copy the remaining elements
223
+            PriorityQueue<MutableInteger> copyTree = createQueue(queue);
224
+
225
+            // Retrieve all remaining elements in both structures
226
+            ArrayList<MutableInteger> remains_in = new ArrayList<>(),
227
+                    remains_cp = new ArrayList<>();
228
+            for (int j = i + 1; j < parameters.deleteOrder.length; ++j) {
229
+                remains_in.add(parameters.data[parameters.deleteOrder[j]]);
230
+                remains_cp.add(copyTree.deleteMin());
231
+            }
232
+            Collections.sort(remains_in);
233
+
234
+            // Check that the copy is now empty, and that both list contains all
235
+            // elements.
236
+            assertTrue(copyTree.isEmpty());
237
+            assertEquals(remains_in, remains_cp);
238
+
239
+            // Check that the size of the original tree is correct.
240
+            assertEquals(--size1, queue.size());
241
+        }
242
+        assertTrue(queue.isEmpty());
243
+    }
244
+
245
+    @Test
246
+    public void testRemoveThenAdd() {
247
+        Assume.assumeFalse(queue.isEmpty());
248
+        int min = Collections.min(Arrays.asList(parameters.data)).get();
249
+        for (MutableInteger mi: parameters.data) {
250
+            queue.remove(mi);
251
+            assertEquals(parameters.data.length - 1, queue.size());
252
+            mi.set(--min);
253
+            queue.insert(mi);
254
+            assertEquals(parameters.data.length, queue.size());
255
+            assertEquals(min, queue.findMin().get());
256
+        }
257
+    }
258
+
259
+}

Loading…
Cancel
Save