|
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 |
|
| 6 | +import java.util.stream.Stream; |
5 | 7 | import org.junit.jupiter.api.Test;
|
| 8 | +import org.junit.jupiter.params.ParameterizedTest; |
| 9 | +import org.junit.jupiter.params.provider.Arguments; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
6 | 11 |
|
7 | 12 | public class BeadSortTest {
|
8 |
| - // 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 |
| - |
19 |
| - @Test |
20 |
| - public void beadSortSingleIntegerArray() { |
21 |
| - int[] inputArray = {4}; |
22 |
| - int[] outputArray = beadSort.sort(inputArray); |
23 |
| - int[] expectedOutput = {4}; |
24 |
| - assertArrayEquals(outputArray, expectedOutput); |
| 13 | + @ParameterizedTest |
| 14 | + @MethodSource("provideArraysForBeadSort") |
| 15 | + public void testBeadSort(int[] inputArray, int[] expectedArray) { |
| 16 | + BeadSort beadSort = new BeadSort(); |
| 17 | + assertArrayEquals(expectedArray, beadSort.sort(inputArray)); |
25 | 18 | }
|
26 | 19 |
|
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); |
| 20 | + private static Stream<Arguments> provideArraysForBeadSort() { |
| 21 | + 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}), |
| 22 | + 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 | 23 | }
|
34 | 24 |
|
35 | 25 | @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); |
| 26 | + public void testWithNegativeNumbers() { |
| 27 | + assertThrows(IllegalArgumentException.class, () -> new BeadSort().sort(new int[] {3, 1, 4, 1, 5, -9})); |
41 | 28 | }
|
42 | 29 | }
|
0 commit comments