|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
5 |
| -import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
7 | 6 |
|
8 | 7 | import java.util.List;
|
@@ -76,20 +75,21 @@ void isSortedListFalse() {
|
76 | 75 |
|
77 | 76 | @ParameterizedTest
|
78 | 77 | @MethodSource("provideArraysForSwap")
|
79 |
| - public <T> void testSwap(T[] array, int i, int j, T[] expected, String message) { |
| 78 | + public <T> void testSwap(T[] array, int i, int j, T[] expected) { |
80 | 79 | SortUtils.swap(array, i, j);
|
81 |
| - assertArrayEquals(expected, array, message); |
| 80 | + assertArrayEquals(expected, array); |
82 | 81 | }
|
83 | 82 |
|
84 |
| - private static Stream<Arguments> provideArraysForSwap() { |
85 |
| - return Stream.of(Arguments.of(new Integer[] {1, 2, 3, 4}, 1, 2, new Integer[] {1, 3, 2, 4}, "Swapping adjacent elements should work correctly."), Arguments.of(new Integer[] {1, 2, 3, 4}, 0, 3, new Integer[] {4, 2, 3, 1}, "Swapping non-adjacent elements should work correctly."), |
86 |
| - Arguments.of(new Integer[] {1, 2, 3, 4}, 2, 2, new Integer[] {1, 2, 3, 4}, "Swapping the same index should not change the array."), Arguments.of(new String[] {"a", "b", "c", "d"}, 0, 3, new String[] {"d", "b", "c", "a"}, "Swapping first and last elements should work correctly."), |
87 |
| - Arguments.of(new String[] {null, "b", "c", null}, 0, 3, new String[] {null, "b", "c", null}, "Swapping null elements should work correctly."), Arguments.of(new Integer[] {}, 0, 0, new Integer[] {}, "Swapping in an empty array should not throw an error.")); |
| 83 | + @ParameterizedTest |
| 84 | + @MethodSource("provideArraysForSwap") |
| 85 | + public <T> void testSwapFlippedIndices(T[] array, int i, int j, T[] expected) { |
| 86 | + SortUtils.swap(array, j, i); |
| 87 | + assertArrayEquals(expected, array); |
88 | 88 | }
|
89 | 89 |
|
90 |
| - @Test |
91 |
| - public void testSwapOutOfBounds() { |
92 |
| - Integer[] array = {1, 2, 3, 4}; |
93 |
| - assertThrows(ArrayIndexOutOfBoundsException.class, () -> SortUtils.swap(array, -1, 4), "Swapping out of bounds should throw an ArrayIndexOutOfBoundsException."); |
| 90 | + private static Stream<Arguments> provideArraysForSwap() { |
| 91 | + return Stream.of(Arguments.of(new Integer[] {1, 2, 3, 4}, 1, 2, new Integer[] {1, 3, 2, 4}), Arguments.of(new Integer[] {1, 2, 3, 4}, 0, 3, new Integer[] {4, 2, 3, 1}), |
| 92 | + Arguments.of(new Integer[] {1, 2, 3, 4}, 2, 2, new Integer[] {1, 2, 3, 4}), Arguments.of(new String[] {"a", "b", "c", "d"}, 0, 3, new String[] {"d", "b", "c", "a"}), |
| 93 | + Arguments.of(new String[] {null, "b", "c", null}, 0, 3, new String[] {null, "b", "c", null}), Arguments.of(new Integer[] {}, 0, 0, new Integer[] {})); |
94 | 94 | }
|
95 | 95 | }
|
0 commit comments