-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_linked_lists.py
More file actions
389 lines (328 loc) · 9.85 KB
/
Copy pathtest_linked_lists.py
File metadata and controls
389 lines (328 loc) · 9.85 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# test_linked_lists.py
"""
Run tests for LinkedList methods using helpers.
"""
from linked_list import LinkedList
from test_helpers import check, linkedlist_to_list, remove_duplicates_tests, test_partition_list_fn
# Test functions for LinedList methods calling the LinkedList class methods and helper functions.
def test_append():
ll = LinkedList(1)
ll.append(2)
ll.append(3)
check([1, 2, 3], linkedlist_to_list(ll.head), "append() test")
def test_pop():
ll = LinkedList(1)
ll.append(2)
popped = ll.pop()
check(2, popped.value, "pop() test")
check([1], linkedlist_to_list(ll.head), "pop() remaining")
def test_prepend():
ll = LinkedList(2)
ll.prepend(1)
ll.prepend(0)
check([0, 1, 2], linkedlist_to_list(ll.head), "prepend() test")
def test_pop_first():
ll = LinkedList(1)
ll.append(2)
popped = ll.pop_first()
check(1, popped.value, "pop_first() test")
check([2], linkedlist_to_list(ll.head), "pop_first() remaining")
def test_get_set():
ll = LinkedList(0)
ll.append(1)
ll.append(2)
ll.set_value(1, 5)
check(5, ll.get(1).value, "get() and set_value() test")
def test_find_middle():
ll = LinkedList(1)
ll.append(2)
ll.append(3)
check(2, ll.find_middle_node().value, "find_middle_node() test")
def test_has_loop():
ll = LinkedList(1)
ll.append(2)
ll.tail.next = ll.head
check(True, ll.has_loop(), "has_loop() test")
def test_find_kth_from_end():
ll = LinkedList(1)
ll.append(2)
ll.append(3)
check(2, ll.find_kth_from_end(2).value, "find_kth_from_end() test")
print("\n=== Finished basic list tests ===\n")
def run_remove_duplicates_tests():
print("\n=== Testing remove_duplicates ===\n")
# Case 1: No duplicates
ll = LinkedList(1)
ll.append(2)
ll.append(3)
remove_duplicates_tests(ll, [1, 2, 3])
# Case 2: Some non-consecutive duplicates
ll = LinkedList(1)
ll.append(2)
ll.append(1)
ll.append(3)
ll.append(2)
remove_duplicates_tests(ll, [1, 2, 3])
# Case 3: All nodes same
ll = LinkedList(1)
ll.append(1)
ll.append(1)
ll.append(1)
remove_duplicates_tests(ll, [1])
# Case 4: Consecutive duplicates in the middle
ll = LinkedList(1)
ll.append(2)
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(4)
remove_duplicates_tests(ll, [1, 2, 3, 4])
# Case 5: Duplicate at end
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(3)
remove_duplicates_tests(ll, [1, 2, 3, 4])
# Case 6: Duplicate at start and end
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(1)
remove_duplicates_tests(ll, [1, 2, 3])
# Case 7: Long list with scattered duplicates
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(2)
ll.append(4)
ll.append(1)
ll.append(5)
ll.append(4)
ll.append(5)
remove_duplicates_tests(ll, [1, 2, 3, 4, 5])
# Case 8: Single node list
ll = LinkedList(99)
remove_duplicates_tests(ll, [99])
# Case 9: Empty list
ll = LinkedList(0)
ll.make_empty()
remove_duplicates_tests(ll, [])
print("\n=== Finished remove_duplicates tests ===\n")
def test_binary_to_decimal():
print("\n=== Testing binary_to_decimal ===\n")
# Test 1: 110 => 6
ll = LinkedList(1)
ll.append(1)
ll.append(0)
result = ll.binary_to_decimal()
check(6, result, "110 => 6")
# Test 2: 1000 => 8
ll = LinkedList(1)
ll.append(0)
ll.append(0)
ll.append(0)
result = ll.binary_to_decimal()
check(8, result, "1000 => 8")
# Test 3: 0 => 0
ll = LinkedList(0)
result = ll.binary_to_decimal()
check(0, result, "0 => 0")
# Test 4: 1 => 1
ll = LinkedList(1)
result = ll.binary_to_decimal()
check(1, result, "1 => 1")
# Test 5: 1101 => 13
ll = LinkedList(1)
ll.append(1)
ll.append(0)
ll.append(1)
result = ll.binary_to_decimal()
check(13, result, "1101 => 13")
# Test 6: Long binary number: 101101 => 45
ll = LinkedList(1)
ll.append(0)
ll.append(1)
ll.append(1)
ll.append(0)
ll.append(1)
result = ll.binary_to_decimal()
check(45, result, "101101 => 45")
# Test 7: All zeros: 0000 => 0
ll = LinkedList(0)
ll.append(0)
ll.append(0)
ll.append(0)
result = ll.binary_to_decimal()
check(0, result, "0000 => 0")
# Test 8: Single node zero
ll = LinkedList(0)
result = ll.binary_to_decimal()
check(0, result, "0 => 0")
# Test 9: Single node one
ll = LinkedList(1)
result = ll.binary_to_decimal()
check(1, result, "1 => 1")
# Test 10: Alternating pattern: 1010 => 10
ll = LinkedList(1)
ll.append(0)
ll.append(1)
ll.append(0)
result = ll.binary_to_decimal()
check(10, result, "1010 => 10")
# Test 11: Edge case: Empty list (should probably return 0)
ll = LinkedList(0)
ll.make_empty()
result = ll.binary_to_decimal()
check(0, result, "Empty list => 0")
print("\n=== Finished binary_to_decimal tests ===\n")
def test_partition_list():
print("\n=== Testing partition lists ===\n")
# Case 1: Normal mix
ll = LinkedList(3)
ll.append(1)
ll.append(4)
ll.append(2)
ll.append(5)
ll.partition_list(3)
check([1, 2, 3, 4, 5], linkedlist_to_list(ll.head), "partition_list() normal mix")
# Case 2: Already partitioned
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.partition_list(3)
check([1, 2, 3, 4], linkedlist_to_list(ll.head), "partition_list() already sorted")
# Case 3: All nodes < x
ll = LinkedList(1)
ll.append(0)
ll.append(2)
ll.partition_list(5)
check([1, 0, 2], linkedlist_to_list(ll.head), "partition_list() all less")
# Case 4: All nodes >= x
ll = LinkedList(5)
ll.append(6)
ll.append(7)
ll.partition_list(5)
check([5, 6, 7], linkedlist_to_list(ll.head), "partition_list() all greater or equal")
# Case 5: All equal to x
ll = LinkedList(3)
ll.append(3)
ll.append(3)
ll.partition_list(3)
check([3, 3, 3], linkedlist_to_list(ll.head), "partition_list() all equal")
# Case 6: Single element
ll = LinkedList(1)
ll.partition_list(2)
check([1], linkedlist_to_list(ll.head), "partition_list() single node")
# Case 7: Empty list
ll = LinkedList(0)
ll.make_empty()
ll.partition_list(2)
check([], linkedlist_to_list(ll.head), "partition_list() empty list")
print("\n=== Finished partition list tests ===\n")
def test_reverse_between():
print("\n=== Testing reverse_between ===\n")
# Test 1: Reverse middle [1,2,3,4,5] between 1 and 3 => [1,4,3,2,5]
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.reverse_between(1, 3)
result = linkedlist_to_list(ll.head)
check([1, 4, 3, 2, 5], result, "Reverse middle [1,2,3,4,5] between 1-3")
# Test 2: Reverse entire list [1,2,3,4,5] => [5,4,3,2,1]
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.reverse_between(0, 4)
result = linkedlist_to_list(ll.head)
check([5, 4, 3, 2, 1], result, "Reverse entire [1,2,3,4,5]")
# Test 3: Reverse sublist of length 1 (should stay same)
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.reverse_between(1, 1)
result = linkedlist_to_list(ll.head)
check([1, 2, 3], result, "Reverse sublist length 1: [1,2,3]")
# Test 4: Reverse at head [1,2,3] between 0 and 1 => [2,1,3]
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.reverse_between(0, 1)
result = linkedlist_to_list(ll.head)
check([2, 1, 3], result, "Reverse head [1,2,3] 0-1")
# Test 5: Reverse at tail [1,2,3] between 1 and 2 => [1,3,2]
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.reverse_between(1, 2)
result = linkedlist_to_list(ll.head)
check([1, 3, 2], result, "Reverse tail [1,2,3] 1-2")
# Test 6: Empty list
ll = LinkedList(1)
ll.make_empty()
ll.reverse_between(0, 0)
result = linkedlist_to_list(ll.head)
check([], result, "Reverse empty list")
# Test 7: Single node, reverse entire
ll = LinkedList(1)
ll.reverse_between(0, 0)
result = linkedlist_to_list(ll.head)
check([1], result, "Reverse single node")
print("\n=== Finished reverse_between tests ===\n")
def test_swap_pairs():
print("\n=== Testing swap_pairs ===\n")
# Test 1: Even number of nodes [1,2,3,4] => [2,1,4,3]
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.swap_pairs()
result = linkedlist_to_list(ll.head)
check([2, 1, 4, 3], result, "Even nodes: [1,2,3,4] -> [2,1,4,3]")
# Test 2: Odd number of nodes [1,2,3,4,5] => [2,1,4,3,5]
ll = LinkedList(1)
ll.append(2)
ll.append(3)
ll.append(4)
ll.append(5)
ll.swap_pairs()
result = linkedlist_to_list(ll.head)
check([2, 1, 4, 3, 5], result, "Odd nodes: [1,2,3,4,5] -> [2,1,4,3,5]")
# Test 3: Single node [1] => [1]
ll = LinkedList(1)
ll.swap_pairs()
result = linkedlist_to_list(ll.head)
check([1], result, "Single node: [1] -> [1]")
# Test 4: Empty list [] => []
ll = LinkedList(1)
ll.make_empty()
ll.swap_pairs()
result = linkedlist_to_list(ll.head)
check([], result, "Empty list: [] -> []")
# Test 5: Two nodes [1,2] => [2,1]
ll = LinkedList(1)
ll.append(2)
ll.swap_pairs()
result = linkedlist_to_list(ll.head)
check([2, 1], result, "Two nodes: [1,2] -> [2,1]")
print("\n=== Finished swap_pairs tests ===\n")
print("\n=====All Tests Completed=====\n")
if __name__ == "__main__":
test_append()
test_pop()
test_prepend()
test_pop_first()
test_get_set()
test_find_middle()
test_has_loop()
test_find_kth_from_end()
run_remove_duplicates_tests()
test_binary_to_decimal()
test_partition_list()
test_reverse_between()
test_swap_pairs()