Example #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
Example #2
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
Example #3
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
Example #4
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
Example #5
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()
Example #6
0
 def test_maximum(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     assert max_heap.maximum() == 16
Example #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]
Example #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]
Example #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]
Example #10
0
 def test_static_methods(self):
     assert MaxHeap.parent(1) == 0
     assert MaxHeap.left(1) == 3
     assert MaxHeap.right(1) == 4