Exemple #1
0
        public void ClearTest()
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance, 0);

            AssertEmptyQueue(queue);

            TestNode a = new TestNode(5);
            TestNode b = new TestNode(10);
            TestNode c = new TestNode(2);
            TestNode d = new TestNode(6);

            AssertEnqueue(queue, a);
            AssertEnqueue(queue, b);
            AssertEnqueue(queue, c);
            AssertEnqueue(queue, d);

            queue.Clear();
            AssertEmptyQueue(queue);

            // Test that elements can be re-inserted after the clear operation
            AssertEnqueue(queue, a);
            Assert.Same(a, queue.Peek());

            AssertEnqueue(queue, b);
            Assert.Same(a, queue.Peek());

            AssertEnqueue(queue, c);
            Assert.Same(c, queue.Peek());

            AssertEnqueue(queue, d);
            Assert.Same(c, queue.Peek());
        }
Exemple #2
0
        public void ClearIgnoringIndexesTest()
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance, 0);

            AssertEmptyQueue(queue);

            TestNode a = new TestNode(5);
            TestNode b = new TestNode(10);
            TestNode c = new TestNode(2);
            TestNode d = new TestNode(6);
            TestNode e = new TestNode(11);

            AssertEnqueue(queue, a);
            AssertEnqueue(queue, b);
            AssertEnqueue(queue, c);
            AssertEnqueue(queue, d);

            queue.ClearIgnoringIndexes();
            AssertEmptyQueue(queue);

            // Elements cannot be re-inserted but new ones can.
            Assert.Throws <ArgumentException>(() => queue.TryEnqueue(a));

            AssertEnqueue(queue, e);
            Assert.Same(e, queue.Peek());
        }
Exemple #3
0
        public void EnqueueTest()
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance, 0);

            AssertEmptyQueue(queue);

            TestNode a = new TestNode(5);
            TestNode b = new TestNode(10);
            TestNode c = new TestNode(2);
            TestNode d = new TestNode(7);
            TestNode e = new TestNode(6);

            AssertEnqueue(queue, a);
            AssertEnqueue(queue, b);
            AssertEnqueue(queue, c);
            AssertEnqueue(queue, d);

            // Remove the first element
            Assert.Same(c, queue.Peek());
            Assert.Same(c, queue.Dequeue());
            Assert.Equal(3, queue.Count);

            // Test that offering another element preserves the priority queue semantics.
            AssertEnqueue(queue, e);
            Assert.Equal(4, queue.Count);
            Assert.Same(a, queue.Peek());
            Assert.Same(a, queue.Dequeue());
            Assert.Equal(3, queue.Count);

            // Keep removing the remaining elements
            Assert.Same(e, queue.Peek());
            Assert.Same(e, queue.Dequeue());
            Assert.Equal(2, queue.Count);

            Assert.Same(d, queue.Peek());
            Assert.Same(d, queue.Dequeue());
            Assert.Equal(1, queue.Count);

            Assert.Same(b, queue.Peek());
            Assert.Same(b, queue.Dequeue());
            AssertEmptyQueue(queue);
        }
Exemple #4
0
        public void TestZeroInitialSize()
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance, 0);

            AssertEmptyQueue(queue);
            TestNode e = new TestNode(1);

            AssertEnqueue(queue, e);
            Assert.Same(e, queue.Peek());
            Assert.Equal(1, queue.Count);
            Assert.False(queue.IsEmpty);
            Assert.Same(e, queue.Dequeue());
            AssertEmptyQueue(queue);
        }
Exemple #5
0
        public void PriorityQueueRemoveTest()
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance, 4);

            AssertEmptyQueue(queue);

            TestNode a          = new TestNode(5);
            TestNode b          = new TestNode(10);
            TestNode c          = new TestNode(2);
            TestNode d          = new TestNode(6);
            TestNode notInQueue = new TestNode(-1);

            AssertEnqueue(queue, a);
            AssertEnqueue(queue, b);
            AssertEnqueue(queue, c);
            AssertEnqueue(queue, d);

            // Remove an element that isn't in the queue.
            Assert.False(queue.TryRemove(notInQueue));
            Assert.Same(c, queue.Peek());
            Assert.Equal(4, queue.Count);

            // Remove the last element in the array, when the array is non-empty.
            Assert.True(queue.TryRemove(b));
            Assert.Same(c, queue.Peek());
            Assert.Equal(3, queue.Count);

            // Re-insert the element after removal
            AssertEnqueue(queue, b);
            Assert.Same(c, queue.Peek());
            Assert.Equal(4, queue.Count);

            // Repeat remove the last element in the array, when the array is non-empty.
            Assert.True(queue.TryRemove(b));
            Assert.Same(c, queue.Peek());
            Assert.Equal(3, queue.Count);

            // Remove the head of the queue.
            Assert.True(queue.TryRemove(c));
            Assert.Same(a, queue.Peek());
            Assert.Equal(2, queue.Count);

            Assert.True(queue.TryRemove(a));
            Assert.Same(d, queue.Peek());
            Assert.Equal(1, queue.Count);

            Assert.True(queue.TryRemove(d));
            AssertEmptyQueue(queue);
        }