Repository: TheAlgorithms/Java
File: src/test/java/com/thealgorithms/sorts/SortUtilsTest.java (Java)

Code before:
    }

    @Test
    void isSortedListFalse() {
        List<Double> list = List.of(1.0, 3.0, -0.15);
        assertFalse(SortUtils.isSorted(list));

        List<Integer> array2 = List.of(14, 15, 16, 1);
        assertFalse(SortUtils.isSorted(array2));

        List<Integer> array3 = List.of(5, 4, 3, 2, 1);
        assertFalse(SortUtils.isSorted(array3));
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwap(T[] array, int i, int j, T[] expected, String message) {
        SortUtils.swap(array, i, j);
        assertArrayEquals(expected, array, message);
    }

    private static Stream<Arguments> provideArraysForSwap() {
        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."),
            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."),
            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."));
    }

    @Test
    public void testSwapOutOfBounds() {
        Integer[] array = {1, 2, 3, 4};
        assertThrows(ArrayIndexOutOfBoundsException.class, () -> SortUtils.swap(array, -1, 4), "Swapping out of bounds should throw an ArrayIndexOutOfBoundsException.");
    }
}


Review comment (suggestion):
I would be careful with the messages:
- _Swapping the same index should not change the array._ - this test does not check that (i.e. the _swap_ could still happen, but the test would pass),
-  _Swapping in an empty array should not throw an error._ - this is an accident.

So from my side: feel free to remove them.

Code after:

    @Test
    void isSortedListFalse() {
        List<Double> list = List.of(1.0, 3.0, -0.15);
        assertFalse(SortUtils.isSorted(list));

        List<Integer> array2 = List.of(14, 15, 16, 1);
        assertFalse(SortUtils.isSorted(array2));

        List<Integer> array3 = List.of(5, 4, 3, 2, 1);
        assertFalse(SortUtils.isSorted(array3));
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwap(T[] array, int i, int j, T[] expected) {
        SortUtils.swap(array, i, j);
        assertArrayEquals(expected, array);
    }

    @ParameterizedTest
    @MethodSource("provideArraysForSwap")
    public <T> void testSwapFlippedIndices(T[] array, int i, int j, T[] expected) {
        SortUtils.swap(array, j, i);
        assertArrayEquals(expected, array);
    }

    private static Stream<Arguments> provideArraysForSwap() {
        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}), 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"}), Arguments.of(new String[] {null, "b", "c", null}, 0, 3, new String[] {null, "b", "c", null}));
    }
}