예제 #1
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());
        }
예제 #2
0
 private static void AssertEnqueue(DefaultPriorityQueue <TestNode> queue, TestNode item)
 {
     Assert.True(queue.TryEnqueue(item));
     Assert.True(queue.Contains(item));
     // An element can not be inserted more than 1 time.
     Assert.Throws <ArgumentException>(() => queue.Enqueue(item));
 }
예제 #3
0
        public void PriorityQueueOrderTest(int[] input, int[] expectedOutput)
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance);

            foreach (int value in input)
            {
                queue.TryEnqueue(new TestNode(value));
            }

            for (int index = 0; index < expectedOutput.Length; index++)
            {
                var item = queue.Dequeue();
                Assert.Equal(expectedOutput[index], item.Value);
            }
            Assert.Equal(0, queue.Count);
        }
예제 #4
0
        public void PriorityChangeTest()
        {
            var queue = new DefaultPriorityQueue <TestNode>(TestNodeComparer.Instance, 0);

            AssertEmptyQueue(queue);
            TestNode a = new TestNode(10);
            TestNode b = new TestNode(20);
            TestNode c = new TestNode(30);
            TestNode d = new TestNode(25);
            TestNode e = new TestNode(23);
            TestNode f = new TestNode(15);

            queue.TryEnqueue(a);
            queue.TryEnqueue(b);
            queue.TryEnqueue(c);
            queue.TryEnqueue(d);
            queue.TryEnqueue(e);
            queue.TryEnqueue(f);

            e.Value = 35;
            queue.PriorityChanged(e);

            a.Value = 40;
            queue.PriorityChanged(a);

            a.Value = 31;
            queue.PriorityChanged(a);

            d.Value = 10;
            queue.PriorityChanged(d);

            f.Value = 5;
            queue.PriorityChanged(f);

            var expectedOrderList = new List <TestNode>(new[] { a, b, c, d, e, f });

            expectedOrderList.Sort(TestNodeComparer.Instance);
            Assert.Equal(expectedOrderList.Count, queue.Count);
            Assert.Equal(expectedOrderList.Count <= 0, queue.IsEmpty);
            for (var idx = 0; idx < expectedOrderList.Count; idx++)
            {
                Assert.Equal(expectedOrderList[idx], queue.Dequeue());
            }
            AssertEmptyQueue(queue);
        }