1
- package com .thealgorithms .recursion ;
1
+ package com .thealgorithms .Recursion ;
2
2
3
3
import org .junit .jupiter .api .Test ;
4
+ import java .util .ArrayList ;
5
+ import java .util .Collections ;
4
6
import java .util .List ;
5
7
import static org .junit .jupiter .api .Assertions .assertEquals ;
6
8
@@ -11,7 +13,8 @@ void testUniquePermutations() {
11
13
String str = "abc" ;
12
14
List <String > expected = List .of ("abc" , "acb" , "bac" , "bca" , "cab" , "cba" );
13
15
List <String > result = UniquePermutations .getUniquePermutations (str );
14
- assertEquals (expected .size (), result .size ());
16
+ Collections .sort (expected ); // Ensure expected is sorted
17
+ Collections .sort (result ); // Ensure result is sorted
15
18
assertEquals (expected , result );
16
19
}
17
20
@@ -20,7 +23,8 @@ void testUniquePermutationsWithDuplicates() {
20
23
String str = "aab" ;
21
24
List <String > expected = List .of ("aab" , "aba" , "baa" );
22
25
List <String > result = UniquePermutations .getUniquePermutations (str );
23
- assertEquals (expected .size (), result .size ());
26
+ Collections .sort (expected ); // Ensure expected is sorted
27
+ Collections .sort (result ); // Ensure result is sorted
24
28
assertEquals (expected , result );
25
29
}
26
30
@@ -29,6 +33,7 @@ void testEmptyString() {
29
33
String str = "" ;
30
34
List <String > result = UniquePermutations .getUniquePermutations (str );
31
35
assertEquals (1 , result .size ()); // only 1 permutation which is an empty string
36
+ assertEquals ("" , result .get (0 )); // Verify the only permutation is the empty string
32
37
}
33
38
34
39
@ Test
@@ -38,4 +43,30 @@ void testSingleCharacter() {
38
43
assertEquals (1 , result .size ());
39
44
assertEquals ("a" , result .get (0 ));
40
45
}
46
+
47
+ @ Test
48
+ void testAllIdenticalCharacters () {
49
+ String str = "aaa" ;
50
+ List <String > expected = List .of ("aaa" );
51
+ List <String > result = UniquePermutations .getUniquePermutations (str );
52
+ assertEquals (expected .size (), result .size ());
53
+ assertEquals (expected , result );
54
+ }
55
+
56
+ @ Test
57
+ void testMixedCaseCharacters () {
58
+ String str = "aAb" ;
59
+ List <String > expected = List .of ("AaB" , "AbA" , "aAB" , "aBA" , "baA" , "bAa" );
60
+ List <String > result = UniquePermutations .getUniquePermutations (str );
61
+ Collections .sort (expected ); // Ensure expected is sorted
62
+ Collections .sort (result ); // Ensure result is sorted
63
+ assertEquals (expected , result );
64
+ }
65
+
66
+ @ Test
67
+ void testNullInput () {
68
+ String str = null ;
69
+ List <String > result = UniquePermutations .getUniquePermutations (str );
70
+ assertEquals (0 , result .size ()); // Expect an empty list for null input
71
+ }
41
72
}
0 commit comments