Beispiel #1
0
        public void Remove_MiddleElement_DecreasesCount()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(2);
            var pointer = heap.Insert(1);

            heap.Insert(0);
            heap.Remove(pointer);
            Assert.That(heap.Count(), Is.EqualTo(2));
        }
Beispiel #2
0
        public void Decrease_MiddleElement_ExtractsAllElementsCorrectly()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(2);
            var pointer = heap.Insert(1);

            heap.Insert(0);
            heap.Decrease(pointer, -1);
            var elements = ExtractAll(heap).ToArray();

            Assert.That(elements, Is.EqualTo(new[] { -1, 0, 2 }));
        }
Beispiel #3
0
        public void Merge_TwoNonEmpty_ExtractsAllElementsCorrectly()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            heap.Insert(2);

            var other = new PairingHeap <int>();

            other.Insert(1);
            other.Insert(3);

            heap.Merge(other);
            Assert.That(ExtractAll(heap), Is.EqualTo(new[] { 0, 1, 2, 3 }));
        }
Beispiel #4
0
        public void Remove_ManyElements_DecreasesCount()
        {
            var heap = new PairingHeap <int>();

            for (var i = 0; i < 15; i++)
            {
                heap.Insert(i);
            }

            var pointer = heap.Insert(5);

            heap.Remove(pointer);

            Assert.That(heap.Count(), Is.EqualTo(15));
        }
Beispiel #5
0
        public void ExtractMinimum_SingleElement_ReturnsElement()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(42);
            Assert.That(heap.ExtractMinimum(), Is.EqualTo(42));
        }
Beispiel #6
0
        public void Insert_Empty_IsNotEmpty()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            Assert.That(heap.IsEmpty, Is.False);
        }
Beispiel #7
0
        public void GetItem_SingleElement_ReturnsValue()
        {
            var heap    = new PairingHeap <int>();
            var pointer = heap.Insert(42);

            Assert.That(heap[pointer], Is.EqualTo(42));
        }
Beispiel #8
0
        public void Merge_WithEmpty_CountUnchanged()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            heap.Merge(new());
            Assert.That(heap.Count(), Is.EqualTo(1));
        }
Beispiel #9
0
        public void ExtractMinimum_SingleElement_RemovesElement()
        {
            var heap = new PairingHeap <int>();

            heap.Insert(0);
            heap.ExtractMinimum();
            Assert.That(heap.IsEmpty, Is.True);
            Assert.That(() => heap.Minimum, Throws.InvalidOperationException);
        }
Beispiel #10
0
        public void Merge_WithNonEmpty_ClearsArgument()
        {
            var heap  = new PairingHeap <int>();
            var other = new PairingHeap <int>();

            other.Insert(0);
            heap.Merge(other);
            Assert.That(other.IsEmpty, Is.True);
        }
Beispiel #11
0
        public void Merge_WithNonEmpty_CountChanged()
        {
            var heap  = new PairingHeap <int>();
            var other = new PairingHeap <int>();

            other.Insert(0);
            heap.Merge(other);
            Assert.That(heap.Count(), Is.EqualTo(1));
        }
Beispiel #12
0
        public void Minimum_ManyElements_IsCorrect(params int[] values)
        {
            var heap = new PairingHeap <int>();

            foreach (var value in values)
            {
                heap.Insert(value);
            }

            Assert.That(heap.Minimum, Is.EqualTo(values.Min()));
        }
Beispiel #13
0
        public void Insert_MultipleTimesIntoEmpty_CountIsCorrect(int size)
        {
            var heap = new PairingHeap <int>();

            for (var i = 0; i < size; i++)
            {
                heap.Insert(0);
            }

            Assert.That(heap.Count(), Is.EqualTo(size));
        }
Beispiel #14
0
        public void ExtractMinimum_ManyElements_CanSortSequence(params int[] values)
        {
            var heap = new PairingHeap <int>();

            foreach (var value in values)
            {
                heap.Insert(value);
            }

            var sorted = new List <int>();

            while (!heap.IsEmpty)
            {
                sorted.Add(heap.ExtractMinimum());
            }

            Assert.That(sorted, Is.EqualTo(values.OrderBy(x => x).ToList()));
        }