Skip to content

Commit 3a5f8c5

Browse files
committed
Add test_sort.py
1 parent fcbf795 commit 3a5f8c5

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

test/basic/test_sort.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Unit tests for the sort module.
3+
"""
4+
5+
import unittest
6+
7+
from src.basic.sort import get_sorted_list, sort_list
8+
9+
10+
class TestSortMethods(unittest.TestCase):
11+
"""Test cases for the sorting functions."""
12+
13+
def test_sort_list_basic(self):
14+
"""Test basic sorting with sort_list function."""
15+
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
16+
result = sort_list(numbers)
17+
self.assertEqual(result, [1, 1, 2, 3, 4, 5, 6, 9])
18+
# Verify it's the same object (in-place sorting)
19+
self.assertIs(result, numbers)
20+
21+
def test_sort_list_reverse(self):
22+
"""Test reverse sorting with sort_list function."""
23+
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
24+
result = sort_list(numbers, reverse=True)
25+
self.assertEqual(result, [9, 6, 5, 4, 3, 2, 1, 1])
26+
27+
def test_sort_list_with_key(self):
28+
"""Test sorting with a key function."""
29+
numbers = [-3, 1, -4, 1, 5, -9, 2, 6]
30+
result = sort_list(numbers, key=abs)
31+
self.assertEqual(result, [1, 1, 2, -3, -4, 5, 6, -9])
32+
33+
def test_get_sorted_list_basic(self):
34+
"""Test basic sorting with get_sorted_list function."""
35+
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
36+
result = get_sorted_list(numbers)
37+
self.assertEqual(result, [1, 1, 2, 3, 4, 5, 6, 9])
38+
# Verify it's a new object (non-destructive sorting)
39+
self.assertIsNot(result, numbers)
40+
# Original list should be unchanged
41+
self.assertEqual(numbers, [3, 1, 4, 1, 5, 9, 2, 6])
42+
43+
def test_get_sorted_list_reverse(self):
44+
"""Test reverse sorting with get_sorted_list function."""
45+
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
46+
result = get_sorted_list(numbers, reverse=True)
47+
self.assertEqual(result, [9, 6, 5, 4, 3, 2, 1, 1])
48+
# Original list should be unchanged
49+
self.assertEqual(numbers, [3, 1, 4, 1, 5, 9, 2, 6])
50+
51+
def test_get_sorted_list_with_key(self):
52+
"""Test sorting with a key function."""
53+
words = ["apple", "Banana", "cherry", "Date"]
54+
result = get_sorted_list(words, key=str.lower)
55+
self.assertEqual(result, ["apple", "Banana", "cherry", "Date"])
56+
57+
def test_get_sorted_list_with_tuple(self):
58+
"""Test sorting a tuple."""
59+
data = (3, 1, 4, 1, 5, 9, 2, 6)
60+
result = get_sorted_list(data)
61+
self.assertEqual(result, [1, 1, 2, 3, 4, 5, 6, 9])
62+
# Result should be a list, not a tuple
63+
self.assertIsInstance(result, list)
64+
65+
def test_get_sorted_list_with_string(self):
66+
"""Test sorting a string."""
67+
text = "python"
68+
result = get_sorted_list(text)
69+
self.assertEqual(result, ["h", "n", "o", "p", "t", "y"])
70+
71+
def test_sort_list_of_dicts(self):
72+
"""Test sorting a list of dictionaries."""
73+
people = [
74+
{"name": "Tanaka", "age": 30},
75+
{"name": "Sato", "age": 25},
76+
{"name": "Suzuki", "age": 40},
77+
]
78+
result = get_sorted_list(people, key=lambda x: x["age"])
79+
expected = [
80+
{"name": "Sato", "age": 25},
81+
{"name": "Tanaka", "age": 30},
82+
{"name": "Suzuki", "age": 40},
83+
]
84+
self.assertEqual(result, expected)
85+
86+
def test_empty_list(self):
87+
"""Test sorting an empty list."""
88+
empty_list = []
89+
self.assertEqual(sort_list(empty_list), [])
90+
self.assertEqual(get_sorted_list(empty_list), [])
91+
92+
def test_already_sorted(self):
93+
"""Test sorting an already sorted list."""
94+
sorted_list = [1, 2, 3, 4, 5]
95+
self.assertEqual(sort_list(sorted_list), [1, 2, 3, 4, 5])
96+
self.assertEqual(get_sorted_list(sorted_list), [1, 2, 3, 4, 5])
97+
98+
def test_all_same_elements(self):
99+
"""Test sorting a list with all identical elements."""
100+
same_elements = [7, 7, 7, 7]
101+
self.assertEqual(sort_list(same_elements), [7, 7, 7, 7])
102+
self.assertEqual(get_sorted_list(same_elements), [7, 7, 7, 7])

0 commit comments

Comments
 (0)