Skip to content

Commit 76430fa

Browse files
committed
test: Add test_linked_list.py
1 parent bb5e360 commit 76430fa

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/test_linked_list.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Test cases for linked list operations.
3+
"""
4+
5+
import unittest
6+
7+
from src.linked_list import ListNode, reverse_linked_list
8+
9+
10+
class TestLinkedList(unittest.TestCase):
11+
def test_reverse_linked_list(self):
12+
"""Test reversing a normal linked list with multiple nodes [1->2->3]."""
13+
head = ListNode(1)
14+
head.next = ListNode(2)
15+
head.next.next = ListNode(3)
16+
17+
reversed_head = reverse_linked_list(head)
18+
19+
self.assertEqual(reversed_head.val, 3)
20+
self.assertEqual(reversed_head.next.val, 2)
21+
self.assertEqual(reversed_head.next.next.val, 1)
22+
self.assertIsNone(reversed_head.next.next.next)
23+
24+
def test_reverse_single_node(self):
25+
"""Test reversing a linked list with a single node [1]."""
26+
head = ListNode(1)
27+
reversed_head = reverse_linked_list(head)
28+
29+
self.assertEqual(reversed_head.val, 1)
30+
self.assertIsNone(reversed_head.next)
31+
32+
def test_reverse_empty_list(self):
33+
"""Test reversing an empty linked list."""
34+
reversed_head = reverse_linked_list(None)
35+
self.assertIsNone(reversed_head)

0 commit comments

Comments
 (0)