def test_delete_at_index(self): doubly = DoublyLinkedList( ['very first', 'first', 'second', 'third', 'fourth', 'very last']) self.assertEqual('second', doubly.get_node_by_index_from_top(2).data) doubly.delete_at_index(2) self.assertEqual('third', doubly.get_node_by_index_from_top(2).data) self.assertEqual( "DoublyLinkedList(['very first', 'first', 'third', 'fourth', 'very last'])", doubly.__repr__()) self.assertEqual('first', doubly.get_node_by_index_from_top(2).leftlink.data) self.assertEqual('third', doubly.get_node_by_index_from_top(1).rightlink.data) doubly.delete_from_the_bottom() doubly.delete_from_the_top() self.assertEqual("DoublyLinkedList(['first', 'third', 'fourth'])", doubly.__repr__()) self.assertEqual('fourth', doubly.last_node.data) self.assertEqual(None, doubly.last_node.rightlink) self.assertEqual('third', doubly.last_node.leftlink.data) self.assertEqual('first', doubly.first_node.data) self.assertEqual(None, doubly.first_node.leftlink) self.assertEqual('third', doubly.first_node.rightlink.data)
def test_insert_in_the_middle(self): doubly = DoublyLinkedList(['first', 'third', 'fourth']) self.assertEqual(doubly.get_node_by_index_from_top(1).data, 'third') doubly.insert_at_index(1, 'second') self.assertEqual( "DoublyLinkedList(['first', 'second', 'third', 'fourth'])", doubly.__repr__()) self.assertEqual(doubly.get_node_by_index_from_top(1).data, 'second') self.assertEqual(doubly.get_node_by_index_from_top(2).data, 'third') self.assertEqual( doubly.get_node_by_index_from_top(0).rightlink.data, 'second') self.assertEqual( doubly.get_node_by_index_from_top(1).leftlink.data, 'first') self.assertEqual( doubly.get_node_by_index_from_top(1).rightlink.data, 'third') self.assertEqual( doubly.get_node_by_index_from_top(2).leftlink.data, 'second') doubly.insert_at_index(0, 'very first') doubly.insert_at_index(5, 'very last') self.assertEqual(None, doubly.insert_at_index(7, 'out of range')) self.assertEqual('very first', doubly.first_node.data) self.assertEqual('very last', doubly.last_node.data) self.assertEqual( "DoublyLinkedList(['very first', 'first', 'second', 'third', 'fourth', 'very last'])", doubly.__repr__())
def test_delete_from_the_bottom(self): doubly = DoublyLinkedList( ['very first', 'first', 'second', 'third', 'fourth', 'very last']) doubly.delete_from_the_bottom() self.assertEqual( "DoublyLinkedList(['very first', 'first', 'second', 'third', 'fourth'])", doubly.__repr__()) self.assertEqual('fourth', doubly.last_node.data) self.assertEqual(None, doubly.last_node.rightlink) self.assertEqual('third', doubly.last_node.leftlink.data)
def test_insert_to_the_top(self): doubly = DoublyLinkedList(['first', 'second', 'third']) self.assertEqual(doubly.get_node_by_index_from_top(0).data, 'first') doubly.insert_to_the_top('very first') self.assertEqual( doubly.__repr__(), "DoublyLinkedList(['very first', 'first', 'second', 'third'])") self.assertEqual( doubly.get_node_by_index_from_top(0).data, 'very first') self.assertEqual(doubly.get_node_by_index_from_top(1).data, 'first') self.assertEqual( doubly.get_node_by_index_from_top(1).leftlink.data, 'very first') self.assertEqual( doubly.get_node_by_index_from_top(0).rightlink.data, 'first') self.assertEqual(doubly.get_node_by_index_from_top(0).leftlink, None) self.assertEqual(doubly.first_node.data, 'very first')
def test_insert_to_the_bottom(self): doubly = DoublyLinkedList(['first', 'second', 'third']) self.assertEqual(doubly.get_node_by_index_from_bottom(0).data, 'third') doubly.insert_to_the_bottom('fourth') self.assertEqual( "DoublyLinkedList(['first', 'second', 'third', 'fourth'])", doubly.__repr__()) self.assertEqual( doubly.get_node_by_index_from_bottom(0).data, 'fourth') self.assertEqual(doubly.get_node_by_index_from_bottom(1).data, 'third') self.assertEqual( doubly.get_node_by_index_from_bottom(1).rightlink.data, 'fourth') self.assertEqual( doubly.get_node_by_index_from_bottom(0).leftlink.data, 'third') self.assertEqual( doubly.get_node_by_index_from_bottom(0).rightlink, None) self.assertEqual(doubly.last_node.data, 'fourth')
def test_doubly_repr(self): doubly = DoublyLinkedList(['first', 'second', 'third']) self.assertEqual(doubly.__repr__(), "DoublyLinkedList(['first', 'second', 'third'])")