def test_merge_non_empty_heap_with_empty_heap(self): ls = [-3, 5, 7, 9, 1, 5, 2] a = MaxHeap(ls) b = MaxHeap() self.assertIsNone(a.merge(b)) self.assertEqual(a.size, len(ls)) self.assertEqual(b.size, 0) self.assertTrue(b.is_empty())
def test_remove_max_when_heap_has_random_size(self): size = randint(3, 100) a = [randint(-100, 100) for _ in range(size)] h = MaxHeap(a) m = max(a) self.assertEqual(h.remove_max(), m) self.assertFalse(h.is_empty()) self.assertEqual(h.size, size - 1)
def test_add_multiple_elements(self): a = [randint(-100, 100) for _ in range(100)] h = MaxHeap() for i, elem in enumerate(a): self.assertIsNone(h.add(elem)) self.assertEqual(h.size, i + 1) self.assertFalse(h.is_empty()) self.assertEqual(h.find_max(), max(a))
def test_delete_all_when_heap_of_random_size(self): size = randint(3, 100) a = [randint(-100, 100) for _ in range(size)] h = MaxHeap(a) for _ in range(size): self.assertIsNone(h.delete(choice(a))) self.assertTrue(is_max_heap(h)) self.assertEqual(h.size, 0) self.assertTrue(h.is_empty())
def test_delete_when_elem_is_last(self): h = MaxHeap([3, 4]) self.assertIsNone(h.delete(4)) self.assertTrue(is_max_heap(h)) self.assertEqual(h.size, 1) self.assertFalse(h.is_empty())
def test_add_add_one(self): h = MaxHeap() self.assertIsNone(h.add(2)) self.assertEqual(h.size, 1) self.assertFalse(h.is_empty()) self.assertEqual(h.find_max(), 2)
def test_clear_heap_of_random_size(self): h = MaxHeap([randint(-100, 100) for _ in range(100)]) self.assertIsNone(h.clear()) self.assertEqual(h.size, 0) self.assertTrue(h.is_empty())
def test_clear_empty_heap(self): h = MaxHeap() self.assertIsNone(h.clear()) self.assertEqual(h.size, 0) self.assertTrue(h.is_empty())
def test_heap_creation_given_list(self): a = [12, 14, 28, 6, 7, 10, 18] h = MaxHeap(a) self.assertFalse(h.is_empty()) self.assertEqual(h.size, len(a))
def test_heap_creation_default(self): h = MaxHeap() self.assertTrue(h.is_empty()) self.assertEqual(h.size, 0)
def test_remove_max_when_heap_has_size_2(self): h = MaxHeap([11, 13]) self.assertEqual(h.remove_max(), 13) self.assertFalse(h.is_empty()) self.assertEqual(h.size, 1)
def test_remove_max_when_heap_has_size_1(self): h = MaxHeap([13]) self.assertEqual(h.remove_max(), 13) self.assertTrue(h.is_empty()) self.assertEqual(h.size, 0)