|
1 | 1 | package com.thealgorithms.stacks;
|
2 | 2 |
|
3 |
| -import org.junit.jupiter.api.Test; |
4 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.stream.Stream; |
| 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; |
5 | 11 |
|
6 |
| -public class NearestElementTest { |
| 12 | +class NearestElementTest { |
7 | 13 |
|
8 |
| - @Test |
9 |
| - public void testNearestGreaterToRight() { |
10 |
| - int[] arr = {4, 5, 2, 10, 8}; |
11 |
| - int[] expected = {5, 10, 10, -1, -1}; |
12 |
| - assertArrayEquals(expected, NearestElement.nearestGreaterToRight(arr)); |
| 14 | + @ParameterizedTest |
| 15 | + @MethodSource("provideGreaterToRightTestCases") |
| 16 | + void testNearestGreaterToRight(int[] input, int[] expected) { |
| 17 | + assertArrayEquals(expected, NearestElement.nearestGreaterToRight(input)); |
13 | 18 | }
|
14 | 19 |
|
15 |
| - @Test |
16 |
| - public void testNearestGreaterToLeft() { |
17 |
| - int[] arr = {4, 5, 2, 10, 8}; |
18 |
| - int[] expected = {-1, -1, 5, -1, 10}; |
19 |
| - assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(arr)); |
| 20 | + static Stream<Arguments> provideGreaterToRightTestCases() { |
| 21 | + return Stream.of( |
| 22 | + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {5, 10, 10, -1, -1}), |
| 23 | + Arguments.of(new int[] {5}, new int[] {-1}), |
| 24 | + Arguments.of(new int[] {}, new int[] {}) |
| 25 | + ); |
20 | 26 | }
|
21 | 27 |
|
22 |
| - @Test |
23 |
| - public void testNearestSmallerToRight() { |
24 |
| - int[] arr = {4, 5, 2, 10, 8}; |
25 |
| - int[] expected = {2, 2, -1, 8, -1}; |
26 |
| - assertArrayEquals(expected, NearestElement.nearestSmallerToRight(arr)); |
| 28 | + @ParameterizedTest |
| 29 | + @MethodSource("provideGreaterToLeftTestCases") |
| 30 | + void testNearestGreaterToLeft(int[] input, int[] expected) { |
| 31 | + assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(input)); |
27 | 32 | }
|
28 | 33 |
|
29 |
| - @Test |
30 |
| - public void testNearestSmallerToLeft() { |
31 |
| - int[] arr = {4, 5, 2, 10, 8}; |
32 |
| - int[] expected = {-1, 4, -1, 2, 2}; |
33 |
| - assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr)); |
| 34 | + static Stream<Arguments> provideGreaterToLeftTestCases() { |
| 35 | + return Stream.of( |
| 36 | + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, -1, 5, -1, 10}), |
| 37 | + Arguments.of(new int[] {5}, new int[] {-1}), |
| 38 | + Arguments.of(new int[] {}, new int[] {}) |
| 39 | + ); |
34 | 40 | }
|
35 | 41 |
|
36 |
| - @Test |
37 |
| - void testEmptyArray() { |
38 |
| - int[] arr = {}; |
39 |
| - assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToRight(arr)); |
40 |
| - assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToLeft(arr)); |
| 42 | + @ParameterizedTest |
| 43 | + @MethodSource("provideSmallerToRightTestCases") |
| 44 | + void testNearestSmallerToRight(int[] input, int[] expected) { |
| 45 | + assertArrayEquals(expected, NearestElement.nearestSmallerToRight(input)); |
41 | 46 | }
|
42 | 47 |
|
43 |
| - @Test |
44 |
| - void testAllEqualElements() { |
45 |
| - int[] arr = {5, 5, 5, 5}; |
46 |
| - assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToRight(arr)); |
47 |
| - assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToLeft(arr)); |
| 48 | + static Stream<Arguments> provideSmallerToRightTestCases() { |
| 49 | + return Stream.of( |
| 50 | + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {2, 2, -1, 8, -1}), |
| 51 | + Arguments.of(new int[] {5}, new int[] {-1}), |
| 52 | + Arguments.of(new int[] {}, new int[] {}) |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + @ParameterizedTest |
| 57 | + @MethodSource("provideSmallerToLeftTestCases") |
| 58 | + void testNearestSmallerToLeft(int[] input, int[] expected) { |
| 59 | + assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(input)); |
| 60 | + } |
| 61 | + |
| 62 | + static Stream<Arguments> provideSmallerToLeftTestCases() { |
| 63 | + return Stream.of( |
| 64 | + Arguments.of(new int[] {4, 5, 2, 10, 8}, new int[] {-1, 4, -1, 2, 2}), |
| 65 | + Arguments.of(new int[] {5}, new int[] {-1}), |
| 66 | + Arguments.of(new int[] {}, new int[] {}) |
| 67 | + ); |
48 | 68 | }
|
49 | 69 |
|
50 | 70 | @Test
|
51 |
| - void testPrivateConstructor() throws Exception { |
52 |
| - var constructor = NearestElement.class.getDeclaredConstructor(); |
53 |
| - constructor.setAccessible(true); |
54 |
| - constructor.newInstance(); |
| 71 | + void testNullInput() { |
| 72 | + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestGreaterToRight(null)); |
| 73 | + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestGreaterToLeft(null)); |
| 74 | + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToRight(null)); |
| 75 | + assertThrows(IllegalArgumentException.class, () -> NearestElement.nearestSmallerToLeft(null)); |
55 | 76 | }
|
56 | 77 | }
|
0 commit comments