Skip to content

Commit 55f7aaf

Browse files
committed
add getitem unittest for SinglyLL class
1 parent 352b675 commit 55f7aaf

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/test_LinkedLists.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,21 @@ def test_iteration(self) -> None:
216216
for node, val in zip(circular_lst, elements):
217217
assert node.data == val
218218

219+
def test_getitem(self) -> None:
220+
vals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
221+
lst = SinglyLL(vals)
222+
circular_lst = SinglyLL(vals, circular=True)
223+
224+
for l in [lst, circular_lst]:
225+
for i in range(-10, 10):
226+
assert l[i].data == vals[i]
227+
228+
for i in range(10, 15):
229+
error_msg = f"Index out of bound, please specify an index between 0 and {len(lst)-1}"
230+
with pytest.raises(IndexError, match=error_msg):
231+
l[i]
232+
233+
for i in [1.5, -8.5, -12.5, "a"]:
234+
error_msg = f"Invalid type {type(i)}. Index must be int"
235+
with pytest.raises(TypeError, match=error_msg):
236+
l[i]

0 commit comments

Comments
 (0)