1+ import unittest
2+ from unittest .mock import patch
3+ import io
4+ import os
5+ import importlib .util
6+
7+ # Absolute path to Quick-Sort.py
8+ file_path = os .path .join (
9+ os .path .dirname (__file__ ), ".." ,
10+ "math" , "Quick-Sort" , "Quick-Sort.py"
11+ )
12+ file_path = os .path .abspath (file_path )
13+
14+ # Load module dynamically
15+ spec = importlib .util .spec_from_file_location ("quick_sort_module" , file_path )
16+ quick_sort_module = importlib .util .module_from_spec (spec )
17+ spec .loader .exec_module (quick_sort_module )
18+
19+ quick_sort = quick_sort_module .quick_sort
20+ main = quick_sort_module .main
21+
22+
23+ class TestQuickSort (unittest .TestCase ):
24+
25+ def test_quick_sort_ascending (self ):
26+ # Ascending order
27+ self .assertEqual (quick_sort ([64 , 34 , 25 , 12 , 22 , 11 , 90 ]), [11 , 12 , 22 , 25 , 34 , 64 , 90 ])
28+ self .assertEqual (quick_sort ([5 , 1 , 4 , 2 , 8 ]), [1 , 2 , 4 , 5 , 8 ])
29+
30+ def test_quick_sort_descending (self ):
31+ # Descending order
32+ self .assertEqual (quick_sort ([64 , 34 , 25 , 12 , 22 , 11 , 90 ], reverse = True ), [90 , 64 , 34 , 25 , 22 , 12 , 11 ])
33+ self .assertEqual (quick_sort ([5 , 1 , 4 , 2 , 8 ], reverse = True ), [8 , 5 , 4 , 2 , 1 ])
34+
35+ def test_quick_sort_empty_and_single_item (self ):
36+ # Empty array
37+ self .assertEqual (quick_sort ([]), [])
38+ # Single-item array
39+ self .assertEqual (quick_sort ([42 ]), [42 ])
40+
41+ def test_quick_sort_negative_integers (self ):
42+ # Lists with negative integers
43+ self .assertEqual (quick_sort ([- 5 , - 1 , - 10 , 0 , 5 ]), [- 10 , - 5 , - 1 , 0 , 5 ])
44+ self .assertEqual (quick_sort ([- 5 , - 1 , - 10 , 0 , 5 ], reverse = True ), [5 , 0 , - 1 , - 5 , - 10 ])
45+
46+ def test_quick_sort_already_sorted (self ):
47+ # Already sorted ascending
48+ self .assertEqual (quick_sort ([1 , 2 , 3 , 4 , 5 ]), [1 , 2 , 3 , 4 , 5 ])
49+ # Already sorted descending
50+ self .assertEqual (quick_sort ([5 , 4 , 3 , 2 , 1 ], reverse = True ), [5 , 4 , 3 , 2 , 1 ])
51+
52+ def test_quick_sort_with_duplicates (self ):
53+ # Lists with duplicate values
54+ self .assertEqual (quick_sort ([5 , 3 , 8 , 3 , 9 , 1 , 5 ]), [1 , 3 , 3 , 5 , 5 , 8 , 9 ])
55+
56+ @patch ('builtins.input' )
57+ @patch ('sys.stdout' , new_callable = io .StringIO )
58+ def test_main_flow_ascending (self , mock_stdout , mock_input ):
59+ # Ascending sort flow
60+ mock_input .side_effect = ["64 34 25" , "1" , "n" ]
61+ main ()
62+ output = mock_stdout .getvalue ()
63+ self .assertIn ("Original list: [64, 34, 25]" , output )
64+ self .assertIn ("Sorted list (Ascending): [25, 34, 64]" , output )
65+
66+ @patch ('builtins.input' )
67+ @patch ('sys.stdout' , new_callable = io .StringIO )
68+ def test_main_flow_descending (self , mock_stdout , mock_input ):
69+ # Descending sort flow
70+ mock_input .side_effect = ["64 34 25" , "2" , "n" ]
71+ main ()
72+ output = mock_stdout .getvalue ()
73+ self .assertIn ("Original list: [64, 34, 25]" , output )
74+ self .assertIn ("Sorted list (Descending): [64, 34, 25]" , output )
75+
76+ @patch ('builtins.input' )
77+ @patch ('sys.stdout' , new_callable = io .StringIO )
78+ def test_main_flow_invalid_inputs (self , mock_stdout , mock_input ):
79+ # Invalid inputs validation flow
80+ mock_input .side_effect = ["" , "64 abc 25" , "64 34 25" , "3" , "64 34 25" , "1" , "y" , "12 11" , "2" , "n" ]
81+ main ()
82+ output = mock_stdout .getvalue ()
83+ self .assertIn ("Error: Input cannot be empty!" , output )
84+ self .assertIn ("Error: Please enter valid integers only." , output )
85+ self .assertIn ("Invalid sorting choice! Please select 1 or 2." , output )
86+ self .assertIn ("Sorted list (Ascending): [25, 34, 64]" , output )
87+ self .assertIn ("Sorted list (Descending): [12, 11]" , output )
88+
89+
90+ if __name__ == '__main__' :
91+ unittest .main ()
0 commit comments