public void MinPriorityQueueShouldWorkCorrectly()
        {
            var queue = new PriorityQueue<int>(reversePriority: true);
            queue.Enqueue(5);
            queue.Enqueue(1);
            queue.Enqueue(18);
            queue.Enqueue(-9);

            Assert.AreEqual(-9, queue.Dequeue());
            Assert.AreEqual(1, queue.Dequeue());
            Assert.AreEqual(5, queue.Dequeue());
            Assert.AreEqual(18, queue.Dequeue());
        }
        public void MinPriorityQueueShouldWorkCorrectly()
        {
            var heap = new MinArrayHeap<int>();
            var queue = new PriorityQueue<int>(heap);
            queue.Enqueue(5);
            queue.Enqueue(1);
            queue.Enqueue(18);
            queue.Enqueue(-9);

            Assert.AreEqual(-9, queue.Dequeue());
            Assert.AreEqual(1, queue.Dequeue());
            Assert.AreEqual(5, queue.Dequeue());
            Assert.AreEqual(18, queue.Dequeue());
        }
        public void DefaultConstructorWithDefaultParametersShouldCreateMaxPriorityQueue()
        {
            var queue = new PriorityQueue<DateTime>();
            var dateA = new DateTime(2000, 1, 1);
            var dateB = new DateTime(2002, 1, 1);
            var dateC = new DateTime(1999, 1, 1);

            queue.Enqueue(dateA);
            queue.Enqueue(dateB);
            queue.Enqueue(dateC);

            Assert.AreEqual(dateB, queue.Dequeue());
            Assert.AreEqual(dateA, queue.Dequeue());
            Assert.AreEqual(dateC, queue.Dequeue());
        }
Exemplo n.º 4
0
        public void Test_ComprarerParam_ShouldCompareProperly()
        {
            var comparer = new StringComparerDescending();
            var pq = new PriorityQueue<string>(comparer);
            int count = 100;
            var min = "zzzzzzzzzz";
            for (int i = 0; i < count; i++)
            {
                var randomString = this.GetRandomString(10);
                pq.Enqueue(randomString);
                if (min.CompareTo(randomString) > 0)
                {
                    min = randomString;
                }
            }

            for (int i = 0; i < count - 1; i++)
            {
                var dequeuedNumber = pq.Dequeue();
            }

            var actual = pq.Dequeue().ToString();
            Assert.AreEqual(min, actual, "Comparer param should work properly.");
        }
Exemplo n.º 5
0
        public void Test_Dequeue_ShouldLeaveBiggestItemLast()
        {
            var pq = new PriorityQueue<int>();
            int count = 100000;
            var max = int.MinValue;
            for (int i = 0; i < count; i++)
            {
                var randomNumber = random.Next(0, count * 10);
                pq.Enqueue(randomNumber);
                if (max < randomNumber)
                {
                    max = randomNumber;
                }
            }

            for (int i = 0; i < count - 1; i++)
            {
                var dequeuedNumber = pq.Dequeue();
            }

            var actual = pq.Dequeue();
            Assert.AreEqual(max, actual, "Dequeue should leave biggest item last.");
        }