Exemplo n.º 1
0
 def test_insert(self):
     new_val = 5
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     max_heap.insert(new_val)
     assert len(max_heap.storage) == 11
     parent = max_heap.parent(max_heap.storage.index(new_val))
     assert max_heap.storage[parent] > new_val
Exemplo n.º 2
0
 def test_increase_key_exception(self):
     max_heap = MaxHeap([1, 2, 3])
     max_heap.build_max_heap()
     with pytest.raises(Exception):
         assert max_heap.increase_key(
             2, 0)  # 0 < 3, hence will raise exception
Exemplo n.º 3
0
 def test_extract_max_exception(self):
     max_heap = MaxHeap()  # storage is empty => should raise an exception
     max_heap.build_max_heap()
     with pytest.raises(Exception):
         max_heap.extract_max()
Exemplo n.º 4
0
 def test_increase_key(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     max_heap.increase_key(3, 17)  # increase the value at index 3 to 12
     assert max_heap.maximum() == 17
Exemplo n.º 5
0
 def test_extract_max(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     result = max_heap.extract_max()
     assert result == 16
     assert max_heap.heap_size == len(max_heap.storage) - 1
Exemplo n.º 6
0
 def test_maximum(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     assert max_heap.maximum() == 16
Exemplo n.º 7
0
 def test_heap_sort(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.heap_sort()
     assert max_heap.storage == [1, 2, 3, 4, 7, 8, 9, 10, 14, 16]
Exemplo n.º 8
0
 def test_build_max_heap(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     assert max_heap.storage == [16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
Exemplo n.º 9
0
 def test_max_heapify(self):
     max_heap = MaxHeap([1, 2, 3])
     max_heap.max_heapify(0)  # heapify at index 0
     assert max_heap.storage == [3, 2, 1]