|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
5 | 6 | import org.junit.jupiter.api.Test;
|
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | + |
| 11 | +import java.util.stream.Stream; |
6 | 12 |
|
7 | 13 | public class BeadSortTest {
|
8 | 14 | // BeadSort can't sort negative number, Character, String. It can sort positive number only
|
9 |
| - private BeadSort beadSort = new BeadSort(); |
10 |
| - |
11 |
| - @Test |
12 |
| - public void beadSortEmptyArray() { |
13 |
| - int[] inputArray = {}; |
14 |
| - int[] outputArray = beadSort.sort(inputArray); |
15 |
| - int[] expectedOutput = {}; |
16 |
| - assertArrayEquals(outputArray, expectedOutput); |
17 |
| - } |
18 | 15 |
|
19 |
| - @Test |
20 |
| - public void beadSortSingleIntegerArray() { |
21 |
| - int[] inputArray = {4}; |
22 |
| - int[] outputArray = beadSort.sort(inputArray); |
23 |
| - int[] expectedOutput = {4}; |
24 |
| - assertArrayEquals(outputArray, expectedOutput); |
| 16 | + @ParameterizedTest |
| 17 | + @MethodSource("provideArraysForBeadSort") |
| 18 | + public void testBeadSort(int[] inputArray, int[] expectedArray) { |
| 19 | + BeadSort beadSort = new BeadSort(); |
| 20 | + assertArrayEquals(expectedArray, beadSort.sort(inputArray)); |
25 | 21 | }
|
26 | 22 |
|
27 |
| - @Test |
28 |
| - public void bogoSortNonDuplicateIntegerArray() { |
29 |
| - int[] inputArray = {6, 1, 99, 27, 15, 23, 36}; |
30 |
| - int[] outputArray = beadSort.sort(inputArray); |
31 |
| - int[] expectedOutput = {1, 6, 15, 23, 27, 36, 99}; |
32 |
| - assertArrayEquals(outputArray, expectedOutput); |
| 23 | + private static Stream<Arguments> provideArraysForBeadSort() { |
| 24 | + return Stream.of(Arguments.of(new int[] {}, new int[] {}), Arguments.of(new int[] {4}, new int[] {4}), Arguments.of(new int[] {6, 1, 99, 27, 15, 23, 36}, new int[] {1, 6, 15, 23, 27, 36, 99}), Arguments.of(new int[] {6, 1, 27, 15, 23, 27, 36, 23}, new int[] {1, 6, 15, 23, 23, 27, 27, 36}), |
| 25 | + Arguments.of(new int[] {5, 5, 5, 5, 5}, new int[] {5, 5, 5, 5, 5}), Arguments.of(new int[] {1, 2, 3, 4, 5}, new int[] {1, 2, 3, 4, 5}), Arguments.of(new int[] {5, 4, 3, 2, 1}, new int[] {1, 2, 3, 4, 5})); |
33 | 26 | }
|
34 | 27 |
|
35 | 28 | @Test
|
36 |
| - public void bogoSortDuplicateIntegerArray() { |
37 |
| - int[] inputArray = {6, 1, 27, 15, 23, 27, 36, 23}; |
38 |
| - int[] outputArray = beadSort.sort(inputArray); |
39 |
| - int[] expectedOutput = {1, 6, 15, 23, 23, 27, 27, 36}; |
40 |
| - assertArrayEquals(outputArray, expectedOutput); |
| 29 | + public void testWithNegativeNumbers() { |
| 30 | + assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, -1, 4, 1, 5, -9})); |
41 | 31 | }
|
42 | 32 | }
|
0 commit comments