-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_circular_linked_list.py
More file actions
78 lines (53 loc) · 2.33 KB
/
test_circular_linked_list.py
File metadata and controls
78 lines (53 loc) · 2.33 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
import unittest
from datastructures.linked_lists.circular import CircularLinkedList
class CircularLinkedListAppendTestCase(unittest.TestCase):
def test_1(self):
"""should append a new node 7 to linked list [1,2,3,4,5,6] to become [1,2,3,4,5,6,7]"""
data = [1, 2, 3, 4, 5, 6]
expected = [1, 2, 3, 4, 5, 6, 7]
linked_list = CircularLinkedList()
for d in data:
linked_list.append(d)
linked_list.append(7)
actual_head = linked_list.head
self.assertIsNotNone(actual_head)
self.assertEqual(1, actual_head.data)
self.assertEqual(expected, list(linked_list))
class CircularLinkedListPrependTestCase(unittest.TestCase):
def test_1(self):
"""should prepend a new node 7 to linked list [1,2,3,4,5,6] to become [7,1,2,3,4,5,6]"""
data = [1, 2, 3, 4, 5, 6]
expected = [7, 1, 2, 3, 4, 5, 6]
linked_list = CircularLinkedList()
for d in data:
linked_list.append(d)
linked_list.prepend(7)
actual_head = linked_list.head
self.assertIsNotNone(actual_head)
self.assertEqual(7, actual_head.data)
self.assertEqual(expected, list(linked_list))
class CircularLinkedListDeleteNodeByKeyTestCase(unittest.TestCase):
def test_1(self):
"""should delete a node 5 from linked list [1,2,3,4,5,6] to become [1,2,3,4,6]"""
data = [1, 2, 3, 4, 5, 6]
expected = [1, 2, 3, 4, 6]
circular_linked_list = CircularLinkedList()
for d in data:
circular_linked_list.append(d)
circular_linked_list.delete_node_by_key(5)
self.assertEqual(expected, list(circular_linked_list))
class CircularLinkedListSplitListTestCase(unittest.TestCase):
def test_1(self):
"""should split a linked list [1,2,3,4,5,6] to become ([1,2,3],[4,5,6])"""
data = [1, 2, 3, 4, 5, 6]
expected = ([1, 2, 3], [4, 5, 6])
circular_linked_list = CircularLinkedList()
for d in data:
circular_linked_list.append(d)
result = circular_linked_list.split_list()
self.assertIsNotNone(result)
first_list_head, second_list_head = result
self.assertEqual(expected[0], list(first_list_head))
self.assertEqual(expected[1], list(second_list_head))
if __name__ == "__main__":
unittest.main()