1- from src .all_permutations import all_permutations
1+ # tests/test_all_permutations.py
2+
23import unittest
34
5+ from src .all_permutations import (
6+ all_permutations_itertools ,
7+ all_permutations_backtracking ,
8+ all_permutations # this is aliased to backtracking by default
9+ )
10+
411
512class TestAllPermutations (unittest .TestCase ):
13+ def setUp (self ):
14+ # We will test these two implementations side by side in every test.
15+ self .funcs_to_test = [
16+ all_permutations_itertools ,
17+ all_permutations_backtracking ,
18+ ]
19+
620 def test_two_elements (self ):
721 input_list = [1 , 2 ]
8- excepted = [[1 , 2 ], [2 , 1 ]]
9- actual = all_permutations (input_list )
22+ expected = [[1 , 2 ], [2 , 1 ]]
1023
11- self .assertListEqual (sorted (excepted ), sorted (actual ))
24+ for func in self .funcs_to_test :
25+ with self .subTest (func = func .__name__ ):
26+ actual = func (input_list [:]) # pass a fresh copy to avoid side‐effects
27+ # Sort both lists of lists before comparing
28+ self .assertListEqual (sorted (expected ), sorted (actual ))
1229
1330 def test_three_elements (self ):
1431 input_list = [3 , 1 , 2 ]
15- excepted = [[1 , 2 , 3 ], [1 , 3 , 2 ], [2 , 1 , 3 ], [2 , 3 , 1 ], [3 , 2 , 1 ], [3 , 1 , 2 ]]
16-
17- actual = all_permutations (input_list )
32+ expected = [
33+ [1 , 2 , 3 ],
34+ [1 , 3 , 2 ],
35+ [2 , 1 , 3 ],
36+ [2 , 3 , 1 ],
37+ [3 , 2 , 1 ],
38+ [3 , 1 , 2 ],
39+ ]
1840
19- self .assertListEqual (sorted (excepted ), sorted (actual ))
41+ for func in self .funcs_to_test :
42+ with self .subTest (func = func .__name__ ):
43+ actual = func (input_list [:])
44+ self .assertListEqual (sorted (expected ), sorted (actual ))
2045
2146 def test_three_strings (self ):
2247 input_list = ["A" , "B" , "C" ]
23- excepted = [
48+ expected = [
2449 ["A" , "B" , "C" ],
2550 ["A" , "C" , "B" ],
2651 ["B" , "A" , "C" ],
@@ -29,13 +54,14 @@ def test_three_strings(self):
2954 ["C" , "B" , "A" ],
3055 ]
3156
32- actual = all_permutations (input_list )
33-
34- self .assertListEqual (sorted (excepted ), sorted (actual ))
57+ for func in self .funcs_to_test :
58+ with self .subTest (func = func .__name__ ):
59+ actual = func (input_list [:])
60+ self .assertListEqual (sorted (expected ), sorted (actual ))
3561
3662 def test_four_elements (self ):
3763 input_list = [3 , 1 , 2 , 4 ]
38- excepted = [
64+ expected = [
3965 [3 , 1 , 2 , 4 ],
4066 [3 , 1 , 4 , 2 ],
4167 [3 , 2 , 1 , 4 ],
@@ -62,9 +88,10 @@ def test_four_elements(self):
6288 [4 , 2 , 1 , 3 ],
6389 ]
6490
65- actual = all_permutations (input_list )
66-
67- self .assertListEqual (sorted (excepted ), sorted (actual ))
91+ for func in self .funcs_to_test :
92+ with self .subTest (func = func .__name__ ):
93+ actual = func (input_list [:])
94+ self .assertListEqual (sorted (expected ), sorted (actual ))
6895
6996
7097if __name__ == "__main__" :
0 commit comments