Skip to content

Commit d9e43ba

Browse files
Fixed test file too for the duplicates change
1 parent a5b3864 commit d9e43ba

1 file changed

Lines changed: 133 additions & 9 deletions

File tree

src/test/java/com/thealgorithms/recursion/Permutations.java

Lines changed: 133 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,156 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import java.util.List;
78
import org.junit.jupiter.api.Test;
89

910
class PermutationsTest {
1011

12+
// ─────────────────────────────────────────────
13+
// Null / Invalid Input Tests
14+
// ─────────────────────────────────────────────
15+
16+
@Test
17+
void testNullArrayThrowsNullPointerException() {
18+
assertThrows(NullPointerException.class, () -> Permutations.permutations((Integer[]) null));
19+
}
20+
21+
@Test
22+
void testArrayWithNullElementThrowsIllegalArgumentException() {
23+
Integer[] items = { 1, null, 3 };
24+
assertThrows(IllegalArgumentException.class, () -> Permutations.permutations(items));
25+
}
26+
27+
// ─────────────────────────────────────────────
28+
// Edge Case Tests
29+
// ─────────────────────────────────────────────
30+
31+
@Test
32+
void testEmptyArrayReturnsOneEmptyPermutation() {
33+
Integer[] items = {};
34+
List<List<Integer>> result = Permutations.permutations(items);
35+
assertEquals(1, result.size());
36+
assertTrue(result.get(0).isEmpty());
37+
}
38+
39+
@Test
40+
void testSingleElementReturnsOnePermutation() {
41+
Integer[] items = { 42 };
42+
List<List<Integer>> result = Permutations.permutations(items);
43+
assertEquals(1, result.size());
44+
assertEquals(List.of(42), result.get(0));
45+
}
46+
47+
// ─────────────────────────────────────────────
48+
// Integer Permutation Tests
49+
// ─────────────────────────────────────────────
50+
1151
@Test
12-
void testIntegerPermutations() {
13-
int[] nums = {1, 2};
14-
List<List<Integer>> result = Permutations.permutations(nums);
52+
void testTwoIntegersReturnsTwoPermutations() {
53+
Integer[] items = { 1, 2 };
54+
List<List<Integer>> result = Permutations.permutations(items);
1555
assertEquals(2, result.size());
56+
assertTrue(result.contains(List.of(1, 2)));
57+
assertTrue(result.contains(List.of(2, 1)));
1658
}
1759

1860
@Test
19-
void testIntegerPermutationsNull() {
20-
assertThrows(NullPointerException.class, () -> Permutations.permutations((int[]) null));
61+
void testThreeIntegersReturnsSixPermutations() {
62+
Integer[] items = { 1, 2, 3 };
63+
List<List<Integer>> result = Permutations.permutations(items);
64+
assertEquals(6, result.size());
2165
}
2266

2367
@Test
24-
void testStringPermutations() {
25-
List<String> result = Permutations.permutations("ab");
68+
void testIntegerPermutationsContainAllExpectedOrders() {
69+
Integer[] items = { 1, 2, 3 };
70+
List<List<Integer>> result = Permutations.permutations(items);
71+
assertTrue(result.contains(List.of(1, 2, 3)));
72+
assertTrue(result.contains(List.of(1, 3, 2)));
73+
assertTrue(result.contains(List.of(2, 1, 3)));
74+
assertTrue(result.contains(List.of(2, 3, 1)));
75+
assertTrue(result.contains(List.of(3, 1, 2)));
76+
assertTrue(result.contains(List.of(3, 2, 1)));
77+
}
78+
79+
// ─────────────────────────────────────────────
80+
// Duplicate Handling Tests
81+
// ─────────────────────────────────────────────
82+
83+
@Test
84+
void testTwoDuplicateIntegersReturnsOnePermutation() {
85+
Integer[] items = { 1, 1 };
86+
List<List<Integer>> result = Permutations.permutations(items);
87+
assertEquals(1, result.size());
88+
assertEquals(List.of(1, 1), result.get(0));
89+
}
90+
91+
@Test
92+
void testArrayWithDuplicatesReturnsCorrectCount() {
93+
Integer[] items = { 1, 1, 2 };
94+
List<List<Integer>> result = Permutations.permutations(items);
95+
// 3!/2! = 3 unique permutations
96+
assertEquals(3, result.size());
97+
assertTrue(result.contains(List.of(1, 1, 2)));
98+
assertTrue(result.contains(List.of(1, 2, 1)));
99+
assertTrue(result.contains(List.of(2, 1, 1)));
100+
}
101+
102+
@Test
103+
void testAllDuplicatesReturnsOnePermutation() {
104+
Integer[] items = { 5, 5, 5 };
105+
List<List<Integer>> result = Permutations.permutations(items);
106+
assertEquals(1, result.size());
107+
assertEquals(List.of(5, 5, 5), result.get(0));
108+
}
109+
110+
// ─────────────────────────────────────────────
111+
// String Permutation Tests
112+
// ─────────────────────────────────────────────
113+
114+
@Test
115+
void testTwoStringsReturnsTwoPermutations() {
116+
String[] items = { "a", "b" };
117+
List<List<String>> result = Permutations.permutations(items);
26118
assertEquals(2, result.size());
119+
assertTrue(result.contains(List.of("a", "b")));
120+
assertTrue(result.contains(List.of("b", "a")));
121+
}
122+
123+
@Test
124+
void testThreeStringsReturnsSixPermutations() {
125+
String[] items = { "x", "y", "z" };
126+
List<List<String>> result = Permutations.permutations(items);
127+
assertEquals(6, result.size());
128+
}
129+
130+
@Test
131+
void testDuplicateStringsReturnsCorrectCount() {
132+
String[] items = { "a", "a", "b" };
133+
List<List<String>> result = Permutations.permutations(items);
134+
assertEquals(3, result.size());
135+
assertTrue(result.contains(List.of("a", "a", "b")));
136+
assertTrue(result.contains(List.of("a", "b", "a")));
137+
assertTrue(result.contains(List.of("b", "a", "a")));
138+
}
139+
140+
// ─────────────────────────────────────────────
141+
// Character Permutation Tests
142+
// ─────────────────────────────────────────────
143+
144+
@Test
145+
void testCharacterPermutations() {
146+
Character[] items = { 'a', 'b', 'c' };
147+
List<List<Character>> result = Permutations.permutations(items);
148+
assertEquals(6, result.size());
27149
}
28150

29151
@Test
30-
void testStringPermutationsNull() {
31-
assertThrows(NullPointerException.class, () -> Permutations.permutations((String) null));
152+
void testDuplicateCharactersReturnsCorrectCount() {
153+
Character[] items = { 'a', 'a', 'b' };
154+
List<List<Character>> result = Permutations.permutations(items);
155+
assertEquals(3, result.size());
32156
}
33157
}

0 commit comments

Comments
 (0)