def test_bubble_down_root(self): h = IndexedHeap() h.data = [{ 'key': 4, 'value': 40, 'index': 0 }, { 'key': 2, 'value': 20, 'index': 1 }, { 'key': 3, 'value': 30, 'index': 2 }] h.bubble_down(0) expected = [{ 'key': 2, 'value': 20, 'index': 0 }, { 'key': 4, 'value': 40, 'index': 1 }, { 'key': 3, 'value': 30, 'index': 2 }] self.assertItemsEqual(h.data, expected, 'sorts the data correctly')
def test_bubble_up_last(self): h = IndexedHeap() h.data = [ { 'key': 4, 'value': 40, 'index': 0 }, { 'key': 3, 'value': 30, 'index': 1 }, { 'key': 2, 'value': 20, 'index': 2 }, ] h.bubble_up(2) expected = [{ 'key': 2, 'value': 20, 'index': 0 }, { 'key': 3, 'value': 30, 'index': 1 }, { 'key': 4, 'value': 40, 'index': 2 }] self.assertItemsEqual(h.data, expected, 'bubbles up the data correctly')
def test_remove(self): h = IndexedHeap() h.data = [{ 'key': 2, 'value': 20, 'index': 0 }, { 'key': 3, 'value': 30, 'index': 1 }, { 'key': 4, 'value': 40, 'index': 2 }] item = h.remove(1) expected = [{ 'key': 2, 'value': 20, 'index': 0 }, { 'key': 4, 'value': 40, 'index': 1 }] self.assertItemsEqual(h.data, expected, 'correctly removes the data') self.assertEqual(item, { 'key': 3, 'value': 30, 'index': 1 }, 'removed item is correct')
def test_get_child_indices(self): h = IndexedHeap() h.data = range(10) self.assertEqual(h.get_child_indices(0), [1, 2], 'correct children for 0') self.assertEqual(h.get_child_indices(1), [3, 4], 'correct children for 1') self.assertEqual(h.get_child_indices(2), [5, 6], 'correct children for 2')
def test_get_min_for_indices(self): h = IndexedHeap() h.data = [{'key': i, 'value': i, 'index': i} for i in range(10)] actual = h.get_min_for_indices([9, None, None]) self.assertEqual(actual, 9, 'no children so root is min') actual = h.get_min_for_indices([7, 8, 9]) self.assertEqual(actual, 7, 'root is the smallest') actual = h.get_min_for_indices([2, 8, 1]) self.assertEqual(actual, 1, 'third is the smallest')
def test_get_parent_index(self): h = IndexedHeap() h.data = range(10) self.assertEqual(h.get_parent_index(0), None, 'no parent for root') self.assertEqual(h.get_parent_index(1), 0, '0 parent for 1') self.assertEqual(h.get_parent_index(2), 0, '0 parent for 2') self.assertEqual(h.get_parent_index(3), 1, '1 parent for 3') self.assertEqual(h.get_parent_index(4), 1, '1 parent for 4') self.assertEqual(h.get_parent_index(5), 2, '2 parent for 5') self.assertEqual(h.get_parent_index(6), 2, '2 parent for 6') self.assertEqual(h.get_parent_index(7), 3, '3 parent for 7') self.assertEqual(h.get_parent_index(8), 3, '3 parent for 8') self.assertEqual(h.get_parent_index(9), 4, '4 parent for 9')
def test_insert(self): h = IndexedHeap() inserted1 = h.insert({'key': 'z', 'value': 300}) self.assertEqual(inserted1, { 'key': 'z', 'value': 300, 'index': 0 }, 'correctly inserted and returned new item') inserted2 = h.insert({'key': 'y', 'value': 200}) self.assertEqual(inserted2, { 'key': 'y', 'value': 200, 'index': 0 }, 'correctly inserted and returned new item') self.assertEqual(inserted1, { 'key': 'z', 'value': 300, 'index': 1 }, 'correctly updated existing items') inserted3 = h.insert({'key': 'x', 'value': 100}) self.assertEqual(inserted3, { 'key': 'x', 'value': 100, 'index': 0 }, 'correctly inserted and returned new item') self.assertEqual(inserted2, { 'key': 'y', 'value': 200, 'index': 2 }, 'correctly inserted and returned item') self.assertEqual(inserted1, { 'key': 'z', 'value': 300, 'index': 1 }, 'correctly updated existing items')
def __init__(self, max_size): Cache.__init__(self, max_size) self.data = {} self.heap = IndexedHeap()
def test_remove_from_single_item_heap(self): h = IndexedHeap() h.insert({'key': 'x', 'value': 1000}) h.remove(0) self.assertEqual(h.data, [], 'empty data in the heap')