/
LinkedListTest.py
172 lines (146 loc) · 5.75 KB
/
LinkedListTest.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
from LinkedList import LinkedList
import unittest
class LinkedListTest(unittest.TestCase):
def test_add_at_the_begining_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_begining(10)
self.assertEqual(mylist.add_at_the_begining(20).data, 20)
# print(mylist.add_at_the_begining(20).data)
def test_add_at_the_begining_non_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_begining(10)
mylist.add_at_the_begining(20)
self.assertEqual(mylist.add_at_the_begining(30).data, 30)
# print(mylist.add_at_the_begining(30).data)
# mylist.print()
def test_add_at_the_end_empty_list(self):
mylist = LinkedList()
self.assertEqual(mylist.add_at_the_end(500).data, 500)
# print(mylist.add_at_the_end(500).data)
def test_add_at_the_end_non_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
temp = mylist.head.next
# print(temp.data)
self.assertEqual(temp.data, 20)
def test_add_at_nth_position_1st_position_non_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
self.assertEqual(mylist.add_at_nth_position(5, 1).data, 5)
# self.assertEqual(mylist.add_at_nth_position(5, 1).next.data, 10)
# print(mylist.add_at_nth_position(5, 1).data)
# mylist.print()
def test_add_at_nth_position_0th_position_invalid(self):
mylist = LinkedList()
self.assertEqual(mylist.add_at_nth_position(5, 0), "Invalid position, it can not be less than or equal to 0")
# print(mylist.add_at_nth_position(5, 0))
def test_add_at_nth_position_positive(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
mylist.add_at_the_end(40)
self.assertEqual(mylist.add_at_nth_position(25, 3).next.next.data, 25)
# mylist.add_at_nth_position(25, 3)
# mylist.print()
def test_add_at_nth_position_invalid_position(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
mylist.add_at_the_end(40)
self.assertEqual(mylist.add_at_nth_position(50, 6), "Invalid position, it exceeds the size of the linked list")
# mylist.print()
def test_remove_at_the_begining_empty_list(self):
mylist = LinkedList()
self.assertEqual(mylist.remove_at_the_begining(), "List is empty, nothing to remove")
# mylist.remove_at_the_begining()
def test_remove_at_the_begining_non_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
self.assertEqual(mylist.remove_at_the_begining().data, 20)
# print(mylist.remove_at_the_begining().data)
def test_remove_at_the_end_empty_list(self):
mylist = LinkedList()
self.assertEqual(mylist.remove_at_the_end(), "List is empty, nothing to remove")
def test_remove_at_the_end_list_has_one_element(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
self.assertEqual(mylist.remove_at_the_end(), None)
# print(mylist.remove_at_the_end())
def test_remove_at_the_end_list_has_more_than_one_elements(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
self.assertEqual(mylist.remove_at_the_end().next.data, 20)
def test_remove_at_nth_position_0th_position_invalid(self):
mylist = LinkedList()
self.assertEqual(mylist.remove_at_nth_position(0), "Invalid position, it can not be less than or equal to 0")
# print(mylist.remove_at_nth_position(-1))
def test_remove_at_nth_position_1st_position(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
self.assertEqual(mylist.remove_at_nth_position(1).data, 20)
# print(mylist.remove_at_nth_position(1).data)
def test_remove_at_nth_position_positive(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
mylist.add_at_the_end(40)
self.assertEqual(mylist.remove_at_nth_position(3).next.next.data, 40)
# print(mylist.remove_at_nth_position(3))
# mylist.print()
def test_remove_at_nth_position_invalid_position(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
self.assertEqual(mylist.remove_at_nth_position(4), "Invalid position, it exceeds the size of the linked list")
# print(mylist.remove_at_nth_position(4))
# mylist.print()
def test_reverse_empty_list(self):
mylist = LinkedList()
self.assertEqual(mylist.reverse(), "List is empty, nothing to reverse")
def test_reverse_non_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
mylist.add_at_the_end(40)
self.assertEqual(mylist.reverse().data, 40)
# print(mylist.reverse().data)
def test_reverse_one_element_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
self.assertEqual(mylist.reverse().data, 10)
# print(mylist.reverse().data)
def test_reverse_using_recursion_empty_list(self):
mylist = LinkedList()
self.assertEqual(mylist.reverse_using_recursion(mylist.head), "List is empty, nothing to reverse")
# print(mylist.reverse_using_recursion(mylist.head))
def test_reverse_using_recursion_one_element_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
self.assertEqual(mylist.reverse_using_recursion(mylist.head).data, 10)
# print(mylist.reverse_using_recursion(mylist.head).data)
def test_reverse_using_recursion_non_empty_list(self):
mylist = LinkedList()
mylist.add_at_the_end(10)
mylist.add_at_the_end(20)
mylist.add_at_the_end(30)
mylist.add_at_the_end(40)
mylist.add_at_the_end(50)
mylist.add_at_the_end(60)
self.assertEqual(mylist.reverse_using_recursion(mylist.head).data, 60)
# mylist.reverse_using_recursion(mylist.head)
# mylist.print()
if __name__ == '__main__':
unittest.main()