public void Peek() { // Create a new heap. ConcurrentBinaryMinHeap <int> heap = new ConcurrentBinaryMinHeap <int>(); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Expect Peek() to return null for an empty heap. Assert.That(heap.Peek(), Is.EqualTo(null)); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Store an element and insert it into the heap. PriorityValuePair <int> elem1 = new PriorityValuePair <int>(1f, 2); heap.Push(elem1); // Ensure that the element was inserted into the heap as the root element. Assert.That(heap.Count, Is.EqualTo(1)); Assert.That(heap.Peek(), Is.EqualTo(elem1)); // Ensure that the element was not removed from the heap. Assert.That(heap.Count, Is.EqualTo(1)); // Insert another element with higher priority than the last. PriorityValuePair <int> elem2 = new PriorityValuePair <int>(2f, 4); heap.Push(elem2); // Ensure that Peak() returns the new root element. Assert.That(heap.Peek(), Is.EqualTo(elem2)); }
public void SwapElements() { // Create a new heap. ConcurrentBinaryMinHeap <int> heap = new ConcurrentBinaryMinHeap <int>(); // Enqueue an element into the queue. var elem1 = new PriorityValuePair <int>(2f, 4); heap.Push(elem1); // Ensure that the element was inserted. Assert.That(heap.Count, Is.EqualTo(1)); Assert.That(heap.Peek(), Is.EqualTo(elem1)); // Try to HeapSwapElements() while the queue only contains 1 element and expect an // InvalidOperationException to be thrown. Assert.Throws <InvalidOperationException>(() => { heap.SwapElements(0, 1); }); // Enqueue another element with higher priority than the last. var elem2 = new PriorityValuePair <int>(1f, 2); heap.Push(elem2); // Ensure that the element was inserted and that the 1st (higher priority) element is // still at the root of the heap. Assert.That(heap.Count, Is.EqualTo(2)); Assert.That(heap.Peek(), Is.EqualTo(elem1)); // Try to HeapSwapElements() with an invalid index1 and expect an // ArgumentOutOfRangeException to be thrown. Assert.Throws <ArgumentOutOfRangeException>(() => { heap.SwapElements(-1, 1); }); // Try to HeapSwapElements() with an invalid index2 and expect an // ArgumentOutOfRangeException to be thrown. Assert.Throws <ArgumentOutOfRangeException>(() => { heap.SwapElements(0, -1); }); // Actually swap elements now. heap.SwapElements(0, 1); // Ensure that the elements were swapped. Assert.That(heap.Count, Is.EqualTo(2)); Assert.That(heap.Peek(), Is.EqualTo(elem2)); Assert.That(heap.Contains(elem1), Is.True); }
public void PopValue() { // Create a new heap. ConcurrentBinaryMinHeap <int> heap = new ConcurrentBinaryMinHeap <int>(); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Try to PopPriority() and expect an NullReferenceException to be thrown. Assert.Throws <NullReferenceException>(() => { heap.PopValue(); }); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Store an element and insert it into the heap. PriorityValuePair <int> elem = new PriorityValuePair <int>(1f, 2); heap.Push(elem); // Ensure that the element was inserted into the heap. Assert.That(heap.Peek(), Is.EqualTo(elem)); // Ensure that the value of the pushed element is returned. Assert.That(heap.PopValue(), Is.EqualTo(2)); // Ensure that the element was removed from the heap. Assert.That(heap.Count, Is.EqualTo(0)); }
public void Pop() { // Create a new heap. ConcurrentBinaryMinHeap <int> heap = new ConcurrentBinaryMinHeap <int>(); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Expect Pop() to return null for an empty heap. Assert.That(heap.Pop(), Is.EqualTo(null)); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Store an element and insert it into the heap. PriorityValuePair <int> elem = new PriorityValuePair <int>(1f, 2); heap.Push(elem); // Ensure that the element was inserted into the heap. Assert.That(heap.Count, Is.EqualTo(1)); Assert.That(heap.Peek(), Is.EqualTo(elem)); // Ensure that the returned element points to the same object we stored earlier. Assert.That(heap.Pop(), Is.EqualTo(elem)); // Ensure that the element was removed from the heap. Assert.That(heap.Count, Is.EqualTo(0)); }
public void PushElement() { // Create a new heap. ConcurrentBinaryMinHeap <int> heap = new ConcurrentBinaryMinHeap <int>(); // Ensure that the heap is empty. Assert.That(heap.Count, Is.EqualTo(0)); // Store an element and insert it into the heap. PriorityValuePair <int> elem = new PriorityValuePair <int>(1f, 2); heap.Push(elem); // Ensure that the element was inserted into the heap. Assert.That(heap.Peek(), Is.EqualTo(elem)); // Store another element with higher priority and insert it as well. elem = new PriorityValuePair <int>(2f, 4); heap.Push(elem); // Ensure that the element was inserted into the queue and is at the root. Assert.That(heap.Peek(), Is.EqualTo(elem)); }