static void TestPopTop() { Console.WriteLine("======| Testing Adding and PopTop |======"); Console.WriteLine(); MyHeap <int> heap = new MyHeap <int>(); int previousVal, currentVal; heap.Add(10); Console.WriteLine("Adding 10 to the heap..."); heap.Add(21); Console.WriteLine("Adding 21 to the heap..."); heap.Add(1); Console.WriteLine("Adding 1 to the heap..."); heap.Add(33); Console.WriteLine("Adding 33 to the heap..."); heap.Add(13); Console.WriteLine("Adding 13 to the heap..."); heap.Add(55); Console.WriteLine("Adding 55 to the heap..."); heap.Add(2); Console.WriteLine("Adding 2 to the heap..."); heap.Add(0); Console.WriteLine("Adding 0 to the heap..."); heap.Add(77); Console.WriteLine("Adding 77 to the heap..."); heap.Add(67); Console.WriteLine("Adding 67 to the heap..."); heap.Add(22); Console.WriteLine("Adding 22 to the heap..."); previousVal = heap.PopTop(); while (heap.Count > 0) { currentVal = heap.PopTop(); Debug.Assert(currentVal < previousVal); Console.WriteLine("Current popped value of " + currentVal + " is less than the previous popped value of " + previousVal); previousVal = currentVal; } Console.WriteLine(); }
static void Main(string[] args) { TestPopTop(); TestHeapSort(); Random rand = new Random(); Console.WriteLine("======| Testing Invariants of Heaps|======"); Console.WriteLine(); MyHeap <int> heap1 = new MyHeap <int>(); for (int i = 0; i < 1000; i++) { heap1.Add(rand.Next(1, 1500)); } TestInvariants(heap1); Console.WriteLine(); MyHeap <int> heap2 = new MyHeap <int>(); for (int i = 0; i < 10000; i++) { heap2.Add(rand.Next(-30000, 30000)); } TestInvariants(heap2); Console.WriteLine(); for (int i = 0; i < rand.Next(300, 700); i++) { heap1.PopTop(); } TestInvariants(heap1); Console.WriteLine(); for (int i = 0; i < 10000; i++) { heap2.PopTop(); } TestInvariants(heap2); Console.ReadLine(); }