Example #1
0
        public void MinPriorityQueueClearRemovesAllItems()
        {
            var priorityQueue = new MinPriorityQueue <string, int>(50);

            List <Tuple <string, int> > items = new List <Tuple <string, int> >
            {
                new Tuple <string, int>("A_50", 50),
                new Tuple <string, int>("A_41", 41),
                new Tuple <string, int>("A_38", 38),
                new Tuple <string, int>("A_37", 37),
                new Tuple <string, int>("A_23", 23),
                new Tuple <string, int>("A_11", 11),
                new Tuple <string, int>("A_5", 5),
                new Tuple <string, int>("A_3", 3),
            }.Randomize()
            .ToList();

            priorityQueue.EnqueueRange(items);
            priorityQueue.Clear();

            Assert.IsTrue(priorityQueue.IsEmpty);
        }
Example #2
0
        public void AddingAfterClearingHeap()
        {
            var pq = new MinPriorityQueue <int, int>();

            int maxHeapElement = 50000;
            int minHeapElement = -50000;

            int addedElements = 0;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            //NOTE: some items are added more than once
            for (int i = 7; i > 0; i -= 2)
            {
                int el = minHeapElement;
                while (el <= maxHeapElement)
                {
                    pq.Enqueue(el, -el);
                    addedElements++;
                    el += i;
                }
            }

            if (pq.Count != addedElements)
            {
                Assert.Fail();
            }

            pq.Clear();

            if (pq.Count != 0)
            {
                Assert.Fail();
            }

            addedElements = 0;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            //NOTE: some items are added more than once
            for (int i = 7; i > 0; i -= 2)
            {
                int el = minHeapElement;
                while (el < maxHeapElement)
                {
                    pq.Enqueue(el, -el);
                    addedElements++;
                    el += i;
                }
            }

            if (pq.Count != addedElements)
            {
                Assert.Fail();
            }

            int removedElements = 0;
            var min             = pq.Peek();

            while (!pq.IsEmpty)
            {
                var kvp = pq.Peek();
                if (min.Key > kvp.Key)
                {
                    Assert.Fail();
                }

                min = pq.Dequeue();
                removedElements++;
            }

            Assert.IsTrue(pq.IsEmpty &&
                          pq.Count == 0 &&
                          addedElements == removedElements);
        }