-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Expand file tree
/
Copy pathPermutations.java
More file actions
157 lines (133 loc) · 6.39 KB
/
Permutations.java
File metadata and controls
157 lines (133 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.thealgorithms.recursion;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import org.junit.jupiter.api.Test;
class PermutationsTest {
// ─────────────────────────────────────────────
// Null / Invalid Input Tests
// ─────────────────────────────────────────────
@Test
void testNullArrayThrowsNullPointerException() {
assertThrows(NullPointerException.class, () -> Permutations.permutations((Integer[]) null));
}
@Test
void testArrayWithNullElementThrowsIllegalArgumentException() {
Integer[] items = {1, null, 3};
assertThrows(IllegalArgumentException.class, () -> Permutations.permutations(items));
}
// ─────────────────────────────────────────────
// Edge Case Tests
// ─────────────────────────────────────────────
@Test
void testEmptyArrayReturnsOneEmptyPermutation() {
Integer[] items = {};
List<List<Integer>> result = Permutations.permutations(items);
assertEquals(1, result.size());
assertTrue(result.get(0).isEmpty());
}
@Test
void testSingleElementReturnsOnePermutation() {
Integer[] items = {42};
List<List<Integer>> result = Permutations.permutations(items);
assertEquals(1, result.size());
assertEquals(List.of(42), result.get(0));
}
// ─────────────────────────────────────────────
// Integer Permutation Tests
// ─────────────────────────────────────────────
@Test
void testTwoIntegersReturnsTwoPermutations() {
Integer[] items = {1, 2};
List<List<Integer>> result = Permutations.permutations(items);
assertEquals(2, result.size());
assertTrue(result.contains(List.of(1, 2)));
assertTrue(result.contains(List.of(2, 1)));
}
@Test
void testThreeIntegersReturnsSixPermutations() {
Integer[] items = {1, 2, 3};
List<List<Integer>> result = Permutations.permutations(items);
assertEquals(6, result.size());
}
@Test
void testIntegerPermutationsContainAllExpectedOrders() {
Integer[] items = {1, 2, 3};
List<List<Integer>> result = Permutations.permutations(items);
assertTrue(result.contains(List.of(1, 2, 3)));
assertTrue(result.contains(List.of(1, 3, 2)));
assertTrue(result.contains(List.of(2, 1, 3)));
assertTrue(result.contains(List.of(2, 3, 1)));
assertTrue(result.contains(List.of(3, 1, 2)));
assertTrue(result.contains(List.of(3, 2, 1)));
}
// ─────────────────────────────────────────────
// Duplicate Handling Tests
// ─────────────────────────────────────────────
@Test
void testTwoDuplicateIntegersReturnsOnePermutation() {
Integer[] items = {1, 1};
List<List<Integer>> result = Permutations.permutations(items);
assertEquals(1, result.size());
assertEquals(List.of(1, 1), result.get(0));
}
@Test
void testArrayWithDuplicatesReturnsCorrectCount() {
Integer[] items = {1, 1, 2};
List<List<Integer>> result = Permutations.permutations(items);
// 3!/2! = 3 unique permutations
assertEquals(3, result.size());
assertTrue(result.contains(List.of(1, 1, 2)));
assertTrue(result.contains(List.of(1, 2, 1)));
assertTrue(result.contains(List.of(2, 1, 1)));
}
@Test
void testAllDuplicatesReturnsOnePermutation() {
Integer[] items = {5, 5, 5};
List<List<Integer>> result = Permutations.permutations(items);
assertEquals(1, result.size());
assertEquals(List.of(5, 5, 5), result.get(0));
}
// ─────────────────────────────────────────────
// String Permutation Tests
// ─────────────────────────────────────────────
@Test
void testTwoStringsReturnsTwoPermutations() {
String[] items = {"a", "b"};
List<List<String>> result = Permutations.permutations(items);
assertEquals(2, result.size());
assertTrue(result.contains(List.of("a", "b")));
assertTrue(result.contains(List.of("b", "a")));
}
@Test
void testThreeStringsReturnsSixPermutations() {
String[] items = {"x", "y", "z"};
List<List<String>> result = Permutations.permutations(items);
assertEquals(6, result.size());
}
@Test
void testDuplicateStringsReturnsCorrectCount() {
String[] items = {"a", "a", "b"};
List<List<String>> result = Permutations.permutations(items);
assertEquals(3, result.size());
assertTrue(result.contains(List.of("a", "a", "b")));
assertTrue(result.contains(List.of("a", "b", "a")));
assertTrue(result.contains(List.of("b", "a", "a")));
}
// ─────────────────────────────────────────────
// Character Permutation Tests
// ─────────────────────────────────────────────
@Test
void testCharacterPermutations() {
Character[] items = {'a', 'b', 'c'};
List<List<Character>> result = Permutations.permutations(items);
assertEquals(6, result.size());
}
@Test
void testDuplicateCharactersReturnsCorrectCount() {
Character[] items = {'a', 'a', 'b'};
List<List<Character>> result = Permutations.permutations(items);
assertEquals(3, result.size());
}
}