Exemple #1
0
        public void Add_SortedElements_ReturnsExpected()
        {
            // arrange
            MaxHeap heap = new MaxHeap(1000);

            // act
            heap.Add(3);
            heap.Add(2);
            heap.Add(1);

            // assert
            Assert.AreEqual(3, heap.Data.ElementAt(0));
            Assert.AreEqual(2, heap.Data.ElementAt(1));
            Assert.AreEqual(1, heap.Data.ElementAt(2));
        }
Exemple #2
0
        public void Add_RandomElements_LevelsSorted()
        {
            // arrange
            const int heapSize = 1000000;
            MaxHeap heap = new MaxHeap(heapSize);

            // act
            Random r = new Random(heapSize);
            for (int i = 0; i < heapSize; i++)
            {
                heap.Add(r.Next());
            }

            // assert
            ValidateSorting(heap);
        }
Exemple #3
0
        public void Remove_HalfRandomElements_HeapSorted()
        {
            // arrange
            const int heapSize = 10;
            MaxHeap heap = new MaxHeap(heapSize);

            // act
            Random r = new Random(heapSize);
            for (int i = 0; i < heapSize; i++)
            {
                heap.Add(r.Next() % 10);
            }

            int j = heapSize / 2;
            while (j >= 0)
            {
                heap.RemoveAt(0);
                j--;
            }

            // assert
            ValidateSorting(heap);
        }
Exemple #4
0
        public void Remove_RemoveAllFromTail_EmptyHeap()
        {
            // arrange
            MaxHeap heap = new MaxHeap(1000);

            // act
            heap.Add(3);
            heap.Add(2);
            heap.Add(1);
            heap.RemoveAt(2);
            heap.RemoveAt(1);
            heap.RemoveAt(0);

            // assert
            Assert.AreEqual(0, heap.Size);
        }
Exemple #5
0
        public void Remove_RandomElements_EmptyHeap()
        {
            // arrange
            const int heapSize = 1000000;
            MaxHeap heap = new MaxHeap(heapSize);

            // act
            Random r = new Random(heapSize);
            for (int i = 0; i < heapSize; i++)
            {
                heap.Add(r.Next());
            }

            int j = heapSize - 1;
            while (j >= 0)
            {
                heap.RemoveAt(0);
                j--;
            }

            // assert
            Assert.AreEqual(0, heap.Size);
        }