|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
5 | 6 |
|
6 | 7 | import java.util.List; |
7 | 8 | import org.junit.jupiter.api.Test; |
8 | 9 |
|
9 | 10 | class PermutationsTest { |
10 | 11 |
|
| 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 | + |
11 | 51 | @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); |
15 | 55 | assertEquals(2, result.size()); |
| 56 | + assertTrue(result.contains(List.of(1, 2))); |
| 57 | + assertTrue(result.contains(List.of(2, 1))); |
16 | 58 | } |
17 | 59 |
|
18 | 60 | @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()); |
21 | 65 | } |
22 | 66 |
|
23 | 67 | @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); |
26 | 118 | 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()); |
27 | 149 | } |
28 | 150 |
|
29 | 151 | @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()); |
32 | 156 | } |
33 | 157 | } |
0 commit comments