Esempio n. 1
0
 def test_delete(self):
     ll = DoubleLinkedList(['A', 'B', 'C'])
     ll.delete('A')
     assert ll.head.data == 'B'  # new head
     assert ll.tail.data == 'C'  # unchanged
     assert ll.size == 2
     ll.delete('C')
     assert ll.head.data == 'B'  # unchanged
     assert ll.tail.data == 'B'  # new tail
     assert ll.size == 1
     ll.delete('B')
     assert ll.head is None  # new head
     assert ll.tail is None  # new head
     assert ll.size == 0
     with self.assertRaises(ValueError):
         ll.delete('X')  # item not in list
Esempio n. 2
0
 def test_size(self):
     ll = DoubleLinkedList()
     assert ll.size == 0
     # append and prepend operations increment size
     ll.append('B')
     assert ll.size == 1
     ll.prepend('A')
     assert ll.size == 2
     ll.append('C')
     assert ll.size == 3
     # delete operations decrement size
     ll.delete('B')
     assert ll.size == 2
     ll.delete('C')
     assert ll.size == 1
     ll.delete('A')
     assert ll.size == 0
Esempio n. 3
0
 def test_length(self):
     ll = DoubleLinkedList()
     assert ll.length() == 0
     # append and prepend operations increase length
     ll.append('B')
     assert ll.head.previous == None
     assert ll.head.next == None
     assert ll.tail.previous == None
     assert ll.tail.next == None
     assert ll.length() == 1
     ll.prepend('A')
     assert ll.head.previous == None
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'A'
     assert ll.tail.next == None
     assert ll.length() == 2
     ll.append('C')
     assert ll.head.previous == None
     assert ll.head.next.data == 'B'
     assert ll.tail.previous.data == 'B'
     assert ll.tail.next == None
     assert ll.length() == 3
     # delete operations decrease length
     ll.delete('B')
     assert ll.head.previous == None
     assert ll.head.next.data == 'C'
     assert ll.tail.previous.data == 'A'
     assert ll.tail.next == None
     assert ll.length() == 2
     ll.delete('C')
     assert ll.head.previous == None
     assert ll.head.next == None
     assert ll.tail.previous == None
     assert ll.tail.next == None
     assert ll.length() == 1
     ll.delete('A')
     assert ll.head == None
     assert ll.head == None
     assert ll.length() == 0