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()); }
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)); }
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); }
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); }