|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
3 | 4 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
4 | 5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
5 | 6 |
|
6 | 7 | import java.util.List;
|
| 8 | +import java.util.stream.Stream; |
7 | 9 | import org.junit.jupiter.api.Test;
|
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.Arguments; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
8 | 13 |
|
9 | 14 | class SortUtilsTest {
|
10 | 15 |
|
@@ -67,4 +72,23 @@ void isSortedListFalse() {
|
67 | 72 | List<Integer> array3 = List.of(5, 4, 3, 2, 1);
|
68 | 73 | assertFalse(SortUtils.isSorted(array3));
|
69 | 74 | }
|
| 75 | + |
| 76 | + @ParameterizedTest |
| 77 | + @MethodSource("provideArraysForSwap") |
| 78 | + public <T> void testSwap(T[] array, int i, int j, T[] expected) { |
| 79 | + SortUtils.swap(array, i, j); |
| 80 | + assertArrayEquals(expected, array); |
| 81 | + } |
| 82 | + |
| 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 | + } |
| 89 | + |
| 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}), Arguments.of(new Integer[] {1, 2, 3, 4}, 2, 2, new Integer[] {1, 2, 3, 4}), |
| 92 | + 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})); |
| 93 | + } |
70 | 94 | }
|
0 commit comments