Skip to content

Commit acba685

Browse files
committed
add pop method unittest for SinglyLL class
1 parent 3f20df2 commit acba685

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tests/test_LinkedLists.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,49 @@ def test_insert(self) -> None:
106106
assert (
107107
circular_lst.tail.next == circular_lst.head
108108
), f"tail.next must be the list head ({lst.head}, not {lst.tail.next})"
109+
110+
def test_pop(self) -> None:
111+
lst = SinglyLL()
112+
113+
assert (
114+
lst.pop() == lst
115+
), "popping from an empty list should return the same list without modification"
116+
117+
lst.insert(1).pop(0)
118+
assert (
119+
lst.head is lst.tail is None
120+
), f"lst must be empty after pop, instead head is {lst.head} and tail is {lst.tail}"
121+
122+
for i in [1, 2, 3, 4, 5]:
123+
lst.insert(i)
124+
125+
lst.pop(0)
126+
assert lst.head == Node(2), f"new head node must be Node(2), not {lst.head}"
127+
128+
lst.pop()
129+
assert lst.tail == Node(4), f"new tail node must be Node(4), not {lst.tail}"
130+
assert lst.tail.next is None, f"tail.next should be None, not {lst.tail.next}"
131+
132+
lst.pop(1)
133+
assert lst.head.next == lst.tail
134+
135+
for i in [2.5, -7.8, "a"]:
136+
error_msg = f"Invalid type {type(i)}. Index must be int"
137+
with pytest.raises(TypeError, match=error_msg):
138+
lst.pop(i)
139+
140+
for i in [2, 3, 4, 5]:
141+
error_msg = f"Index out of bound, please specify an index between 0 and {len(lst)-1}"
142+
with pytest.raises(IndexError, match=error_msg):
143+
lst.pop(i)
144+
145+
# Test circular list:
146+
circular_lst = SinglyLL([1, 2, 3, 4, 5], circular=True)
147+
148+
circular_lst.pop()
149+
assert circular_lst.tail == Node(
150+
4
151+
), f"new tail node must be Node(4), not {circular_lst.tail}"
152+
assert (
153+
circular_lst.tail.next == circular_lst.head
154+
), f" tail.next should refer to head ({circular_lst.head}, not {circular_lst.tail.next})"

0 commit comments

Comments
 (0)