Exemplo n.º 1
0
        public void Contains()
        {
            // Create a new heap.
            ConcurrentBinaryMinHeap <int> heap = new ConcurrentBinaryMinHeap <int>();

            // Create and store a new element.
            PriorityValuePair <int> elem = new PriorityValuePair <int>(1f, 2);

            // Ensure the queue contains the element.
            Assert.That(heap.Contains(elem), Is.False);

            // Push it onto the heap.
            heap.Push(elem);

            // Ensure the queue now contains the element.
            Assert.That(heap.Contains(elem), Is.True);
        }
Exemplo n.º 2
0
        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);
        }