Пример #1
0
    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)
Пример #2
0
    def test_delete_from_one_element_list(self):
        doubly = DoublyLinkedList(['first'])

        with self.assertRaises(IndexError):
            doubly.delete_from_the_bottom()

        with self.assertRaises(IndexError):
            doubly.delete_from_the_top()

        with self.assertRaises(IndexError):
            doubly.delete_at_index(1)
Пример #3
0
    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)