def test_add_add_one(self): h = MinMaxHeap() self.assertIsNone(h.add(2)) self.assertEqual(h.size, 1) self.assertFalse(h.is_empty()) self.assertEqual(h.find_max(), 2) self.assertEqual(h.find_min(), 2)
def test_merge_non_empty_heap_with_empty_heap(self): ls = [-3, 5, 7, 9, 1, 5, 2] a = MinMaxHeap(ls) b = MinMaxHeap() self.assertIsNone(a.merge(b)) self.assertEqual(a.size, len(ls)) self.assertEqual(b.size, 0) self.assertTrue(b.is_empty())
def test_remove_min_when_heap_has_random_size(self): size = randint(3, 100) a = [randint(-100, 100) for _ in range(size)] h = MinMaxHeap(a) m = min(a) self.assertEqual(h.remove_min(), m) self.assertFalse(h.is_empty()) self.assertEqual(h.size, size - 1)
def test_delete_all_when_heap_of_random_size(self): size = randint(3, 100) a = [randint(-100, 100) for _ in range(size)] h = MinMaxHeap(a) for _ in range(size): self.assertIsNone(h.delete(choice(a))) self.assertTrue(is_min_max_heap(h)) self.assertEqual(h.size, 0) self.assertTrue(h.is_empty())
def test_add_multiple_elements(self): a = [randint(-100, 100) for _ in range(100)] h = MinMaxHeap() 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)) self.assertEqual(h.find_min(), min(a))
def test_delete_when_elem_is_last(self): h = MinMaxHeap([3, 4]) self.assertIsNone(h.delete(4)) self.assertTrue(is_min_max_heap(h)) self.assertEqual(h.size, 1) self.assertFalse(h.is_empty())
def test_clear_heap_of_random_size(self): h = MinMaxHeap([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 = MinMaxHeap() 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 = MinMaxHeap(a) self.assertFalse(h.is_empty()) self.assertEqual(h.size, len(a))
def test_heap_creation_default(self): h = MinMaxHeap() self.assertTrue(h.is_empty()) self.assertEqual(h.size, 0)
def test_remove_min_when_heap_has_size_2(self): h = MinMaxHeap([11, 13]) self.assertEqual(h.remove_min(), 11) self.assertFalse(h.is_empty()) self.assertEqual(h.size, 1)
def test_remove_min_when_heap_has_size_1(self): h = MinMaxHeap([13]) self.assertEqual(h.remove_min(), 13) self.assertTrue(h.is_empty()) self.assertEqual(h.size, 0)