-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist_test.py
196 lines (173 loc) · 6.29 KB
/
linkedlist_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!python
from linkedlist import LinkedList, Node
import unittest
class NodeTest(unittest.TestCase):
def test_init(self):
data = 'ABC'
node = Node(data)
# Initializer should add instance properties
assert node.data is data
assert node.next is None
def test_linking_nodes(self):
node1 = Node('A')
node2 = Node('B')
node3 = Node('C')
# Link nodes together
node1.next = node2
node2.next = node3
# Node links should be transitive
assert node1.next is node2 # One link
assert node1.next.next is node3 # Two links
class LinkedListTest(unittest.TestCase):
def test_init(self):
ll = LinkedList()
# Initializer should add instance properties
assert ll.head is None # First node
assert ll.tail is None # Last node
def test_init_with_list(self):
ll = LinkedList(['A', 'B', 'C'])
# Initializer should append items in order
assert ll.head.data == 'A' # First item
assert ll.tail.data == 'C' # Last item
def test_items_after_append(self):
ll = LinkedList()
assert ll.items() == []
# Append should add new item to tail of list
ll.append('A')
assert ll.items() == ['A']
ll.append('B')
assert ll.items() == ['A', 'B']
ll.append('C')
assert ll.items() == ['A', 'B', 'C']
def test_items_after_prepend(self):
ll = LinkedList()
assert ll.items() == []
# Prepend should add new item to head of list
ll.prepend('C')
assert ll.items() == ['C']
ll.prepend('B')
assert ll.items() == ['B', 'C']
ll.prepend('A')
assert ll.items() == ['A', 'B', 'C']
def test_length_after_append(self):
ll = LinkedList()
assert ll.length() == 0
# Append should increase length
ll.append('A')
assert ll.length() == 1
ll.append('B')
assert ll.length() == 2
ll.append('C')
assert ll.length() == 3
def test_length_after_prepend(self):
ll = LinkedList()
assert ll.length() == 0
# Prepend should increase length
ll.prepend('C')
assert ll.length() == 1
ll.prepend('B')
assert ll.length() == 2
ll.prepend('A')
assert ll.length() == 3
def test_length_after_append_and_prepend(self):
ll = LinkedList()
assert ll.length() == 0
# Append and prepend should increase length
ll.append('C')
assert ll.length() == 1
ll.prepend('B')
assert ll.length() == 2
ll.append('D')
assert ll.length() == 3
ll.prepend('A')
assert ll.length() == 4
def test_length_after_delete(self):
ll = LinkedList(['A', 'B', 'C', 'D', 'E'])
assert ll.length() == 5
# Delete should decrease length
ll.delete('A')
assert ll.length() == 4
ll.delete('E')
assert ll.length() == 3
ll.delete('C')
assert ll.length() == 2
ll.delete('D')
assert ll.length() == 1
ll.delete('B')
assert ll.length() == 0
def test_append(self):
ll = LinkedList()
# Append should always update tail node
ll.append('A')
assert ll.head.data == 'A' # New head
assert ll.tail.data == 'A' # New tail
ll.append('B')
assert ll.head.data == 'A' # Unchanged
assert ll.tail.data == 'B' # New tail
ll.append('C')
assert ll.head.data == 'A' # Unchanged
assert ll.tail.data == 'C' # New tail
def test_prepend(self):
ll = LinkedList()
# Prepend should always update head node
ll.prepend('C')
assert ll.head.data == 'C' # New head
assert ll.tail.data == 'C' # New head
ll.prepend('B')
assert ll.head.data == 'B' # New head
assert ll.tail.data == 'C' # Unchanged
ll.prepend('A')
assert ll.head.data == 'A' # New head
assert ll.tail.data == 'C' # Unchanged
def test_find(self):
ll = LinkedList(['A', 'B', 'C'])
assert ll.find(lambda item: item == 'B') == 'B' # Match equality
assert ll.find(lambda item: item < 'B') == 'A' # Match less than
assert ll.find(lambda item: item > 'B') == 'C' # Match greater than
assert ll.find(lambda item: item == 'X') is None # No matching item
def test_delete_with_3_items(self):
ll = LinkedList(['A', 'B', 'C'])
assert ll.head.data == 'A' # First item
assert ll.tail.data == 'C' # Last item
ll.delete('A')
assert ll.head.data == 'B' # New head
assert ll.tail.data == 'C' # Unchanged
ll.delete('C')
assert ll.head.data == 'B' # Unchanged
assert ll.tail.data == 'B' # New tail
ll.delete('B')
assert ll.head is None # No head
assert ll.tail is None # No tail
# Delete should raise error if item was already deleted
with self.assertRaises(ValueError):
ll.delete('A') # Item no longer in list
with self.assertRaises(ValueError):
ll.delete('B') # Item no longer in list
with self.assertRaises(ValueError):
ll.delete('C') # Item no longer in list
def test_delete_with_5_items(self):
ll = LinkedList(['A', 'B', 'C', 'D', 'E'])
assert ll.head.data == 'A' # First item
assert ll.tail.data == 'E' # Last item
ll.delete('A')
assert ll.head.data == 'B' # New head
assert ll.tail.data == 'E' # Unchanged
ll.delete('E')
assert ll.head.data == 'B' # Unchanged
assert ll.tail.data == 'D' # New tail
ll.delete('C')
assert ll.head.data == 'B' # Unchanged
assert ll.tail.data == 'D' # Unchanged
ll.delete('D')
assert ll.head.data == 'B' # Unchanged
assert ll.tail.data == 'B' # New tail
ll.delete('B')
assert ll.head is None # No head
assert ll.tail is None # No tail
def test_delete_with_item_not_in_list(self):
ll = LinkedList(['A', 'B', 'C'])
# Delete should raise error if item not found
with self.assertRaises(ValueError):
ll.delete('X') # Item not found in list
if __name__ == '__main__':
unittest.main()