Exemple #1
0
        public void HeapEqualGivesEarlierQueued()
        {
            // In this test we see if equal items on the heap always returns the earlier item.

            // We test for both heap orders.
            HeapOrder[] heapOrders = new HeapOrder[] { HeapOrder.Min, HeapOrder.Max };

            // For each heap order
            foreach (HeapOrder heapOrder in heapOrders)
            {
                // Create our heap with the given order
                Heap <int> intHeap = new Heap <int>(heapOrder, new Heap <int> .CompareFunction(
                                                        (int first, int second) =>
                {
                    // We'll return that everything is equal.
                    return(0);
                }));


                // Generate random numbers and add them to our list and heap.
                Random     random = new Random();
                int        count  = 20;
                List <int> items  = new List <int>();
                for (int i = 0; i < count; i++)
                {
                    items.Add(random.Next(int.MinValue, int.MaxValue));
                    intHeap.Push(items[i]);
                }

                // Now for each item in our list, we make sure the order is the same.
                while (items.Count > 0)
                {
                    Assert.Equal(items[0], intHeap.Pop());
                    items.RemoveAt(0);
                }

                Assert.Empty(items);
                Assert.Equal(0, intHeap.Count);
            }
        }
Exemple #2
0
 public Heap(HeapOrder order)
 {
     _order = order;
 }