コード例 #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
コード例 #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
コード例 #3
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
コード例 #4
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()
コード例 #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
コード例 #6
0
 def test_maximum(self):
     max_heap = MaxHeap(TestMaxHeap.storage)
     max_heap.build_max_heap()
     assert max_heap.maximum() == 16
コード例 #7
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]