def test_maxheap_insert(self): """ Test method "insert". """ ref = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) res = max_heap.MaxHeap() res.insert(4) res.insert(16) res.insert(9) res.insert(2) res.insert(1) res.insert(10) res.insert(3) res.insert(14) res.insert(8) res.insert(7) self.assertEqual(res, ref)
def test_maxheap_build_maxheap_iterative(self): """ Test method "build_max_heap" - iterative. """ ref = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] res = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7] heap = max_heap.MaxHeap() heap.build_max_heap(res, False) self.assertEqual(res, ref)
def test_maxheap_clear(self): """ Test method "clear". """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) heap.clear() test1 = len(heap) == 0 test2 = heap.array == [] test = test1 and test2 self.assertTrue(test)
def test_maxheap_parent(self): """ Test method "parent". """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) index = 5 parent_index = heap.parent(index) parent_value = heap.array[parent_index] test1 = parent_index == 2 test2 = parent_value == 10 test = test1 and test2 self.assertTrue(test)
def test_maxheap_right_child_not(self): """ Test method "right_child" - inverted. """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) index = 2 right_index = heap.right_child(index) right_value = heap.array[right_index] test1 = right_index == 4 test2 = right_value == 7 test = test1 and test2 self.assertFalse(test)
def setUp(self): self.heap1 = max_heap.MaxHeap() self.heap1.insert(4) self.heap1.insert(1) self.heap1.insert(3) self.heap1.insert(2) self.heap1.insert(16) self.heap1.insert(9) self.heap1.insert(10) self.heap1.insert(14) self.heap1.insert(8) self.heap1.insert(7)
def test_maxheap_left_child_not(self): """ Test method "left_child" - inverted. """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) index = 2 left_index = heap.left_child(index) left_value = heap.array[left_index] test1 = left_index == 3 test2 = left_value == 8 test = test1 and test2 self.assertFalse(test)
def test_maxheap_is_leaf(self): """ Test method "is_lead". """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) t_1 = heap.is_leaf(0) == False t_2 = heap.is_leaf(1) == False t_3 = heap.is_leaf(2) == False t_4 = heap.is_leaf(3) == False t_5 = heap.is_leaf(4) == False t_6 = heap.is_leaf(5) == True t_7 = heap.is_leaf(6) == True t_8 = heap.is_leaf(7) == True t_9 = heap.is_leaf(8) == True t_10 = heap.is_leaf(9) == True test = t_1 and t_2 and t_3 and t_4 and t_5 and t_6 and t_7 and t_8 and t_9 and t_10 self.assertTrue(test)
def test_maxheap_is_empty_not(self): """ Test method "is_empty" - inverted. """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) self.assertFalse(heap.is_empty())
def test_maxheap_is_empty(self): """ Test method "is_empty". """ heap = max_heap.MaxHeap() self.assertTrue(heap.is_empty())
def test_maxheap_len(self): """ Test operator "len". """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) self.assertEqual(10, len(heap))
def test_maxheap_is_max_heap(self): """ Test method "is_max_heap". """ heap = max_heap.MaxHeap([4, 1, 3, 2, 16, 9, 10, 14, 8, 7]) self.assertTrue(heap.is_max_heap())