예제 #1
0
 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)
예제 #2
0
 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())
예제 #3
0
 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)
예제 #4
0
    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())
예제 #5
0
    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))
예제 #6
0
 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())
예제 #7
0
 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())
예제 #8
0
 def test_clear_empty_heap(self):
     h = MinMaxHeap()
     self.assertIsNone(h.clear())
     self.assertEqual(h.size, 0)
     self.assertTrue(h.is_empty())
예제 #9
0
 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))
예제 #10
0
 def test_heap_creation_default(self):
     h = MinMaxHeap()
     self.assertTrue(h.is_empty())
     self.assertEqual(h.size, 0)
예제 #11
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)
예제 #12
0
 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)