public void Clear() { _binaryMaxHeap.Add(5); _binaryMaxHeap.Add(4); _binaryMaxHeap.Add(9); _binaryMaxHeap.Add(13); Assert.NotZero(_binaryMaxHeap.Count); _binaryMaxHeap.Clear(); Assert.Zero(_binaryMaxHeap.Count); }
public void AddingAfterClearingHeap() { var heap = new BinaryMaxHeap <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) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } heap.Clear(); if (heap.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) { heap.Add(el); addedElements++; el += i; } } if (heap.Count != addedElements) { Assert.Fail(); } int removedElements = 0; var max = heap.PeekMax(); while (!heap.IsEmpty) { if (max < heap.PeekMax()) { Assert.Fail(); } max = heap.PopMax(); removedElements++; } Assert.IsTrue(heap.IsEmpty && heap.Count == 0 && addedElements == removedElements); }