1
- from src .all_permutations import all_permutations
1
+ # tests/test_all_permutations.py
2
+
2
3
import unittest
3
4
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
+
4
11
5
12
class 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
+
6
20
def test_two_elements (self ):
7
21
input_list = [1 , 2 ]
8
- excepted = [[1 , 2 ], [2 , 1 ]]
9
- actual = all_permutations (input_list )
22
+ expected = [[1 , 2 ], [2 , 1 ]]
10
23
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 ))
12
29
13
30
def test_three_elements (self ):
14
31
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
+ ]
18
40
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 ))
20
45
21
46
def test_three_strings (self ):
22
47
input_list = ["A" , "B" , "C" ]
23
- excepted = [
48
+ expected = [
24
49
["A" , "B" , "C" ],
25
50
["A" , "C" , "B" ],
26
51
["B" , "A" , "C" ],
@@ -29,13 +54,14 @@ def test_three_strings(self):
29
54
["C" , "B" , "A" ],
30
55
]
31
56
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 ))
35
61
36
62
def test_four_elements (self ):
37
63
input_list = [3 , 1 , 2 , 4 ]
38
- excepted = [
64
+ expected = [
39
65
[3 , 1 , 2 , 4 ],
40
66
[3 , 1 , 4 , 2 ],
41
67
[3 , 2 , 1 , 4 ],
@@ -62,9 +88,10 @@ def test_four_elements(self):
62
88
[4 , 2 , 1 , 3 ],
63
89
]
64
90
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 ))
68
95
69
96
70
97
if __name__ == "__main__" :
0 commit comments