From bd45c66bf531e470e3ab94d3a44ddad638fdcdb6 Mon Sep 17 00:00:00 2001 From: nelson-brochado Date: Sun, 1 Jan 2017 16:50:42 +0100 Subject: [PATCH] Converted test_MinHeap tests to unit tests --- run.sh | 5 +- setup.py | 3 +- tests/ds/test_MinHeap.py | 918 ++++++++++++++++++--------------------- 3 files changed, 430 insertions(+), 496 deletions(-) diff --git a/run.sh b/run.sh index 1ef47098..9dfab3e9 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,8 @@ #!/usr/bin/env bash +# RUN SPECIFIC TEST +# ./run.sh -st folder_name_inside_tests test_name.py + # colors used when printing GREEN=$(tput setaf 2) RED=$(tput setaf 1) @@ -86,8 +89,6 @@ install_dependencies() printf "${GREEN}Done.${NORMAL}\n\n" } -# Syntax to invoke the run of a specific test: -# ./run.sh -st folder_name_inside_tests test_name.py test_in_virtual_environment() { # Creates and switches to the new virtual environment diff --git a/setup.py b/setup.py index d28a2629..1c8b434a 100755 --- a/setup.py +++ b/setup.py @@ -4,7 +4,6 @@ """ ## Resources - [https://packaging.python.org/en/latest/distributing/#readme-rst](https://packaging.python.org/en/latest/distributing/#readme-rst) -- [https://packaging.python.org/en/latest/distributing/#readme-rst](https://packaging.python.org/en/latest/distributing/#readme-rst) - [http://docs.python-guide.org/en/latest/writing/structure/](http://docs.python-guide.org/en/latest/writing/structure/) """ @@ -26,7 +25,7 @@ def read(file_name): description="Algorithms and Data Structures", long_description=read("README.md"), license="MIT", - keywords="algorithms data structures", + keywords="algorithms data structures python", url="https://github.com/nbro/ands", include_package_data=True, exclude_package_data={'': ['__pycache__']}, diff --git a/tests/ds/test_MinHeap.py b/tests/ds/test_MinHeap.py index 3c4fd86f..800fe021 100755 --- a/tests/ds/test_MinHeap.py +++ b/tests/ds/test_MinHeap.py @@ -6,7 +6,7 @@ Creation: 14/02/16 -Last update: 30/06/16 +Last update: 01/01/17 Tests for the MinHeap class. """ @@ -17,639 +17,573 @@ from ands.ds.MinHeap import MinHeap, is_min_heap, HeapNode -def assert_is_min_heap(h): - assert is_min_heap(h) - - class TestMinHeap(unittest.TestCase): + def test_empty_heap_creation(self): h = MinHeap() - assert_is_min_heap(h) - assert h.is_empty() - assert h.size() == 0 + self.assertTrue(is_min_heap(h)) + self.assertTrue(h.is_empty()) + self.assertEqual(h.size(), 0) def test_non_empty_heap_creation(self): ls = [12, 14, 28, 6, 7, 10, 18] h = MinHeap(ls) - assert not h.is_empty() - assert h.size() == len(ls) - assert_is_min_heap(h) + self.assertFalse(h.is_empty()) + self.assertEqual(h.size(), len(ls)) + self.assertTrue(is_min_heap(h)) - def test_build_min_heap(self): + def test_build_min_heap_one(self): h = MinHeap([12]) - assert h.search_by_value(12) == 0 - assert h.contains(12) - assert h.size() == 1 - assert not h.is_empty() - assert h.find_min() == HeapNode(12) - assert_is_min_heap(h) - assert h.remove_min() == HeapNode(12) - assert h.is_empty() - assert_is_min_heap(h) + self.assertFalse(h.is_empty()) + self.assertEqual(h.size(), 1) + self.assertTrue(h.contains(12)) + self.assertEqual(h.search_by_value(12), 0) + self.assertEqual(h.find_min(), HeapNode(12)) + self.assertTrue(is_min_heap(h)) - h = MinHeap([28, 14, 12, 10, 7, 6, 18]) - assert h.search_by_value(6) == 0 - assert h.contains(28) - assert h.contains(7) - assert h.size() == 7 - assert not h.is_empty() - assert h.find_min() == HeapNode(6) - assert_is_min_heap(h) + self.assertEqual(h.remove_min(), HeapNode(12)) + self.assertTrue(h.is_empty()) + self.assertTrue(is_min_heap(h)) + + def test_build_min_heap_many(self): + ls = [28, 14, 12, 10, 7, 6, 18] + h = MinHeap(ls) + + self.assertFalse(h.is_empty()) + self.assertEqual(h.size(), 7) + self.assertEqual(h.search_by_value(6), 0) + self.assertEqual(h.find_min(), HeapNode(6)) + self.assertTrue(is_min_heap(h)) + + for e in ls: + self.assertTrue(h.contains(e)) m = h.remove_min() - assert m == HeapNode(6) - assert h.find_min() == HeapNode(7) - assert h.search_by_value(6) == -1 - assert not h.contains(6) - assert h.contains(7) - assert h.size() == 6 - assert not h.is_empty() - assert_is_min_heap(h) + self.assertFalse(h.is_empty()) + self.assertEqual(h.size(), 6) + self.assertEqual(m, HeapNode(6)) + self.assertEqual(h.find_min(), HeapNode(7)) + self.assertEqual(h.search_by_value(6), -1) + self.assertFalse(h.contains(6)) + self.assertTrue(is_min_heap(h)) def test_push_down(self): h = MinHeap([28, 14, 12, 10, 7, 6, 18]) h.heap[0] = HeapNode(94) h.push_down(0) - assert_is_min_heap(h) + self.assertTrue(is_min_heap(h)) def test_push_up(self): h = MinHeap([28, 14, 12, 10, 7, 6, 18]) h.heap[h.size() - 1] = HeapNode(3) h.push_up(h.size() - 1) - assert_is_min_heap(h) + self.assertTrue(is_min_heap(h)) - def test_add(self): + def test_add_none(self): h = MinHeap() + self.assertRaises(ValueError, h.add, None) - try: - h.add(None) - assert False - except ValueError: - pass - + def test_add_one(self): + h = MinHeap() h.add(12) - assert not h.is_empty() - assert h.size() == 1 - assert h.find_min() == HeapNode(12) - assert h.heap[0] == h.find_min() - assert_is_min_heap(h) - assert h.remove_min() == HeapNode(12) - assert h.is_empty() + self.assertFalse(h.is_empty()) + self.assertEqual(h.size(), 1) + self.assertEqual(h.find_min(), HeapNode(12)) + self.assertEqual(h.heap[0], h.find_min()) + self.assertTrue(is_min_heap(h)) + + self.assertEqual(h.remove_min(), HeapNode(12)) + self.assertTrue(h.is_empty()) + def test_add_many(self): + h = MinHeap() ls = [] t = 100 for i in range(t): ls.append(randint(-100, 100)) h.add(ls[-1]) - assert h.size() == (i + 1) - assert h.contains(HeapNode(ls[-1])) - assert_is_min_heap(h) + self.assertEqual(h.size(), i + 1) + self.assertTrue(h.contains(HeapNode(ls[-1]))) + self.assertTrue(is_min_heap(h)) c = 0 while not h.is_empty(): n = h.remove_min() - assert n and n.key in ls + self.assertIsNotNone(n) + self.assertTrue(n.key in ls) c += 1 - assert_is_min_heap(h) - - assert c == t - assert h.is_empty() + self.assertTrue(is_min_heap(h)) - s = h.size() - h.add(HeapNode(1024)) - assert h.size() == (s + 1) - assert h.search(HeapNode(1024)) != -1 - assert_is_min_heap(h) + self.assertEqual(c, t) + self.assertTrue(h.is_empty()) - s = h.size() + # TODO: test_add_many_with_value + def test_add_one_with_value(self): + h = MinHeap() h.add(HeapNode(2048, "2^11")) - assert h.size() == (s + 1) - assert h.contains(HeapNode(2048, "2^11")) - assert_is_min_heap(h) + self.assertEqual(h.size(), 1) + self.assertTrue(h.contains(HeapNode(2048, "2^11"))) + self.assertTrue(is_min_heap(h)) + + def test_delete_bad_index(self): + h = MinHeap([18, 6, 7, 28, 14, 12]) + + self.assertRaises(IndexError, h.delete, -1) + self.assertRaises(IndexError, h.delete, 6) + self.assertRaises(TypeError, h.delete, 3.14159) + self.assertRaises(TypeError, h.delete, None) + self.assertRaises(TypeError, h.delete, "0") + self.assertTrue(is_min_heap(h)) def test_delete(self): h = MinHeap([12, 14, 28, 7, 6, 18]) - def test_bad_index(i): - try: - h.delete(i) - assert False - except IndexError: - pass - except TypeError: - pass - - test_bad_index(-1) - test_bad_index(None) - test_bad_index(6) - test_bad_index("0") - test_bad_index(3.14159) - assert_is_min_heap(h) - while not h.is_empty(): - r = randint(0, h.size() - 1) - n = h.delete(r) - assert n - assert_is_min_heap(h) + p = randint(0, h.size() - 1) + n = h.delete(p) + self.assertIsNotNone(n) + self.assertTrue(is_min_heap(h)) - assert h.is_empty() + self.assertTrue(h.is_empty()) def test_find_min(self): - h = MinHeap([28, 14, 12, 10]) - assert h.size() == 4 - - m = h.find_min() - assert m == h.remove_min() == HeapNode(10) - assert h.size() == 3 - assert_is_min_heap(h) - - m = h.find_min() - assert m == h.remove_min() == HeapNode(12) - assert h.size() == 2 - assert_is_min_heap(h) - - m = h.find_min() - assert m == h.remove_min() == HeapNode(14) - assert h.size() == 1 - assert_is_min_heap(h) - - m = h.find_min() - assert m == h.remove_min() == HeapNode(28) - assert h.size() == 0 - assert not h.find_min() - assert_is_min_heap(h) + ls = [28, 14, 12, 10] + h = MinHeap(ls) + + n = len(ls) + for i in range(n): + self.assertEqual(HeapNode(min(ls)), h.find_min()) + self.assertEqual(h.find_min(), h.remove_min()) + self.assertEqual(h.size(), len(ls) - 1) + ls.remove(min(ls)) + self.assertTrue(is_min_heap(h)) + + self.assertEqual(len(ls), 0) + self.assertTrue(h.is_empty()) def test_remove_min(self): ls = [28, 14, 12, 10, 7, 6, 18] - h = MinHeap(ls) - assert_is_min_heap(h) c = len(ls) while not h.is_empty(): m = h.remove_min() + self.assertIsNotNone(m) + self.assertIn(m.key, ls) + self.assertEqual(h.size(), c - 1) + for e in h.heap: - assert m.key <= e.key - assert m - assert m.key in ls - assert h.size() == (c - 1) + self.assertLessEqual(m.key, e.key) + c -= 1 - assert_is_min_heap(h) + self.assertTrue(is_min_heap(h)) - assert c == 0 - assert not h.remove_min() - assert_is_min_heap(h) + self.assertEqual(c, 0) + self.assertIsNone(h.remove_min()) + self.assertTrue(is_min_heap(h)) - def test_search(self): + def test_search_when_exists(self): ls = [28, 14, 12, 10, 7, 6, 18] h = MinHeap(ls) v = h.search(12) - assert v in range(0, len(ls)) - assert h.size() == len(ls) - assert_is_min_heap(h) + self.assertIn(v, range(0, len(ls))) + self.assertEqual(h.size(), len(ls)) + self.assertTrue(is_min_heap(h)) v = h.search(HeapNode(14)) - assert v in range(0, len(ls)) - assert h.size() == len(ls) - assert_is_min_heap(h) - - v = h.search(HeapNode(12, "Noi")) - assert v == -1 - assert h.size() == len(ls) - assert_is_min_heap(h) - - try: - h.search(None) - assert False - except ValueError: - assert_is_min_heap(h) - - def test_search_by_value(self): - ls = [28, 14, 12] + self.assertIn(v, range(0, len(ls))) + self.assertEqual(h.size(), len(ls)) + self.assertTrue(is_min_heap(h)) + + def test_search_when_not_exists(self): + ls = [28, 12, 14] + h = MinHeap(ls) + + v = h.search(HeapNode(12, "twelve")) + self.assertEqual(v, -1) + self.assertEqual(h.size(), len(ls)) + self.assertTrue(is_min_heap(h)) + + self.assertRaises(ValueError, h.search, None) + self.assertTrue(is_min_heap(h)) + + def test_search_by_value_bad(self): + ls = [11, 12, 13, 14] h = MinHeap(ls) - try: - h.search_by_value(HeapNode(14)) - assert False - except Exception: - assert_is_min_heap(h) + self.assertRaises(Exception, h.search_by_value, HeapNode(13)) + self.assertTrue(is_min_heap(h)) - try: - v = h.search_by_value(None) - assert False - except ValueError: - assert_is_min_heap(h) + self.assertRaises(ValueError, h.search_by_value, None) + self.assertTrue(is_min_heap(h)) + + def test_search_by_value_default(self): + ls = [28, 14, 12] + h = MinHeap(ls) v = h.search_by_value(14) - assert v in range(0, len(ls)) - assert h.size() == len(ls) - assert_is_min_heap(h) + self.assertIn(v, range(0, len(ls))) + self.assertEqual(h.size(), len(ls)) + self.assertTrue(is_min_heap(h)) - ls = [(12, "Noi"), (14, "Tu")] + def test_search_by_value_custom(self): + ls = [(12, "twelve"), (14, "fourteen")] h = MinHeap(ls) - v = h.search_by_value("Tu") - assert v in range(0, len(ls)) - assert h.size() == len(ls) - assert_is_min_heap(h) + + v = h.search_by_value("fourteen") + self.assertIn(v, range(0, len(ls))) + self.assertEqual(h.size(), len(ls)) + self.assertTrue(is_min_heap(h)) def test_contains(self): ls = [28, 14, 12] h = MinHeap(ls) - assert h.contains(28) and h.contains(14) and h.contains(12) - assert not h.contains(200) and not h.contains(7) - assert_is_min_heap(h) - try: - h.contains(None) - assert False - except ValueError: - assert_is_min_heap(h) + for e in ls: + self.assertTrue(h.contains(e)) - def test_merge(self): - h = MinHeap([12, 14, 28]) - assert_is_min_heap(h) - assert h.size() == 3 + self.assertFalse(h.contains(3)) + self.assertTrue(is_min_heap(h)) - h2 = MinHeap([(30, "15x2"), (1, "1")]) - assert h2.size() == 2 - assert_is_min_heap(h) + def test_contains_none(self): + ls = [99, 31] + h = MinHeap(ls) + self.assertRaises(ValueError, h.contains, None) + self.assertTrue(is_min_heap(h)) + + def test_merge(self): + h = MinHeap([0, -1, 2]) + h2 = MinHeap([(30, "thirty"), (1, "one")]) h.merge(h2) - assert h.size() == 5 - assert h.find_min() == HeapNode(1, "1") - assert h2.size() == 2 - assert_is_min_heap(h) + + self.assertEqual(h.size(), 5) + self.assertEqual(h.find_min(), HeapNode(-1)) + + self.assertEqual(h2.size(), 2) + self.assertEqual(h2.find_min(), HeapNode(1, "one")) + + self.assertTrue(is_min_heap(h)) + self.assertTrue(is_min_heap(h2)) def test_replace(self): h = MinHeap([28, 12, 14, 7]) p = h.replace(0, 3) - assert p == HeapNode(7) - assert h.size() == 4 - assert_is_min_heap(h) + + self.assertEqual(p, HeapNode(7)) + self.assertEqual(h.size(), 4) + self.assertTrue(is_min_heap(h)) p = h.replace(3, 1) - assert p == HeapNode(28) - assert h.size() == 4 - assert_is_min_heap(h) + self.assertEqual(p, HeapNode(28)) + self.assertEqual(h.size(), 4) + self.assertTrue(is_min_heap(h)) p = h.replace(0, HeapNode(1)) - assert p == HeapNode(1) - assert h.size() == 4 - assert_is_min_heap(h) - - try: - h.replace(0, None) - assert False - except ValueError: - pass - - try: - h.replace(-1, 3) - assert False - except IndexError: - pass + self.assertEqual(p, HeapNode(1)) + self.assertEqual(h.size(), 4) + self.assertTrue(is_min_heap(h)) + + def test_replace_bad(self): + h = MinHeap([7, 3, 2, 5]) + self.assertRaises(ValueError, h.replace, 0, None) + self.assertRaises(IndexError, h.replace, -1, 3) def test_clear(self): - h = MinHeap([12, 14]) - assert h.size() == 2 + h = MinHeap() + + h.clear() + self.assertTrue(h.is_empty()) + + h.add(13) h.clear() - assert h.size() == 0 + self.assertTrue(h.is_empty()) def test_swap(self): h = MinHeap([12, 14, 28]) h.swap(0, 2) - assert not is_min_heap(h) + self.assertFalse(is_min_heap(h)) + h.swap(0, 2) - assert_is_min_heap(h) + self.assertTrue(is_min_heap(h)) + + def test_swap_bad(self): + h = MinHeap([29, 31, 99]) + self.assertRaises(IndexError, h.swap, 0, 3) + self.assertRaises(IndexError, h.swap, -3, 2) + self.assertTrue(is_min_heap(h)) - try: - h.swap(0, 3) - assert False - except IndexError: - pass + def test_is_good_index_bad(self): + h = MinHeap([97, 101, 103]) - try: - h.swap(-3, 2) - assert False - except IndexError: - pass + self.assertRaises(TypeError, h.is_good_index, 3.14159) + self.assertRaises(TypeError, h.is_good_index, "quelli che benpensano") + self.assertRaises(TypeError, h.is_good_index, None) + self.assertRaises(TypeError, h.is_good_index, h.swap) def test_is_good_index(self): - h = MinHeap([12, 14, 28]) + h = MinHeap([1001, 11, 17]) - def assert_type(a): - try: - h.is_good_index(a) - assert False - except TypeError: - pass + self.assertTrue(h.is_good_index(0)) + self.assertTrue(h.is_good_index(1)) + self.assertTrue(h.is_good_index(2)) + self.assertFalse(h.is_good_index(-1)) + self.assertFalse(h.is_good_index(3)) - assert_type(3.14159) - assert_type("cyka blyat") - assert_type(None) - assert_type(h.swap) + def test_parent_index_bad(self): + h = MinHeap([23, 27, 29]) - assert h.is_good_index(0) - assert h.is_good_index(1) - assert h.is_good_index(2) - assert not h.is_good_index(-1) - assert not h.is_good_index(3) + self.assertRaises(IndexError, h.parent_index, -1) + self.assertRaises(IndexError, h.parent_index, 3) def test_parent_index(self): - h = MinHeap([12, 14, 28]) + h = MinHeap([41, 43]) - try: - h.parent_index(-1) - assert False - except IndexError: - pass + self.assertEqual(h.parent_index(0), -1) + self.assertEqual(h.parent_index(1), 0) - try: - h.parent_index(3) - assert False - except IndexError: - pass + def test_grandparent_index_bad(self): + h = MinHeap([1, 2, 3, 4, 5]) - assert h.parent_index(0) == -1 - assert h.parent_index(1) == 0 - assert h.parent_index(2) == 0 + self.assertRaises(IndexError, h.parent_index, -1) + self.assertRaises(IndexError, h.parent_index, 5) def test_grandparent_index(self): - h = MinHeap([12, 14, 28, 6, 7]) - - try: - h.parent_index(-1) - assert False - except IndexError: - pass - - try: - h.parent_index(5) - assert False - except IndexError: - pass - - assert h.grandparent_index(0) == -1 - assert h.grandparent_index(1) == -1 - assert h.grandparent_index(2) == -1 - assert h.grandparent_index(3) == 0 - assert h.grandparent_index(4) == 0 + h = MinHeap([1, 2, 3, 4, 5]) + + self.assertEqual(h.grandparent_index(0), -1) + self.assertEqual(h.grandparent_index(1), -1) + self.assertEqual(h.grandparent_index(2), -1) + self.assertEqual(h.grandparent_index(3), 0) + self.assertEqual(h.grandparent_index(4), 0) + + def test_left_index_bad(self): + h = MinHeap([2, 3, 5]) + + self.assertRaises(IndexError, h.left_index, -1) + self.assertRaises(IndexError, h.left_index, 3) def test_left_index(self): - h = MinHeap([12, 14, 28]) + h = MinHeap([2, 3, 5]) - try: - h.left_index(-1) - assert False - except IndexError: - pass + self.assertEqual(h.left_index(0), 1) + self.assertEqual(h.left_index(1), -1) + self.assertEqual(h.left_index(2), -1) - try: - h.left_index(3) - assert False - except IndexError: - pass + def test_right_index_bad(self): + h = MinHeap([9, 8, 7, 6]) - assert h.left_index(0) == 1 - assert h.left_index(1) == -1 - assert h.left_index(2) == -1 + self.assertRaises(IndexError, h.right_index, -1) + self.assertRaises(IndexError, h.right_index, 4) def test_right_index(self): - h = MinHeap([12, 14, 28, 6]) - try: - h.right_index(-1) - assert False - except IndexError: - pass - - try: - h.right_index(4) - assert False - except IndexError: - pass - - assert h.right_index(0) == 2 - assert h.right_index(1) == -1 - assert h.right_index(2) == -1 - assert h.right_index(3) == -1 - assert h.left_index(1) == 3 + h = MinHeap([9, 8, 7, 6]) + + self.assertEqual(h.right_index(0), 2) + self.assertEqual(h.right_index(1), -1) + self.assertEqual(h.right_index(2), -1) + self.assertEqual(h.right_index(3), -1) + + def test_has_children_bad(self): + h = MinHeap([11, 12, 13, 14]) + + self.assertRaises(IndexError, h.has_children, -1) + self.assertRaises(IndexError, h.has_children, 4) def test_has_children(self): - h = MinHeap([12, 14, 28, 6]) + h = MinHeap([11, 12, 13, 14]) + + self.assertTrue(h.has_children(0)) + self.assertTrue(h.has_children(1)) + self.assertFalse(h.has_children(2)) + self.assertFalse(h.has_children(3)) - try: - h.has_children(-1) - assert False - except IndexError: - pass + def test_is_child_bad(self): + h = MinHeap([12, 14, 28, 6]) - assert h.has_children(0) - assert h.has_children(1) - assert not h.has_children(2) - assert not h.has_children(3) + self.assertRaises(IndexError, h.is_child, -1, 3) + self.assertRaises(IndexError, h.is_child, 0, 4) def test_is_child(self): h = MinHeap([12, 14, 28, 6]) - try: - h.is_child(-1, 3) - assert False - except IndexError: - pass - - try: - h.is_child(0, 4) - assert False - except IndexError: - pass - - assert not h.is_child(0, 0) - assert not h.is_child(1, 1) - assert not h.is_child(2, 2) - assert not h.is_child(3, 3) - - assert h.is_child(3, 1) - assert h.is_child(2, 0) - assert h.is_child(1, 0) - assert not h.is_child(3, 0) - assert not h.is_child(3, 2) + self.assertFalse(h.is_child(0, 0)) + self.assertFalse(h.is_child(1, 1)) + self.assertFalse(h.is_child(2, 2)) + self.assertFalse(h.is_child(3, 3)) + + self.assertTrue(h.is_child(3, 1)) + self.assertTrue(h.is_child(2, 0)) + self.assertTrue(h.is_child(1, 0)) + + self.assertFalse(h.is_child(3, 0)) + self.assertFalse(h.is_child(3, 2)) + + def test_is_grandchild_bad(self): + h = MinHeap([31, 33, 37, 39, 41]) + + self.assertRaises(IndexError, h.is_grandchild, -1, 3) + self.assertRaises(IndexError, h.is_grandchild, 0, 6) def test_is_grandchild(self): - h = MinHeap([12, 14, 28, 6, 7]) - - try: - h.is_grandchild(-1, 3) - assert False - except IndexError: - pass - - try: - h.is_grandchild(0, 6) - assert False - except IndexError: - pass - - assert not h.is_grandchild(0, 0) - assert not h.is_grandchild(1, 1) - assert not h.is_grandchild(2, 2) - assert not h.is_grandchild(3, 3) - assert not h.is_grandchild(4, 4) - - assert h.is_grandchild(3, 0) - assert not h.is_grandchild(3, 1) - assert not h.is_grandchild(3, 2) - assert not h.is_grandchild(3, 4) - - assert h.is_grandchild(4, 0) - assert not h.is_grandchild(4, 1) - assert not h.is_grandchild(4, 2) - assert not h.is_grandchild(4, 3) - - assert not h.is_grandchild(1, 0) - assert not h.is_grandchild(1, 2) - assert not h.is_grandchild(1, 3) - assert not h.is_grandchild(1, 4) - - assert not h.is_grandchild(2, 0) - assert not h.is_grandchild(2, 1) - assert not h.is_grandchild(2, 3) - assert not h.is_grandchild(2, 4) + h = MinHeap([31, 33, 37, 39, 41]) + + self.assertFalse(h.is_grandchild(0, 0)) + self.assertFalse(h.is_grandchild(1, 1)) + self.assertFalse(h.is_grandchild(2, 2)) + self.assertFalse(h.is_grandchild(3, 3)) + self.assertFalse(h.is_grandchild(4, 4)) + + self.assertTrue(h.is_grandchild(3, 0)) + self.assertFalse(h.is_grandchild(3, 1)) + self.assertFalse(h.is_grandchild(3, 2)) + self.assertFalse(h.is_grandchild(3, 4)) + + self.assertTrue(h.is_grandchild(4, 0)) + self.assertFalse(h.is_grandchild(4, 1)) + self.assertFalse(h.is_grandchild(4, 2)) + self.assertFalse(h.is_grandchild(4, 3)) + + self.assertFalse(h.is_grandchild(1, 0)) + self.assertFalse(h.is_grandchild(1, 2)) + self.assertFalse(h.is_grandchild(1, 3)) + self.assertFalse(h.is_grandchild(1, 4)) + + self.assertFalse(h.is_grandchild(2, 0)) + self.assertFalse(h.is_grandchild(2, 1)) + self.assertFalse(h.is_grandchild(2, 3)) + self.assertFalse(h.is_grandchild(2, 4)) + + def test_is_parent_bad(self): + h = MinHeap([-1, -2, -3, -4]) + + self.assertRaises(IndexError, h.is_parent, -1, 3) + self.assertRaises(IndexError, h.is_parent, 0, 6) def test_is_parent(self): - h = MinHeap([12, 14, 28, 6, 7]) - - try: - h.is_parent(-1, 3) - assert False - except IndexError: - pass - - try: - h.is_parent(0, 6) - assert False - except IndexError: - pass - - assert not h.is_parent(0, 0) - assert not h.is_parent(1, 1) - assert not h.is_parent(2, 2) - assert not h.is_parent(3, 3) - assert not h.is_parent(4, 4) - - assert h.is_parent(0, 1) - assert h.is_parent(0, 2) - assert not h.is_parent(0, 3) - assert not h.is_parent(0, 4) - - assert h.is_parent(1, 3) - assert h.is_parent(1, 4) - assert not h.is_parent(1, 2) - assert not h.is_parent(1, 0) - - assert not h.is_parent(2, 0) - assert not h.is_parent(2, 1) - assert not h.is_parent(2, 3) - assert not h.is_parent(2, 4) - - assert not h.is_parent(3, 0) - assert not h.is_parent(3, 1) - assert not h.is_parent(3, 2) - assert not h.is_parent(3, 4) - - assert not h.is_parent(4, 0) - assert not h.is_parent(4, 1) - assert not h.is_parent(4, 2) - assert not h.is_parent(4, 3) + h = MinHeap([-1, -2, -3, -4, -5]) + + self.assertFalse(h.is_parent(0, 0)) + self.assertFalse(h.is_parent(1, 1)) + self.assertFalse(h.is_parent(2, 2)) + self.assertFalse(h.is_parent(3, 3)) + self.assertFalse(h.is_parent(4, 4)) + + self.assertTrue(h.is_parent(0, 1)) + self.assertTrue(h.is_parent(0, 2)) + self.assertFalse(h.is_parent(0, 3)) + self.assertFalse(h.is_parent(0, 4)) + + self.assertTrue(h.is_parent(1, 3)) + self.assertTrue(h.is_parent(1, 4)) + self.assertFalse(h.is_parent(1, 2)) + self.assertFalse(h.is_parent(1, 0)) + + self.assertFalse(h.is_parent(2, 0)) + self.assertFalse(h.is_parent(2, 1)) + self.assertFalse(h.is_parent(2, 3)) + self.assertFalse(h.is_parent(2, 4)) + + self.assertFalse(h.is_parent(3, 0)) + self.assertFalse(h.is_parent(3, 1)) + self.assertFalse(h.is_parent(3, 2)) + self.assertFalse(h.is_parent(3, 4)) + + self.assertFalse(h.is_parent(4, 0)) + self.assertFalse(h.is_parent(4, 1)) + self.assertFalse(h.is_parent(4, 2)) + self.assertFalse(h.is_parent(4, 3)) + + def test_is_grandparent_bad(self): + h = MinHeap([9, 99, 999, 9999, 99999]) + + self.assertRaises(IndexError, h.is_grandparent, -1, 3) + self.assertRaises(IndexError, h.is_grandparent, 0, 6) def test_is_grandparent(self): - h = MinHeap([12, 14, 28, 6, 7]) - - try: - h.is_grandparent(-1, 3) - assert False - except IndexError: - pass - - try: - h.is_grandparent(0, 6) - assert False - except IndexError: - pass - - assert not h.is_grandparent(0, 0) - assert not h.is_grandparent(1, 1) - assert not h.is_grandparent(2, 2) - assert not h.is_grandparent(3, 3) - assert not h.is_grandparent(4, 4) - - assert not h.is_grandparent(0, 1) - assert not h.is_grandparent(0, 2) - assert h.is_grandparent(0, 3) - assert h.is_grandparent(0, 4) - - assert not h.is_grandparent(1, 0) - assert not h.is_grandparent(1, 2) - assert not h.is_grandparent(1, 3) - assert not h.is_grandparent(1, 4) - - assert not h.is_grandparent(2, 0) - assert not h.is_grandparent(2, 1) - assert not h.is_grandparent(2, 3) - assert not h.is_grandparent(2, 4) - - assert not h.is_grandparent(3, 0) - assert not h.is_grandparent(3, 1) - assert not h.is_grandparent(3, 2) - assert not h.is_grandparent(3, 4) - - assert not h.is_grandparent(4, 0) - assert not h.is_grandparent(4, 1) - assert not h.is_grandparent(4, 2) - assert not h.is_grandparent(4, 3) + h = MinHeap([9, 99, 999, 9999, 99999]) + + self.assertFalse(h.is_grandparent(0, 0)) + self.assertFalse(h.is_grandparent(1, 1)) + self.assertFalse(h.is_grandparent(2, 2)) + self.assertFalse(h.is_grandparent(3, 3)) + self.assertFalse(h.is_grandparent(4, 4)) + + self.assertFalse(h.is_grandparent(0, 1)) + self.assertFalse(h.is_grandparent(0, 2)) + self.assertTrue(h.is_grandparent(0, 3)) + self.assertTrue(h.is_grandparent(0, 4)) + + self.assertFalse(h.is_grandparent(1, 0)) + self.assertFalse(h.is_grandparent(1, 2)) + self.assertFalse(h.is_grandparent(1, 3)) + self.assertFalse(h.is_grandparent(1, 4)) + + self.assertFalse(h.is_grandparent(2, 0)) + self.assertFalse(h.is_grandparent(2, 1)) + self.assertFalse(h.is_grandparent(2, 3)) + self.assertFalse(h.is_grandparent(2, 4)) + + self.assertFalse(h.is_grandparent(3, 0)) + self.assertFalse(h.is_grandparent(3, 1)) + self.assertFalse(h.is_grandparent(3, 2)) + self.assertFalse(h.is_grandparent(3, 4)) + + self.assertFalse(h.is_grandparent(4, 0)) + self.assertFalse(h.is_grandparent(4, 1)) + self.assertFalse(h.is_grandparent(4, 2)) + self.assertFalse(h.is_grandparent(4, 3)) + + def test_is_on_even_level_bad(self): + ls = [12, 14, 28, 6, 7, 18, 10, 3, 1] + h = MinHeap(ls) + + self.assertRaises(IndexError, h.is_on_even_level, -1) + self.assertRaises(IndexError, h.is_on_even_level, len(ls)) def test_is_on_even_level(self): - h = MinHeap([12, 14, 28, 6, 7, 18, 10, 3, 1]) - - try: - h.is_on_even_level(-1) - assert False - except IndexError: - pass - - assert h.is_on_even_level(0) - assert not h.is_on_even_level(1) - assert not h.is_on_even_level(2) - assert h.is_on_even_level(3) - assert h.is_on_even_level(4) - assert h.is_on_even_level(5) - assert h.is_on_even_level(6) - assert not h.is_on_even_level(7) - assert not h.is_on_even_level(8) + ls = [12, 14, 28, 6, 7, 18, 10, 3, 1] + h = MinHeap(ls) + + self.assertTrue(h.is_on_even_level(0)) + + self.assertFalse(h.is_on_even_level(1)) + self.assertFalse(h.is_on_even_level(2)) + + self.assertTrue(h.is_on_even_level(3)) + self.assertTrue(h.is_on_even_level(4)) + self.assertTrue(h.is_on_even_level(5)) + self.assertTrue(h.is_on_even_level(6)) + + self.assertFalse(h.is_on_even_level(7)) + self.assertFalse(h.is_on_even_level(8)) + + def test_is_on_odd_level_bad(self): + ls = [12, 14, 28, 6, 7, 18, 10, 3, 1] + h = MinHeap(ls) + + self.assertRaises(IndexError, h.is_on_odd_level, -1) + self.assertRaises(IndexError, h.is_on_odd_level, len(ls)) def test_is_on_odd_level(self): - h = MinHeap([12, 14, 28, 6, 7, 18, 10, 3, 1]) - - try: - h.is_on_odd_level(10) - assert False - except IndexError: - pass - - assert not h.is_on_odd_level(0) - assert h.is_on_odd_level(1) - assert h.is_on_odd_level(2) - assert not h.is_on_odd_level(3) - assert not h.is_on_odd_level(4) - assert not h.is_on_odd_level(5) - assert not h.is_on_odd_level(6) - assert h.is_on_odd_level(7) - assert h.is_on_odd_level(8) + ls = [12, 14, 28, 6, 7, 18, 10, 3, 1] + h = MinHeap(ls) + + self.assertFalse(h.is_on_odd_level(0)) + self.assertTrue(h.is_on_odd_level(1)) + self.assertTrue(h.is_on_odd_level(2)) + self.assertFalse(h.is_on_odd_level(3)) + self.assertFalse(h.is_on_odd_level(4)) + self.assertFalse(h.is_on_odd_level(5)) + self.assertFalse(h.is_on_odd_level(6)) + self.assertTrue(h.is_on_odd_level(7)) + self.assertTrue(h.is_on_odd_level(8)) if __name__ == "__main__":